scoop:scoop.ui.component:0.1.0
src/menus/row.scoop
Selected production source · src/menus/row.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.svg.*5
import scoop.icons.lucide.check as checkIcon6
import scoop.graphics.text.*7
import scoop.surface.*8
import scoop.ui.*9
import scoop.ui.style.*10
import scoop.ui.style.Theme11
import scoop.window.*12
import scoop.window.accessibility.*13
import scoop.ui.component.row as menuRow14
import scoop.ui.component.label as menuLabel15
import scoop.ui.component.frame as menuFrame16
import scoop.ui.direction as menuDirection18
internal class MenuRowProps(19
val entry: MenuEntry,20
val options: MenuOptions,21
val theme: Theme,22
val focus: FocusHandle?,23
val open: Binding<Boolean>,24
val chain: MenuChain,25
val selection: Boolean = false26
)28
internal class MenuRow(private val props: Props<MenuRowProps>, scope: ComponentScope): Component(29
scope30
) {31
private val anchor = LayoutAnchor()32
private val link = HoverPopupLink()33
private val submenu = OverlayHandle()34
private val submenuSemantics = SemanticsReference()35
private val candidates = MenuFocus()36
private val keyboard = state(false)37
private val highlighted = state(false)38
private val open =39
binding({ props.value.open.value }, { value -> props.value.open.value = value })40
private val portalOpen =41
binding(42
{ open.value },43
{44
value -> if (value)45
open.value = true46
else47
link.dismiss { open.value = false }48
}49
)50
private fun enter() {51
if (!(menuItem(props.value.entry)?.enabled ?: false) || !props.value.options.enabled)52
return53
keyboard.value = true54
open.value = true55
candidates.edge(false)?.requestFocus(FocusOrigin.Keyboard)56
}57
private fun activate() {58
val input = props.value59
if (!input.options.enabled)60
return61
when (input.entry) {62
case Submenu(_, _) -> enter()63
else -> {64
activateSemantically()65
}66
}67
}68
private fun setExpanded(value: Boolean): SemanticActionResult {69
val input = props.value70
if (!input.options.enabled || !(menuItem(input.entry)?.enabled ?: false))71
return SemanticActionResult.Rejected72
when (input.entry) {73
case Submenu(_, _) -> {74
portalOpen.value = value75
return if (open.value == value)76
SemanticActionResult.Completed77
else78
SemanticActionResult.Rejected79
}80
else -> return SemanticActionResult.Unsupported81
}82
}83
private fun activateSemantically(): SemanticActionResult {84
val input = props.value85
if (!input.options.enabled)86
return SemanticActionResult.Rejected87
when (input.entry) {88
case Submenu(_, _) -> return setExpanded(true)89
else -> {90
val outcome = activateMenu(input.entry)91
if (outcome.close)92
input.chain.close()93
return outcome.result94
}95
}96
}97
public override fun render(): View {98
val input = props.value99
val theme = input.theme100
val density = theme.tokens.typography.body.fontSize / 14.0101
when (input.entry) {102
case Label(_, val content, val inset) -> return menuFrame(103
panelCaption(104
content,105
theme.tokens.typography.body.copy {106
fontSize = 12.0 * density107
lineHeight = Some(16.0 / 12.0)108
weight = 500109
color =110
componentForeground(111
theme,112
theme.tokens.palette.muted.foreground,113
theme.tokens.palette.popover.background114
)115
},116
input.options.fonts117
)118
) {119
logicalPadding =120
Some(121
LogicalInsets(122
(123
if (inset)124
28.0125
else126
6.0127
) *128
density,129
4.0 * density,130
6.0 * density,131
4.0 * density132
)133
)134
alignment = Alignment(AxisAlignment.Start, AxisAlignment.Start)135
}136
case Separator(_) -> return panelFrame(137
separator {138
thickness = 1.0139
color =140
Some(141
componentForeground(142
theme,143
theme.tokens.palette.border,144
theme.tokens.palette.popover.background,145
3.0146
)147
)148
},149
Insets(0.0, 4.0 * density, 0.0, 4.0 * density)150
)151
else -> {}152
}153
val item = menuItem(input.entry)!!154
val enabled = input.options.enabled && item.enabled155
val active = enabled && (highlighted.value || input.open.value)156
val colors = menuRowColors(theme, item.destructive, active, enabled)157
val color = colors.foreground158
val base =159
theme.tokens.typography.body.copy {160
fontSize = 14.0 * density;161
lineHeight = Some(20.0 / 14.0);162
weight = 400;163
this.color = color164
}165
val text = item.style.text?.resolve(base) ?: base166
val isSub =167
when (input.entry) {168
case Submenu(_, _) -> true;169
else -> false170
}171
val checked = menuChecked(input.entry)172
val indicator =173
when (input.entry) {174
case Checkbox(_, _, _, _) -> true175
case Radio(_, _, _, _) -> true176
else -> false177
}178
val native = Some(scope.nativeWindow)179
val child =180
menuRow {181
when (item.icon) {182
case Some(val icon) -> this.item(panelSlot(icon, 0L)) { shrink = 0.0 }183
case None -> {}184
}185
this.item(186
panelSlot(187
item.content ?:188
menuLabel(189
styledText {190
style = text;191
this.text(item.label)192
}193
) {194
fonts = input.options.fonts195
settings =196
Some(197
paragraphStyle {198
maxLines = Some(1L);199
ellipsis = "…"200
}201
)202
},203
1L204
)205
) { grow = 1.0 }206
when (item.shortcut) {207
case Some(val chord) -> when (native) {208
case Some(val value) -> this.item(209
panelSlot(210
menuDirection(211
menuLabel(212
styledText {213
style =214
text.copy {215
fontSize = 12.0 * density216
this.color =217
if (active || !enabled)218
text.color219
else220
componentForeground(221
theme,222
theme.tokens.palette.muted.foreground,223
if (colors.background.alpha == 0.0)224
theme.tokens.palette.popover.background225
else226
colors.background227
)228
}229
this.text(shortcutCaption(value.shortcutLabel(chord)))230
}231
) {232
fonts = input.options.fonts233
settings = Some(paragraphStyle { maxLines = Some(1L) })234
},235
LayoutDirection.Ltr236
),237
2L238
)239
) { shrink = 0.0 }240
case None -> {}241
}242
case None -> {}243
}244
if (isSub)245
this.item(246
panelSlot(247
navigationIcon(NavigationGlyph.End, 16.0 * density, text.color),248
3L249
)250
) { shrink = 0.0 }251
else252
if (indicator)253
this.item(254
panelSlot(255
element(256
MenuCheckProps(checked, 16.0 * density, text.color),257
{ current, mount -> MenuCheck(current, scope = mount) }258
),259
3L260
)261
) { shrink = 0.0 }262
spacing =263
(264
if (input.selection)265
8.0266
else267
6.0268
) *269
density270
if (input.selection)271
logicalPadding = Some(LogicalInsets(end = 2.0 * density))272
alignment = CrossAlignment.Center273
if (item.inset && item.style.surface.padding == None)274
logicalPadding = Some(LogicalInsets(start = 22.0 * density))275
}276
val semantics =277
item.semantics.copy {278
if (name == None && relations.labelledBy.size == 0L)279
name = Some(item.label)280
if (isSub)281
relations = semanticControls(relations, this @MenuRow.submenuSemantics.id)282
}283
val action =284
semanticButton(285
ButtonProps(286
ButtonContent.Custom(child),287
{ activate() },288
enabled = enabled,289
focus = choiceFocus(item.focus, input.focus!!),290
variant = ButtonVariant.Ghost,291
style = menuRowStyle(item.style, theme, item.destructive, active, enabled),292
theme = input.options.theme,293
semantics = semantics294
),295
SemanticRole.MenuItem,296
semantics.properties(297
enabled = enabled,298
checked =299
if (indicator)300
Some(301
if (checked)302
SemanticChecked.On303
else304
SemanticChecked.Off305
)306
else307
None,308
expanded =309
if (isSub)310
Some(open.value)311
else312
None313
),314
SemanticActions(315
invoke = Some { activateSemantically() },316
toggle =317
if (indicator)318
Some { activateSemantically() }319
else320
None,321
setExpanded =322
if (isSub)323
Some { value -> setExpanded(value) }324
else325
None326
)327
)328
val popup =329
when (input.entry) {330
case Submenu(_, val content) -> component(331
MenuPopupProps(332
portalOpen,333
content,334
input.options,335
OverlayPlacement.Anchored(336
anchor,337
OverlaySide.End,338
OverlayAlignment.Start,339
0.0,340
8.0341
),342
submenu,343
candidates,344
input.chain,345
takeFocus = keyboard.value,346
link = Some(link),347
onClosed =348
{349
_ ->350
keyboard.value = false351
link.dismiss()352
},353
semantics =354
Some(355
ComponentSemantics(356
name = Some(item.label),357
reference = Some(submenuSemantics)358
)359
)360
),361
{ current, mount -> MenuPopup(current, scope = mount) }362
)363
else -> empty()364
}365
val hover = input.options.submenuHover366
return element(367
MenuRowLayoutProps(368
anchor,369
link,370
open,371
input.focus!!,372
HoverOptions(373
hover.openDelay,374
hover.closeDelay,375
hover.transitDelay,376
hover.corridorPadding,377
(keyboard.value && input.open.value) || hover.focus,378
input.options.enabled && item.enabled && isSub && hover.enabled,379
hover.longPress380
),381
{382
value -> if (highlighted.value != value)383
highlighted.value = value384
},385
{ enter() },386
isSub,387
{ keyboard.value = false }388
),389
{ current, mount -> MenuRowLayout(current, scope = mount) },390
[action, popup]391
)392
}393
}395
internal fun menuRowColors(396
theme: Theme,397
destructive: Boolean,398
active: Boolean,399
enabled: Boolean = true400
): ColorPair {401
val colors = theme.tokens.palette402
if (!enabled)403
return ColorPair(404
Color.Transparent,405
componentForeground(406
theme,407
if (theme.tokens.disabledOpacity < 1.0)408
colors.popover.foreground409
else410
colors.disabledText,411
colors.popover.background,412
4.5413
)414
)415
val fill =416
if (!active)417
Color.Transparent418
else419
if (destructive)420
colors.destructive.background.withAlpha(421
if (theme.tokens.mode == ThemeMode.Dark)422
0.2423
else424
0.1425
)426
else427
colors.accent.background428
return componentSurfaceColors(429
theme,430
ColorPair(431
fill,432
if (destructive)433
colors.destructive.background434
else435
if (active)436
colors.accent.foreground437
else438
colors.popover.foreground439
),440
colors.popover.background441
)442
}444
internal fun menuRowStyle(445
local: ButtonStyle,446
theme: Theme,447
destructive: Boolean,448
active: Boolean,449
enabled: Boolean = true450
): ButtonStyle {451
val density = theme.tokens.typography.body.fontSize / 14.0452
val fill = menuRowColors(theme, destructive, active, enabled).background453
return local.copy {454
surface {455
if (suggestedSize == None)456
suggestedSize = Some(Size(0.0, 28.0 * density))457
if (minimumSize == None)458
minimumSize = Some(Size(0.0, 0.0))459
if (padding == None)460
padding = Some(Insets(6.0 * density, 4.0 * density, 6.0 * density, 4.0 * density))461
if (focus == None)462
focus = Some(OutlineStyle(Color.Transparent, 0.0, 0.0))463
style =464
styleOverride {465
background(Some(Paint(fill)));466
border(None)467
corners(Corners.all(6.0 * density));468
boxShadows([])469
}.then(local.surface.style)470
}471
}472
}474
internal class MenuCheckProps(val checked: Boolean, val size: Double, val color: Color)476
internal class MenuCheck(private val props: Props<MenuCheckProps>, scope: ComponentScope): Element<477
Unit478
>(scope) {479
private var glyph: SvgDocument? = None480
public override fun onMount() {481
glyph = Some(scope.resource { SvgDocument.parse(checkIcon.source) })482
}483
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<484
Unit485
> =486
LayoutResult(487
LayoutMetrics(constraints.constrain(Size(props.value.size, props.value.size))),488
Unit489
)490
public override fun scene(context: SceneContext, measured: Unit) {491
val input = props.value492
val size = context.size493
if (input.checked)494
context.draw {495
paint -> paint.canvas.withState {496
canvas ->497
canvas.scale(size.width / 24.0, size.height / 24.0)498
canvas.drawSvg(499
glyph!!,500
Rect(0.0, 0.0, 24.0, 24.0),501
SvgDrawOptions(currentColor = Some(input.color))502
)503
}504
}505
}506
}