scoop:scoop.ui.component:0.1.0
src/navigation_menu/panel.scoop
Selected production source · src/navigation_menu/panel.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.time.Duration6
import scoop.ui.*7
import scoop.ui.style.*8
import scoop.window.accessibility.*9
import scoop.ui.component.row as navMenuRow11
internal class NavPanelInput(12
val root: NavMenuControl,13
val key: ViewKey,14
val panel: NavigationMenuPanelProps,15
val focus: FocusHandle16
)18
internal 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? = None26
var wasOpen = false27
private var hoverSession = false28
val input: NavigationMenuProps get() = props.value.root.props.value29
val enabled: Boolean get() = input.enabled && props.value.panel.trigger.enabled30
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
else38
if (opened)39
layer.requestClose()40
}41
)42
val portalOpen =43
binding(44
{ opened },45
{46
value -> if (value)47
show(false)48
else49
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
return59
if (!focus)60
hoverSession = true61
if (!opened)62
keyboard.value = focus63
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.Rejected70
if (opened == value)71
return SemanticActionResult.Completed72
if (value)73
show(false)74
else75
layer.requestClose()76
return if (opened == value)77
SemanticActionResult.Completed78
else79
SemanticActionResult.Rejected80
}81
fun toggle() {82
if (!enabled)83
return84
if (opened)85
layer.requestClose()86
else87
show(props.value.focus.isFocusVisible)88
}89
fun update() {90
if (wasOpen && !opened) {91
keyboard.value = false92
hoverSession = false93
link.dismiss()94
}95
wasOpen = opened96
}97
fun hoverOptions(): HoverOptions {98
val base = input.hover99
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.longPress107
)108
}109
}111
internal class NavMenuPanel(112
private val props: Props<NavPanelInput>,113
scope: ComponentScope114
): Component(scope) {115
private val control = NavPanelControl(props)116
public override fun render(): View {117
val control = this.control118
val p = props.value119
val root = control.input120
val theme = p.root.themes.read(root.theme)121
val d = theme.tokens.typography.body.fontSize / 14.0122
val opened = control.opened123
val input = p.panel124
val spec = input.trigger125
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.fonts142
else143
root.fonts144
)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.Vertical164
),165
{ current, mount -> NavChevron(current, scope = mount) }166
)167
) { shrink = 0.0 }168
spacing = 4.0 * d;169
alignment = CrossAlignment.Center170
}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.palette189
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.reference209
),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.anchor235
else236
control.anchor,237
if (root.orientation == FlexAxis.Horizontal)238
OverlaySide.Bottom239
else240
OverlaySide.End,241
root.alignment,242
root.gap * d,243
root.viewportPadding * d244
)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
None260
else261
exit262
)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
false278
),279
{ current, mount -> HoverPopupLayout(current, scope = mount) },280
[trigger, popup]281
)282
]283
)284
}285
}