Scoop API

scoop:scoop.ui.component:0.1.0

src/field/group.scoop

Selected production source · src/field/group.scoop

1package scoop.ui.component
3import scoop.graphics.text.*
4import scoop.ui.*
5import scoop.ui.style.*
7public enum FieldStackKind {
8 Group, Content
9}
11public class FieldStackProps public constructor(
12 public val children: Array<View>,
13 public val kind: FieldStackKind = FieldStackKind.Group,
14 public val spacing: Double? = None,
15 public val theme: ThemeSource? = None,
16 public val padding: Insets = Insets(),
17 public val style: StyleOverride = styleOverride()
18) {
19 init {
20 fieldLength(spacing);
21 padding.totals()
22 }
25public class FieldStack public constructor(
26 private val props: Props<FieldStackProps>,
27 scope: ComponentScope
28): Component(scope) {
29 private val themes = ThemeAccess(scope)
30 public override fun onMount() {
31 themes.mount(props.value.theme)
32 }
33 public override fun render(): View {
34 val input = props.value
35 val density = themes.read(input.theme).tokens.typography.body.fontSize / 14.0
36 val content =
37 makeColumn {
38 this.addAll(input.children)
39 this.spacing =
40 input.spacing ?:
41 (
42 if (input.kind == FieldStackKind.Group)
43 20.0
44 else
45 2.0
46 ) *
47 density
48 this.alignment = CrossAlignment.Stretch
49 }
50 return panelFrame(content, input.padding, input.style)
51 }
54public class FieldStackBuilder internal constructor(): ChildrenBuilder() {
55 public var spacing: Double? = None
56 public var theme: ThemeSource? = None
57 public var padding: Insets = Insets()
58 public var style: StyleOverride = styleOverride()
59 public fun style(configure: StyleOverrideBuilder.() -> Unit) {
60 style = style.copy(configure)
61 }
62 internal fun snapshot(kind: FieldStackKind): FieldStackProps =
63 FieldStackProps(childrenSnapshot(), kind, spacing, theme, padding, style)
66public fun fieldGroup(configure: FieldStackBuilder.() -> Unit = {}): View =
67 makeFieldStack(FieldStackKind.Group, configure)
69public fun fieldContent(configure: FieldStackBuilder.() -> Unit = {}): View =
70 makeFieldStack(FieldStackKind.Content, configure)
72internal fun makeFieldStack(kind: FieldStackKind, configure: FieldStackBuilder.() -> Unit): View {
73 val builder = FieldStackBuilder()
74 builder.configure()
75 return component(
76 builder.snapshot(kind),
77 { current, mount -> FieldStack(current, scope = mount) },
78 builder.key
79 )
82public class FieldSetProps public constructor(
83 public val content: View,
84 public val legend: PanelContent? = None,
85 public val variant: FieldLegendVariant = FieldLegendVariant.Legend,
86 public val description: PanelContent? = None,
87 public val spacing: Double? = None,
88 public val fonts: FontCollection? = None,
89 public val theme: ThemeSource? = None,
90 public val padding: Insets = Insets(),
91 public val style: StyleOverride = styleOverride()
92) {
93 init {
94 fieldLength(spacing);
95 padding.totals()
96 }
99public class FieldSet public constructor(
100 private val props: Props<FieldSetProps>,
101 scope: ComponentScope
102): Component(scope) {
103 private val themes = ThemeAccess(scope)
104 public override fun onMount() {
105 themes.mount(props.value.theme)
106 }
107 public override fun render(): View {
108 val input = props.value
109 val theme = themes.read(input.theme)
110 val content =
111 makeColumn {
112 when (input.legend) {
113 case Some(val value) -> this.add(
114 panelSlot(
115 panelCaption(
116 value,
117 fieldTextStyle(fieldLegendRole(input.variant), theme),
118 input.fonts
119 ),
120 0L
121 )
122 )
123 case None -> {}
124 }
125 when (input.description) {
126 case Some(val value) -> this.add(
127 panelSlot(
128 panelCaption(
129 value,
130 fieldTextStyle(FieldTextRole.Description, theme),
131 input.fonts
132 ),
133 1L
134 )
135 )
136 case None -> {}
137 }
138 this.add(panelSlot(input.content, 2L))
139 this.spacing = input.spacing ?: 16.0 * theme.tokens.typography.body.fontSize / 14.0
140 this.alignment = CrossAlignment.Stretch
141 }
142 return panelFrame(content, input.padding, input.style)
143 }
146public class FieldSetBuilder internal constructor() {
147 public var legend: PanelContent? = None
148 public var description: PanelContent? = None
149 public var variant: FieldLegendVariant = FieldLegendVariant.Legend
150 public var spacing: Double? = None
151 public var fonts: FontCollection? = None
152 public var theme: ThemeSource? = None
153 public var padding: Insets = Insets()
154 public var style: StyleOverride = styleOverride()
155 public var key: ViewKey = ViewKey.Position
156 public fun legend(text: String) {
157 legend = Some(fieldPlain(text))
158 }
159 public fun legend(text: StyledText) {
160 legend = Some(fieldStyled(text))
161 }
162 public fun legend(content: View) {
163 legend = Some(PanelContent.Custom(content))
164 }
165 public fun description(text: String) {
166 description = Some(fieldPlain(text))
167 }
168 public fun description(text: StyledText) {
169 description = Some(fieldStyled(text))
170 }
171 public fun description(content: View) {
172 description = Some(PanelContent.Custom(content))
173 }
174 public fun style(configure: StyleOverrideBuilder.() -> Unit) {
175 style = style.copy(configure)
176 }
177 internal fun snapshot(content: View): FieldSetProps =
178 FieldSetProps(content, legend, variant, description, spacing, fonts, theme, padding, style)
181public fun fieldSet(content: View, configure: FieldSetBuilder.() -> Unit = {}): View =
182 makeFieldSet(content, configure)
184internal fun makeFieldSet(content: View, configure: FieldSetBuilder.() -> Unit): View {
185 val builder = FieldSetBuilder()
186 builder.configure()
187 return component(
188 builder.snapshot(content),
189 { current, mount -> FieldSet(current, scope = mount) },
190 builder.key
191 )