Scoop API

scoop:scoop.window:0.1.0

src/input/shortcuts.scoop

Selected production source · src/input/shortcuts.scoop

1package scoop.window
3/** Choose logical meaning or a physical keyboard position explicitly. */
4public enum ShortcutKey {
5 Logical(LogicalKey), Physical(PhysicalKey)
6}
8/** Primary follows the host convention; the other flags describe actual modifiers. */
9public struct ShortcutModifiers(
10 val primary: Boolean = false,
11 val shift: Boolean = false,
12 val control: Boolean = false,
13 val alt: Boolean = false,
14 val superKey: Boolean = false,
15 val function: Boolean = false
18/** One immutable key combination, shared by matching and display. */
19public class ShortcutChord public constructor(
20 public val key: ShortcutKey,
21 public val modifiers: ShortcutModifiers = ShortcutModifiers()
22) {
23 init {
24 when (key) {
25 case Logical(LogicalKey.Dead(_)) -> throw IllegalArgumentException(
26 Some("dead keys cannot be shortcut bindings")
27 )
28 case Logical(LogicalKey.Character(val text)) -> requireWindow(
29 text.byteLength > 0L,
30 "shortcut character cannot be empty"
31 )
32 else -> {}
33 }
34 }
35 public fun matches(event: KeyEvent): Boolean =
36 chordMatches(this, event, platformPrimaryModifier())
38 public fun equivalentTo(other: ShortcutChord): Boolean =
39 equivalentChords(this, other, platformPrimaryModifier())
42internal enum PrimaryModifier {
43 Super, Control
46internal fun resolvedModifiers(
47 value: ShortcutModifiers,
48 primary: PrimaryModifier
49): ShortcutModifiers =
50 ShortcutModifiers(
51 shift = value.shift,
52 control = value.control || (value.primary && primary == PrimaryModifier.Control),
53 alt = value.alt,
54 superKey = value.superKey || (value.primary && primary == PrimaryModifier.Super),
55 function = value.function
56 )
58internal fun chordMatches(
59 chord: ShortcutChord,
60 event: KeyEvent,
61 primary: PrimaryModifier
62): Boolean {
63 val value = resolvedModifiers(chord.modifiers, primary)
64 val actual = event.modifiers
65 if (actual.altGraph == Some(true))
66 return false
67 if (
68 value.shift !=
69 actual.shift ||
70 value.control !=
71 actual.control ||
72 value.alt !=
73 actual.alt ||
74 value.superKey !=
75 actual.superKey ||
76 value.function !=
77 actual.function
78 )
79 return false
80 return when (chord.key) {
81 case Logical(val key) -> key.matchesShortcut(event.logicalKey)
82 case Physical(val key) -> key == event.physicalKey
83 }
86internal fun equivalentChords(
87 left: ShortcutChord,
88 right: ShortcutChord,
89 primary: PrimaryModifier
90): Boolean {
91 if (resolvedModifiers(left.modifiers, primary) != resolvedModifiers(right.modifiers, primary))
92 return false
93 return when ((left.key, right.key)) {
94 case(Logical(val a), Logical(val b)) -> a.matchesShortcut(b)
95 case(Physical(val a), Physical(val b)) -> a == b
96 else -> false
97 }
100/** Compares received keys without deriving keyboard events from inserted text. */
101public fun LogicalKey.matchesShortcut(other: LogicalKey): Boolean =
102 when ((this, other)) {
103 case(Character(val left), Character(val right)) -> platformShortcutCharactersEqual(
104 left,
105 right
106 )
107 case(Dead(_), _) -> false
108 case(_, Dead(_)) -> false
109 else -> this == other
110 }
112public class ShortcutLabel internal constructor(
113 public val parts: Array<String>,
114 public val separator: String
117/** Queries the active keyboard layout without changing its input source or focus. */
118public fun Window.shortcutLabel(chord: ShortcutChord): ShortcutLabel {
119 owner.requireOpen()
120 return platformShortcutLabel(chord)