scoop:scoop.window:0.1.0
src/input/shortcuts.scoop
Selected production source · src/input/shortcuts.scoop
1
package scoop.window3
/** Choose logical meaning or a physical keyboard position explicitly. */4
public enum ShortcutKey {5
Logical(LogicalKey), Physical(PhysicalKey)6
}8
/** Primary follows the host convention; the other flags describe actual modifiers. */9
public 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 = false16
)18
/** One immutable key combination, shared by matching and display. */19
public 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())40
}42
internal enum PrimaryModifier {43
Super, Control44
}46
internal fun resolvedModifiers(47
value: ShortcutModifiers,48
primary: PrimaryModifier49
): 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.function56
)58
internal fun chordMatches(59
chord: ShortcutChord,60
event: KeyEvent,61
primary: PrimaryModifier62
): Boolean {63
val value = resolvedModifiers(chord.modifiers, primary)64
val actual = event.modifiers65
if (actual.altGraph == Some(true))66
return false67
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.function78
)79
return false80
return when (chord.key) {81
case Logical(val key) -> key.matchesShortcut(event.logicalKey)82
case Physical(val key) -> key == event.physicalKey83
}84
}86
internal fun equivalentChords(87
left: ShortcutChord,88
right: ShortcutChord,89
primary: PrimaryModifier90
): Boolean {91
if (resolvedModifiers(left.modifiers, primary) != resolvedModifiers(right.modifiers, primary))92
return false93
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 == b96
else -> false97
}98
}100
/** Compares received keys without deriving keyboard events from inserted text. */101
public fun LogicalKey.matchesShortcut(other: LogicalKey): Boolean =102
when ((this, other)) {103
case(Character(val left), Character(val right)) -> platformShortcutCharactersEqual(104
left,105
right106
)107
case(Dead(_), _) -> false108
case(_, Dead(_)) -> false109
else -> this == other110
}112
public class ShortcutLabel internal constructor(113
public val parts: Array<String>,114
public val separator: String115
)117
/** Queries the active keyboard layout without changing its input source or focus. */118
public fun Window.shortcutLabel(chord: ShortcutChord): ShortcutLabel {119
owner.requireOpen()120
return platformShortcutLabel(chord)121
}