Scoop API

scoop:scoop.ui.component:0.1.0

src/disclosure/trigger.scoop

Selected production source · src/disclosure/trigger.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.svg.*
5import scoop.icons.lucide.chevronDown
6import scoop.graphics.text.*
7import scoop.math.PI
8import scoop.math.min
9import scoop.ui.*
10import scoop.ui.style.*
11import scoop.window.accessibility.*
12import scoop.ui.component.row as makeRow
14/** Non-interactive content for the owning disclosure's single activation target. */
15public class TriggerSpec public constructor(
16 public val content: ButtonContent,
17 public val enabled: Boolean = true,
18 public val indicator: Boolean = true,
19 public val variant: ButtonVariant = ButtonVariant.Ghost,
20 public val style: ButtonStyle? = None,
21 public val focus: FocusOptions = FocusOptions(),
22 public val fonts: FontCollection? = None,
23 public val semantics: ComponentSemantics = ComponentSemantics()
26internal fun disclosureTrigger(
27 input: TriggerSpec,
28 enabled: Boolean,
29 fonts: FontCollection?,
30 theme: ThemeSource?,
31 presence: Presence?,
32 open: Boolean,
33 currentOpen: () -> Boolean,
34 setOpen: (Boolean) -> SemanticActionResult,
35 semantics: ComponentSemantics
36): View =
37 semanticButton(
38 ButtonProps(
39 ButtonContent.Resolved {
40 appearance ->
41 val content =
42 when (input.content) {
43 case Label(val label) -> ButtonContent.Label(
44 LabelProps(
45 label.text,
46 disclosureFonts(label.fonts, disclosureFonts(input.fonts, fonts)),
47 label.settings,
48 label.inlines,
49 label.theme,
50 label.baseStyle,
51 label.underline,
52 label.semantics
53 )
54 )
55 else -> input.content
56 }
57 makeRow {
58 this.item(actionContent(content, appearance)) { this.grow = 1.0 }
59 if (input.indicator)
60 this.add(
61 element(
62 DisclosureArrowProps(
63 presence,
64 open,
65 appearance.iconSize,
66 appearance.text.color
67 ),
68 { current, mount -> DisclosureArrow(current, scope = mount) }
69 )
70 )
71 this.spacing = appearance.spacing
72 this.alignment = CrossAlignment.Center
73 }
74 },
75 { setOpen(!currentOpen()) },
76 enabled && input.enabled,
77 input.style ?: buttonStyle { this.kind = SurfaceKind.Plain },
78 theme,
79 input.focus,
80 variant = input.variant,
81 semantics = semantics
82 ),
83 SemanticRole.Disclosure,
84 semantics.properties(enabled = enabled && input.enabled, expanded = Some(open)),
85 SemanticActions(invoke = Some { setOpen(!currentOpen()) }, setExpanded = Some(setOpen))
86 )
88private class DisclosureArrowProps(
89 val presence: Presence?,
90 val open: Boolean,
91 val size: Double,
92 val color: Color
95private class DisclosureArrow(
96 private val props: Props<DisclosureArrowProps>,
97 scope: ComponentScope
98): Element<Unit>(scope) {
99 private var outline: SvgDocument? = None
100 public override fun onMount() {
101 outline = Some(scope.resource { SvgDocument.parse(chevronDown.source) })
102 }
103 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
104 Unit
105 > =
106 LayoutResult(
107 LayoutMetrics(constraints.constrain(Size(props.value.size, props.value.size))),
108 Unit
109 )
110 public override fun scene(context: SceneContext, measured: Unit) {
111 val input = props.value
112 val size = context.size
113 val side = min(size.width, size.height)
114 val angle = disclosureProgress(input.presence, input.open) * PI
115 context.draw {
116 paint -> paint.canvas.withState {
117 canvas ->
118 canvas.translate(size.width / 2.0, size.height / 2.0)
119 canvas.rotate(angle)
120 canvas.scale(side / 24.0, side / 24.0)
121 canvas.translate(-12.0, -12.0)
122 canvas.drawSvg(
123 outline!!,
124 Rect(0.0, 0.0, 24.0, 24.0),
125 SvgDrawOptions(currentColor = Some(input.color))
126 )
127 }
128 }
129 }
132private fun disclosureFonts(primary: FontCollection?, fallback: FontCollection?): FontCollection? =
133 when (primary) {
134 case Some(_) -> primary
135 case None -> fallback
136 }