scoop:scoop.ui.component:0.1.0
src/combobox/popup.scoop
Selected production source · src/combobox/popup.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.*5
import scoop.ui.style.*6
import scoop.ui.style.Theme8
/** Only inspect enough rows to fill the bounded viewport, even for a large provided source. */9
private fun <T: Equals<T>> comboListHeight(10
source: ChoiceSource<T>,11
limit: Double,12
density: Double13
): Double {14
var result = 0.015
for (index in 0L ..< source.count) {16
if (result >= limit)17
return limit18
result += when (source.entryAt(index)) {19
case Separator(_) -> 9.0 * density20
else -> 28.0 * density21
}22
}23
return upperBound(result, limit)24
}26
internal fun <T: Equals<T>> comboPopup(27
control: ComboboxControl<T>,28
source: ChoiceSource<T>,29
theme: Theme30
): View {31
val input = control.props.value32
val d = theme.tokens.typography.body.fontSize / 14.033
val children = ArrayList<View>()34
val popupInput = input.inputPlacement == ComboboxInputPlacement.Popup35
if (popupInput)36
children.add(panelFrame(comboField(control, theme, true), Insets.all(4.0 * d)))37
val noResults = source.choiceCount == 0L38
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.background50
)51
},52
input.input.fonts,53
true54
),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.mutable72
)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.theme83
enabled = input.mutable && control.open.value;84
focus = FocusOptions(tabStop = false)85
scrollbars = ScrollbarVisibility.Hidden;86
chain = ScrollChain.Contain87
lineStep = 28.0 * d;88
overscan = 28.0 * d89
}90
)91
}92
val height =93
if (noResults)94
input.maxHeight * d95
else96
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.palette104
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
)115
}117
private class ComboPopupLayoutProps(val height: Double, val hasInput: Boolean, val empty: Boolean)119
private class ComboPopupLayout(120
private val props: Props<ComboPopupLayoutProps>,121
scope: ComponentScope122
): Element<Unit>(scope) {123
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<124
Unit125
> {126
val input = props.value127
val width = constraints.maxWidth ?: constraints.minWidth128
var top = 0.0129
var index = 0L130
if (input.hasInput) {131
top =132
context.measureChild(133
0L,134
Constraints(width, Some(width), maxHeight = constraints.maxHeight)135
).size.height136
context.placeChild(0L, Point(0.0, 0.0));137
index = 1L138
}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
else150
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
Unit156
)157
}158
public override fun scene(context: SceneContext, measured: Unit) {159
context.children()160
}161
}