scoop:scoop.window:0.1.0
platform/darwin/commands.scoop
Selected production source · platform/darwin/commands.scoop
1
package scoop.window3
internal fun platformTextCommands(4
pointer: Ptr<Unit>,5
key: LogicalKey,6
modifiers: Modifiers,7
state: ElementState,8
synthetic: Boolean9
): Array<TextEditCommand> {10
if (state != ElementState.Pressed || synthetic)11
return []12
@Unsafe13
{14
val count = nativeTextCommandCount(pointer)15
if (count == 0L)16
return when (darwinEditingCommand(key, modifiers)) {17
case Some(val command) -> [command]18
case None -> []19
}20
return Array<TextEditCommand>(count) {21
index -> @Unsafe22
{23
return@Array decodeTextCommand(nativeTextCommand(pointer, index))24
}25
}26
}27
}29
private fun decodeTextCommand(code: Int): TextEditCommand {30
val movement = code.and(255)31
if (movement >= 1 && movement <= 20)32
return TextEditCommand.Move(decodeMovement(movement), code.and(256) != 0)33
return when (code) {34
32 -> TextEditCommand.Delete(TextDeletion.Backward)35
33 -> TextEditCommand.Delete(TextDeletion.Forward)36
34 -> TextEditCommand.Delete(TextDeletion.WordBackward)37
35 -> TextEditCommand.Delete(TextDeletion.WordForward)38
36 -> TextEditCommand.Delete(TextDeletion.LineStart)39
37 -> TextEditCommand.Delete(TextDeletion.LineEnd)40
38 -> TextEditCommand.Delete(TextDeletion.ParagraphStart)41
39 -> TextEditCommand.Delete(TextDeletion.ParagraphEnd)42
40 -> TextEditCommand.Copy43
41 -> TextEditCommand.Cut44
42 -> TextEditCommand.Paste45
43 -> TextEditCommand.SelectAll46
44 -> TextEditCommand.Undo47
45 -> TextEditCommand.Redo48
46 -> TextEditCommand.InsertNewline49
47 -> TextEditCommand.InsertTab50
48 -> TextEditCommand.InsertBacktab51
49 -> TextEditCommand.Cancel52
50 -> TextEditCommand.DeleteSelection53
else -> throw WindowException("unknown native text command", WindowErrorKind.NativeFailure)54
}55
}57
private fun decodeMovement(code: Int): TextMovement =58
when (code) {59
1 -> TextMovement.Backward60
2 -> TextMovement.Forward61
3 -> TextMovement.Left62
4 -> TextMovement.Right63
5 -> TextMovement.Up64
6 -> TextMovement.Down65
7 -> TextMovement.WordBackward66
8 -> TextMovement.WordForward67
9 -> TextMovement.WordLeft68
10 -> TextMovement.WordRight69
11 -> TextMovement.LineStart70
12 -> TextMovement.LineEnd71
13 -> TextMovement.LineLeft72
14 -> TextMovement.LineRight73
15 -> TextMovement.ParagraphStart74
16 -> TextMovement.ParagraphEnd75
17 -> TextMovement.DocumentStart76
18 -> TextMovement.DocumentEnd77
19 -> TextMovement.PageUp78
20 -> TextMovement.PageDown79
else -> throw WindowException("unknown native text movement", WindowErrorKind.NativeFailure)80
}82
@Extern(name = "scoop_window_command_count")83
private fun nativeTextCommandCount(event: Ptr<Unit>): Long85
@Extern(name = "scoop_window_command")86
private fun nativeTextCommand(event: Ptr<Unit>, index: Long): Int