Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/navigation.scoop

Selected production source · src/text/editor/navigation.scoop

1package scoop.ui.text
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.math.max
6import scoop.window.*
8internal struct EditorNavigation(val selection: EditorSelection, val preferredX: Double?)
10/** A scalar IME position can be inside a grapheme without changing the canonical selection. */
11internal fun editorVisualPosition(layout: TextEditLayout, position: TextPosition): TextPosition {
12 return editorBoundaryPosition(layout.graphemeBoundaries, position)
15internal fun editorBoundaryPosition(boundaries: Array<Long>, position: TextPosition): TextPosition {
16 var first = 0L
17 var last = boundaries.size
18 while (first < last) {
19 val middle = first + (last - first) / 2L
20 if (boundaries[middle] < position.byteOffset)
21 first = middle + 1L
22 else
23 last = middle
24 }
25 if (first < boundaries.size && boundaries[first] == position.byteOffset)
26 return position
27 return TextPosition(
28 if (position.affinity == TextAffinity.Upstream && first > 0L)
29 boundaries[first - 1L]
30 else
31 if (first < boundaries.size)
32 boundaries[first]
33 else
34 boundaries[boundaries.size - 1L],
35 position.affinity
36 )
39internal fun editorMove(
40 layout: TextEditLayout,
41 selection: EditorSelection,
42 movement: TextMovement,
43 extend: Boolean,
44 preferredX: Double?,
45 pageHeight: Double
46): EditorNavigation {
47 val anchor = editorVisualPosition(layout, selection.anchor)
48 val cursor = editorVisualPosition(layout, selection.cursor)
49 if (!extend && selection.anchor.byteOffset != selection.cursor.byteOffset) {
50 val collapse =
51 when (movement) {
52 case Backward -> Some(
53 if (anchor.byteOffset < cursor.byteOffset)
54 anchor
55 else
56 cursor
57 )
58 case Forward -> Some(
59 if (anchor.byteOffset > cursor.byteOffset)
60 anchor
61 else
62 cursor
63 )
64 case Left -> Some(editorVisualEdge(layout, anchor, cursor, false))
65 case Right -> Some(editorVisualEdge(layout, anchor, cursor, true))
66 else -> None
67 }
68 when (collapse) {
69 case Some(val value) -> return EditorNavigation(EditorSelection(value, value), None)
70 case None -> {}
71 }
72 }
73 val vertical =
74 movement ==
75 TextMovement.Up ||
76 movement ==
77 TextMovement.Down ||
78 movement ==
79 TextMovement.PageUp ||
80 movement ==
81 TextMovement.PageDown
82 var column: Double? = None
83 val next =
84 if (movement == TextMovement.PageUp || movement == TextMovement.PageDown) {
85 val caret = layout.caret(cursor) ?: Rect(0.0, 0.0, 0.0, 0.0)
86 val x = preferredX ?: caret.x
87 column = Some(x)
88 val distance = max(pageHeight, caret.height)
89 layout.hitTest(
90 Point(
91 x,
92 caret.y +
93 caret.height /
94 2.0 +
95 if (movement == TextMovement.PageUp)
96 -distance
97 else
98 distance
99 )
100 )
101 } else
102 when (movement) {
103 case Backward -> TextPosition(
104 layout.moveGrapheme(selection.cursor.byteOffset, false)
105 )
106 case Forward -> TextPosition(layout.moveGrapheme(selection.cursor.byteOffset, true))
107 case WordBackward -> TextPosition(
108 layout.moveWord(selection.cursor.byteOffset, false)
109 )
110 case WordForward -> TextPosition(layout.moveWord(selection.cursor.byteOffset, true))
111 case ParagraphStart -> TextPosition(
112 (layout.logicalLineRange(cursor) ?: TextRange(0L, 0L)).start
113 )
114 case ParagraphEnd -> TextPosition(
115 editorParagraphEnd(layout, cursor),
116 TextAffinity.Upstream
117 )
118 else -> {
119 val result = layout.moveCaret(cursor, editorCaretMovement(movement), preferredX)
120 when (result) {
121 case Some(val value) -> {
122 if (vertical)
123 column = Some(value.preferredX)
124 value.position
125 }
126 case None -> cursor
127 }
128 }
129 }
130 return EditorNavigation(
131 EditorSelection(
132 if (extend)
133 selection.anchor
134 else
135 next,
136 next
137 ),
138 if (vertical)
139 if (column != None)
140 column
141 else
142 preferredX
143 else
144 None
145 )
148private fun editorCaretMovement(movement: TextMovement): CaretMovement =
149 when (movement) {
150 case Left -> CaretMovement.Left
151 case Right -> CaretMovement.Right
152 case Up -> CaretMovement.Up
153 case Down -> CaretMovement.Down
154 case WordLeft -> CaretMovement.WordLeft
155 case WordRight -> CaretMovement.WordRight
156 case LineStart -> CaretMovement.LineStart
157 case LineEnd -> CaretMovement.LineEnd
158 case LineLeft -> CaretMovement.LineLeft
159 case LineRight -> CaretMovement.LineRight
160 case DocumentStart -> CaretMovement.DocumentStart
161 case DocumentEnd -> CaretMovement.DocumentEnd
162 else -> throw IllegalArgumentException(Some("movement is not a visual caret command"))
163 }
165private fun editorVisualEdge(
166 layout: TextEditLayout,
167 a: TextPosition,
168 b: TextPosition,
169 forward: Boolean
170): TextPosition {
171 val left =
172 layout.caret(a) ?:
173 return if (forward)
174 b
175 else
176 a
177 val right =
178 layout.caret(b) ?:
179 return if (forward)
180 a
181 else
182 b
183 val before = left.y < right.y || (left.y == right.y && left.x <= right.x)
184 return if (before == forward)
185 b
186 else
187 a
190internal fun editorParagraphEnd(layout: TextEditLayout, position: TextPosition): Long {
191 val range = layout.logicalLineRange(position) ?: return position.byteOffset
192 var end = range.start
193 for (line in layout.lines)
194 if (
195 line.range.start >=
196 range.start &&
197 line.range.start <
198 range.end &&
199 line.range.end <=
200 range.end
201 )
202 end = line.contentRange.end
203 return end
206internal fun editorDeleteRange(
207 layout: TextEditLayout,
208 selection: Utf8Selection,
209 deletion: TextDeletion
210): Utf8Range {
211 val selected = selection.range
212 if (selected.start != selected.end)
213 return selected
214 val forward =
215 deletion ==
216 TextDeletion.Forward ||
217 deletion ==
218 TextDeletion.WordForward ||
219 deletion ==
220 TextDeletion.LineEnd ||
221 deletion ==
222 TextDeletion.ParagraphEnd
223 val before = editorVisualPosition(layout, TextPosition(selection.cursor, TextAffinity.Upstream))
224 val after =
225 editorVisualPosition(layout, TextPosition(selection.cursor, TextAffinity.Downstream))
226 if (
227 before.byteOffset !=
228 after.byteOffset &&
229 (deletion == TextDeletion.Backward || deletion == TextDeletion.Forward)
230 )
231 return Utf8Range(before.byteOffset, after.byteOffset)
232 val origin =
233 if (forward)
234 before
235 else
236 after
237 val end =
238 when (deletion) {
239 case Backward -> layout.moveGrapheme(origin.byteOffset, false)
240 case Forward -> layout.moveGrapheme(origin.byteOffset, true)
241 case WordBackward -> layout.moveWord(origin.byteOffset, false)
242 case WordForward -> layout.moveWord(origin.byteOffset, true)
243 case LineStart -> layout.moveCaret(
244 origin,
245 CaretMovement.LineStart
246 )?.position?.byteOffset ?:
247 origin.byteOffset
248 case LineEnd -> layout.moveCaret(origin, CaretMovement.LineEnd)?.position?.byteOffset ?:
249 origin.byteOffset
250 case ParagraphStart -> (
251 layout.logicalLineRange(origin) ?: TextRange(origin.byteOffset, origin.byteOffset)
252 ).start
253 case ParagraphEnd -> editorParagraphEnd(layout, origin)
254 }
255 return if (end < origin.byteOffset)
256 Utf8Range(end, origin.byteOffset)
257 else
258 Utf8Range(origin.byteOffset, end)