Scoop API

scoop:scoop.ui.component:0.1.0

src/label.scoop

Selected production source · src/label.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.text.*
7import scoop.ui.style.ThemeSource
8import scoop.window.accessibility.*
10public enum LabelText {
11 Plain(String, SpanStyle?), Styled(StyledText)
14public 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()
25/** A normal component; text measurement and ownership belong to the public ParagraphHost. */
26public class Label public constructor(
27 private val props: Props<LabelProps>,
28 scope: ComponentScope
29): Component(scope) {
30 private var plain: PlainLabel? = None
31 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.value
43 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 style
49 )
50 case Styled(val content) -> content
51 }
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.content
69 }
70 case None -> {}
71 }
72 val resolved =
73 when (style) {
74 case Some(val value) -> value.resolve(base);
75 case None -> base
76 }
77 val next =
78 PlainLabel(
79 text,
80 base,
81 style,
82 styledText {
83 this.text(text)
84 this.style = resolved
85 }
86 )
87 plain = Some(next)
88 return next.content
89 }
92public fun label(text: String, configure: PlainLabelBuilder.() -> Unit = {}): View =
93 makePlainLabel(text, configure)
95public fun label(content: StyledText, configure: StyledLabelBuilder.() -> Unit = {}): View =
96 makeStyledLabel(content, configure)
98private class PlainLabel(
99 val text: String,
100 val base: TextStyle,
101 val localStyle: SpanStyle?,
102 val content: StyledText
105private class LabelInkProps(val content: StyledText, val input: LabelProps)
107private class LabelInk(private val props: Props<LabelInkProps>, scope: ComponentScope): Element<
108 ParagraphMeasurement
109>(scope) {
110 private var previous: LabelInkProps? = None
111 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.defaultFonts
115 init {
116 scope.routeSemanticsChildren(semantics)
117 }
118 public override fun update(): UiInvalidation {
119 val previousFonts = fonts
120 fonts = props.value.input.fonts ?: scope.defaultFonts
121 val value = props.value
122 when (semantics) {
123 case Some(val handle) -> {
124 val contentChanged =
125 when (previous) {
126 case Some(val old) -> old.content !== value.content
127 case None -> true
128 }
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.relations
135 )
136 }
137 case None -> {}
138 }
139 val fontChanged = previousFonts !== fonts
140 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 -> true
152 }
153 val paintChanged =
154 when (previous) {
155 case Some(val old) -> old.input.underline != value.input.underline
156 case None -> true
157 }
158 previous = Some(value)
159 return if (changed)
160 UiInvalidation.Layout
161 else
162 if (paintChanged)
163 UiInvalidation.Scene
164 else
165 UiInvalidation.None
166 }
167 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
168 ParagraphMeasurement
169 > {
170 val value = props.value
171 val measured =
172 paragraph.measure(
173 context,
174 constraints,
175 fonts,
176 value.content,
177 value.input.settings,
178 value.input.inlines
179 )
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.0
203 ),
204 Paint(color)
205 )
206 }
207 }
208 case None -> {}
209 }
210 }