Scoop API

scoop:scoop.ui.component:0.1.0

src/field/label.scoop

Selected production source · src/field/label.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.surface.InputArea
6import scoop.ui.*
7import scoop.ui.style.*
9public enum FieldLabelContent {
10 Text(LabelText), Custom(View)
13public class FieldLabelProps public constructor(
14 public val content: FieldLabelContent,
15 public val target: FieldTarget,
16 public val fonts: FontCollection? = None,
17 public val theme: ThemeSource? = None,
18 public val textStyle: SpanStyle? = None,
19 public val settings: ParagraphStyle? = None,
20 public val style: StyleOverride = styleOverride(),
21 public val semantics: ComponentSemantics = ComponentSemantics()
24public class FieldLabel public constructor(
25 private val props: Props<FieldLabelProps>,
26 scope: ComponentScope
27): Component(scope) {
28 private val themes = ThemeAccess(scope)
29 public override fun onMount() {
30 themes.mount(props.value.theme)
31 }
32 public override fun render(): View {
33 val input = props.value
34 val theme = themes.read(input.theme)
35 val base =
36 theme.tokens.typography.body.copy {
37 this.lineHeight = Some(1.0)
38 this.weight = 500
39 this.color = theme.tokens.palette.background.foreground
40 }
41 val child =
42 when (input.content) {
43 case Custom(val view) -> view
44 case Text(val value) -> {
45 val content =
46 when (value) {
47 case Plain(val value, val local) -> LabelText.Styled(
48 typographyContent(
49 TypographyContent.Plain(value),
50 local?.resolve(base) ?: base,
51 input.textStyle
52 )
53 )
54 case Styled(val value) -> LabelText.Styled(
55 typographyContent(
56 TypographyContent.Styled(value),
57 base,
58 input.textStyle
59 )
60 )
61 }
62 component(
63 LabelProps(content, input.fonts, input.settings, baseStyle = Some(base)),
64 { current, mount -> Label(current, scope = mount) }
65 )
66 }
67 }
68 val decorated =
69 frame(child) {
70 this.padding = Some(Insets())
71 this.style = input.style
72 }
73 return element(
74 FieldLabelInkProps(
75 input.target,
76 input.target.connection,
77 input.target.isEnabled,
78 input.semantics
79 ),
80 { current, mount -> FieldLabelInk(current, scope = mount) },
81 [decorated]
82 )
83 }
86private class FieldLabelInkProps(
87 val target: FieldTarget,
88 val connection: FieldLink?,
89 val enabled: Boolean,
90 val semantics: ComponentSemantics
93private class FieldLabelInk(
94 private val props: Props<FieldLabelInkProps>,
95 scope: ComponentScope
96): Element<Unit>(scope) {
97 private val annotation = scope.resource { FieldAnnotation(scope) }
98 private var area: InputArea? = None
99 private var press: Press? = None
100 private var previous: FieldLabelInkProps? = None
101 public override fun onMount() {
102 val input = scope.createArea()
103 area = Some(input)
104 press = Some(scope.press(input, PressOptions(false, false)) {})
105 scope.routeChildren(input)
106 }
107 public override fun update(): UiInvalidation {
108 val value = props.value
109 when (previous) {
110 case Some(val old) -> if (
111 old.target !== value.target || !sameFieldLink(old.connection, value.connection)
112 )
113 press!!.update(PressOptions(false, false)) {}
114 case None -> {}
115 }
116 previous = Some(value)
117 area!!.scope.enabled = value.enabled
118 press!!.update(PressOptions(value.enabled, false)) {
119 val current = props.value
120 current.connection?.activate(scope, current.target)
121 }
122 annotation.update(
123 Some(value.target),
124 FieldAnnotationKind.Label,
125 value.semantics,
126 value.enabled
127 )
128 return UiInvalidation.Layout
129 }
130 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
131 Unit
132 > {
133 val child = context.measureChild(0L, constraints)
134 context.placeChild(0L, Point(0.0, 0.0))
135 return LayoutResult(child, Unit)
136 }
137 public override fun scene(context: SceneContext, measured: Unit) {
138 val enabled = props.value.enabled
139 annotation.scene(context)
140 context.place(area!!, Rect(0.0, 0.0, context.size.width, context.size.height))
141 if (!enabled)
142 context.saveLayer(0.5)
143 context.children()
144 if (!enabled)
145 context.restore()
146 }
149private fun sameFieldLink(left: FieldLink?, right: FieldLink?): Boolean =
150 when (left) {
151 case Some(val a) -> when (right) {
152 case Some(val b) -> a === b
153 case None -> false
154 }
155 case None -> when (right) {
156 case Some(_) -> false
157 case None -> true
158 }
159 }
161public class FieldLabelBuilder internal constructor() {
162 public var fonts: FontCollection? = None
163 public var theme: ThemeSource? = None
164 public var textStyle: SpanStyle? = None
165 public var settings: ParagraphStyle? = None
166 public var style: StyleOverride = styleOverride()
167 public var key: ViewKey = ViewKey.Position
168 public var semantics: ComponentSemantics = ComponentSemantics()
169 public fun textStyle(configure: SpanStyleBuilder.() -> Unit): Unit {
170 textStyle = Some((textStyle ?: spanStyle()).copy(configure))
171 }
172 public fun settings(configure: ParagraphStyleBuilder.() -> Unit): Unit {
173 settings = Some((settings ?: paragraphStyle()).copy(configure))
174 }
175 public fun style(configure: StyleOverrideBuilder.() -> Unit): Unit {
176 style = style.copy(configure)
177 }
178 internal fun snapshot(content: FieldLabelContent, target: FieldTarget): FieldLabelProps =
179 FieldLabelProps(content, target, fonts, theme, textStyle, settings, style, semantics)
182public fun fieldLabel(
183 text: String,
184 target: FieldTarget,
185 configure: FieldLabelBuilder.() -> Unit = {}
186): View = makeFieldLabel(FieldLabelContent.Text(LabelText.Plain(text, None)), target, configure)
188public fun fieldLabel(
189 text: StyledText,
190 target: FieldTarget,
191 configure: FieldLabelBuilder.() -> Unit = {}
192): View = makeFieldLabel(FieldLabelContent.Text(LabelText.Styled(text)), target, configure)
194public fun fieldLabel(
195 content: View,
196 target: FieldTarget,
197 configure: FieldLabelBuilder.() -> Unit = {}
198): View = makeFieldLabel(FieldLabelContent.Custom(content), target, configure)
200internal fun makeFieldLabel(
201 content: FieldLabelContent,
202 target: FieldTarget,
203 configure: FieldLabelBuilder.() -> Unit
204): View {
205 val builder = FieldLabelBuilder()
206 builder.configure()
207 return component(
208 builder.snapshot(content, target),
209 { current, mount -> FieldLabel(current, scope = mount) },
210 builder.key
211 )