Unverified Commit 47985faa authored by Aleksandr Veselov's avatar Aleksandr Veselov Committed by GitHub

Make focus tracking work for windows (#503)

parent cb53dfe0
...@@ -4,6 +4,7 @@ import kotlinx.coroutines.* ...@@ -4,6 +4,7 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.swing.Swing import kotlinx.coroutines.swing.Swing
import java.awt.* import java.awt.*
import java.awt.event.FocusEvent
import java.awt.event.InputMethodEvent import java.awt.event.InputMethodEvent
import java.beans.PropertyChangeEvent import java.beans.PropertyChangeEvent
import javax.accessibility.Accessible import javax.accessibility.Accessible
...@@ -92,10 +93,14 @@ internal open class HardwareLayer( ...@@ -92,10 +93,14 @@ internal open class HardwareLayer(
fun requestNativeFocusOnAccessible(accessible: Accessible?) { fun requestNativeFocusOnAccessible(accessible: Accessible?) {
_focusedAccessible = accessible _focusedAccessible = accessible
val focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager() when (hostOs) {
val listeners = focusManager.getPropertyChangeListeners("focusOwner") OS.Windows -> requestAccessBridgeFocusOnAccessible()
val event = PropertyChangeEvent(focusManager, "focusOwner", null, accessible) OS.MacOS -> requestMacOSFocusOnAccessible(accessible)
listeners.forEach { it.propertyChange(event) } else -> {
_focusedAccessible = null
return
}
}
// Listener spawns asynchronous notification post procedure, reading current focus owner // Listener spawns asynchronous notification post procedure, reading current focus owner
// and its accessibility context. This timeout is used to deal with concurrency // and its accessibility context. This timeout is used to deal with concurrency
...@@ -106,6 +111,18 @@ internal open class HardwareLayer( ...@@ -106,6 +111,18 @@ internal open class HardwareLayer(
_focusedAccessible = null _focusedAccessible = null
} }
} }
private fun requestAccessBridgeFocusOnAccessible() {
val focusEvent = FocusEvent(this, FocusEvent.FOCUS_GAINED)
focusListeners.forEach { it.focusGained(focusEvent) }
}
private fun requestMacOSFocusOnAccessible(accessible: Accessible?) {
val focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager()
val listeners = focusManager.getPropertyChangeListeners("focusOwner")
val event = PropertyChangeEvent(focusManager, "focusOwner", null, accessible)
listeners.forEach { it.propertyChange(event) }
}
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment