Scoop API

scoop:scoop.ui:0.1.0

src/text/selectable/semantic_scene.scoop

Selected production source · src/text/selectable/semantic_scene.scoop

1package scoop.ui.text
3import scoop.graphics.Rect
4import scoop.graphics.text.TextLayout
5import scoop.ui.*
7internal class SelectableSemanticLayout(
8 val layout: TextLayout,
9 val text: SelectableSemanticText,
10 val native: NativeTextSource
13internal fun selectableSemantics(
14 host: SelectableTextHost,
15 context: SceneContext,
16 frame: SelectableFrame,
17 viewport: Rect
18) {
19 val binding = host.semantics.binding ?: return
20 if (host.content !== frame.content)
21 return
22 val text = selectableSemanticText(host)
23 if (binding.handle.properties.value != Some(text.text))
24 return
25 val source =
26 when (host.semanticLayout) {
27 case Some(val previous) -> if (
28 !previous.native.isClosed &&
29 previous.layout ===
30 frame.layout &&
31 previous.text ===
32 text
33 )
34 previous.native
35 else
36 selectableSemanticSource(host, frame.layout, text)
37 case None -> selectableSemanticSource(host, frame.layout, text)
38 }
39 context.textSemantics(binding, viewport, source)
42private fun selectableSemanticSource(
43 host: SelectableTextHost,
44 layout: TextLayout,
45 text: SelectableSemanticText
46): NativeTextSource {
47 val source = selectableSemanticSource(layout, text)
48 host.semantics.replaceSource(source)
49 host.semanticLayout = Some(SelectableSemanticLayout(layout, text, source))
50 return source
53internal fun selectableSemanticSource(
54 layout: TextLayout,
55 text: SelectableSemanticText
56): NativeTextSource {
57 val mapping = ArrayList<SemanticTextMapping>()
58 var shift = 0L
59 for (edit in text.edits) {
60 val start = edit.range.start + shift
61 val end = start + edit.text.byteLength
62 mapping.add(SemanticTextMapping(start, end, edit.range.start, edit.range.end))
63 shift = end - edit.range.end
64 }
65 val rows = layout.lines
66 val lines =
67 Array<SemanticTextLine>(rows.size) {
68 index ->
69 val line = rows[index]
70 SemanticTextLine(
71 line.range.start,
72 line.range.end,
73 line.visibleRange.start,
74 line.visibleRange.end
75 )
76 }
77 return semanticTextSource(
78 layout,
79 text.text.byteLength,
80 text.boundaries,
81 mapping.toArray(),
82 lines,
83 expand = true
84 )