Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix positioning of interop views. #922

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ public fun <T : Component> SwingPanel(
val focusSwitcher = remember { FocusSwitcher(componentInfo, focusManager) }

Box(
modifier = modifier.onGloballyPositioned { childCoordinates ->
val coordinates = childCoordinates.parentCoordinates!!
modifier = modifier.onGloballyPositioned { coordinates ->
val location = coordinates.localToWindow(Offset.Zero).round()
val size = coordinates.size
componentInfo.container.setBounds(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.ui.awt

import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.size
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.runApplicationTest
import java.awt.Point
import javax.swing.JPanel
import javax.swing.SwingUtilities
import kotlin.test.Test
import kotlin.test.assertEquals

class SwingPanelTest {
/**
* Test the positioning of a [SwingPanel] with offset.
* See https://github.com/JetBrains/compose-multiplatform/issues/4005
*/
@Test
fun swingPanelWithOffset() = runApplicationTest {
lateinit var panel: JPanel
launchTestApplication {
Window(
onCloseRequest = {}
) {
SwingPanel(
modifier = Modifier.size(100.dp).offset(50.dp, 50.dp),
factory = {
JPanel().also {
panel = it
}
}
)
}
}
awaitIdle()

val locationInRootPane =
SwingUtilities.convertPoint(panel, Point(0, 0), SwingUtilities.getRootPane(panel))
assertEquals(expected = Point(50, 50), locationInRootPane)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ fun <T : UIView> UIKitView(
val interopContext = LocalUIKitInteropContext.current

Place(
modifier.onGloballyPositioned { childCoordinates ->
val coordinates = childCoordinates.parentCoordinates!!
modifier.onGloballyPositioned { coordinates ->
localToWindowOffset = coordinates.localToWindow(Offset.Zero).round()
val newRectInPixels = IntRect(localToWindowOffset, coordinates.size)
if (rectInPixels != newRectInPixels) {
Expand Down Expand Up @@ -195,8 +194,7 @@ fun <T : UIViewController> UIKitViewController(
val interopContext = LocalUIKitInteropContext.current

Place(
modifier.onGloballyPositioned { childCoordinates ->
val coordinates = childCoordinates.parentCoordinates!!
modifier.onGloballyPositioned { coordinates ->
localToWindowOffset = coordinates.localToWindow(Offset.Zero).round()
val newRectInPixels = IntRect(localToWindowOffset, coordinates.size)
if (rectInPixels != newRectInPixels) {
Expand Down