scoop:scoop.ui.component:0.1.0
src/field/text.scoop
Selected production source · src/field/text.scoop
1
package scoop.ui.component3
import scoop.graphics.text.*4
import scoop.ui.*5
import scoop.ui.style.*7
public enum FieldTextRole {8
Title, Description, Legend, LegendLabel, Error9
}11
public enum FieldLegendVariant {12
Legend, Label13
}15
public class FieldTextProps public constructor(16
public val content: PanelContent,17
public val role: FieldTextRole,18
public val fonts: FontCollection? = None,19
public val theme: ThemeSource? = None,20
public val textStyle: SpanStyle? = None,21
public val style: StyleOverride = styleOverride(),22
public val target: FieldTarget? = None,23
public val semantics: ComponentSemantics = ComponentSemantics()24
)26
public class FieldText public constructor(27
private val props: Props<FieldTextProps>,28
scope: ComponentScope29
): Component(scope) {30
private val themes = ThemeAccess(scope)31
public override fun onMount() {32
themes.mount(props.value.theme)33
}34
public override fun render(): View {35
val input = props.value36
val theme = themes.read(input.theme)37
val base = fieldTextStyle(input.role, theme)38
return fieldAnnotated(39
panelFrame(40
panelCaption(input.content, input.textStyle?.resolve(base) ?: base, input.fonts),41
visual = input.style42
),43
input.target,44
when (input.role) {45
case Description -> FieldAnnotationKind.Description46
case Error -> FieldAnnotationKind.Error47
else -> FieldAnnotationKind.Label48
},49
input.semantics50
)51
}52
}54
internal fun fieldTextStyle(role: FieldTextRole, theme: Theme): TextStyle {55
val body = theme.tokens.typography.body56
val legend = role == FieldTextRole.Legend57
return body.copy {58
this.fontSize =59
if (legend)60
body.fontSize * 16.0 / 14.061
else62
body.fontSize63
this.lineHeight =64
Some(65
if (legend)66
1.567
else68
20.0 / 14.069
)70
this.weight =71
if (role == FieldTextRole.Description || role == FieldTextRole.Error)72
40073
else74
50075
this.color =76
when (role) {77
case Description -> theme.tokens.palette.muted.foreground78
case Error -> theme.tokens.palette.destructive.background79
else -> theme.tokens.palette.background.foreground80
}81
}82
}84
public open class FieldTextBuilder internal constructor() {85
public var fonts: FontCollection? = None86
public var theme: ThemeSource? = None87
public var textStyle: SpanStyle? = None88
public var style: StyleOverride = styleOverride()89
public var key: ViewKey = ViewKey.Position90
public var target: FieldTarget? = None91
public var semantics: ComponentSemantics = ComponentSemantics()92
public fun textStyle(configure: SpanStyleBuilder.() -> Unit) {93
textStyle = Some((textStyle ?: spanStyle()).copy(configure))94
}95
public fun style(configure: StyleOverrideBuilder.() -> Unit) {96
style = style.copy(configure)97
}98
internal fun snapshot(content: PanelContent, role: FieldTextRole): FieldTextProps =99
FieldTextProps(content, role, fonts, theme, textStyle, style, target, semantics)100
}102
public class FieldLegendBuilder internal constructor(): FieldTextBuilder() {103
public var variant: FieldLegendVariant = FieldLegendVariant.Legend104
}106
internal fun fieldPlain(107
text: String,108
semantics: ComponentSemantics = ComponentSemantics()109
): PanelContent = PanelContent.Label(LabelProps(LabelText.Plain(text, None), semantics = semantics))111
internal fun fieldStyled(text: StyledText): PanelContent =112
PanelContent.Label(LabelProps(LabelText.Styled(text)))114
public fun fieldTitle(text: String, configure: FieldTextBuilder.() -> Unit = {}): View =115
makeFieldText(fieldPlain(text), FieldTextRole.Title, configure)117
public fun fieldTitle(text: StyledText, configure: FieldTextBuilder.() -> Unit = {}): View =118
makeFieldText(fieldStyled(text), FieldTextRole.Title, configure)120
public fun fieldTitle(content: View, configure: FieldTextBuilder.() -> Unit = {}): View =121
makeFieldText(PanelContent.Custom(content), FieldTextRole.Title, configure)123
public fun fieldDescription(text: String, configure: FieldTextBuilder.() -> Unit = {}): View =124
makeFieldText(fieldPlain(text), FieldTextRole.Description, configure)126
public fun fieldDescription(text: StyledText, configure: FieldTextBuilder.() -> Unit = {}): View =127
makeFieldText(fieldStyled(text), FieldTextRole.Description, configure)129
public fun fieldDescription(content: View, configure: FieldTextBuilder.() -> Unit = {}): View =130
makeFieldText(PanelContent.Custom(content), FieldTextRole.Description, configure)132
public fun fieldLegend(text: String, configure: FieldLegendBuilder.() -> Unit = {}): View =133
makeFieldLegend(fieldPlain(text), configure)135
public fun fieldLegend(text: StyledText, configure: FieldLegendBuilder.() -> Unit = {}): View =136
makeFieldLegend(fieldStyled(text), configure)138
public fun fieldLegend(content: View, configure: FieldLegendBuilder.() -> Unit = {}): View =139
makeFieldLegend(PanelContent.Custom(content), configure)141
internal fun makeFieldText(142
content: PanelContent,143
role: FieldTextRole,144
configure: FieldTextBuilder.() -> Unit145
): View {146
val builder = FieldTextBuilder()147
builder.configure()148
return component(149
builder.snapshot(content, role),150
{ current, mount -> FieldText(current, scope = mount) },151
builder.key152
)153
}155
internal fun makeFieldLegend(156
content: PanelContent,157
configure: FieldLegendBuilder.() -> Unit158
): View {159
val builder = FieldLegendBuilder()160
builder.configure()161
return component(162
builder.snapshot(content, fieldLegendRole(builder.variant)),163
{ current, mount -> FieldText(current, scope = mount) },164
builder.key165
)166
}168
internal fun fieldLegendRole(variant: FieldLegendVariant): FieldTextRole =169
if (variant == FieldLegendVariant.Legend)170
FieldTextRole.Legend171
else172
FieldTextRole.LegendLabel