scoop:scoop.ui.component:0.1.0
src/menus/menubar.scoop
Selected production source · src/menus/menubar.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.FocusOrigin5
import scoop.ui.*6
import scoop.ui.style.*8
public class MenubarEntry public constructor(9
public val key: ViewKey,10
public val trigger: PopupTriggerSpec,11
public val content: MenuContent12
)14
public class MenubarProps public constructor(15
public val open: Binding<ViewKey?>,16
public val entries: Array<MenubarEntry>,17
public val options: MenuOptions = MenuOptions(),18
public val style: StyleOverride = styleOverride(),19
public val onOpenChange: (ViewKey?) -> Unit = {}20
) {21
init {22
for (index in 0L ..< entries.size) {23
requireComponent(24
entries[index].key != ViewKey.Position,25
"menubar requires an explicit key"26
)27
for (previous in 0L ..< index)28
requireComponent(29
entries[previous].key != entries[index].key,30
"duplicate menubar key"31
)32
}33
}34
}36
public class Menubar public constructor(37
private val props: Props<MenubarProps>,38
scope: ComponentScope39
): Component(scope) {40
private val themes = ThemeAccess(scope)41
private val storage = ChoiceHandles()42
private var handles: Array<FocusHandle> = []43
public override fun onMount() {44
themes.mount(props.value.options.theme)45
}46
private fun setOpen(key: ViewKey?) {47
val binding = props.value.open48
val previous = binding.value49
if (previous == key)50
return51
binding.value = key52
val accepted = binding.value53
if (accepted != previous)54
props.value.onOpenChange(accepted)55
}56
private fun select(key: ViewKey, origin: FocusOrigin) {57
val input = props.value58
if (!input.options.enabled)59
return60
for (index in 0L ..< input.entries.size)61
if (input.entries[index].key == key && input.entries[index].trigger.enabled) {62
setOpen(Some(key))63
if (input.open.value == Some(key))64
handles[index].requestFocus(origin)65
return66
}67
}68
private fun navigate(handle: FocusHandle) {69
if (props.value.open.value == None)70
return71
for (index in 0L ..< handles.size)72
if (handles[index] === handle) {73
setOpen(Some(props.value.entries[index].key));74
return75
}76
}77
private fun horizontal(key: ViewKey, reverse: Boolean) {78
val input = props.value79
var current = -1L80
for (index in 0L ..< input.entries.size)81
if (input.entries[index].key == key)82
current = index83
if (current < 0L)84
return85
var index = current86
var visited = 0L87
while (visited < input.entries.size) {88
index += if (reverse)89
-1L90
else91
1L92
if ((index < 0L || index >= input.entries.size) && !input.options.loop)93
return94
if (index < 0L)95
index = input.entries.size - 1L96
if (index >= input.entries.size)97
index = 0L98
if (input.entries[index].trigger.enabled) {99
select(input.entries[index].key, FocusOrigin.Keyboard);100
return101
}102
visited += 1L103
}104
}105
public override fun render(): View {106
val input = props.value107
val theme = themes.read(input.options.theme)108
val density = theme.tokens.typography.body.fontSize / 14.0109
val keys = Array<ViewKey>(input.entries.size) { index -> input.entries[index].key }110
handles =111
storage.resolve(112
keys,113
Array<FocusOptions>(keys.size) { index -> input.entries[index].trigger.focus }114
)115
val children =116
Array<View>(keys.size) {117
index ->118
val entry = input.entries[index]119
val key = entry.key120
val trigger = entry.trigger121
val opened = input.open.value == Some(key) && trigger.enabled122
val button =123
PopupTriggerSpec(124
trigger.content,125
trigger.enabled,126
ButtonVariant.Ghost,127
ButtonSize.Sm,128
menubarTriggerStyle(trigger.style, theme, opened),129
choiceFocus(trigger.focus, handles[index]),130
trigger.fonts,131
trigger.semantics132
)133
component(134
MenuRootInput(135
Some(136
binding(137
{138
props.value.open.value ==139
Some(key) &&140
menubarEnabled(props.value, key)141
},142
{143
value -> if (value)144
setOpen(Some(key))145
else146
if (props.value.open.value == Some(key))147
setOpen(None)148
}149
)150
),151
entry.content,152
input.options,153
MenuRootTrigger.Button(button, None),154
focus = OverlayFocusOptions(restoreTarget = Some(handles[index])),155
onOpenChange = {},156
horizontal = Some({ reverse -> horizontal(key, reverse) }),157
hovered =158
{159
if (props.value.open.value != None)160
select(key, FocusOrigin.Pointer)161
}162
),163
{ current, mount -> MenubarMenu(current, scope = mount) }164
)165
}166
val visual =167
styleOverride {168
background(Some(Paint(theme.tokens.palette.background.background)))169
border(Some(Border.all(1.0, theme.tokens.palette.border)))170
corners(Corners.all(8.0 * density))171
}.then(input.style)172
return panelFrame(173
choiceLayout(174
children,175
keys,176
handles,177
FlexAxis.Horizontal,178
2.0 * density,179
input.options.loop,180
input.options.enabled,181
styleOverride(),182
navigate = { handle -> navigate(handle) },183
fitCross = false184
),185
Insets.all(4.0 * density),186
visual187
)188
}189
}191
internal fun menubarTriggerStyle(local: ButtonStyle, theme: Theme, opened: Boolean): ButtonStyle {192
val density = theme.tokens.typography.body.fontSize / 14.0193
val active = styleOverride { background(Some(Paint(theme.tokens.palette.muted.background))) }194
return local.copy {195
surface {196
if (suggestedSize == None)197
suggestedSize = Some(Size(0.0, 24.0 * density))198
if (minimumSize == None)199
minimumSize = Some(Size(0.0, 0.0))200
if (padding == None)201
padding = Some(Insets(6.0 * density, 2.0 * density, 6.0 * density, 2.0 * density))202
if (focus == None)203
focus = Some(OutlineStyle(Color.Transparent, 0.0, 0.0))204
style =205
styleOverride {206
if (opened)207
background(Some(Paint(theme.tokens.palette.muted.background)))208
border(None)209
corners(Corners.all(4.0 * density))210
boxShadows([])211
}.then(local.surface.style)212
}213
states {214
hovered = active.then(local.surface.style).then(local.states.hovered)215
pressed = active.then(local.surface.style).then(local.states.pressed)216
}217
}218
}220
private fun menubarEnabled(input: MenubarProps, key: ViewKey): Boolean {221
for (entry in input.entries)222
if (entry.key == key)223
return entry.trigger.enabled && input.options.enabled224
return false225
}227
private class MenubarMenu(228
private val props: Props<MenuRootInput>,229
scope: ComponentScope230
): Component(scope) {231
private val root = MenuRoot(scope, { props.value })232
public override fun onMount() {233
root.mount()234
}235
public override fun render(): View = root.render()236
}238
public class MenubarBuilder internal constructor(): MenuOptionsBuilder() {239
private val entries = ArrayList<MenubarEntry>()240
public var barStyle: StyleOverride = styleOverride()241
public var onOpenChange: (ViewKey?) -> Unit = {}242
public fun add(entry: MenubarEntry) {243
entries.add(entry)244
}245
public fun menu(246
key: ViewKey,247
text: String,248
content: MenuContent,249
configure: PopupTriggerBuilder.() -> Unit = {}250
) {251
val builder = PopupTriggerBuilder()252
builder.configure()253
add(254
MenubarEntry(255
key,256
builder.snapshot(ButtonContent.Label(LabelProps(LabelText.Plain(text, None)))),257
content258
)259
)260
}261
public fun menu(262
key: ViewKey,263
trigger: View,264
content: MenuContent,265
configure: PopupTriggerBuilder.() -> Unit = {}266
) {267
val builder = PopupTriggerBuilder()268
builder.configure()269
add(MenubarEntry(key, builder.snapshot(ButtonContent.Custom(trigger)), content))270
}271
internal fun snapshot(open: Binding<ViewKey?>): MenubarProps =272
MenubarProps(open, entries.toArray(), options(), barStyle, onOpenChange)273
}275
public fun menubar(open: Binding<ViewKey?>, configure: MenubarBuilder.() -> Unit = {}): View =276
makeMenubar(open, configure)278
internal fun makeMenubar(open: Binding<ViewKey?>, configure: MenubarBuilder.() -> Unit): View {279
val builder = MenubarBuilder()280
builder.configure()281
return component(282
builder.snapshot(open),283
{ current, mount -> Menubar(current, scope = mount) },284
builder.key285
)286
}