Scoop API

scoop:scoop.ui.component:0.1.0

src/content/item_group.scoop

Selected production source · src/content/item_group.scoop

1package scoop.ui.component
3import scoop.graphics.Color
4import scoop.ui.*
5import scoop.ui.style.*
7public class ItemGroupProps public constructor(
8 public val children: Array<View>,
9 public val size: ItemSize = ItemSize.Default,
10 public val theme: ThemeSource? = None,
11 public val style: StyleOverride = styleOverride()
14public class ItemGroup public constructor(
15 private val props: Props<ItemGroupProps>,
16 scope: ComponentScope
17): Component(scope) {
18 private val themes = ThemeAccess(scope)
19 public override fun onMount() {
20 themes.mount(props.value.theme)
21 }
22 public override fun render(): View {
23 val input = props.value
24 val density = themes.read(input.theme).tokens.typography.body.fontSize / 14.0
25 val layout =
26 LinearProps(
27 Array<FlexItem>(input.children.size) { index -> FlexItem(input.children[index]) },
28 itemGroupGap(input.size) * density,
29 CrossAlignment.Stretch
30 )
31 return styled(
32 element(
33 FlexProps(FlexAxis.Vertical, layout),
34 { current, mount -> ItemGroupLayout(current, scope = mount) },
35 input.children
36 )
37 ) { this.style = input.style.resolve(style()) }
38 }
41private class ItemGroupLayout(
42 private val props: Props<FlexProps>,
43 scope: ComponentScope
44): Element<Unit>(scope) {
45 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
46 Unit
47 > =
48 LayoutResult(
49 measureFlex(
50 context,
51 Constraints(
52 constraints.maxWidth ?: constraints.minWidth,
53 constraints.maxWidth,
54 constraints.minHeight,
55 constraints.maxHeight
56 ),
57 props.value.layout.items,
58 props.value.base
59 ),
60 Unit
61 )
62 public override fun scene(context: SceneContext, measured: Unit) {
63 context.children()
64 }
67internal fun itemGroupGap(size: ItemSize): Double =
68 when (size) {
69 case Default -> 16.0
70 case Sm -> 10.0
71 case Xs -> 8.0
72 }
74public class ItemGroupBuilder internal constructor(private val size: ItemSize): ChildrenBuilder() {
75 public var theme: ThemeSource? = None
76 public var style: StyleOverride = styleOverride()
77 public fun style(configure: StyleOverrideBuilder.() -> Unit): Unit {
78 style = style.copy(configure)
79 }
80 public fun item(configure: ItemBuilder.() -> Unit = {}): Unit {
81 add(makeItem(None, size, configure))
82 }
83 public fun item(content: View, configure: ItemBuilder.() -> Unit = {}): Unit {
84 add(makeItem(Some(content), size, configure))
85 }
86 internal fun snapshot(): ItemGroupProps = ItemGroupProps(childrenSnapshot(), size, theme, style)
89public fun itemGroup(
90 size: ItemSize = ItemSize.Default,
91 configure: ItemGroupBuilder.() -> Unit
92): View = makeItemGroup(size, configure)
94internal fun makeItemGroup(size: ItemSize, configure: ItemGroupBuilder.() -> Unit): View {
95 val builder = ItemGroupBuilder(size)
96 builder.configure()
97 return component(
98 builder.snapshot(),
99 { current, mount -> ItemGroup(current, scope = mount) },
100 builder.key
101 )
104public class ItemSeparatorProps public constructor(
105 public val color: Color? = None,
106 public val theme: ThemeSource? = None
109public class ItemSeparator public constructor(
110 private val props: Props<ItemSeparatorProps>,
111 scope: ComponentScope
112): Component(scope) {
113 private val themes = ThemeAccess(scope)
114 public override fun onMount() {
115 themes.mount(props.value.theme)
116 }
117 public override fun render(): View {
118 val input = props.value
119 val density = themes.read(input.theme).tokens.typography.body.fontSize / 14.0
120 return panelFrame(
121 component(
122 SeparatorProps(color = input.color, theme = input.theme),
123 { current, mount -> Separator(current, scope = mount) }
124 ),
125 Insets(0.0, 8.0 * density, 0.0, 8.0 * density)
126 )
127 }
130public class ItemSeparatorBuilder internal constructor() {
131 public var color: Color? = None
132 public var theme: ThemeSource? = None
133 public var key: ViewKey = ViewKey.Position
136public fun itemSeparator(configure: ItemSeparatorBuilder.() -> Unit = {}): View =
137 makeItemSeparator(configure)
139internal fun makeItemSeparator(configure: ItemSeparatorBuilder.() -> Unit): View {
140 val builder = ItemSeparatorBuilder()
141 builder.configure()
142 return component(
143 ItemSeparatorProps(builder.color, builder.theme),
144 { current, mount -> ItemSeparator(current, scope = mount) },
145 builder.key
146 )