Scoop API

scoop:scoop.window:0.1.0

src/text/types.scoop

Selected production source · src/text/types.scoop

1package scoop.window
3/** A half-open UTF-8 byte range. Operations validate bounds and character boundaries. */
4public struct Utf8Range(val start: Long, val end: Long)
6/** Selection endpoints preserve the anchor and active cursor direction. */
7public struct Utf8Selection(val anchor: Long, val cursor: Long) {
8 public val range: Utf8Range get() =
9 if (anchor <= cursor)
10 Utf8Range(anchor, cursor)
11 else
12 Utf8Range(cursor, anchor)
15/** Replacement coordinates refer to the temporary text after preceding batch operations. */
16public enum TextReplacement {
17 CurrentSelection, Range(Utf8Range)
20/** One ordered edit within a native input transaction. */
21public enum TextInputOperation {
22 ClearPreedit,
23 Preedit(val text: String, val selection: Utf8Selection?, val replacement: TextReplacement),
24 Commit(val text: String, val replacement: TextReplacement),
25 DeleteSurrounding(val beforeBytes: Long, val afterBytes: Long),
26 SetSelection(val selection: Utf8Selection)
29/** One native document connection, replaced whenever native text input reopens. */
30public struct TextInputConnection(val window: WindowId, val generation: Long)
32/** Ordered native batch identity; it is independent of the application's text revision. */
33public struct TextInputOrigin(val connection: TextInputConnection, val sequence: Long)
35/** An owned, ordered edit transaction against an optional editor revision. */
36public class TextInputBatch public constructor(
37 public val baseRevision: Long?,
38 operations: List<TextInputOperation>,
39 public val origin: TextInputOrigin? = None
40) {
41 public val operations: Array<TextInputOperation> = operations.toArray()
44/** Text has exactly one insertion channel, including ordinary printable keys. */
45public enum TextInputEvent {
46 Enabled(val connection: TextInputConnection?), Disabled(
47 val connection: TextInputConnection?
48 ), Edit(TextInputBatch)
51/** An immutable UTF-8 snapshot, published to native text input only by an explicit request. */
52public struct TextInputState(
53 val surroundingText: String,
54 val selection: Utf8Selection,
55 val markedRange: Utf8Range? = None,
56 val caretRect: LogicalRect = LogicalRect(LogicalPosition(0.0, 0.0), LogicalSize(0.0, 0.0)),
57 val revision: Long = 0L
58) {
59 /** Validates the complete snapshot, including both directional selection endpoints. */
60 public fun validate() {
61 requireWindow(revision >= 0L, "negative text revision")
62 val bytes = surroundingText.toByteArray()
63 checkTextRange(bytes, selection.range)
64 when (markedRange) {
65 case Some(val range) -> checkTextRange(bytes, range);
66 case None -> {}
67 }
68 checkRect(caretRect)
69 }
70 /** Applies one transaction atomically to a new value; this snapshot remains unchanged. */
71 public fun apply(batch: TextInputBatch): TextInputState = applyTextBatch(this, batch)
74internal fun checkTextRange(bytes: Array<Byte>, range: Utf8Range) {
75 requireWindow(
76 range.start >= 0L && range.start <= range.end && range.end <= bytes.size,
77 "invalid UTF-8 range"
78 )
79 requireWindow(
80 textBoundary(bytes, range.start) && textBoundary(bytes, range.end),
81 "range splits a UTF-8 character"
82 )
85private fun textBoundary(bytes: Array<Byte>, offset: Long): Boolean =
86 offset == bytes.size || bytes[offset].toInt().and(192) != 128
88internal fun textSlice(text: String, range: Utf8Range): String {
89 val bytes = text.toByteArray()
90 checkTextRange(bytes, range)
91 return String.fromUtf8(
92 Array<Byte>(range.end - range.start) { index -> bytes[range.start + index] }
93 )
96/** Copies a validated UTF-8 byte range without reinterpreting it as character indices. */
97public fun String.utf8Slice(range: Utf8Range): String = textSlice(this, range)