scoop:scoop.ui:0.1.0
src/text/inline.scoop
Selected production source · src/text/inline.scoop
1
package scoop.ui.text3
import scoop.collections.HashMap4
import scoop.graphics.*5
import scoop.window.*6
import scoop.window.accessibility.*7
import scoop.graphics.text.*8
import scoop.ui.*10
/** Uses a child's reported baseline, or an explicit paragraph alignment. */11
public enum InlineViewAlignment {12
Baseline(TextBaseline), Explicit(InlineAlignment)13
}15
/** One occurrence; the view and any resources used by it retain their normal owners. */16
public 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)22
)24
/** Keeps occurrence identity separate from the child view's own key. */25
public 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 = 0L30
for (index in 0L ..< ordered.size) {31
val item = ordered[index]32
val occurrence = content.inlineObjects[index]33
val start = occurrence.range.start + shift34
val end = start + occurrence.alternateText.byteLength35
shift = end - occurrence.range.end36
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
key47
)48
)49
}50
return result.toArray()51
}53
private class InlineChildProps(val text: String, val name: String, val range: Utf8Range)55
private class InlineChild(56
private val props: Props<InlineChildProps>,57
scope: ComponentScope58
): Element<Unit>(scope) {59
private var semantics: SemanticsHandle? = None60
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.value66
handle.textChild =67
when (parent) {68
case Some(val value) -> if (69
value.role == SemanticRole.Text || value.role == SemanticRole.TextField70
)71
Some(SemanticTextChildBinding(value, input.text, input.range))72
else73
None74
case None -> None75
}76
handle.update(SemanticProperties(name = Some(input.name)))77
return UiInvalidation.Scene78
}79
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<80
Unit81
> {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
}96
}98
internal 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
}114
}116
internal 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.alphabeticBaseline123
else124
measured.ideographicBaseline125
InlineAlignment.Baseline(kind, value ?: measured.size.height)126
}127
case Explicit(val value) -> value128
}129
return InlineMetrics(key, measured.size, resolved)130
}