Scoop API

scoop:scoop.ui.component:0.1.0

src/overlays/popover.scoop

Selected production source · src/overlays/popover.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.TextAlignment
5import scoop.surface.*
6import scoop.time.Duration
7import scoop.ui.*
8import scoop.ui.style.*
9import scoop.window.accessibility.*
10import scoop.ui.component.column as popupColumn
12public class Popover public constructor(
13 private val props: Props<PopoverProps>,
14 scope: ComponentScope
15): Component(scope) {
16 private val anchor = LayoutAnchor()
17 private val localHandle = OverlayHandle()
18 private val semanticReference = SemanticsReference()
19 private val themes = ThemeAccess(scope)
20 private val titles = ActionTextStyle()
21 private val descriptions = ActionTextStyle()
22 public override fun onMount() {
23 themes.mount(props.value.theme)
24 }
25 private fun setOpen(value: Boolean) {
26 val input = props.value
27 val previous = input.open.value
28 if (previous == value)
29 return
30 input.open.value = value
31 val accepted = input.open.value
32 if (accepted != previous)
33 props.value.onOpenChange(accepted)
34 }
35 private fun toggle() {
36 val input = props.value
37 if (!input.enabled || !(input.trigger?.enabled ?: false))
38 return
39 if (input.open.value)
40 (input.handle ?: localHandle).requestClose()
41 else
42 setOpen(true)
43 }
44 private fun dismissSemantically(): SemanticActionResult {
45 if (!props.value.open.value)
46 return SemanticActionResult.Unavailable;
47 (props.value.handle ?: localHandle).requestClose()
48 return if (!props.value.open.value)
49 SemanticActionResult.Completed
50 else
51 SemanticActionResult.Rejected
52 }
53 public override fun render(): View {
54 val inputs = props
55 val input = inputs.value
56 val theme = themes.read(input.theme)
57 val palette = theme.tokens.palette
58 val density = theme.tokens.typography.body.fontSize / 14.0
59 val titleStyle =
60 titles.resolve(theme.tokens.typography.action, palette.popover.foreground, None)
61 val descriptionStyle =
62 descriptions.resolve(
63 theme.tokens.typography.body,
64 componentForeground(theme, palette.muted.foreground, palette.popover.background),
65 None
66 )
67 val content =
68 popupColumn {
69 if (panelPresent(input.title) || panelPresent(input.description))
70 add(
71 panelSlot(
72 popupColumn {
73 when (input.title) {
74 case Some(val value) -> add(
75 panelSlot(panelCaption(value, titleStyle, input.fonts), 0L)
76 )
77 case None -> {}
78 }
79 when (input.description) {
80 case Some(val value) -> add(
81 panelSlot(
82 panelCaption(value, descriptionStyle, input.fonts),
83 1L
84 )
85 )
86 case None -> {}
87 }
88 spacing = 2.0 * density
89 alignment = CrossAlignment.Stretch
90 },
91 0L
92 )
93 )
94 add(panelSlot(input.content, 1L))
95 spacing = 10.0 * density
96 alignment = CrossAlignment.Stretch
97 }
98 val panel =
99 panelFrame(
100 content,
101 Insets.all(10.0 * density),
102 styleOverride {
103 background(Some(Paint(palette.popover.background)))
104 border(Some(Border.all(1.0, componentPanelBorder(theme, palette.popover))))
105 corners(Corners.all(8.0 * density))
106 boxShadows(theme.tokens.effects.floating)
107 }.then(input.style),
108 Some(input.width * density)
109 )
110 val trigger =
111 when (input.trigger) {
112 case Some(val value) -> component(
113 ButtonProps(
114 navigationLabel(
115 value.content,
116 if (panelPresent(value.fonts))
117 value.fonts
118 else
119 input.fonts,
120 TextAlignment.Center
121 ),
122 { toggle() },
123 input.enabled && value.enabled,
124 value.style,
125 input.theme,
126 value.focus,
127 variant = value.variant,
128 size = value.size,
129 semantics =
130 value.semantics.copy {
131 relations =
132 semanticControls(
133 relations,
134 (
135 input.semantics.reference ?:
136 this @Popover.semanticReference
137 ).id
138 )
139 }
140 ),
141 { current, mount -> Button(current, scope = mount) }
142 )
143 case None -> empty()
144 }
145 val motion =
146 theme.tokens.motion.resolve(
147 input.transition.resolve(Some(AnimationSpec.Tween(Duration.milliseconds(100L))))
148 )
149 val localAnchor = anchor
150 val overlayHandle = input.handle ?: localHandle
151 val semantics =
152 input.semantics.copy {
153 if (name == None && relations.labelledBy.size == 0L)
154 name = semanticCaption(input.title)
155 if (description == None)
156 description = semanticCaption(input.description)
157 reference = Some(input.semantics.reference ?: this @Popover.semanticReference)
158 }
159 val popup =
160 portal(
161 binding({ props.value.open.value }, { value -> setOpen(value) }),
162 element(
163 PopupMotionProps(
164 input.enabled,
165 8.0 * density,
166 popupArrowInk(
167 input.arrow,
168 density,
169 input.style.background.resolve(Some(Paint(palette.popover.background)))
170 )
171 ),
172 { current, mount -> PopupMotion(current, scope = mount) },
173 [
174 semanticContent(
175 panel,
176 SemanticRole.Dialog,
177 semantics.properties(
178 enabled = input.enabled,
179 modal = input.mode == OverlayMode.Modal
180 ),
181 actions = SemanticActions(dismiss = Some { dismissSemantically() }),
182 relations = semantics.relations,
183 mergeLabels = false,
184 reference = semantics.reference
185 )
186 ]
187 )
188 ) {
189 placement =
190 OverlayPlacement.Anchored(
191 input.anchor ?: localAnchor,
192 input.side,
193 input.alignment,
194 input.gap * density,
195 input.viewportPadding * density,
196 input.matchAnchorWidth
197 )
198 handle = Some(overlayHandle)
199 anchorVisibility = input.anchorVisibility
200 mode = input.mode
201 focus = Some(input.focus)
202 dismiss = input.dismissal
203 transition = PresenceOptions(motion, motion)
204 onCloseRequest = { reason -> inputs.value.onCloseRequest(reason) }
205 onClosed = { reason -> inputs.value.onClosed(reason) }
206 }
207 return element({ mount -> PopoverLayout(localAnchor, scope = mount) }, [trigger, popup])
208 }
211public fun popover(
212 open: Binding<Boolean>,
213 content: View,
214 configure: PopoverBuilder.() -> Unit = {}
215): View = makePopover(open, content, configure)