scoop:scoop.ui.component:0.1.0
src/command/row.scoop
Selected production source · src/command/row.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.surface.*6
import scoop.ui.*7
import scoop.ui.style.*8
import scoop.ui.style.Theme9
import scoop.window.*10
import scoop.window.accessibility.*11
import scoop.ui.component.row as commandRow12
import scoop.ui.component.label as commandLabel13
import scoop.ui.direction as commandDirection15
internal class CommandRows(16
val source: CommandSource,17
val input: CommandProps,18
val theme: Theme,19
private val active: Binding<ViewKey?>,20
private val highlight: (ViewKey) -> Unit,21
private val select: (ViewKey, Boolean) -> SemanticActionResult22
): ItemSource {23
public override val count: Long get() = source.count24
public override fun keyAt(index: Long): ViewKey = source.keyAt(index)25
public override fun indexOfKey(key: ViewKey): Long? = source.indexOfKey(key)26
fun describe(index: Long): SemanticItemProperties? =27
Some(28
when (val entry = source.entryAt(index)) {29
case Item(val item) -> SemanticItemProperties(30
name = Some(item.label),31
enabled = input.input.enabled && item.enabled32
)33
case Label(_, val content) -> SemanticItemProperties(34
name = candidateCaption(content)35
)36
case Separator(_) -> SemanticItemProperties(name = Some(""))37
}38
)39
public override fun build(index: Long): View =40
component(41
CommandRowProps(source.entryAt(index), input, theme, active, highlight, select),42
{ current, mount -> CommandRow(current, scope = mount) }43
)44
}46
internal class CommandRowProps(47
val entry: CommandEntry,48
val input: CommandProps,49
val theme: Theme,50
val active: Binding<ViewKey?>,51
val highlight: (ViewKey) -> Unit,52
val select: (ViewKey, Boolean) -> SemanticActionResult53
)55
private class CommandRow(56
private val props: Props<CommandRowProps>,57
scope: ComponentScope58
): Component(scope) {59
public override fun render(): View {60
val input = props.value61
val theme = input.theme62
val density = theme.tokens.typography.body.fontSize / 14.063
val text =64
theme.tokens.typography.body.copy {65
fontSize = 14.0 * density;66
lineHeight = Some(20.0 / 14.0);67
weight = 40068
color = theme.tokens.palette.popover.foreground69
}70
val fonts = input.input.input.fonts71
when (input.entry) {72
case Label(_, val content) -> return panelFrame(73
panelCaption(74
content,75
text.copy {76
fontSize = 12.0 * density;77
lineHeight = Some(16.0 / 12.0);78
weight = 50079
color = theme.tokens.palette.muted.foreground80
},81
fonts82
),83
Insets(8.0 * density, 6.0 * density, 8.0 * density, 6.0 * density)84
)85
case Separator(_) -> return panelFrame(86
separator { thickness = 1.0 },87
Insets(0.0, 4.0 * density, 0.0, 4.0 * density)88
)89
else -> {}90
}91
val item = commandItem(input.entry)!!92
val active = input.active.value == Some(item.key)93
val enabled = input.input.input.enabled && item.enabled94
val window = Some(scope.nativeWindow)95
val textDirection = scope.layoutDirection.textDirection()96
val body =97
commandRow {98
when (item.icon) {99
case Some(val icon) -> this.item(panelSlot(icon, 0L)) { shrink = 0.0 };100
case None -> {}101
}102
this.item(103
panelSlot(104
item.content ?:105
commandLabel(106
styledText {107
style = text;108
this.text(item.label)109
}110
) {111
this.fonts = fonts112
settings =113
Some(114
paragraphStyle {115
direction = textDirection116
maxLines = Some(1L);117
ellipsis = "…"118
}119
)120
},121
1L122
)123
) { grow = 1.0 }124
when (item.shortcut) {125
case Some(val chord) -> when (window) {126
case Some(val window) -> this.item(127
panelSlot(128
commandDirection(129
commandLabel(130
styledText {131
style =132
text.copy {133
fontSize = 12.0 * density;134
if (!active)135
color =136
theme.tokens.palette.muted.foreground137
}138
this.text(shortcutCaption(window.shortcutLabel(chord)))139
}140
) {141
this.fonts = fonts;142
settings = Some(paragraphStyle { maxLines = Some(1L) })143
},144
LayoutDirection.Ltr145
),146
2L147
)148
) { shrink = 0.0 }149
case None -> {}150
}151
case None -> this.item(152
panelSlot(153
element(154
MenuCheckProps(active, 16.0 * density, text.color),155
{ current, mount -> MenuCheck(current, scope = mount) }156
),157
2L158
)159
) { shrink = 0.0 }160
}161
spacing = 8.0 * density162
alignment = CrossAlignment.Center163
}164
val decorated =165
panelFrame(166
body,167
Insets(8.0 * density, 6.0 * density, 8.0 * density, 6.0 * density),168
styleOverride {169
if (active && enabled)170
background(Some(Paint(theme.tokens.palette.muted.background)))171
corners(Corners.all(4.0 * density))172
}.then(item.style)173
)174
return element(175
CandidateRowInkProps(item.key, item.label, enabled, input.highlight, input.select),176
{ current, mount -> CandidateRowInk(current, scope = mount) },177
[decorated]178
)179
}180
}