Scoop API

scoop:scoop.ui:0.1.0

src/text/semantic_reveal.scoop

Selected production source · src/text/semantic_reveal.scoop

1package scoop.ui.text
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.math.min
6import scoop.math.max
7import scoop.ui.*
8import scoop.window.*
9import scoop.window.accessibility.*
11internal fun semanticScrollAlignment(value: SemanticScrollAlignment): ScrollAlignment =
12 when (value) {
13 case Start -> ScrollAlignment.Start
14 case Center -> ScrollAlignment.Center
15 case End -> ScrollAlignment.End
16 case Nearest -> ScrollAlignment.Nearest
17 }
19/** Reads only accepted geometry and cancels when the document or its input boundary changes. */
20internal fun semanticEditorReveal(
21 handle: SemanticsHandle,
22 host: TextEditHost,
23 range: Utf8Range,
24 scroll: SemanticTextScroll
25): SemanticActionResult {
26 val model = host.current
27 val text = model.text
28 return handle.controller.revealText(
29 handle,
30 {
31 if (
32 host.closed ||
33 host.current !==
34 model ||
35 model.text !=
36 text ||
37 !host.options.enabled ||
38 host.options.protectedText ||
39 !host.area.scope.isInputAllowed
40 )
41 None
42 else {
43 val frame = host.accepted
44 when (frame) {
45 case Some(val accepted) -> if (
46 accepted.editor !== model || accepted.display.source.text != text
47 )
48 None
49 else {
50 val caret =
51 accepted.caretStyle.caret(
52 accepted.layout,
53 editorVisualPosition(
54 accepted.layout,
55 accepted.display.toDisplay(TextPosition(range.start))
56 )
57 )
58 val region =
59 semanticTextRegion(
60 accepted.layout.selection(accepted.display.displayRange(range)),
61 caret
62 )
63 when (region) {
64 case Some(val bounds) -> host.area.toWindow(bounds)
65 case None -> None
66 }
67 }
68 case None -> None
69 }
70 }
71 },
72 semanticScrollAlignment(scroll.horizontal),
73 semanticScrollAlignment(scroll.vertical)
74 )
77internal fun semanticSelectableReveal(
78 handle: SemanticsHandle,
79 host: SelectableTextHost,
80 text: SelectableSemanticText,
81 range: Utf8Range,
82 scroll: SemanticTextScroll
83): SemanticActionResult {
84 val content = host.content
85 val start =
86 text.mapping.reverse(
87 TextPosition(
88 range.start,
89 if (range.start == range.end)
90 TextAffinity.Downstream
91 else
92 TextAffinity.Upstream
93 )
94 )
95 val end = text.mapping.reverse(TextPosition(range.end, TextAffinity.Downstream))
96 return handle.controller.revealText(
97 handle,
98 {
99 if (
100 host.closed ||
101 host.content !==
102 content ||
103 !host.options.enabled ||
104 !host.area.scope.isInputAllowed
105 )
106 None
107 else
108 when (host.inputFrame()) {
109 case Some(val frame) -> {
110 val region =
111 semanticTextRegion(
112 frame.layout.selection(TextRange(start.byteOffset, end.byteOffset)),
113 frame.layout.caret(start)
114 )
115 when (region) {
116 case Some(val bounds) -> host.area.toWindow(bounds)
117 case None -> None
118 }
119 }
120 case None -> None
121 }
122 },
123 semanticScrollAlignment(scroll.horizontal),
124 semanticScrollAlignment(scroll.vertical)
125 )
128private fun semanticTextRegion(boxes: Array<TextBox>, caret: Rect?): Rect? {
129 if (boxes.size == 0L)
130 return caret
131 var left = boxes[0L].rect.x
132 var right = left + boxes[0L].rect.width
133 var top = boxes[0L].rect.y
134 var bottom = top + boxes[0L].rect.height
135 for (box in boxes) {
136 left = min(left, box.rect.x)
137 right = max(right, box.rect.x + box.rect.width)
138 top = min(top, box.rect.y)
139 bottom = max(bottom, box.rect.y + box.rect.height)
140 }
141 return Some(Rect(left, top, max(right - left, 1.0), bottom - top))