Scoop API

scoop:scoop.ui.component:0.1.0

src/menus/row.scoop

Selected production source · src/menus/row.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.svg.*
5import scoop.icons.lucide.check as checkIcon
6import scoop.graphics.text.*
7import scoop.surface.*
8import scoop.ui.*
9import scoop.ui.style.*
10import scoop.ui.style.Theme
11import scoop.window.*
12import scoop.window.accessibility.*
13import scoop.ui.component.row as menuRow
14import scoop.ui.component.label as menuLabel
15import scoop.ui.component.frame as menuFrame
16import scoop.ui.direction as menuDirection
18internal class MenuRowProps(
19 val entry: MenuEntry,
20 val options: MenuOptions,
21 val theme: Theme,
22 val focus: FocusHandle?,
23 val open: Binding<Boolean>,
24 val chain: MenuChain,
25 val selection: Boolean = false
28internal class MenuRow(private val props: Props<MenuRowProps>, scope: ComponentScope): Component(
29 scope
30) {
31 private val anchor = LayoutAnchor()
32 private val link = HoverPopupLink()
33 private val submenu = OverlayHandle()
34 private val submenuSemantics = SemanticsReference()
35 private val candidates = MenuFocus()
36 private val keyboard = state(false)
37 private val highlighted = state(false)
38 private val open =
39 binding({ props.value.open.value }, { value -> props.value.open.value = value })
40 private val portalOpen =
41 binding(
42 { open.value },
43 {
44 value -> if (value)
45 open.value = true
46 else
47 link.dismiss { open.value = false }
48 }
49 )
50 private fun enter() {
51 if (!(menuItem(props.value.entry)?.enabled ?: false) || !props.value.options.enabled)
52 return
53 keyboard.value = true
54 open.value = true
55 candidates.edge(false)?.requestFocus(FocusOrigin.Keyboard)
56 }
57 private fun activate() {
58 val input = props.value
59 if (!input.options.enabled)
60 return
61 when (input.entry) {
62 case Submenu(_, _) -> enter()
63 else -> {
64 activateSemantically()
65 }
66 }
67 }
68 private fun setExpanded(value: Boolean): SemanticActionResult {
69 val input = props.value
70 if (!input.options.enabled || !(menuItem(input.entry)?.enabled ?: false))
71 return SemanticActionResult.Rejected
72 when (input.entry) {
73 case Submenu(_, _) -> {
74 portalOpen.value = value
75 return if (open.value == value)
76 SemanticActionResult.Completed
77 else
78 SemanticActionResult.Rejected
79 }
80 else -> return SemanticActionResult.Unsupported
81 }
82 }
83 private fun activateSemantically(): SemanticActionResult {
84 val input = props.value
85 if (!input.options.enabled)
86 return SemanticActionResult.Rejected
87 when (input.entry) {
88 case Submenu(_, _) -> return setExpanded(true)
89 else -> {
90 val outcome = activateMenu(input.entry)
91 if (outcome.close)
92 input.chain.close()
93 return outcome.result
94 }
95 }
96 }
97 public override fun render(): View {
98 val input = props.value
99 val theme = input.theme
100 val density = theme.tokens.typography.body.fontSize / 14.0
101 when (input.entry) {
102 case Label(_, val content, val inset) -> return menuFrame(
103 panelCaption(
104 content,
105 theme.tokens.typography.body.copy {
106 fontSize = 12.0 * density
107 lineHeight = Some(16.0 / 12.0)
108 weight = 500
109 color =
110 componentForeground(
111 theme,
112 theme.tokens.palette.muted.foreground,
113 theme.tokens.palette.popover.background
114 )
115 },
116 input.options.fonts
117 )
118 ) {
119 logicalPadding =
120 Some(
121 LogicalInsets(
122 (
123 if (inset)
124 28.0
125 else
126 6.0
127 ) *
128 density,
129 4.0 * density,
130 6.0 * density,
131 4.0 * density
132 )
133 )
134 alignment = Alignment(AxisAlignment.Start, AxisAlignment.Start)
135 }
136 case Separator(_) -> return panelFrame(
137 separator {
138 thickness = 1.0
139 color =
140 Some(
141 componentForeground(
142 theme,
143 theme.tokens.palette.border,
144 theme.tokens.palette.popover.background,
145 3.0
146 )
147 )
148 },
149 Insets(0.0, 4.0 * density, 0.0, 4.0 * density)
150 )
151 else -> {}
152 }
153 val item = menuItem(input.entry)!!
154 val enabled = input.options.enabled && item.enabled
155 val active = enabled && (highlighted.value || input.open.value)
156 val colors = menuRowColors(theme, item.destructive, active, enabled)
157 val color = colors.foreground
158 val base =
159 theme.tokens.typography.body.copy {
160 fontSize = 14.0 * density;
161 lineHeight = Some(20.0 / 14.0);
162 weight = 400;
163 this.color = color
164 }
165 val text = item.style.text?.resolve(base) ?: base
166 val isSub =
167 when (input.entry) {
168 case Submenu(_, _) -> true;
169 else -> false
170 }
171 val checked = menuChecked(input.entry)
172 val indicator =
173 when (input.entry) {
174 case Checkbox(_, _, _, _) -> true
175 case Radio(_, _, _, _) -> true
176 else -> false
177 }
178 val native = Some(scope.nativeWindow)
179 val child =
180 menuRow {
181 when (item.icon) {
182 case Some(val icon) -> this.item(panelSlot(icon, 0L)) { shrink = 0.0 }
183 case None -> {}
184 }
185 this.item(
186 panelSlot(
187 item.content ?:
188 menuLabel(
189 styledText {
190 style = text;
191 this.text(item.label)
192 }
193 ) {
194 fonts = input.options.fonts
195 settings =
196 Some(
197 paragraphStyle {
198 maxLines = Some(1L);
199 ellipsis = "…"
200 }
201 )
202 },
203 1L
204 )
205 ) { grow = 1.0 }
206 when (item.shortcut) {
207 case Some(val chord) -> when (native) {
208 case Some(val value) -> this.item(
209 panelSlot(
210 menuDirection(
211 menuLabel(
212 styledText {
213 style =
214 text.copy {
215 fontSize = 12.0 * density
216 this.color =
217 if (active || !enabled)
218 text.color
219 else
220 componentForeground(
221 theme,
222 theme.tokens.palette.muted.foreground,
223 if (colors.background.alpha == 0.0)
224 theme.tokens.palette.popover.background
225 else
226 colors.background
227 )
228 }
229 this.text(shortcutCaption(value.shortcutLabel(chord)))
230 }
231 ) {
232 fonts = input.options.fonts
233 settings = Some(paragraphStyle { maxLines = Some(1L) })
234 },
235 LayoutDirection.Ltr
236 ),
237 2L
238 )
239 ) { shrink = 0.0 }
240 case None -> {}
241 }
242 case None -> {}
243 }
244 if (isSub)
245 this.item(
246 panelSlot(
247 navigationIcon(NavigationGlyph.End, 16.0 * density, text.color),
248 3L
249 )
250 ) { shrink = 0.0 }
251 else
252 if (indicator)
253 this.item(
254 panelSlot(
255 element(
256 MenuCheckProps(checked, 16.0 * density, text.color),
257 { current, mount -> MenuCheck(current, scope = mount) }
258 ),
259 3L
260 )
261 ) { shrink = 0.0 }
262 spacing =
263 (
264 if (input.selection)
265 8.0
266 else
267 6.0
268 ) *
269 density
270 if (input.selection)
271 logicalPadding = Some(LogicalInsets(end = 2.0 * density))
272 alignment = CrossAlignment.Center
273 if (item.inset && item.style.surface.padding == None)
274 logicalPadding = Some(LogicalInsets(start = 22.0 * density))
275 }
276 val semantics =
277 item.semantics.copy {
278 if (name == None && relations.labelledBy.size == 0L)
279 name = Some(item.label)
280 if (isSub)
281 relations = semanticControls(relations, this @MenuRow.submenuSemantics.id)
282 }
283 val action =
284 semanticButton(
285 ButtonProps(
286 ButtonContent.Custom(child),
287 { activate() },
288 enabled = enabled,
289 focus = choiceFocus(item.focus, input.focus!!),
290 variant = ButtonVariant.Ghost,
291 style = menuRowStyle(item.style, theme, item.destructive, active, enabled),
292 theme = input.options.theme,
293 semantics = semantics
294 ),
295 SemanticRole.MenuItem,
296 semantics.properties(
297 enabled = enabled,
298 checked =
299 if (indicator)
300 Some(
301 if (checked)
302 SemanticChecked.On
303 else
304 SemanticChecked.Off
305 )
306 else
307 None,
308 expanded =
309 if (isSub)
310 Some(open.value)
311 else
312 None
313 ),
314 SemanticActions(
315 invoke = Some { activateSemantically() },
316 toggle =
317 if (indicator)
318 Some { activateSemantically() }
319 else
320 None,
321 setExpanded =
322 if (isSub)
323 Some { value -> setExpanded(value) }
324 else
325 None
326 )
327 )
328 val popup =
329 when (input.entry) {
330 case Submenu(_, val content) -> component(
331 MenuPopupProps(
332 portalOpen,
333 content,
334 input.options,
335 OverlayPlacement.Anchored(
336 anchor,
337 OverlaySide.End,
338 OverlayAlignment.Start,
339 0.0,
340 8.0
341 ),
342 submenu,
343 candidates,
344 input.chain,
345 takeFocus = keyboard.value,
346 link = Some(link),
347 onClosed =
348 {
349 _ ->
350 keyboard.value = false
351 link.dismiss()
352 },
353 semantics =
354 Some(
355 ComponentSemantics(
356 name = Some(item.label),
357 reference = Some(submenuSemantics)
358 )
359 )
360 ),
361 { current, mount -> MenuPopup(current, scope = mount) }
362 )
363 else -> empty()
364 }
365 val hover = input.options.submenuHover
366 return element(
367 MenuRowLayoutProps(
368 anchor,
369 link,
370 open,
371 input.focus!!,
372 HoverOptions(
373 hover.openDelay,
374 hover.closeDelay,
375 hover.transitDelay,
376 hover.corridorPadding,
377 (keyboard.value && input.open.value) || hover.focus,
378 input.options.enabled && item.enabled && isSub && hover.enabled,
379 hover.longPress
380 ),
381 {
382 value -> if (highlighted.value != value)
383 highlighted.value = value
384 },
385 { enter() },
386 isSub,
387 { keyboard.value = false }
388 ),
389 { current, mount -> MenuRowLayout(current, scope = mount) },
390 [action, popup]
391 )
392 }
395internal fun menuRowColors(
396 theme: Theme,
397 destructive: Boolean,
398 active: Boolean,
399 enabled: Boolean = true
400): ColorPair {
401 val colors = theme.tokens.palette
402 if (!enabled)
403 return ColorPair(
404 Color.Transparent,
405 componentForeground(
406 theme,
407 if (theme.tokens.disabledOpacity < 1.0)
408 colors.popover.foreground
409 else
410 colors.disabledText,
411 colors.popover.background,
412 4.5
413 )
414 )
415 val fill =
416 if (!active)
417 Color.Transparent
418 else
419 if (destructive)
420 colors.destructive.background.withAlpha(
421 if (theme.tokens.mode == ThemeMode.Dark)
422 0.2
423 else
424 0.1
425 )
426 else
427 colors.accent.background
428 return componentSurfaceColors(
429 theme,
430 ColorPair(
431 fill,
432 if (destructive)
433 colors.destructive.background
434 else
435 if (active)
436 colors.accent.foreground
437 else
438 colors.popover.foreground
439 ),
440 colors.popover.background
441 )
444internal fun menuRowStyle(
445 local: ButtonStyle,
446 theme: Theme,
447 destructive: Boolean,
448 active: Boolean,
449 enabled: Boolean = true
450): ButtonStyle {
451 val density = theme.tokens.typography.body.fontSize / 14.0
452 val fill = menuRowColors(theme, destructive, active, enabled).background
453 return local.copy {
454 surface {
455 if (suggestedSize == None)
456 suggestedSize = Some(Size(0.0, 28.0 * density))
457 if (minimumSize == None)
458 minimumSize = Some(Size(0.0, 0.0))
459 if (padding == None)
460 padding = Some(Insets(6.0 * density, 4.0 * density, 6.0 * density, 4.0 * density))
461 if (focus == None)
462 focus = Some(OutlineStyle(Color.Transparent, 0.0, 0.0))
463 style =
464 styleOverride {
465 background(Some(Paint(fill)));
466 border(None)
467 corners(Corners.all(6.0 * density));
468 boxShadows([])
469 }.then(local.surface.style)
470 }
471 }
474internal class MenuCheckProps(val checked: Boolean, val size: Double, val color: Color)
476internal class MenuCheck(private val props: Props<MenuCheckProps>, scope: ComponentScope): Element<
477 Unit
478>(scope) {
479 private var glyph: SvgDocument? = None
480 public override fun onMount() {
481 glyph = Some(scope.resource { SvgDocument.parse(checkIcon.source) })
482 }
483 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
484 Unit
485 > =
486 LayoutResult(
487 LayoutMetrics(constraints.constrain(Size(props.value.size, props.value.size))),
488 Unit
489 )
490 public override fun scene(context: SceneContext, measured: Unit) {
491 val input = props.value
492 val size = context.size
493 if (input.checked)
494 context.draw {
495 paint -> paint.canvas.withState {
496 canvas ->
497 canvas.scale(size.width / 24.0, size.height / 24.0)
498 canvas.drawSvg(
499 glyph!!,
500 Rect(0.0, 0.0, 24.0, 24.0),
501 SvgDrawOptions(currentColor = Some(input.color))
502 )
503 }
504 }
505 }