Scoop API

scoop:scoop.ui.component:0.1.0

src/field/text.scoop

Selected production source · src/field/text.scoop

1package scoop.ui.component
3import scoop.graphics.text.*
4import scoop.ui.*
5import scoop.ui.style.*
7public enum FieldTextRole {
8 Title, Description, Legend, LegendLabel, Error
9}
11public enum FieldLegendVariant {
12 Legend, Label
15public 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()
26public class FieldText public constructor(
27 private val props: Props<FieldTextProps>,
28 scope: ComponentScope
29): 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.value
36 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.style
42 ),
43 input.target,
44 when (input.role) {
45 case Description -> FieldAnnotationKind.Description
46 case Error -> FieldAnnotationKind.Error
47 else -> FieldAnnotationKind.Label
48 },
49 input.semantics
50 )
51 }
54internal fun fieldTextStyle(role: FieldTextRole, theme: Theme): TextStyle {
55 val body = theme.tokens.typography.body
56 val legend = role == FieldTextRole.Legend
57 return body.copy {
58 this.fontSize =
59 if (legend)
60 body.fontSize * 16.0 / 14.0
61 else
62 body.fontSize
63 this.lineHeight =
64 Some(
65 if (legend)
66 1.5
67 else
68 20.0 / 14.0
69 )
70 this.weight =
71 if (role == FieldTextRole.Description || role == FieldTextRole.Error)
72 400
73 else
74 500
75 this.color =
76 when (role) {
77 case Description -> theme.tokens.palette.muted.foreground
78 case Error -> theme.tokens.palette.destructive.background
79 else -> theme.tokens.palette.background.foreground
80 }
81 }
84public open class FieldTextBuilder internal constructor() {
85 public var fonts: FontCollection? = None
86 public var theme: ThemeSource? = None
87 public var textStyle: SpanStyle? = None
88 public var style: StyleOverride = styleOverride()
89 public var key: ViewKey = ViewKey.Position
90 public var target: FieldTarget? = None
91 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)
102public class FieldLegendBuilder internal constructor(): FieldTextBuilder() {
103 public var variant: FieldLegendVariant = FieldLegendVariant.Legend
106internal fun fieldPlain(
107 text: String,
108 semantics: ComponentSemantics = ComponentSemantics()
109): PanelContent = PanelContent.Label(LabelProps(LabelText.Plain(text, None), semantics = semantics))
111internal fun fieldStyled(text: StyledText): PanelContent =
112 PanelContent.Label(LabelProps(LabelText.Styled(text)))
114public fun fieldTitle(text: String, configure: FieldTextBuilder.() -> Unit = {}): View =
115 makeFieldText(fieldPlain(text), FieldTextRole.Title, configure)
117public fun fieldTitle(text: StyledText, configure: FieldTextBuilder.() -> Unit = {}): View =
118 makeFieldText(fieldStyled(text), FieldTextRole.Title, configure)
120public fun fieldTitle(content: View, configure: FieldTextBuilder.() -> Unit = {}): View =
121 makeFieldText(PanelContent.Custom(content), FieldTextRole.Title, configure)
123public fun fieldDescription(text: String, configure: FieldTextBuilder.() -> Unit = {}): View =
124 makeFieldText(fieldPlain(text), FieldTextRole.Description, configure)
126public fun fieldDescription(text: StyledText, configure: FieldTextBuilder.() -> Unit = {}): View =
127 makeFieldText(fieldStyled(text), FieldTextRole.Description, configure)
129public fun fieldDescription(content: View, configure: FieldTextBuilder.() -> Unit = {}): View =
130 makeFieldText(PanelContent.Custom(content), FieldTextRole.Description, configure)
132public fun fieldLegend(text: String, configure: FieldLegendBuilder.() -> Unit = {}): View =
133 makeFieldLegend(fieldPlain(text), configure)
135public fun fieldLegend(text: StyledText, configure: FieldLegendBuilder.() -> Unit = {}): View =
136 makeFieldLegend(fieldStyled(text), configure)
138public fun fieldLegend(content: View, configure: FieldLegendBuilder.() -> Unit = {}): View =
139 makeFieldLegend(PanelContent.Custom(content), configure)
141internal fun makeFieldText(
142 content: PanelContent,
143 role: FieldTextRole,
144 configure: FieldTextBuilder.() -> Unit
145): 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.key
152 )
155internal fun makeFieldLegend(
156 content: PanelContent,
157 configure: FieldLegendBuilder.() -> Unit
158): 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.key
165 )
168internal fun fieldLegendRole(variant: FieldLegendVariant): FieldTextRole =
169 if (variant == FieldLegendVariant.Legend)
170 FieldTextRole.Legend
171 else
172 FieldTextRole.LegendLabel