forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Filter events if there are focusable
WINDOW
layers (#1187)
## Proposed Changes - Filter events if there are focusable `WINDOW` layers ## Testing Test: open focusable `WINDOW` layer without `dismissOnClickOutside`, try to click on the main content
- Loading branch information
1 parent
ab9df91
commit 6c18c40
Showing
4 changed files
with
132 additions
and
68 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
compose/ui/ui/src/desktopMain/kotlin/androidx/compose/ui/awt/AwtEventFilter.desktop.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2024 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 java.awt.event.KeyEvent | ||
import java.awt.event.MouseEvent | ||
|
||
internal interface AwtEventFilter { | ||
fun shouldSendMouseEvent(event: MouseEvent): Boolean = true | ||
fun shouldSendKeyEvent(event: KeyEvent): Boolean = true | ||
|
||
companion object { | ||
val Empty = object : AwtEventFilter { | ||
} | ||
} | ||
} | ||
|
||
internal class AwtEventFilters( | ||
private vararg val filters: AwtEventFilter | ||
) : AwtEventFilter { | ||
override fun shouldSendMouseEvent(event: MouseEvent): Boolean { | ||
return filters.all { it.shouldSendMouseEvent(event) } | ||
} | ||
|
||
override fun shouldSendKeyEvent(event: KeyEvent): Boolean { | ||
return filters.all { it.shouldSendKeyEvent(event) } | ||
} | ||
} | ||
|
||
/** | ||
* Filter out mouse events that report the primary button has changed state to pressed, | ||
* but aren't themselves a mouse press event. This is needed because on macOS, AWT sends | ||
* us spurious enter/exit events that report the primary button as pressed when resizing | ||
* the window by its corner/edge. This causes false-positives in detectTapGestures. | ||
* See https://github.com/JetBrains/compose-multiplatform/issues/2850 for more details. | ||
*/ | ||
internal object OnlyValidPrimaryMouseButtonFilter : AwtEventFilter { | ||
private var isPrimaryButtonPressed = false | ||
|
||
override fun shouldSendMouseEvent(event: MouseEvent): Boolean { | ||
val eventReportsPrimaryButtonPressed = | ||
(event.modifiersEx and MouseEvent.BUTTON1_DOWN_MASK) != 0 | ||
if ((event.button == MouseEvent.BUTTON1) && | ||
((event.id == MouseEvent.MOUSE_PRESSED) || | ||
(event.id == MouseEvent.MOUSE_RELEASED))) { | ||
isPrimaryButtonPressed = eventReportsPrimaryButtonPressed // Update state | ||
} | ||
if (eventReportsPrimaryButtonPressed && !isPrimaryButtonPressed) { | ||
return false // Ignore such events | ||
} | ||
|
||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters