scoop:scoop.ui.component:0.1.0
src/field/label.scoop
Selected production source · src/field/label.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.surface.InputArea6
import scoop.ui.*7
import scoop.ui.style.*9
public enum FieldLabelContent {10
Text(LabelText), Custom(View)11
}13
public 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()22
)24
public class FieldLabel public constructor(25
private val props: Props<FieldLabelProps>,26
scope: ComponentScope27
): 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.value34
val theme = themes.read(input.theme)35
val base =36
theme.tokens.typography.body.copy {37
this.lineHeight = Some(1.0)38
this.weight = 50039
this.color = theme.tokens.palette.background.foreground40
}41
val child =42
when (input.content) {43
case Custom(val view) -> view44
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.textStyle52
)53
)54
case Styled(val value) -> LabelText.Styled(55
typographyContent(56
TypographyContent.Styled(value),57
base,58
input.textStyle59
)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.style72
}73
return element(74
FieldLabelInkProps(75
input.target,76
input.target.connection,77
input.target.isEnabled,78
input.semantics79
),80
{ current, mount -> FieldLabelInk(current, scope = mount) },81
[decorated]82
)83
}84
}86
private class FieldLabelInkProps(87
val target: FieldTarget,88
val connection: FieldLink?,89
val enabled: Boolean,90
val semantics: ComponentSemantics91
)93
private class FieldLabelInk(94
private val props: Props<FieldLabelInkProps>,95
scope: ComponentScope96
): Element<Unit>(scope) {97
private val annotation = scope.resource { FieldAnnotation(scope) }98
private var area: InputArea? = None99
private var press: Press? = None100
private var previous: FieldLabelInkProps? = None101
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.value109
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.enabled118
press!!.update(PressOptions(value.enabled, false)) {119
val current = props.value120
current.connection?.activate(scope, current.target)121
}122
annotation.update(123
Some(value.target),124
FieldAnnotationKind.Label,125
value.semantics,126
value.enabled127
)128
return UiInvalidation.Layout129
}130
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<131
Unit132
> {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.enabled139
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
}147
}149
private fun sameFieldLink(left: FieldLink?, right: FieldLink?): Boolean =150
when (left) {151
case Some(val a) -> when (right) {152
case Some(val b) -> a === b153
case None -> false154
}155
case None -> when (right) {156
case Some(_) -> false157
case None -> true158
}159
}161
public class FieldLabelBuilder internal constructor() {162
public var fonts: FontCollection? = None163
public var theme: ThemeSource? = None164
public var textStyle: SpanStyle? = None165
public var settings: ParagraphStyle? = None166
public var style: StyleOverride = styleOverride()167
public var key: ViewKey = ViewKey.Position168
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)180
}182
public fun fieldLabel(183
text: String,184
target: FieldTarget,185
configure: FieldLabelBuilder.() -> Unit = {}186
): View = makeFieldLabel(FieldLabelContent.Text(LabelText.Plain(text, None)), target, configure)188
public fun fieldLabel(189
text: StyledText,190
target: FieldTarget,191
configure: FieldLabelBuilder.() -> Unit = {}192
): View = makeFieldLabel(FieldLabelContent.Text(LabelText.Styled(text)), target, configure)194
public fun fieldLabel(195
content: View,196
target: FieldTarget,197
configure: FieldLabelBuilder.() -> Unit = {}198
): View = makeFieldLabel(FieldLabelContent.Custom(content), target, configure)200
internal fun makeFieldLabel(201
content: FieldLabelContent,202
target: FieldTarget,203
configure: FieldLabelBuilder.() -> Unit204
): 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.key211
)212
}