scoop:scoop.ui.component:0.1.0
src/select/popup.scoop
Selected production source · src/select/popup.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.time.Duration6
import scoop.ui.*7
import scoop.ui.style.*8
import scoop.ui.style.Theme9
import scoop.window.accessibility.*11
internal class SelectPopupProps(12
val open: Binding<Boolean>,13
val content: MenuContent,14
val selected: ViewKey?,15
val focus: MenuFocus,16
val chain: MenuChain,17
val anchor: LayoutAnchor,18
val handle: OverlayHandle,19
val options: MenuOptions,20
val initial: OverlayFocusOptions,21
val position: SelectPosition,22
val maxHeight: Double,23
val scrollButtons: Boolean,24
val scroll: ScrollState,25
val style: StyleOverride,26
val transition: StyleValue<AnimationSpec?>,27
val onCloseRequest: (OverlayCloseReason) -> Boolean,28
val onClosed: (OverlayCloseReason) -> Unit29
)31
internal class SelectPopup(32
private val props: Props<SelectPopupProps>,33
scope: ComponentScope34
): Component(scope) {35
private val themes = ThemeAccess(scope)36
public override fun onMount() {37
themes.mount(props.value.options.theme)38
}39
public override fun render(): View {40
val input = props.value41
val theme = themes.read(input.options.theme)42
val density = theme.tokens.typography.body.fontSize / 14.043
val panel =44
component(45
SelectPanelProps(input, theme),46
{ current, mount -> SelectPanel(current, scope = mount) }47
)48
val motion =49
theme.tokens.motion.resolve(50
input.transition.resolve(51
if (input.position == SelectPosition.ItemAligned)52
None53
else54
Some(AnimationSpec.Tween(Duration.milliseconds(100L)))55
)56
)57
val inputs = props58
return portal(59
input.open,60
element(61
PopupMotionProps(input.options.enabled, 8.0 * density),62
{ current, mount -> PopupMotion(current, scope = mount) },63
[64
semanticContent(65
panel,66
SemanticRole.Menu,67
input.options.semantics.properties(68
enabled = input.options.enabled,69
modal = input.options.mode == OverlayMode.Modal70
),71
SemanticActions(72
dismiss =73
Some {74
input.handle.requestClose()75
if (!inputs.value.open.value)76
SemanticActionResult.Completed77
else78
SemanticActionResult.Rejected79
}80
),81
relations = input.options.semantics.relations,82
mergeLabels = false,83
reference = input.options.semantics.reference84
)85
]86
)87
) {88
placement =89
if (input.position == SelectPosition.ItemAligned)90
OverlayPlacement.ContentAligned(input.anchor)91
else92
OverlayPlacement.Anchored(93
input.anchor,94
alignment = OverlayAlignment.Center,95
matchAnchorWidth = true96
)97
handle = Some(input.handle);98
mode = input.options.mode;99
focus = Some(input.initial)100
dismiss =101
OverlayDismiss(102
true,103
if (input.options.mode == OverlayMode.Modal)104
OutsidePress.CloseAndConsume105
else106
OutsidePress.CloseAndContinue,107
true108
)109
transition = PresenceOptions(motion, motion)110
onCloseRequest = { reason -> inputs.value.onCloseRequest(reason) }111
onClosed = { reason -> inputs.value.onClosed(reason) }112
}113
}114
}116
internal class SelectPanelProps(val popup: SelectPopupProps, val theme: Theme)118
/** Alignment follows the opening selection, including reopening a retained exit transition. */119
internal class SelectPanel(120
private val props: Props<SelectPanelProps>,121
scope: ComponentScope122
): Component(scope) {123
private var aligned = props.value.popup.selected124
private var opened = true125
public override fun render(): View {126
val input = props.value127
val popup = input.popup128
val current = popup.open.value129
if (current && !opened)130
aligned = popup.selected131
opened = current132
val rows =133
Array<View>(popup.content.entries.size) {134
index ->135
val entry = popup.content.entries[index]136
component(137
MenuRowProps(138
entry,139
popup.options,140
input.theme,141
when (menuItem(entry)) {142
case Some(val item) -> Some(popup.focus.handle(item.key));143
case None -> None144
},145
binding({ false }, { _ -> }),146
popup.chain,147
true148
),149
{ current, mount -> MenuRow(current, scope = mount) },150
menuKey(entry)151
)152
}153
val list =154
element(155
SelectListProps(popup.content, aligned),156
{ current, mount -> SelectList(current, scope = mount) },157
rows158
)159
val body =160
element(161
MenuNavigationProps(popup.focus, popup.options, popup.chain),162
{ current, mount -> MenuNavigation(current, scope = mount) },163
[list]164
)165
val viewport =166
element(input, { current, mount -> SelectViewport(current, scope = mount) }, [body])167
val theme = input.theme168
val density = theme.tokens.typography.body.fontSize / 14.0169
val decoration =170
styleOverride {171
background(Some(Paint(theme.tokens.palette.popover.background)))172
border(173
Some(Border.all(1.0, componentPanelBorder(theme, theme.tokens.palette.popover)))174
)175
corners(Corners.all(8.0 * density));176
boxShadows(theme.tokens.effects.floating)177
}.then(popup.style).resolve(style())178
return component(179
StyledProps(viewport, decoration),180
{ current, mount -> Styled(current, scope = mount) }181
)182
}183
}185
private class SelectListProps(val content: MenuContent, val aligned: ViewKey?)187
/** Measures each finite candidate once and translates the selected row into list coordinates. */188
private class SelectList(private val props: Props<SelectListProps>, scope: ComponentScope): Element<189
Unit190
>(scope) {191
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<192
Unit193
> {194
var y = 0.0195
var width = constraints.minWidth196
var anchor: Rect? = None197
for (index in 0L ..< context.childCount) {198
val metrics =199
context.measureChild(index, Constraints(constraints.minWidth, constraints.maxWidth))200
context.placeChild(index, Point(0.0, y))201
val entry = props.value.content.entries[index]202
if (Some(menuKey(entry)) == props.value.aligned && (menuItem(entry)?.enabled ?: false))203
anchor = Some(Rect(0.0, y, metrics.size.width, metrics.size.height))204
y += metrics.size.height;205
width = lowerBound(width, metrics.size.width)206
}207
return LayoutResult(208
LayoutMetrics(constraints.constrain(Size(width, y)), overlayAnchor = anchor),209
Unit210
)211
}212
public override fun scene(context: SceneContext, measured: Unit) {213
context.children()214
}215
}