scoop:scoop.ui.component:0.1.0
src/label.scoop
Selected production source · src/label.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.text.*7
import scoop.ui.style.ThemeSource8
import scoop.window.accessibility.*10
public enum LabelText {11
Plain(String, SpanStyle?), Styled(StyledText)12
}14
public class LabelProps public constructor(15
public val text: LabelText,16
public val fonts: FontCollection? = None,17
public val settings: ParagraphStyle? = None,18
public val inlines: Array<InlineView> = [],19
public val theme: ThemeSource? = None,20
public val baseStyle: TextStyle? = None,21
public val underline: Color? = None,22
public val semantics: ComponentSemantics = ComponentSemantics()23
)25
/** A normal component; text measurement and ownership belong to the public ParagraphHost. */26
public class Label public constructor(27
private val props: Props<LabelProps>,28
scope: ComponentScope29
): Component(scope) {30
private var plain: PlainLabel? = None31
private val themes = ThemeAccess(scope)32
public override fun onMount() {33
when (props.value.text) {34
case Plain(_, _) -> when (props.value.baseStyle) {35
case None -> themes.mount(props.value.theme)36
case Some(_) -> {}37
}38
case Styled(_) -> {}39
}40
}41
public override fun render(): View {42
val input = props.value43
val content =44
when (input.text) {45
case Plain(val text, val style) -> plainContent(46
text,47
input.baseStyle ?: themes.read(input.theme).tokens.typography.body,48
style49
)50
case Styled(val content) -> content51
}52
return element(53
LabelInkProps(content, input),54
{ current, mount -> LabelInk(current, scope = mount) },55
inlineChildren(content, input.inlines)56
)57
}58
private fun plainContent(text: String, base: TextStyle, style: SpanStyle?): StyledText {59
when (plain) {60
case Some(val previous) -> {61
if (62
previous.text ==63
text &&64
previous.base ===65
base &&66
sameSpanStyle(previous.localStyle, style)67
)68
return previous.content69
}70
case None -> {}71
}72
val resolved =73
when (style) {74
case Some(val value) -> value.resolve(base);75
case None -> base76
}77
val next =78
PlainLabel(79
text,80
base,81
style,82
styledText {83
this.text(text)84
this.style = resolved85
}86
)87
plain = Some(next)88
return next.content89
}90
}92
public fun label(text: String, configure: PlainLabelBuilder.() -> Unit = {}): View =93
makePlainLabel(text, configure)95
public fun label(content: StyledText, configure: StyledLabelBuilder.() -> Unit = {}): View =96
makeStyledLabel(content, configure)98
private class PlainLabel(99
val text: String,100
val base: TextStyle,101
val localStyle: SpanStyle?,102
val content: StyledText103
)105
private class LabelInkProps(val content: StyledText, val input: LabelProps)107
private class LabelInk(private val props: Props<LabelInkProps>, scope: ComponentScope): Element<108
ParagraphMeasurement109
>(scope) {110
private var previous: LabelInkProps? = None111
private val paragraph = scope.resource { ParagraphHost() }112
private val semantics = createComponentSemantics(scope, SemanticRole.Text)113
private var plainText = ""114
private var fonts = props.value.input.fonts ?: scope.defaultFonts115
init {116
scope.routeSemanticsChildren(semantics)117
}118
public override fun update(): UiInvalidation {119
val previousFonts = fonts120
fonts = props.value.input.fonts ?: scope.defaultFonts121
val value = props.value122
when (semantics) {123
case Some(val handle) -> {124
val contentChanged =125
when (previous) {126
case Some(val old) -> old.content !== value.content127
case None -> true128
}129
if (contentChanged)130
plainText = value.content.toPlainText()131
handle.bindReference(value.input.semantics.reference)132
handle.update(133
value.input.semantics.properties(value = Some(plainText), readOnly = true),134
relations = value.input.semantics.relations135
)136
}137
case None -> {}138
}139
val fontChanged = previousFonts !== fonts140
val changed =141
fontChanged ||142
when (previous) {143
case Some(val old) -> old.content !==144
value.content ||145
!sameLabelParagraph(old.input.settings, value.input.settings) ||146
!(147
old.input.inlines ===148
value.input.inlines ||149
(old.input.inlines.size == 0L && value.input.inlines.size == 0L)150
)151
case None -> true152
}153
val paintChanged =154
when (previous) {155
case Some(val old) -> old.input.underline != value.input.underline156
case None -> true157
}158
previous = Some(value)159
return if (changed)160
UiInvalidation.Layout161
else162
if (paintChanged)163
UiInvalidation.Scene164
else165
UiInvalidation.None166
}167
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<168
ParagraphMeasurement169
> {170
val value = props.value171
val measured =172
paragraph.measure(173
context,174
constraints,175
fonts,176
value.content,177
value.input.settings,178
value.input.inlines179
)180
return LayoutResult(measured.metrics, measured, { measured.close() })181
}182
public override fun scene(context: SceneContext, measured: ParagraphMeasurement) {183
when (semantics) {184
case Some(val handle) -> context.semantics(185
handle,186
Rect(0.0, 0.0, context.size.width, context.size.height)187
)188
case None -> {}189
}190
paragraph.scene(context, measured)191
when (props.value.input.underline) {192
case Some(val color) -> {193
val boxes =194
measured.layout.selection(TextRange(0L, props.value.content.text.byteLength))195
context.draw {196
paint -> for (box in boxes)197
paint.canvas.drawRect(198
Rect(199
box.rect.x,200
box.rect.y + box.rect.height - 1.0,201
box.rect.width,202
1.0203
),204
Paint(color)205
)206
}207
}208
case None -> {}209
}210
}211
}