-
Notifications
You must be signed in to change notification settings - Fork 5.7k
[JEWEL-368] Fix custom title bar mouse event (drag/press) not working correctly #3362
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,115 @@ | ||||||||||||||||||||||||||
| package org.jetbrains.jewel.window.utils | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| import androidx.compose.runtime.Composable | ||||||||||||||||||||||||||
| import androidx.compose.runtime.DisposableEffect | ||||||||||||||||||||||||||
| import androidx.compose.ui.Modifier | ||||||||||||||||||||||||||
| import androidx.compose.ui.layout.LayoutCoordinates | ||||||||||||||||||||||||||
| import androidx.compose.ui.layout.positionInWindow | ||||||||||||||||||||||||||
| import androidx.compose.ui.node.CompositionLocalConsumerModifierNode | ||||||||||||||||||||||||||
| import androidx.compose.ui.node.GlobalPositionAwareModifierNode | ||||||||||||||||||||||||||
| import androidx.compose.ui.node.ModifierNodeElement | ||||||||||||||||||||||||||
| import androidx.compose.ui.node.currentValueOf | ||||||||||||||||||||||||||
| import androidx.compose.ui.platform.InspectorInfo | ||||||||||||||||||||||||||
| import com.jetbrains.WindowDecorations | ||||||||||||||||||||||||||
| import org.jetbrains.jewel.window.DecoratedWindowScope | ||||||||||||||||||||||||||
| import org.jetbrains.jewel.window.LocalTitleBarInfo | ||||||||||||||||||||||||||
| import org.jetbrains.jewel.window.TitleBarInfo | ||||||||||||||||||||||||||
| import java.awt.event.MouseAdapter | ||||||||||||||||||||||||||
| import java.awt.event.MouseEvent | ||||||||||||||||||||||||||
| import kotlin.collections.set | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| internal data class ClientRegion( | ||||||||||||||||||||||||||
| val x: Float, | ||||||||||||||||||||||||||
| val y: Float, | ||||||||||||||||||||||||||
| val width: Int, | ||||||||||||||||||||||||||
| val height: Int, | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| public fun Modifier.clientRegion(key: String): Modifier = then(RegisterClientRegionElement(key)) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| private data class RegisterClientRegionElement( | ||||||||||||||||||||||||||
| private val key: String | ||||||||||||||||||||||||||
| ) : ModifierNodeElement<RegisterClientRegionNode>() { | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| override fun create() = RegisterClientRegionNode(key) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| override fun update(node: RegisterClientRegionNode) = node.updateKey(key) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| override fun InspectorInfo.inspectableProperties() { | ||||||||||||||||||||||||||
| name = "registerRegion" | ||||||||||||||||||||||||||
| properties["key"] = key | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| private class RegisterClientRegionNode( | ||||||||||||||||||||||||||
| var key: String | ||||||||||||||||||||||||||
| ) : Modifier.Node(), GlobalPositionAwareModifierNode, CompositionLocalConsumerModifierNode { | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| private var titleBarInfo: TitleBarInfo? = null | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| override fun onAttach() { | ||||||||||||||||||||||||||
| titleBarInfo = currentValueOf(LocalTitleBarInfo) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| override fun onGloballyPositioned(coordinates: LayoutCoordinates) { | ||||||||||||||||||||||||||
| val info = titleBarInfo ?: return | ||||||||||||||||||||||||||
| val position = coordinates.positionInWindow() | ||||||||||||||||||||||||||
| val size = coordinates.size | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| info.clientRegions[key] = ClientRegion( | ||||||||||||||||||||||||||
| x = position.x, | ||||||||||||||||||||||||||
| y = position.y, | ||||||||||||||||||||||||||
| width = size.width, | ||||||||||||||||||||||||||
| height = size.height | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| override fun onDetach() { | ||||||||||||||||||||||||||
| titleBarInfo?.clientRegions?.remove(key) | ||||||||||||||||||||||||||
| titleBarInfo = null | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| fun updateKey(newKey: String) { | ||||||||||||||||||||||||||
| if (key != newKey) { | ||||||||||||||||||||||||||
| titleBarInfo?.clientRegions?.remove(key) | ||||||||||||||||||||||||||
| key = newKey | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+72
to
+77
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we need to keep the last defined value in the 'clientRegions' map? The code bellow is not correct, but I think you can get the main idea.
Suggested change
|
||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @Composable | ||||||||||||||||||||||||||
| internal fun DecoratedWindowScope.WindowMouseEventEffect(titleBar: WindowDecorations.CustomTitleBar) { | ||||||||||||||||||||||||||
| val titleBarInfo = LocalTitleBarInfo.current | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| DisposableEffect(window, window.graphicsConfiguration, titleBar) { | ||||||||||||||||||||||||||
| val graphicsConfig = window.graphicsConfiguration | ||||||||||||||||||||||||||
| val scaleX = graphicsConfig?.defaultTransform?.scaleX ?: 1.0 | ||||||||||||||||||||||||||
| val scaleY = graphicsConfig?.defaultTransform?.scaleY ?: 1.0 | ||||||||||||||||||||||||||
| val listener = object : MouseAdapter() { | ||||||||||||||||||||||||||
| override fun mousePressed(e: MouseEvent) = updateHitTest(e) | ||||||||||||||||||||||||||
| override fun mouseReleased(e: MouseEvent) = updateHitTest(e) | ||||||||||||||||||||||||||
| override fun mouseDragged(e: MouseEvent) = updateHitTest(e) | ||||||||||||||||||||||||||
| override fun mouseMoved(e: MouseEvent) = updateHitTest(e) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| private fun updateHitTest(e: MouseEvent) { | ||||||||||||||||||||||||||
| // Convert from AWT raw pixels to Compose positionInWindow() coordinates | ||||||||||||||||||||||||||
| val x = e.x * scaleX | ||||||||||||||||||||||||||
| val y = e.y * scaleY | ||||||||||||||||||||||||||
|
Comment on lines
+95
to
+97
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you checked if Also, if you use the Rect suggestion from the other comment, you can convert the event to offset using something like this: |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| val isClientRegion = titleBarInfo.clientRegions.any { region -> | ||||||||||||||||||||||||||
| (x >= region.value.x && x <= region.value.x + region.value.width) && | ||||||||||||||||||||||||||
| (y >= region.value.y && y <= region.value.y + region.value.height) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| titleBar.forceHitTest(isClientRegion) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| window.addMouseListener(listener) | ||||||||||||||||||||||||||
| window.addMouseMotionListener(listener) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| onDispose { | ||||||||||||||||||||||||||
| window.removeMouseListener(listener) | ||||||||||||||||||||||||||
| window.removeMouseMotionListener(listener) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about using
androidx.compose.ui.geometry.Rectinstead?You can get the create it with
Rect(it.positionOnScreen(), it.size.toSize())and use the.contains()method to check if the point is inside the area.