Scoop API

scoop:scoop.window:0.1.0

src/text/services_native.scoop

Selected production source · src/text/services_native.scoop

1package scoop.window
3internal fun platformTextServiceCapabilities(): TextServiceCapabilities {
4 @Unsafe
5 {
6 val flags = nativeTextServiceFlags()
7 return TextServiceCapabilities(
8 flags.and(1) != 0,
9 flags.and(2) != 0,
10 flags.and(4) != 0,
11 flags.and(8) != 0,
12 flags.and(16) != 0
13 )
14 }
17internal fun platformCheckText(text: String, options: TextCheckingOptions): Array<
18 TextCheckFinding
19> {
20 @Unsafe
21 {
22 val flags =
23 (
24 if (options.spelling)
25 1
26 else
27 0
28 ) +
29 (
30 if (options.grammar)
31 2
32 else
33 0
34 ) +
35 (
36 if (options.replacements)
37 4
38 else
39 0
40 )
41 val result =
42 text.withUtf8Bytes {
43 bytes,
44 length -> @Unsafe
45 {
46 return@withUtf8Bytes (options.language ?: "").withUtf8Bytes {
47 language,
48 languageLength -> @Unsafe
49 {
50 return@withUtf8Bytes nativeTextCheck(
51 bytes,
52 length,
53 flags,
54 language,
55 languageLength
56 )
57 }
58 }
59 }
60 }
61 val owner = created(result, "check text")
62 try {
63 return Array<TextCheckFinding>(nativeTextServiceCount(owner)) {
64 index -> @Unsafe
65 {
66 val item = nativeTextServiceItem(owner, index)
67 return@Array TextCheckFinding(
68 Utf8Range(item.start, item.end),
69 when (item.kind) {
70 1 -> TextCheckKind.Spelling
71 2 -> TextCheckKind.Grammar
72 3 -> TextCheckKind.Replacement
73 else -> throw WindowException(
74 "decode text check",
75 WindowErrorKind.NativeFailure
76 )
77 },
78 textServiceString(owner, index, -1L),
79 Array<String>(item.suggestions) {
80 suggestion -> textServiceString(owner, index, suggestion)
81 }
82 )
83 }
84 }
85 } finally {
86 nativeTextServiceDestroy(owner)
87 }
88 }
91private fun textServiceString(owner: Ptr<Unit>, index: Long, suggestion: Long): String {
92 @Unsafe
93 {
94 val value = nativeTextServiceString(owner, index, suggestion)
95 return String.fromUtf8(value.bytes, value.length)
96 }
99internal fun platformPerformTextService(name: String, text: String): String? {
100 @Unsafe
101 {
102 val result =
103 name.withUtf8Bytes {
104 service,
105 serviceLength -> @Unsafe
106 {
107 return@withUtf8Bytes text.withUtf8Bytes {
108 bytes,
109 length -> @Unsafe
110 {
111 return@withUtf8Bytes nativePerformTextService(
112 service,
113 serviceLength,
114 bytes,
115 length
116 )
117 }
118 }
119 }
120 }
121 return decodeClipboardText(result, "perform text service")
122 }
125internal fun platformShowTextDefinition(
126 window: Ptr<Unit>,
127 text: String,
128 anchor: LogicalRect
129): Boolean {
130 @Unsafe
131 {
132 return text.withUtf8Bytes {
133 bytes,
134 length -> @Unsafe
135 {
136 val status =
137 nativeShowTextDefinition(
138 window,
139 bytes,
140 length,
141 anchor.origin.x,
142 anchor.origin.y,
143 anchor.size.width,
144 anchor.size.height
145 )
146 if (status > 1L)
147 checkNativeStatus(status, "show text definition")
148 return@withUtf8Bytes status == 1L
149 }
150 }
151 }
154@CLayout
155private struct NativeTextCheckItem(
156 val start: Long,
157 val end: Long,
158 val kind: Int,
159 val suggestions: Long
162@CLayout
163private struct NativeTextServiceString(val bytes: Ptr<UInt8>, val length: Long)
165@Extern(name = "scoop_window_text_service_flags")
166private fun nativeTextServiceFlags(): Int
168@Extern(name = "scoop_window_text_check")
169private fun nativeTextCheck(
170 text: Ptr<UInt8>,
171 length: Long,
172 flags: Int,
173 language: Ptr<UInt8>,
174 languageLength: Long
175): NativeCreateResult
177@Extern(name = "scoop_window_text_service_count")
178private fun nativeTextServiceCount(owner: Ptr<Unit>): Long
180@Extern(name = "scoop_window_text_service_item")
181private fun nativeTextServiceItem(owner: Ptr<Unit>, index: Long): NativeTextCheckItem
183@Extern(name = "scoop_window_text_service_string")
184private fun nativeTextServiceString(
185 owner: Ptr<Unit>,
186 index: Long,
187 suggestion: Long
188): NativeTextServiceString
190@Extern(name = "scoop_window_text_service_destroy")
191private fun nativeTextServiceDestroy(owner: Ptr<Unit>)
193@Extern(name = "scoop_window_text_service_perform")
194private fun nativePerformTextService(
195 name: Ptr<UInt8>,
196 nameLength: Long,
197 text: Ptr<UInt8>,
198 length: Long
199): NativeClipboardText
201@Extern(name = "scoop_window_text_definition")
202private fun nativeShowTextDefinition(
203 window: Ptr<Unit>,
204 text: Ptr<UInt8>,
205 length: Long,
206 x: Double,
207 y: Double,
208 width: Double,
209 height: Double
210): Long