Scoop API

scoop:scoop.window:0.1.0

src/native/text.scoop

Selected production source · src/native/text.scoop

1package scoop.window
3/** Enables platform composition for this window; ordinary text uses the same edit channel. */
4public fun Window.setImeAllowed(allowed: Boolean): RequestStatus {
5 owner.requireOpen()
6 @Unsafe
7 {
8 if (
9 nativeImeAllowed(
10 owner.pointer!!,
11 if (allowed)
12 1
13 else
14 0
15 ) !=
16 0
17 )
18 throw WindowException("native text input", WindowErrorKind.Unavailable)
19 }
20 return RequestStatus.Applied
23/** Publishes a candidate-window rectangle in logical content coordinates. */
24public fun Window.setImeCursorArea(area: LogicalRect) {
25 owner.requireOpen()
26 checkImeCursorArea(area)
27 @Unsafe
28 {
29 checkNativeStatus(
30 nativeImeCaret(
31 owner.pointer!!,
32 area.origin.x,
33 area.origin.y,
34 area.size.width,
35 area.size.height
36 ),
37 "ime_caret"
38 )
39 }
42internal fun checkImeCursorArea(area: LogicalRect) {
43 checkRect(area)
44 requireWindow(
45 (area.origin.x + area.size.width).isFinite() &&
46 (area.origin.y + area.size.height).isFinite(),
47 "IME cursor rectangle overflow"
48 )
51@CLayout
52internal struct NativeTextEdit(
53 val kind: Int,
54 val anchor: Long,
55 val cursor: Long,
56 val text: Ptr<UInt8>?,
57 val textLength: Long,
58 val hasReplacement: Int,
59 val replacementStart: Long,
60 val replacementEnd: Long
63@Extern(name = "scoop_window_ime_allowed")
64internal fun nativeImeAllowed(window: Ptr<Unit>, allowed: Int): Int
66@Extern(name = "scoop_window_ime_caret")
67internal fun nativeImeCaret(
68 window: Ptr<Unit>,
69 x: Double,
70 y: Double,
71 width: Double,
72 height: Double
73): Long
75@Extern(name = "scoop_window_text_count")
76internal fun nativeTextCount(event: Ptr<Unit>): Long
78@Extern(name = "scoop_window_text_edit")
79internal fun nativeTextEdit(event: Ptr<Unit>, index: Long): NativeTextEdit
81/** Copies native-owned strings before releasing the enclosing event. */
82internal fun copyNativeTextBatch(
83 pointer: Ptr<Unit>,
84 window: Long,
85 event: NativeWindowEventData
86): TextInputBatch {
87 @Unsafe
88 {
89 val count = nativeTextCount(pointer)
90 val operations = ArrayList<TextInputOperation>()
91 for (index in 0L ..< count) {
92 val edit = nativeTextEdit(pointer, index)
93 val text =
94 when (edit.text) {
95 case Some(val bytes) -> String.fromUtf8(bytes, edit.textLength)
96 case None -> ""
97 }
98 val replacement =
99 if (edit.hasReplacement != 0)
100 TextReplacement.Range(Utf8Range(edit.replacementStart, edit.replacementEnd))
101 else
102 TextReplacement.CurrentSelection
103 operations.add(
104 when (edit.kind) {
105 0 -> TextInputOperation.Preedit(
106 text,
107 if (edit.anchor < 0L)
108 None
109 else
110 Some(Utf8Selection(edit.anchor, edit.cursor)),
111 replacement
112 )
113 1 -> TextInputOperation.Commit(text, replacement)
114 2 -> TextInputOperation.ClearPreedit
115 3 -> TextInputOperation.DeleteSurrounding(edit.anchor, edit.cursor)
116 4 -> TextInputOperation.SetSelection(Utf8Selection(edit.anchor, edit.cursor))
117 else -> throw WindowException(
118 "unknown native text operation",
119 WindowErrorKind.NativeFailure
120 )
121 }
122 )
123 }
124 return TextInputBatch(
125 if (event.flags.and(1) != 0)
126 Some(event.b)
127 else
128 None,
129 operations,
130 when (platformTextConnection(window, event)) {
131 case Some(val connection) -> Some(TextInputOrigin(connection, event.c))
132 case None -> None
133 }
134 )
135 }