Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/layout.scoop

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

1package scoop.ui.text
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.math.max
6import scoop.surface.*
7import scoop.ui.*
9internal class EditorInputLayout(
10 val layout: TextEditLayout,
11 val display: TextDisplay,
12 val caretStyle: EditorCaretStyle
15internal class EditorFrame(
16 private val retained: RetainedEditorLayout,
17 val snapshot: ScrollSnapshot,
18 val presentationOffset: Point,
19 val editor: TextEditorState,
20 val projection: TextProjection,
21 val revision: Long,
22 val display: TextDisplay,
23 val cells: TextEditCells?,
24 val caretStyle: EditorCaretStyle,
25 val caretPadding: Double
26): Closeable {
27 val layout: TextEditLayout get() = retained.layout
28 public override fun close() {
29 retained.close()
30 }
33private fun editorLayout(
34 host: TextEditHost,
35 width: Double?,
36 cells: TextEditCells?,
37 caretStyle: EditorCaretStyle
38): EditorInputLayout {
39 host.checkDocument(host.current.committedText)
40 val input = editorSnapshot(host.current.storage.value)
41 val cached = host.currentDisplay
42 val display =
43 when (cached) {
44 case Some(val previous) -> if (
45 previous.source == input && host.currentProjection === host.options.projection
46 )
47 previous
48 else
49 projectEditorText(input, host.options.projection)
50 case None -> projectEditorText(input, host.options.projection)
51 }
52 val text = display.text
53 val style = host.appearance.textStyle
54 val content =
55 when (host.content) {
56 case Some(val previous) -> if (previous.text == text && previous.style === style)
57 previous
58 else
59 styledText {
60 this.text(text)
61 this.style = style
62 }
63 case None -> styledText {
64 this.text(text)
65 this.style = style
66 }
67 }
68 host.content = Some(content)
69 val paragraph =
70 host.body.update(
71 host.appearance.fonts,
72 content,
73 paragraphStyle {
74 this.alignment = host.options.alignment
75 this.direction =
76 host.options.direction ?: host.scope.layoutDirection.textDirection()
77 },
78 width,
79 []
80 )
81 val layout = editorGeometry(host, paragraph, cells)
82 host.currentProjection = host.options.projection
83 host.currentDisplay = Some(display)
84 return EditorInputLayout(layout, display, caretStyle)
87private fun editorHint(host: TextEditHost, width: Double?): TextLayout? {
88 if (host.current.text.byteLength != 0L || host.placeholder.byteLength == 0L)
89 return None
90 val text =
91 if (host.options.multiline)
92 host.placeholder
93 else
94 mapEditorText(host.placeholder, editorNewlineEdits(host.placeholder)).text
95 val style = host.appearance.placeholderStyle
96 val content =
97 when (host.hintContent) {
98 case Some(val previous) -> if (previous.text == text && previous.style === style)
99 previous
100 else
101 styledText {
102 this.text(text)
103 this.style = style
104 }
105 case None -> styledText {
106 this.text(text)
107 this.style = style
108 }
109 }
110 host.hintContent = Some(content)
111 return Some(
112 host.hint.update(
113 host.appearance.fonts,
114 content,
115 paragraphStyle {
116 this.alignment = host.options.alignment
117 this.direction =
118 host.options.direction ?: host.scope.layoutDirection.textDirection()
119 },
120 width,
121 []
122 )
123 )
126private fun editorWidth(host: TextEditHost, viewport: Double?, padding: Double): Double? {
127 if (!host.options.multiline || !host.options.wrap)
128 return None
129 return when (viewport) {
130 case Some(val width) -> Some(max(0.0, width - padding))
131 case None -> None
132 }
135internal fun editorMeasure(
136 host: TextEditHost,
137 constraints: Constraints,
138 cells: TextEditCells?,
139 scrollPadding: (Size, Size) -> ScrollPadding
140): TextEditMeasurement {
141 constraints.check()
142 val caretStyle = editorCaretStyle(host)
143 // An unwrapped line bounds the caret height of its wrapped fragments, including fallback fonts.
144 val padding =
145 if (caretStyle.aspectRatio > 0.0)
146 caretStyle.padding(editorLayout(host, None, cells, caretStyle).layout)
147 else
148 caretStyle.width
149 val width = editorWidth(host, constraints.maxWidth, padding)
150 val text = editorLayout(host, width, cells, caretStyle)
151 val layout = text.layout
152 val paragraphMode =
153 when (cells) {
154 case None -> true;
155 case Some(_) -> false
156 }
157 val hint =
158 if (paragraphMode)
159 editorHint(host, width)
160 else
161 None
162 val caretPadding =
163 if (paragraphMode)
164 padding
165 else
166 0.0
167 val naturalWidth = max(layout.size.width, hint?.size?.width ?: 0.0) + caretPadding
168 val size = constraints.constrain(Size(naturalWidth, layout.size.height))
169 var contentWidth = layout.size.width
170 for (line in layout.lines)
171 contentWidth = max(contentWidth, line.rect.x + line.rect.width)
172 val content =
173 Size(max(size.width, contentWidth + caretPadding), max(size.height, layout.size.height))
174 val scroll = host.viewport.layout(size, content, padding = scrollPadding(size, content))
175 return TextEditMeasurement(
176 LayoutMetrics(size, Some(layout.alphabeticBaseline), Some(layout.ideographicBaseline)),
177 layout.retain(),
178 scroll,
179 host.current,
180 host.options.projection,
181 host.current.revision,
182 text.display,
183 cells,
184 caretStyle,
185 caretPadding,
186 when (hint) {
187 case Some(_) -> Some(host.hint.retainLayout());
188 case None -> None
189 },
190 host.hint.generation
191 )
194/** Pointer geometry stays paired with the last successful frame, even after a failed resize. */
195internal fun editorInputText(host: TextEditHost): EditorInputLayout {
196 when (host.accepted) {
197 case Some(val frame) -> {
198 if (frame.display.source.text == host.current.text)
199 return EditorInputLayout(frame.layout, frame.display, frame.caretStyle)
200 return editorLayout(
201 host,
202 editorWidth(host, Some(frame.snapshot.viewport.width), frame.caretPadding),
203 frame.cells,
204 frame.caretStyle
205 )
206 }
207 case None -> return editorLayout(host, None, None, editorCaretStyle(host))
208 }
211internal fun editorCaret(host: TextEditHost, text: EditorInputLayout): Rect? =
212 text.caretStyle.caret(
213 text.layout,
214 editorVisualPosition(text.layout, text.display.mapping.forward(host.current.cursor))
215 )
217internal fun editorScene(host: TextEditHost, context: SceneContext, measure: TextEditMeasurement) {
218 val frame =
219 EditorFrame(
220 measure.retained.layout.retain(),
221 measure.scrollSnapshot,
222 measure.scroll.presentationOffset,
223 measure.editor,
224 measure.projection,
225 measure.revision,
226 measure.display,
227 measure.cells,
228 measure.caretStyle,
229 measure.caretPadding
230 )
231 host.staged?.close()
232 host.staged = Some(frame)
233 val layout = frame.layout
234 val appearance = host.appearance
235 val input = host.current.inputState
236 val caret = editorCaret(host, EditorInputLayout(layout, frame.display, frame.caretStyle))
237 val dropCaret =
238 when (host.dropTarget) {
239 case Some(val target) -> target.caret(frame)
240 case None -> None
241 }
242 val selectedRange = frame.display.displayRange(input.selection.range)
243 val selection = layout.selection(selectedRange)
244 val marked =
245 when (input.markedRange) {
246 case Some(val range) -> layout.selection(frame.display.displayRange(range))
247 case None -> []
248 }
249 val active = host.area.isFocusActive
250 val color =
251 if (active)
252 appearance.selectionColor
253 else
254 appearance.inactiveSelectionColor
255 val textColor =
256 if (active)
257 appearance.selectionTextColor
258 else
259 appearance.inactiveSelectionTextColor
260 val ink =
261 when (textColor) {
262 case Some(val value) -> if (selection.size > 0L)
263 Some(TextSelectionInk(selectedRange, value))
264 else
265 None
266 case None -> None
267 }
268 val bodyRevision = host.bodyInk.update(layout.generation, ink)
269 host.viewport.present(
270 context,
271 measure.scroll,
272 Some {
273 content ->
274 val snapshot = frame.snapshot
275 host.area.setCaret(caret, Some(input.revision))
276 val viewport =
277 Rect(
278 frame.presentationOffset.x - snapshot.padding.left,
279 frame.presentationOffset.y - snapshot.padding.top,
280 snapshot.viewport.width,
281 snapshot.viewport.height
282 )
283 content.place(host.area, viewport)
284 editorSemantics(host, content, frame, viewport)
285 content.draw {
286 paint -> for (box in selection)
287 paint.canvas.drawRect(box.rect, Paint(color))
288 }
289 val bounds = layout.paintBounds
290 content.drawTextLayer(host.bodyKey, editorInkBounds(bounds), bodyRevision) {
291 paint -> drawSelectionText(paint.canvas, editorInkBounds(bounds), selection, ink) {
292 canvas -> canvas.drawText(layout)
293 }
294 }
295 if (
296 frame.layout.cellRects.size ==
297 0L &&
298 input.surroundingText.byteLength ==
299 0L &&
300 host.placeholder.byteLength >
301 0L
302 ) {
303 val hint = measure.hint!!.layout
304 content.drawTextLayer(
305 host.hintKey,
306 editorInkBounds(hint.paintBounds),
307 measure.hintGeneration
308 ) { paint -> paint.canvas.drawText(hint) }
309 }
310 content.draw {
311 paint ->
312 for (box in marked)
313 paint.canvas.drawRect(
314 Rect(box.rect.x, box.rect.y + box.rect.height - 1.0, box.rect.width, 1.0),
315 Paint(appearance.markedColor)
316 )
317 if (
318 active &&
319 host.caretVisible &&
320 dropCaret ==
321 None &&
322 input.selection.anchor ==
323 input.selection.cursor
324 ) {
325 when (caret) {
326 case Some(val rect) -> paint.canvas.drawRect(
327 rect,
328 Paint(appearance.caretColor)
329 )
330 case None -> {}
331 }
332 }
333 when (dropCaret) {
334 case Some(val rect) -> paint.canvas.drawRect(rect, Paint(appearance.caretColor))
335 case None -> {}
336 }
337 }
338 editorSelectionHandles(host, content, frame, viewport)
339 }
340 )
341 context.onFrameComplete {
342 completion ->
343 when (host.staged) {
344 case Some(val staged) -> if (staged === frame)
345 host.staged = None
346 case None -> {}
347 }
348 if (host.closed || completion.submission == FrameSubmission.Discarded) {
349 frame.close()
350 return@onFrameComplete
351 }
352 val previous = host.accepted
353 host.accepted = Some(frame)
354 previous?.close()
355 host.dropTarget?.accepted()
356 host.dataSource?.update()
357 host.touchHandles.accepted()
358 if (host.closed)
359 return@onFrameComplete
360 if (host.current.revision == frame.revision) {
361 if (
362 host.revealNeeded &&
363 !editorDragging(host) &&
364 !(host.dropTarget?.isHovering ?: false) &&
365 host.area.isFocusActive &&
366 host.revealRevision ==
367 host.focus.node.root.surface.focusRevision
368 ) {
369 host.revealNeeded = false
370 when (caret) {
371 case Some(val rect) -> host.keyboard.reveal(rect)
372 case None -> {}
373 }
374 }
375 editorDragAccepted(host)
376 editorScheduleBlink(host)
377 }
378 }
381private fun editorInkBounds(bounds: Rect): Rect =
382 Rect(bounds.x - 1.0, bounds.y - 1.0, bounds.width + 2.0, bounds.height + 2.0)