Scoop API

scoop:scoop.surface:0.1.0

src/shared/host/shortcuts.scoop

Selected production source · src/shared/host/shortcuts.scoop

1package scoop.surface
3/** One registration shared by the host's current and future window roots. */
4internal class HostInputAction(
5 val owner: SurfaceHost,
6 val handler: (RoutedEventContext, InputEvent) -> Unit
7): Closeable {
8 var closed = false
9 val active: Boolean get() = !closed && owner.acceptsShortcuts
11 fun accepts(input: InputScope): Boolean {
12 if (!active || input.owner.path(input).size == 0L)
13 return false
14 return when (input.owner.currentFocusBoundary) {
15 case Some(_) -> false
16 case None -> true
17 }
18 }
20 public override fun close() {
21 owner.checkThread()
22 if (closed)
23 return
24 closed = true
25 when (owner.shortcutAction) {
26 case Some(val action) -> if (action === this) {
27 owner.shortcutAction = None
28 }
29 case None -> {}
30 }
31 }
34internal fun SurfaceHost.registerShortcuts(
35 handler: (RoutedEventContext, InputEvent) -> Unit
36): 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 action
49internal 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 }