Scoop API

scoop:scoop.ui.component:0.1.0

src/field/separator.scoop

Selected production source · src/field/separator.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
8public class FieldSeparatorProps public constructor(
9 public val content: PanelContent? = None,
10 public val fonts: FontCollection? = None,
11 public val theme: ThemeSource? = None,
12 public val color: Color? = None,
13 public val style: StyleOverride = styleOverride()
16public class FieldSeparator public constructor(
17 private val props: Props<FieldSeparatorProps>,
18 scope: ComponentScope
19): Component(scope) {
20 private val themes = ThemeAccess(scope)
21 public override fun onMount() {
22 themes.mount(props.value.theme)
23 }
24 public override fun render(): View {
25 val input = props.value
26 val theme = themes.read(input.theme)
27 val line = SeparatorProps(color = Some(input.color ?: theme.tokens.palette.border))
28 val children = ArrayList<View>()
29 children.add(
30 component(
31 line,
32 { current, mount -> Separator(current, scope = mount) },
33 ViewKey.Number(0L)
34 )
35 )
36 when (input.content) {
37 case Some(val value) -> {
38 children.add(
39 component(
40 line,
41 { current, mount -> Separator(current, scope = mount) },
42 ViewKey.Number(1L)
43 )
44 )
45 children.add(
46 panelSlot(
47 makeRow {
48 this.add(
49 panelCaption(
50 value,
51 fieldTextStyle(FieldTextRole.Description, theme),
52 input.fonts
53 )
54 )
55 },
56 2L
57 )
58 )
59 }
60 case None -> {}
61 }
62 return panelFrame(
63 element(
64 theme.tokens.typography.body.fontSize / 14.0,
65 { current, mount -> FieldSeparatorLayout(current, scope = mount) },
66 children.toArray()
67 ),
68 visual = input.style
69 )
70 }
73private class FieldSeparatorLayout(
74 private val props: Props<Double>,
75 scope: ComponentScope
76): Element<Unit>(scope) {
77 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
78 Unit
79 > {
80 val hasContent = context.childCount == 3L
81 val gap = upperBound(8.0 * props.value, (constraints.maxWidth ?: 16.0 * props.value) / 2.0)
82 val caption =
83 if (hasContent)
84 context.measureChild(
85 2L,
86 Constraints(
87 maxWidth = insetMaximum(constraints.maxWidth, gap * 2.0),
88 maxHeight = constraints.maxHeight
89 )
90 )
91 else
92 LayoutMetrics(Size(0.0, 0.0))
93 val size =
94 constraints.constrain(
95 Size(
96 constraints.maxWidth ?:
97 (
98 caption.size.width +
99 if (hasContent)
100 gap * 2.0
101 else
102 0.0
103 ),
104 lowerBound(20.0 * props.value, caption.size.height)
105 )
106 )
107 val lineWidth =
108 if (hasContent)
109 lowerBound((size.width - caption.size.width - gap * 2.0) / 2.0, 0.0)
110 else
111 size.width
112 val lineConstraints =
113 Constraints(
114 minWidth = lineWidth,
115 maxWidth = Some(lineWidth),
116 maxHeight = Some(size.height)
117 )
118 val first = context.measureChild(0L, lineConstraints)
119 val second =
120 if (hasContent)
121 context.measureChild(1L, lineConstraints)
122 else
123 first
124 if (size.width > 0.0 && size.height > 0.0) {
125 context.placeChild(0L, Point(0.0, (size.height - first.size.height) / 2.0))
126 if (hasContent) {
127 context.placeChild(
128 1L,
129 Point(size.width - lineWidth, (size.height - second.size.height) / 2.0)
130 )
131 context.placeChild(
132 2L,
133 Point(
134 (size.width - caption.size.width) / 2.0,
135 (size.height - caption.size.height) / 2.0
136 )
137 )
138 }
139 }
140 return LayoutResult(
141 offsetMetrics(caption, size, (size.height - caption.size.height) / 2.0),
142 Unit
143 )
144 }
145 public override fun scene(context: SceneContext, measured: Unit) {
146 containerScene(context, true)
147 }
150public class FieldSeparatorBuilder internal constructor() {
151 public var fonts: FontCollection? = None
152 public var theme: ThemeSource? = None
153 public var color: Color? = None
154 public var style: StyleOverride = styleOverride()
155 public var key: ViewKey = ViewKey.Position
156 public fun style(configure: StyleOverrideBuilder.() -> Unit) {
157 style = style.copy(configure)
158 }
161public fun fieldSeparator(configure: FieldSeparatorBuilder.() -> Unit = {}): View =
162 makeFieldSeparator(None, configure)
164public fun fieldSeparator(text: String, configure: FieldSeparatorBuilder.() -> Unit = {}): View =
165 makeFieldSeparator(Some(fieldPlain(text)), configure)
167public fun fieldSeparator(
168 text: StyledText,
169 configure: FieldSeparatorBuilder.() -> Unit = {}
170): View = makeFieldSeparator(Some(fieldStyled(text)), configure)
172public fun fieldSeparator(content: View, configure: FieldSeparatorBuilder.() -> Unit = {}): View =
173 makeFieldSeparator(Some(PanelContent.Custom(content)), configure)
175internal fun makeFieldSeparator(
176 content: PanelContent?,
177 configure: FieldSeparatorBuilder.() -> Unit
178): View {
179 val builder = FieldSeparatorBuilder()
180 builder.configure()
181 return component(
182 FieldSeparatorProps(content, builder.fonts, builder.theme, builder.color, builder.style),
183 { current, mount -> FieldSeparator(current, scope = mount) },
184 builder.key
185 )