Scoop API

scoop:scoop.surface:0.1.0

src/shared/input/defaults.scoop

Selected production source · src/shared/input/defaults.scoop

1package scoop.surface
3internal enum InputDefaultKind {
4 Ordinary, Shortcut, HostShortcut
5}
7internal class InputDefaultAction(
8 val scope: InputScope,
9 val handler: (RoutedEventContext, InputEvent) -> Unit,
10 val kind: InputDefaultKind
11): Closeable {
12 var closed = false
13 public override fun close() {
14 scope.owner.checkThread()
15 if (closed)
16 return
17 closed = true
18 val actions = scope.defaultActions
19 var index = 0L
20 while (index < actions.size) {
21 if (actions[index] === this) {
22 val removed = index
23 scope.defaultActions =
24 Array<InputDefaultAction>(actions.size - 1L) {
25 position -> actions[
26 if (position < removed)
27 position
28 else
29 position + 1L
30 ]
31 }
32 return
33 }
34 index += 1L
35 }
36 }
39/** Adds ordinary node behavior before the node's shortcut bindings. */
40public fun InputScope.addDefaultAction(
41 handler: (RoutedEventContext, InputEvent) -> Unit
42): Closeable {
43 checkOpen()
44 val value = InputDefaultAction(this, handler, InputDefaultKind.Ordinary)
45 appendDefaultAction(value)
46 return value
49internal fun InputScope.addShortcutAction(
50 handler: (RoutedEventContext, InputEvent) -> Unit
51): Closeable {
52 checkOpen()
53 for (action in defaultActions)
54 requireInput(
55 action.kind != InputDefaultKind.Shortcut,
56 "input scope already has a shortcut scope"
57 )
58 val value = InputDefaultAction(this, handler, InputDefaultKind.Shortcut)
59 appendDefaultAction(value)
60 return value
63internal fun InputScope.addHostShortcutAction(handler: (RoutedEventContext, InputEvent) -> Unit) {
64 checkOpen()
65 requireInput(this === owner.rootScope, "host shortcuts require the window root")
66 for (action in defaultActions)
67 requireInput(
68 action.kind != InputDefaultKind.HostShortcut,
69 "window already has host shortcuts"
70 )
71 appendDefaultAction(InputDefaultAction(this, handler, InputDefaultKind.HostShortcut))
74private fun InputScope.appendDefaultAction(value: InputDefaultAction) {
75 val previous = defaultActions
76 defaultActions =
77 Array<InputDefaultAction>(previous.size + 1L) {
78 index -> if (index == previous.size)
79 value
80 else
81 previous[index]
82 }
85internal fun InputScope.retireDefaultActions() {
86 for (action in defaultActions)
87 action.closed = true
88 defaultActions = []