scoop:scoop.surface:0.1.0
src/shared/host/shortcuts.scoop
Selected production source · src/shared/host/shortcuts.scoop
1
package scoop.surface3
/** One registration shared by the host's current and future window roots. */4
internal class HostInputAction(5
val owner: SurfaceHost,6
val handler: (RoutedEventContext, InputEvent) -> Unit7
): Closeable {8
var closed = false9
val active: Boolean get() = !closed && owner.acceptsShortcuts11
fun accepts(input: InputScope): Boolean {12
if (!active || input.owner.path(input).size == 0L)13
return false14
return when (input.owner.currentFocusBoundary) {15
case Some(_) -> false16
case None -> true17
}18
}20
public override fun close() {21
owner.checkThread()22
if (closed)23
return24
closed = true25
when (owner.shortcutAction) {26
case Some(val action) -> if (action === this) {27
owner.shortcutAction = None28
}29
case None -> {}30
}31
}32
}34
internal fun SurfaceHost.registerShortcuts(35
handler: (RoutedEventContext, InputEvent) -> Unit36
): HostInputAction {37
checkThread()38
if (!acceptsShortcuts)39
throw IllegalStateException(Some("Surface host is closed"))40
when (shortcutAction) {41
case Some(_) -> throw IllegalArgumentException(Some("host already has a shortcut scope"))42
case None -> {}43
}44
val action = HostInputAction(this, handler)45
shortcutAction = Some(action)46
return action47
}49
internal fun SurfaceHost.bindShortcuts(input: InputScope) {50
input.addHostShortcutAction {51
context,52
event -> when (shortcutAction) {53
case Some(val action) -> if (action.accepts(context.currentTarget)) {54
action.handler(context, event)55
}56
case None -> {}57
}58
}59
}