scoop:scoop.ui:0.1.0
src/text/semantic_reveal.scoop
Selected production source · src/text/semantic_reveal.scoop
1
package scoop.ui.text3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.math.min6
import scoop.math.max7
import scoop.ui.*8
import scoop.window.*9
import scoop.window.accessibility.*11
internal fun semanticScrollAlignment(value: SemanticScrollAlignment): ScrollAlignment =12
when (value) {13
case Start -> ScrollAlignment.Start14
case Center -> ScrollAlignment.Center15
case End -> ScrollAlignment.End16
case Nearest -> ScrollAlignment.Nearest17
}19
/** Reads only accepted geometry and cancels when the document or its input boundary changes. */20
internal fun semanticEditorReveal(21
handle: SemanticsHandle,22
host: TextEditHost,23
range: Utf8Range,24
scroll: SemanticTextScroll25
): SemanticActionResult {26
val model = host.current27
val text = model.text28
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.isInputAllowed40
)41
None42
else {43
val frame = host.accepted44
when (frame) {45
case Some(val accepted) -> if (46
accepted.editor !== model || accepted.display.source.text != text47
)48
None49
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
caret62
)63
when (region) {64
case Some(val bounds) -> host.area.toWindow(bounds)65
case None -> None66
}67
}68
case None -> None69
}70
}71
},72
semanticScrollAlignment(scroll.horizontal),73
semanticScrollAlignment(scroll.vertical)74
)75
}77
internal fun semanticSelectableReveal(78
handle: SemanticsHandle,79
host: SelectableTextHost,80
text: SelectableSemanticText,81
range: Utf8Range,82
scroll: SemanticTextScroll83
): SemanticActionResult {84
val content = host.content85
val start =86
text.mapping.reverse(87
TextPosition(88
range.start,89
if (range.start == range.end)90
TextAffinity.Downstream91
else92
TextAffinity.Upstream93
)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.isInputAllowed105
)106
None107
else108
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 -> None118
}119
}120
case None -> None121
}122
},123
semanticScrollAlignment(scroll.horizontal),124
semanticScrollAlignment(scroll.vertical)125
)126
}128
private fun semanticTextRegion(boxes: Array<TextBox>, caret: Rect?): Rect? {129
if (boxes.size == 0L)130
return caret131
var left = boxes[0L].rect.x132
var right = left + boxes[0L].rect.width133
var top = boxes[0L].rect.y134
var bottom = top + boxes[0L].rect.height135
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))142
}