Scoop API

scoop:scoop.ui.component:0.1.0

src/combobox/popup.scoop

Selected production source · src/combobox/popup.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
5import scoop.ui.style.*
6import scoop.ui.style.Theme
8/** Only inspect enough rows to fill the bounded viewport, even for a large provided source. */
9private fun <T: Equals<T>> comboListHeight(
10 source: ChoiceSource<T>,
11 limit: Double,
12 density: Double
13): Double {
14 var result = 0.0
15 for (index in 0L ..< source.count) {
16 if (result >= limit)
17 return limit
18 result += when (source.entryAt(index)) {
19 case Separator(_) -> 9.0 * density
20 else -> 28.0 * density
21 }
22 }
23 return upperBound(result, limit)
26internal fun <T: Equals<T>> comboPopup(
27 control: ComboboxControl<T>,
28 source: ChoiceSource<T>,
29 theme: Theme
30): View {
31 val input = control.props.value
32 val d = theme.tokens.typography.body.fontSize / 14.0
33 val children = ArrayList<View>()
34 val popupInput = input.inputPlacement == ComboboxInputPlacement.Popup
35 if (popupInput)
36 children.add(panelFrame(comboField(control, theme, true), Insets.all(4.0 * d)))
37 val noResults = source.choiceCount == 0L
38 if (noResults)
39 children.add(
40 panelFrame(
41 panelCaption(
42 input.empty,
43 theme.tokens.typography.body.copy {
44 fontSize = 14.0 * d;
45 color =
46 componentForeground(
47 theme,
48 theme.tokens.palette.muted.foreground,
49 theme.tokens.palette.popover.background
50 )
51 },
52 input.input.fonts,
53 true
54 ),
55 Insets(8.0 * d, 8.0 * d, 8.0 * d, 8.0 * d)
56 )
57 )
58 else {
59 val rows = control.rows(source, theme)
60 val selected = comboValues(input.selection)
61 children.add(
62 virtualList(rows, ItemExtent.Variable(28.0 * d)) {
63 itemSemantics =
64 Some(
65 ItemSemantics(
66 rows,
67 {
68 index -> comboItemSemantics(
69 source.entryAt(index),
70 selected,
71 input.mutable
72 )
73 },
74 selection =
75 Some(comboSelectionSemantics(source, selected, input.multiple)),
76 setSelected =
77 Some { key, value -> control.select(key, false, Some(value)) }
78 )
79 )
80 state = Some(control.scroll);
81 handle = Some(control.list);
82 this.theme = input.input.theme
83 enabled = input.mutable && control.open.value;
84 focus = FocusOptions(tabStop = false)
85 scrollbars = ScrollbarVisibility.Hidden;
86 chain = ScrollChain.Contain
87 lineStep = 28.0 * d;
88 overscan = 28.0 * d
89 }
90 )
91 }
92 val height =
93 if (noResults)
94 input.maxHeight * d
95 else
96 comboListHeight(source, input.maxHeight * d, d)
97 val body =
98 element(
99 ComboPopupLayoutProps(height, popupInput, noResults),
100 { current, mount -> ComboPopupLayout(current, scope = mount) },
101 children.toArray()
102 )
103 val ink = theme.tokens.palette
104 return panelFrame(
105 body,
106 Insets.all(4.0 * d),
107 styleOverride {
108 background(Some(Paint(ink.popover.background)))
109 border(Some(Border.all(1.0, componentPanelBorder(theme, ink.popover))))
110 corners(Corners.all(8.0 * d));
111 boxShadows(theme.tokens.effects.floating)
112 }.then(input.popupStyle),
113 Some((input.popupWidth ?: input.width) * d)
114 )
117private class ComboPopupLayoutProps(val height: Double, val hasInput: Boolean, val empty: Boolean)
119private class ComboPopupLayout(
120 private val props: Props<ComboPopupLayoutProps>,
121 scope: ComponentScope
122): Element<Unit>(scope) {
123 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
124 Unit
125 > {
126 val input = props.value
127 val width = constraints.maxWidth ?: constraints.minWidth
128 var top = 0.0
129 var index = 0L
130 if (input.hasInput) {
131 top =
132 context.measureChild(
133 0L,
134 Constraints(width, Some(width), maxHeight = constraints.maxHeight)
135 ).size.height
136 context.placeChild(0L, Point(0.0, 0.0));
137 index = 1L
138 }
139 val height =
140 upperBound(
141 input.height,
142 lowerBound(0.0, (constraints.maxHeight ?: (top + input.height)) - top)
143 )
144 val list =
145 context.measureChild(
146 index,
147 if (input.empty)
148 Constraints(width, Some(width), maxHeight = Some(height))
149 else
150 Constraints.tight(Size(width, height))
151 )
152 context.placeChild(index, Point(0.0, top))
153 return LayoutResult(
154 LayoutMetrics(constraints.constrain(Size(width, top + list.size.height))),
155 Unit
156 )
157 }
158 public override fun scene(context: SceneContext, measured: Unit) {
159 context.children()
160 }