Scoop API

scoop:scoop.surface:0.1.0

src/shared/shortcuts/dispatch.scoop

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

1package scoop.surface
3import scoop.window.*
5internal fun runShortcutBindings(
6 context: RoutedEventContext,
7 event: InputEvent,
8 bindings: Array<ShortcutBinding>,
9 isActive: () -> Boolean
10) {
11 if (!isActive())
12 return
13 val key =
14 when (event) {
15 case Keyboard(val value) -> value
16 else -> return
17 }
18 if (key.state != ElementState.Pressed || key.synthetic || !shortcutAllowed(context, key))
19 return
20 var chosen: ShortcutBinding? = None
21 for (binding in bindings) {
22 if (!isActive())
23 return
24 if (
25 binding.enabled &&
26 (!key.isRepeat || binding.allowRepeat) &&
27 binding.chord.matches(key) &&
28 binding.canExecute()
29 ) {
30 if (!isActive())
31 return
32 when (chosen) {
33 case Some(_) -> throw IllegalArgumentException(
34 Some("multiple shortcut bindings match this key event")
35 )
36 case None -> chosen = Some(binding)
37 }
38 }
39 }
40 when (chosen) {
41 case Some(val binding) -> if (isActive()) {
42 context.consume()
43 binding.action()
44 }
45 case None -> {}
46 }