Scoop API

scoop:scoop.ui:0.1.0

src/text/inline.scoop

Selected production source · src/text/inline.scoop

1package scoop.ui.text
3import scoop.collections.HashMap
4import scoop.graphics.*
5import scoop.window.*
6import scoop.window.accessibility.*
7import scoop.graphics.text.*
8import scoop.ui.*
10/** Uses a child's reported baseline, or an explicit paragraph alignment. */
11public enum InlineViewAlignment {
12 Baseline(TextBaseline), Explicit(InlineAlignment)
15/** One occurrence; the view and any resources used by it retain their normal owners. */
16public class InlineView internal constructor(
17 public val key: InlineKey,
18 public val view: View,
19 public val size: Size? = None,
20 public val alignment: InlineViewAlignment =
21 InlineViewAlignment.Baseline(TextBaseline.Alphabetic)
24/** Keeps occurrence identity separate from the child view's own key. */
25public fun inlineChildren(content: StyledText, inlines: Array<InlineView>): Array<View> {
26 val ordered = orderedInlines(content, inlines)
27 val text = content.toPlainText()
28 val result = ArrayList<View>()
29 var shift = 0L
30 for (index in 0L ..< ordered.size) {
31 val item = ordered[index]
32 val occurrence = content.inlineObjects[index]
33 val start = occurrence.range.start + shift
34 val end = start + occurrence.alternateText.byteLength
35 shift = end - occurrence.range.end
36 val key =
37 when (item.key) {
38 case Text(val value) -> ViewKey.Text(value)
39 case Number(val value) -> ViewKey.Number(value)
40 }
41 result.add(
42 element(
43 InlineChildProps(text, occurrence.alternateText, Utf8Range(start, end)),
44 { props, mount -> InlineChild(props, scope = mount) },
45 [item.view],
46 key
47 )
48 )
49 }
50 return result.toArray()
53private class InlineChildProps(val text: String, val name: String, val range: Utf8Range)
55private class InlineChild(
56 private val props: Props<InlineChildProps>,
57 scope: ComponentScope
58): Element<Unit>(scope) {
59 private var semantics: SemanticsHandle? = None
60 public override fun update(): UiInvalidation {
61 val handle = semantics ?: scope.semantics(SemanticRole.Group)
62 semantics = Some(handle)
63 scope.routeSemanticsChildren(semantics)
64 val parent = scope.semanticAncestor()
65 val input = props.value
66 handle.textChild =
67 when (parent) {
68 case Some(val value) -> if (
69 value.role == SemanticRole.Text || value.role == SemanticRole.TextField
70 )
71 Some(SemanticTextChildBinding(value, input.text, input.range))
72 else
73 None
74 case None -> None
75 }
76 handle.update(SemanticProperties(name = Some(input.name)))
77 return UiInvalidation.Scene
78 }
79 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
80 Unit
81 > {
82 val child = context.measureChild(0L, constraints)
83 context.placeChild(0L, Point(0.0, 0.0))
84 return LayoutResult(child, Unit)
85 }
86 public override fun scene(context: SceneContext, value: Unit) {
87 when (semantics) {
88 case Some(val handle) -> context.semantics(
89 handle,
90 Rect(0.0, 0.0, context.size.width, context.size.height)
91 )
92 case None -> {}
93 }
94 context.children()
95 }
98internal fun orderedInlines(content: StyledText, inlines: Array<InlineView>): Array<InlineView> {
99 requireUi(
100 inlines.size == content.inlineObjects.size,
101 "inline views must cover every text object"
102 )
103 val values = HashMap<InlineKey, InlineView>()
104 for (item in inlines) {
105 when (values.put(item.key, item)) {
106 case Some(_) -> throw IllegalArgumentException(Some("duplicate inline view key"))
107 case None -> {}
108 }
109 }
110 return Array<InlineView>(content.inlineObjects.size) {
111 index -> values[content.inlineObjects[index].key] ?:
112 throw IllegalArgumentException(Some("missing inline view key"))
113 }
116internal fun InlineView.metrics(measured: LayoutMetrics): InlineMetrics {
117 val resolved =
118 when (alignment) {
119 case Baseline(val kind) -> {
120 val value =
121 if (kind == TextBaseline.Alphabetic)
122 measured.alphabeticBaseline
123 else
124 measured.ideographicBaseline
125 InlineAlignment.Baseline(kind, value ?: measured.size.height)
126 }
127 case Explicit(val value) -> value
128 }
129 return InlineMetrics(key, measured.size, resolved)