scoop:scoop.ui:0.1.0
src/text/selectable/semantic_scene.scoop
Selected production source · src/text/selectable/semantic_scene.scoop
1
package scoop.ui.text3
import scoop.graphics.Rect4
import scoop.graphics.text.TextLayout5
import scoop.ui.*7
internal class SelectableSemanticLayout(8
val layout: TextLayout,9
val text: SelectableSemanticText,10
val native: NativeTextSource11
)13
internal fun selectableSemantics(14
host: SelectableTextHost,15
context: SceneContext,16
frame: SelectableFrame,17
viewport: Rect18
) {19
val binding = host.semantics.binding ?: return20
if (host.content !== frame.content)21
return22
val text = selectableSemanticText(host)23
if (binding.handle.properties.value != Some(text.text))24
return25
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
text33
)34
previous.native35
else36
selectableSemanticSource(host, frame.layout, text)37
case None -> selectableSemanticSource(host, frame.layout, text)38
}39
context.textSemantics(binding, viewport, source)40
}42
private fun selectableSemanticSource(43
host: SelectableTextHost,44
layout: TextLayout,45
text: SelectableSemanticText46
): NativeTextSource {47
val source = selectableSemanticSource(layout, text)48
host.semantics.replaceSource(source)49
host.semanticLayout = Some(SelectableSemanticLayout(layout, text, source))50
return source51
}53
internal fun selectableSemanticSource(54
layout: TextLayout,55
text: SelectableSemanticText56
): NativeTextSource {57
val mapping = ArrayList<SemanticTextMapping>()58
var shift = 0L59
for (edit in text.edits) {60
val start = edit.range.start + shift61
val end = start + edit.text.byteLength62
mapping.add(SemanticTextMapping(start, end, edit.range.start, edit.range.end))63
shift = end - edit.range.end64
}65
val rows = layout.lines66
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.end75
)76
}77
return semanticTextSource(78
layout,79
text.text.byteLength,80
text.boundaries,81
mapping.toArray(),82
lines,83
expand = true84
)85
}