compose-multiplatform icon indicating copy to clipboard operation
compose-multiplatform copied to clipboard

LocalInspectionMode.current cannot get true in preview mode

Open ancircle0 opened this issue 2 years ago • 4 comments

Describe the bug LocalInspectionMode.current in preview mode is not true ,but return true in jetpack compose preview mode.

Affected platforms Select one of the platforms below:

  • Desktop

Versions

  • Kotlin version*: 1.7.10
  • Compose Multiplatform version*: 1.2.0
  • OS version(s)* (required for Desktop and iOS issues): Windows Professional edition
  • OS architecture (x86 or arm64): x86
  • JDK (for desktop issues): openjdk version "17.0.6" 2023-01-17 LTS OpenJDK Runtime Environment Zulu17.40+19-CA (build 17.0.6+10-LTS) OpenJDK 64-Bit Server VM Zulu17.40+19-CA (build 17.0.6+10-LTS, mixed mode, sharing)

To Reproduce Steps and/or the code snippet to reproduce the behavior: No repeat step is required

Expected behavior LocalInspectionMode.current can return true in preview mode

Screenshots R(OG1M$JI%M100KXBS9RL}I

Additional context Nothing

ancircle0 avatar Mar 09 '23 08:03 ancircle0

Thanks, we'll take a look at it.

m-sasha avatar Mar 09 '23 20:03 m-sasha

Is this going to be picked up?

alexfacciorusso avatar Nov 02 '23 18:11 alexfacciorusso

I am looking at the compose-multiplatform code to check if there is anything that can be done from within the repo, but the clues bring to androidx.compose.desktop.ui.tooling.preview.runtime.NonInteractivePreviewFacade which uses a TestComposeWindow to render the composable and expose it as a window.

Now, a quick fix in that code would be from

        window.setContent @Composable {
            // We need to delay the reflection instantiation of the class until we are in the
            // composable to ensure all the right initialization has happened and the Composable
            // class loads correctly.
            androidx.compose.ui.tooling.ComposableInvoker.invokeComposable(
                className,
                methodName,
                currentComposer
            )
        }

to something like

        window.setContent @Composable {
            // We need to delay the reflection instantiation of the class until we are in the
            // composable to ensure all the right initialization has happened and the Composable
            // class loads correctly.
            CompositionLocalProvider(LocalInspectionMode provides true) {
              androidx.compose.ui.tooling.ComposableInvoker.invokeComposable(
                  className,
                  methodName,
                  currentComposer
              )
           )
        }

It may be a simplification (or maybe there is something else simpler that can be done) but yeah, it seems the issue is, as many times is, enclosed within the androidx monorepo.

This is quite an annoying bug as previews are not only not configurable for desktop, but within previews we cannot even know the view is indeed a preview and do something for that.

Furthermore, there seems to be yet another bug (at least on Windows) where if the system is in dark mode, the preview composable will be rendered in dark mode, but with a white background. I will file a separate one for this after I confirm it with further tests from my side.

alexfacciorusso avatar Nov 03 '23 12:11 alexfacciorusso

Just ran into this too, would love to see a fix.

As a workaround, I'm patching my previews fairly easily as follows:

@Composable
fun PreviewWrapper(content: @Composable () -> Unit) =
    CompositionLocalProvider(LocalInspectionMode provides true, content = content)

// Then...
@Preview
@Composable
private fun MyPreview() = PreviewWrapper {
    BasicText(text = "Is a preview: ${LocalInspectionMode.current}") // Is a preview: true
}

iamcalledrob avatar May 23 '24 11:05 iamcalledrob

Just ran into this too. Probably an issue with the Preview plugin not treating jetbrains.preview the same.

chrisjenx avatar May 30 '24 17:05 chrisjenx

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

okushnikov avatar Jul 14 '24 14:07 okushnikov