Skip to content

Commit

Permalink
Fix container size in case of ImageComposeScene (#1375)
Browse files Browse the repository at this point in the history
Fixes JetBrains/compose-multiplatform#4875

## Testing

`:compose:ui:ui:desktopTest --tests
"androidx.compose.ui.ImageComposeSceneTest"`

## Release Notes

### Fixes - Multiple Platforms
- Fix container size for `Dialog` centering inside `ImageComposeScene`
  • Loading branch information
MatkovIvan authored May 27, 2024
1 parent 053f16e commit d1f3898
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import androidx.compose.ui.test.InternalTestApi
import androidx.compose.ui.test.junit4.DesktopScreenshotTestRule
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import kotlin.time.Duration.Companion.seconds
import kotlin.time.ExperimentalTime
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -94,6 +95,19 @@ class ImageComposeSceneTest {
}
}

@Test
fun `run dialog in center`() {
val image = renderComposeScene(
width = 80,
height = 40,
) {
Dialog(onDismissRequest = {}) {
Box(Modifier.size(20.dp).background(Color.Red))
}
}
screenshotRule.write(image)
}

@Test
fun `run multiple ImageComposeScene`() {
for (i in 1..300) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.PointerKeyboardModifiers
import androidx.compose.ui.input.pointer.PointerType
import androidx.compose.ui.node.RootForTest
import androidx.compose.ui.platform.PlatformContext
import androidx.compose.ui.platform.WindowInfo
import androidx.compose.ui.platform.WindowInfoImpl
import androidx.compose.ui.scene.ComposeSceneContext
import androidx.compose.ui.scene.ComposeScenePointer
import androidx.compose.ui.scene.MultiLayerComposeScene
import androidx.compose.ui.unit.Constraints
Expand Down Expand Up @@ -132,11 +136,29 @@ class ImageComposeScene @ExperimentalComposeUiApi constructor(

private val surface = Surface.makeRasterN32Premul(width, height)

private val imageSize = IntSize(width, height)

private val _windowInfo = WindowInfoImpl().apply {
isWindowFocused = true
containerSize = imageSize
}

private val _platformContext = object : PlatformContext by PlatformContext.Empty {
override val windowInfo: WindowInfo
get() = _windowInfo
}

private val _sceneContext = object : ComposeSceneContext {
override val platformContext: PlatformContext
get() = _platformContext
}

private val scene = MultiLayerComposeScene(
density = density,
layoutDirection = layoutDirection,
size = IntSize(width, height),
size = imageSize,
coroutineContext = coroutineContext,
composeSceneContext = _sceneContext
).also {
it.setContent(content = content)
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d1f3898

Please # to comment.