Scoop API

scoop:scoop.ui.component:0.1.0

src/navigation_menu/panel.scoop

Selected production source · src/navigation_menu/panel.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.time.Duration
6import scoop.ui.*
7import scoop.ui.style.*
8import scoop.window.accessibility.*
9import scoop.ui.component.row as navMenuRow
11internal class NavPanelInput(
12 val root: NavMenuControl,
13 val key: ViewKey,
14 val panel: NavigationMenuPanelProps,
15 val focus: FocusHandle
18internal class NavPanelControl(val props: Props<NavPanelInput>) {
19 val anchor = LayoutAnchor()
20 val layer = OverlayHandle()
21 val link = HoverPopupLink()
22 val popupReference = SemanticsReference()
23 val keyboard = state(false)
24 val contentFocused = state(false)
25 var contentFocus: FocusScope? = None
26 var wasOpen = false
27 private var hoverSession = false
28 val input: NavigationMenuProps get() = props.value.root.props.value
29 val enabled: Boolean get() = input.enabled && props.value.panel.trigger.enabled
30 val opened: Boolean get() = enabled && props.value.root.open == Some(props.value.key)
31 val hoverOpen =
32 binding(
33 { opened && hoverSession },
34 {
35 value -> if (value)
36 show(false)
37 else
38 if (opened)
39 layer.requestClose()
40 }
41 )
42 val portalOpen =
43 binding(
44 { opened },
45 {
46 value -> if (value)
47 show(false)
48 else
49 if (props.value.root.open == Some(props.value.key)) {
50 props.value.root.setOpen(None)
51 if (!opened)
52 link.dismiss()
53 }
54 }
55 )
56 fun show(focus: Boolean) {
57 if (!enabled)
58 return
59 if (!focus)
60 hoverSession = true
61 if (!opened)
62 keyboard.value = focus
63 props.value.root.setOpen(Some(props.value.key))
64 if (opened && focus)
65 contentFocus?.requestFocus()
66 }
67 fun expand(value: Boolean): SemanticActionResult {
68 if (!enabled)
69 return SemanticActionResult.Rejected
70 if (opened == value)
71 return SemanticActionResult.Completed
72 if (value)
73 show(false)
74 else
75 layer.requestClose()
76 return if (opened == value)
77 SemanticActionResult.Completed
78 else
79 SemanticActionResult.Rejected
80 }
81 fun toggle() {
82 if (!enabled)
83 return
84 if (opened)
85 layer.requestClose()
86 else
87 show(props.value.focus.isFocusVisible)
88 }
89 fun update() {
90 if (wasOpen && !opened) {
91 keyboard.value = false
92 hoverSession = false
93 link.dismiss()
94 }
95 wasOpen = opened
96 }
97 fun hoverOptions(): HoverOptions {
98 val base = input.hover
99 return HoverOptions(
100 base.openDelay,
101 base.closeDelay,
102 base.transitDelay,
103 base.corridorPadding,
104 base.focus || contentFocused.value,
105 base.enabled && enabled,
106 base.longPress
107 )
108 }
111internal class NavMenuPanel(
112 private val props: Props<NavPanelInput>,
113 scope: ComponentScope
114): Component(scope) {
115 private val control = NavPanelControl(props)
116 public override fun render(): View {
117 val control = this.control
118 val p = props.value
119 val root = control.input
120 val theme = p.root.themes.read(root.theme)
121 val d = theme.tokens.typography.body.fontSize / 14.0
122 val opened = control.opened
123 val input = p.panel
124 val spec = input.trigger
125 val motion =
126 theme.tokens.motion.resolve(
127 root.transition.resolve(Some(AnimationSpec.Tween(Duration.milliseconds(350L))))
128 )
129 val exit =
130 theme.tokens.motion.resolve(
131 root.transition.resolve(Some(AnimationSpec.Tween(Duration.milliseconds(150L))))
132 )
133 val turn =
134 theme.tokens.motion.resolve(
135 root.transition.resolve(Some(AnimationSpec.Tween(Duration.milliseconds(300L))))
136 )
137 val label =
138 navigationLabel(
139 spec.content,
140 if (panelPresent(spec.fonts))
141 spec.fonts
142 else
143 root.fonts
144 )
145 val popupSemantics =
146 input.semantics.copy {
147 reference = Some(input.semantics.reference ?: control.popupReference)
148 }
149 val trigger =
150 popupSemanticButton(
151 ButtonProps(
152 ButtonContent.Resolved {
153 content -> navMenuRow {
154 this.item(actionContent(label, content))
155 this.item(
156 element(
157 NavChevronProps(
158 opened,
159 control.enabled,
160 12.0 * d,
161 content.text.color,
162 turn,
163 root.orientation == FlexAxis.Vertical
164 ),
165 { current, mount -> NavChevron(current, scope = mount) }
166 )
167 ) { shrink = 0.0 }
168 spacing = 4.0 * d;
169 alignment = CrossAlignment.Center
170 }
171 },
172 { control.toggle() },
173 control.enabled,
174 navMenuStyle(theme, spec.style, opened, false, true),
175 root.theme,
176 choiceFocus(spec.focus, p.focus),
177 variant = spec.variant,
178 size = spec.size,
179 semantics =
180 spec.semantics.copy {
181 relations =
182 semanticControls(relations, (popupSemantics.reference?.id ?: None))
183 }
184 ),
185 opened,
186 { next -> control.expand(next) }
187 )
188 val palette = theme.tokens.palette
189 val popup =
190 portal(
191 control.portalOpen,
192 element(
193 PopupMotionProps(control.enabled, 0.0, minimumScale = 0.9),
194 { current, mount -> PopupMotion(current, scope = mount) },
195 [
196 element(
197 control,
198 { current, mount -> NavPanelContent(current, scope = mount) },
199 [
200 panelFrame(
201 semanticContent(
202 input.content,
203 SemanticRole.Group,
204 popupSemantics.properties(enabled = control.enabled),
205 SemanticActions(dismiss = Some { control.expand(false) }),
206 popupSemantics.relations,
207 mergeLabels = false,
208 reference = popupSemantics.reference
209 ),
210 Insets.all(4.0 * d),
211 styleOverride {
212 background(Some(Paint(palette.popover.background)))
213 border(
214 Some(
215 Border.all(
216 1.0,
217 componentPanelBorder(theme, palette.popover)
218 )
219 )
220 )
221 corners(Corners.all(8.0 * d));
222 boxShadows(theme.tokens.effects.floating)
223 }.then(root.popupStyle).then(input.style),
224 Some(input.width * d)
225 )
226 ]
227 )
228 ]
229 )
230 ) {
231 placement =
232 OverlayPlacement.Anchored(
233 if (root.viewport)
234 p.root.anchor
235 else
236 control.anchor,
237 if (root.orientation == FlexAxis.Horizontal)
238 OverlaySide.Bottom
239 else
240 OverlaySide.End,
241 root.alignment,
242 root.gap * d,
243 root.viewportPadding * d
244 )
245 handle = Some(control.layer)
246 focus =
247 Some(
248 OverlayFocusOptions(
249 input.initialFocus,
250 autoFocus = control.keyboard.value,
251 restoreTarget = Some(p.focus)
252 )
253 )
254 dismiss = OverlayDismiss(true, OutsidePress.CloseAndContinue, true)
255 transition =
256 PresenceOptions(
257 motion,
258 if (p.root.open != None && !opened)
259 None
260 else
261 exit
262 )
263 onCloseRequest =
264 { reason -> control.input.onCloseRequest(control.props.value.key, reason) }
265 onClosed = { reason -> control.input.onClosed(control.props.value.key, reason) }
266 }
267 return element(
268 control,
269 { current, mount -> NavPanelEvents(current, scope = mount) },
270 [
271 element(
272 HoverPopupLayoutProps(
273 control.anchor,
274 control.link,
275 control.hoverOpen,
276 control.hoverOptions(),
277 false
278 ),
279 { current, mount -> HoverPopupLayout(current, scope = mount) },
280 [trigger, popup]
281 )
282 ]
283 )
284 }