Scoop API

scoop:scoop.ui.component:0.1.0

src/select/popup.scoop

Selected production source · src/select/popup.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.time.Duration
6import scoop.ui.*
7import scoop.ui.style.*
8import scoop.ui.style.Theme
9import scoop.window.accessibility.*
11internal 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) -> Unit
31internal class SelectPopup(
32 private val props: Props<SelectPopupProps>,
33 scope: ComponentScope
34): 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.value
41 val theme = themes.read(input.options.theme)
42 val density = theme.tokens.typography.body.fontSize / 14.0
43 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 None
53 else
54 Some(AnimationSpec.Tween(Duration.milliseconds(100L)))
55 )
56 )
57 val inputs = props
58 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.Modal
70 ),
71 SemanticActions(
72 dismiss =
73 Some {
74 input.handle.requestClose()
75 if (!inputs.value.open.value)
76 SemanticActionResult.Completed
77 else
78 SemanticActionResult.Rejected
79 }
80 ),
81 relations = input.options.semantics.relations,
82 mergeLabels = false,
83 reference = input.options.semantics.reference
84 )
85 ]
86 )
87 ) {
88 placement =
89 if (input.position == SelectPosition.ItemAligned)
90 OverlayPlacement.ContentAligned(input.anchor)
91 else
92 OverlayPlacement.Anchored(
93 input.anchor,
94 alignment = OverlayAlignment.Center,
95 matchAnchorWidth = true
96 )
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.CloseAndConsume
105 else
106 OutsidePress.CloseAndContinue,
107 true
108 )
109 transition = PresenceOptions(motion, motion)
110 onCloseRequest = { reason -> inputs.value.onCloseRequest(reason) }
111 onClosed = { reason -> inputs.value.onClosed(reason) }
112 }
113 }
116internal class SelectPanelProps(val popup: SelectPopupProps, val theme: Theme)
118/** Alignment follows the opening selection, including reopening a retained exit transition. */
119internal class SelectPanel(
120 private val props: Props<SelectPanelProps>,
121 scope: ComponentScope
122): Component(scope) {
123 private var aligned = props.value.popup.selected
124 private var opened = true
125 public override fun render(): View {
126 val input = props.value
127 val popup = input.popup
128 val current = popup.open.value
129 if (current && !opened)
130 aligned = popup.selected
131 opened = current
132 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 -> None
144 },
145 binding({ false }, { _ -> }),
146 popup.chain,
147 true
148 ),
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 rows
158 )
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.theme
168 val density = theme.tokens.typography.body.fontSize / 14.0
169 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 }
185private class SelectListProps(val content: MenuContent, val aligned: ViewKey?)
187/** Measures each finite candidate once and translates the selected row into list coordinates. */
188private class SelectList(private val props: Props<SelectListProps>, scope: ComponentScope): Element<
189 Unit
190>(scope) {
191 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
192 Unit
193 > {
194 var y = 0.0
195 var width = constraints.minWidth
196 var anchor: Rect? = None
197 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 Unit
210 )
211 }
212 public override fun scene(context: SceneContext, measured: Unit) {
213 context.children()
214 }