scoop:scoop.window:0.1.0
src/text/services_native.scoop
Selected production source · src/text/services_native.scoop
1
package scoop.window3
internal fun platformTextServiceCapabilities(): TextServiceCapabilities {4
@Unsafe5
{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) != 013
)14
}15
}17
internal fun platformCheckText(text: String, options: TextCheckingOptions): Array<18
TextCheckFinding19
> {20
@Unsafe21
{22
val flags =23
(24
if (options.spelling)25
126
else27
028
) +29
(30
if (options.grammar)31
232
else33
034
) +35
(36
if (options.replacements)37
438
else39
040
)41
val result =42
text.withUtf8Bytes {43
bytes,44
length -> @Unsafe45
{46
return@withUtf8Bytes (options.language ?: "").withUtf8Bytes {47
language,48
languageLength -> @Unsafe49
{50
return@withUtf8Bytes nativeTextCheck(51
bytes,52
length,53
flags,54
language,55
languageLength56
)57
}58
}59
}60
}61
val owner = created(result, "check text")62
try {63
return Array<TextCheckFinding>(nativeTextServiceCount(owner)) {64
index -> @Unsafe65
{66
val item = nativeTextServiceItem(owner, index)67
return@Array TextCheckFinding(68
Utf8Range(item.start, item.end),69
when (item.kind) {70
1 -> TextCheckKind.Spelling71
2 -> TextCheckKind.Grammar72
3 -> TextCheckKind.Replacement73
else -> throw WindowException(74
"decode text check",75
WindowErrorKind.NativeFailure76
)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
}89
}91
private fun textServiceString(owner: Ptr<Unit>, index: Long, suggestion: Long): String {92
@Unsafe93
{94
val value = nativeTextServiceString(owner, index, suggestion)95
return String.fromUtf8(value.bytes, value.length)96
}97
}99
internal fun platformPerformTextService(name: String, text: String): String? {100
@Unsafe101
{102
val result =103
name.withUtf8Bytes {104
service,105
serviceLength -> @Unsafe106
{107
return@withUtf8Bytes text.withUtf8Bytes {108
bytes,109
length -> @Unsafe110
{111
return@withUtf8Bytes nativePerformTextService(112
service,113
serviceLength,114
bytes,115
length116
)117
}118
}119
}120
}121
return decodeClipboardText(result, "perform text service")122
}123
}125
internal fun platformShowTextDefinition(126
window: Ptr<Unit>,127
text: String,128
anchor: LogicalRect129
): Boolean {130
@Unsafe131
{132
return text.withUtf8Bytes {133
bytes,134
length -> @Unsafe135
{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.height145
)146
if (status > 1L)147
checkNativeStatus(status, "show text definition")148
return@withUtf8Bytes status == 1L149
}150
}151
}152
}154
@CLayout155
private struct NativeTextCheckItem(156
val start: Long,157
val end: Long,158
val kind: Int,159
val suggestions: Long160
)162
@CLayout163
private struct NativeTextServiceString(val bytes: Ptr<UInt8>, val length: Long)165
@Extern(name = "scoop_window_text_service_flags")166
private fun nativeTextServiceFlags(): Int168
@Extern(name = "scoop_window_text_check")169
private fun nativeTextCheck(170
text: Ptr<UInt8>,171
length: Long,172
flags: Int,173
language: Ptr<UInt8>,174
languageLength: Long175
): NativeCreateResult177
@Extern(name = "scoop_window_text_service_count")178
private fun nativeTextServiceCount(owner: Ptr<Unit>): Long180
@Extern(name = "scoop_window_text_service_item")181
private fun nativeTextServiceItem(owner: Ptr<Unit>, index: Long): NativeTextCheckItem183
@Extern(name = "scoop_window_text_service_string")184
private fun nativeTextServiceString(185
owner: Ptr<Unit>,186
index: Long,187
suggestion: Long188
): NativeTextServiceString190
@Extern(name = "scoop_window_text_service_destroy")191
private fun nativeTextServiceDestroy(owner: Ptr<Unit>)193
@Extern(name = "scoop_window_text_service_perform")194
private fun nativePerformTextService(195
name: Ptr<UInt8>,196
nameLength: Long,197
text: Ptr<UInt8>,198
length: Long199
): NativeClipboardText201
@Extern(name = "scoop_window_text_definition")202
private fun nativeShowTextDefinition(203
window: Ptr<Unit>,204
text: Ptr<UInt8>,205
length: Long,206
x: Double,207
y: Double,208
width: Double,209
height: Double210
): Long