Scoop API

scoop:scoop.ui.component:0.1.0

src/menus/root.scoop

Selected production source · src/menus/root.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.TextAlignment
5import scoop.surface.*
6import scoop.ui.*
7import scoop.ui.style.*
8import scoop.window.accessibility.*
10public class DropdownMenu public constructor(
11 private val props: Props<DropdownMenuProps>,
12 scope: ComponentScope
13): Component(scope) {
14 private val root = MenuRoot(scope, { dropdownInput(props.value) })
15 public override fun onMount() {
16 root.mount()
17 }
18 public override fun render(): View = root.render()
21public class ContextMenu public constructor(
22 private val props: Props<ContextMenuProps>,
23 scope: ComponentScope
24): Component(scope) {
25 private val root =
26 MenuRoot(
27 scope,
28 {
29 val value = props.value
30 MenuRootInput(
31 value.open,
32 value.menu,
33 value.options,
34 MenuRootTrigger.Context(value.content),
35 gap = 0.0,
36 viewportPadding = value.viewportPadding,
37 focus = value.focus,
38 handle = value.handle,
39 dismiss = value.dismiss,
40 onOpenChange = value.onOpenChange,
41 onCloseRequest = value.onCloseRequest,
42 onClosed = value.onClosed,
43 longPress = value.longPress
44 )
45 }
46 )
47 public override fun onMount() {
48 root.mount()
49 }
50 public override fun render(): View = root.render()
53/** Root state is limited to optional uncontrolled open, opening position and a pending Tab. */
54internal class MenuRoot(private val scope: ComponentScope, private val read: () -> MenuRootInput) {
55 private val local = state(false)
56 private val anchor = LayoutAnchor()
57 private val link = HoverPopupLink()
58 private val handle = OverlayHandle()
59 private val triggerFocus = FocusHandle()
60 private val popupReference = SemanticsReference()
61 private val candidates = MenuFocus()
62 private val themes = ThemeAccess(scope)
63 private val last = state(false)
64 private val point = state<Point?>(None)
65 private var pendingTab: Boolean? = None
66 private val open = binding({ read().open?.value ?: local.value }, { value -> setOpen(value) })
67 private val chain =
68 MenuChain(
69 { close() },
70 { reverse -> tab(reverse) },
71 Some(
72 {
73 reverse -> when (read().horizontal) {
74 case Some(val move) -> move(reverse);
75 case None -> {}
76 }
77 }
78 )
79 )
80 fun mount() {
81 themes.mount(read().options.theme)
82 }
83 private fun setOpen(value: Boolean) {
84 val previous = open.value
85 if (value)
86 pendingTab = None
87 if (previous == value)
88 return
89 when (read().open) {
90 case Some(val binding) -> binding.value = value
91 case None -> local.value = value
92 }
93 val accepted = open.value
94 if (accepted != previous)
95 read().onOpenChange(accepted)
96 }
97 private fun close() {
98 (read().handle ?: handle).requestClose()
99 }
100 private fun tab(reverse: Boolean) {
101 pendingTab = Some(reverse)
102 close()
103 if (open.value)
104 pendingTab = None
105 }
106 private fun closed(reason: OverlayCloseReason) {
107 val next = pendingTab
108 pendingTab = None
109 when (next) {
110 case Some(val reverse) -> if (scope.isMounted)
111 scope.moveFocus(reverse);
112 case None -> {}
113 }
114 read().onClosed(reason)
115 }
116 private fun show(at: Point?, backwards: Boolean) {
117 if (!read().options.enabled)
118 return
119 point.value = at
120 last.value = backwards
121 setOpen(true)
122 if (open.value)
123 candidates.edge(backwards)?.requestFocus(FocusOrigin.Keyboard)
124 }
125 private fun toggle() {
126 if (!read().options.enabled)
127 return
128 if (open.value)
129 close()
130 else
131 show(None, false)
132 }
133 private fun semanticOpen(value: Boolean): SemanticActionResult {
134 if (!scope.isMounted)
135 return SemanticActionResult.Unavailable
136 if (!read().options.enabled)
137 return SemanticActionResult.Rejected
138 if (open.value == value)
139 return SemanticActionResult.Completed
140 if (value) {
141 point.value = None
142 last.value = false
143 setOpen(true)
144 } else
145 close()
146 return if (open.value == value)
147 SemanticActionResult.Completed
148 else
149 SemanticActionResult.Rejected
150 }
151 fun render(): View {
152 val input = read()
153 val theme = themes.read(input.options.theme)
154 val density = theme.tokens.typography.body.fontSize / 14.0
155 val contextMenu =
156 when (input.trigger) {
157 case Context(_) -> true;
158 else -> false
159 }
160 val child =
161 when (input.trigger) {
162 case Button(val value, val group) -> popupSemanticButton(
163 ButtonProps(
164 navigationLabel(
165 value.content,
166 when (value.fonts) {
167 case Some(_) -> value.fonts;
168 case None -> input.options.fonts
169 },
170 TextAlignment.Center
171 ),
172 { toggle() },
173 input.options.enabled && value.enabled,
174 value.style,
175 input.options.theme,
176 choiceFocus(value.focus, value.focus.handle ?: triggerFocus),
177 variant = value.variant,
178 size = value.size,
179 group = group,
180 semantics =
181 value.semantics.copy {
182 relations =
183 semanticControls(
184 relations,
185 (
186 input.options.semantics.reference ?:
187 this @MenuRoot.popupReference
188 ).id
189 )
190 }
191 ),
192 open.value,
193 { value -> semanticOpen(value) }
194 )
195 case Anchor(_) -> empty()
196 case Context(val view) -> semanticContent(
197 view,
198 SemanticRole.Group,
199 SemanticProperties(expanded = Some(open.value)),
200 SemanticActions(
201 showContextMenu = Some { semanticOpen(true) },
202 setExpanded = Some { value -> semanticOpen(value) }
203 ),
204 relations =
205 semanticControls(
206 SemanticRelations(),
207 (input.options.semantics.reference ?: this @MenuRoot.popupReference).id
208 ),
209 mergeLabels = false
210 )
211 }
212 val position =
213 when (point.value) {
214 case Some(val value) -> OverlayPlacement.AtPoint(
215 value,
216 input.side,
217 input.alignment,
218 input.gap * density,
219 input.viewportPadding * density
220 )
221 case None -> OverlayPlacement.Anchored(
222 input.anchor ?: anchor,
223 input.side,
224 input.alignment,
225 input.gap * density,
226 input.viewportPadding * density
227 )
228 }
229 val dismissal =
230 input.dismiss ?:
231 OverlayDismiss(
232 true,
233 if (input.options.mode == OverlayMode.Modal)
234 OutsidePress.CloseAndConsume
235 else
236 OutsidePress.CloseAndContinue,
237 true
238 )
239 val popup =
240 component(
241 MenuPopupProps(
242 open,
243 input.menu,
244 input.options,
245 position,
246 input.handle ?: handle,
247 candidates,
248 chain,
249 root = true,
250 last = last.value,
251 initial = input.focus,
252 dismiss = dismissal,
253 anchorVisibility = input.anchorVisibility,
254 link = Some(link),
255 onCloseRequest = { reason -> read().onCloseRequest(reason) },
256 onClosed = { reason -> closed(reason) },
257 semantics =
258 Some(
259 input.options.semantics.copy {
260 reference = Some(reference ?: this @MenuRoot.popupReference)
261 }
262 )
263 ),
264 { current, mount -> MenuPopup(current, scope = mount) }
265 )
266 return element(
267 MenuRootLayoutProps(
268 anchor,
269 link,
270 contextMenu,
271 input.options.enabled,
272 open.value,
273 input.longPress,
274 { at, backwards -> show(at, backwards) },
275 { read().hovered() },
276 {
277 if (open.value)
278 pendingTab = None
279 }
280 ),
281 { current, mount -> MenuRootLayout(current, scope = mount) },
282 [child, popup]
283 )
284 }
287public fun dropdownMenu(
288 open: Binding<Boolean>,
289 content: MenuContent,
290 configure: DropdownMenuBuilder.() -> Unit = {}
291): View = makeDropdownMenu(open, content, configure)
293/** Adds context-menu entry points without changing the enabled state of the wrapped content. */
294public fun contextMenu(
295 content: View,
296 menu: MenuContent,
297 configure: ContextMenuBuilder.() -> Unit = {}
298): View = makeContextMenu(content, menu, configure)