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

Avoid calling SkiaLayer.revalidate() when the window itself is moved. #1010

Merged
merged 1 commit into from
Jan 15, 2025

Conversation

m-sasha
Copy link
Member

@m-sasha m-sasha commented Jan 13, 2025

Calling SkiaLayer.revalidate() when the window moves causes unnecessary layout and repainting.
This PR makes sure to only call it when the actual position of the layer in the window changes.

@m-sasha m-sasha requested a review from igordmn January 13, 2025 13:28
@m-sasha
Copy link
Member Author

m-sasha commented Jan 13, 2025

I still need to verify that it doesn't break what #988 was fixing.

@m-sasha
Copy link
Member Author

m-sasha commented Jan 15, 2025

The following may be a suitable reproducer of the issue originally fixed in #988

@OptIn(ExperimentalFoundationApi::class)
fun main() = SwingUtilities.invokeAndWait {
    JFrame("Compose Test").also {
        it.size = Dimension(800, 700)
        val panel = JPanel()
        it.contentPane.add(panel)
        it.contentPane.layout = null
        panel.setSize(400, 300)
        panel.setBounds(100, 100, 400, 500)
        val composePanel = ComposePanel()
        composePanel.setSize(400, 300)
        composePanel.apply {
            setContent {
                Column(Modifier.size(400.dp, 500.dp)) {
                    Box(
                        Modifier
                            .size(400.dp, 200.dp)
                            .background(Color.Red)
                            .onClick {
                                panel.location = panel.location.let { l -> Point(l.x + 20, l.y + 20) }
                                println("Moving")
                            }
                    )
                    Box(
                        Modifier
                            .size(400.dp, 200.dp)
                            .background(Color.Blue)
                            .onClick {
                                composePanel.size = composePanel.size.let { s -> Dimension(s.width + 1, s.height + 1) }
                                println("Resizing")
                            }
                    )
                }
            }
        }
        panel.add(composePanel)
        it.isVisible = true
    }
}

Clicking the red rectangle should move the whole thing by 20 pixels down and to the right. If we remove the AncestorListener from SkiaLayer.awt.kt altogether, then it doesn't move (clicking the blue rectangle causes it to redraw at the correct location).
With the version in this PR, it appears to work correctly when clicking the red part.

Copy link
Collaborator

@igordmn igordmn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m-sasha m-sasha merged commit 555dcff into master Jan 15, 2025
6 checks passed
@m-sasha m-sasha deleted the m-sasha/fix-layout-on-window-move branch January 15, 2025 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants