Scoop API

scoop:scoop.window:0.1.0

platform/darwin/commands.scoop

Selected production source · platform/darwin/commands.scoop

1package scoop.window
3internal fun platformTextCommands(
4 pointer: Ptr<Unit>,
5 key: LogicalKey,
6 modifiers: Modifiers,
7 state: ElementState,
8 synthetic: Boolean
9): Array<TextEditCommand> {
10 if (state != ElementState.Pressed || synthetic)
11 return []
12 @Unsafe
13 {
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 -> @Unsafe
22 {
23 return@Array decodeTextCommand(nativeTextCommand(pointer, index))
24 }
25 }
26 }
29private 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.Copy
43 41 -> TextEditCommand.Cut
44 42 -> TextEditCommand.Paste
45 43 -> TextEditCommand.SelectAll
46 44 -> TextEditCommand.Undo
47 45 -> TextEditCommand.Redo
48 46 -> TextEditCommand.InsertNewline
49 47 -> TextEditCommand.InsertTab
50 48 -> TextEditCommand.InsertBacktab
51 49 -> TextEditCommand.Cancel
52 50 -> TextEditCommand.DeleteSelection
53 else -> throw WindowException("unknown native text command", WindowErrorKind.NativeFailure)
54 }
57private fun decodeMovement(code: Int): TextMovement =
58 when (code) {
59 1 -> TextMovement.Backward
60 2 -> TextMovement.Forward
61 3 -> TextMovement.Left
62 4 -> TextMovement.Right
63 5 -> TextMovement.Up
64 6 -> TextMovement.Down
65 7 -> TextMovement.WordBackward
66 8 -> TextMovement.WordForward
67 9 -> TextMovement.WordLeft
68 10 -> TextMovement.WordRight
69 11 -> TextMovement.LineStart
70 12 -> TextMovement.LineEnd
71 13 -> TextMovement.LineLeft
72 14 -> TextMovement.LineRight
73 15 -> TextMovement.ParagraphStart
74 16 -> TextMovement.ParagraphEnd
75 17 -> TextMovement.DocumentStart
76 18 -> TextMovement.DocumentEnd
77 19 -> TextMovement.PageUp
78 20 -> TextMovement.PageDown
79 else -> throw WindowException("unknown native text movement", WindowErrorKind.NativeFailure)
80 }
82@Extern(name = "scoop_window_command_count")
83private fun nativeTextCommandCount(event: Ptr<Unit>): Long
85@Extern(name = "scoop_window_command")
86private fun nativeTextCommand(event: Ptr<Unit>, index: Long): Int