scoop:scoop.ui.component:0.1.0
src/combobox/row.scoop
Selected production source · src/combobox/row.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.ui.style.Theme8
import scoop.window.accessibility.*9
import scoop.ui.component.row as comboRow10
import scoop.ui.component.label as comboLabel12
internal class ComboboxRows<T: Equals<T>>(13
val source: ChoiceSource<T>,14
val input: ComboboxProps<T>,15
val theme: Theme,16
val active: Binding<ViewKey?>,17
val highlight: (ViewKey) -> Unit,18
val select: (ViewKey, Boolean) -> SemanticActionResult19
): ItemSource {20
public override val count: Long get() = source.count21
public override fun keyAt(index: Long): ViewKey = source.keyAt(index)22
public override fun indexOfKey(key: ViewKey): Long? = source.indexOfKey(key)23
public override fun build(index: Long): View =24
component(25
ComboRowProps(source.entryAt(index), input, theme, active, highlight, select),26
{ current, mount -> ComboRow(current, scope = mount) }27
)28
}30
private class ComboRowProps<T: Equals<T>>(31
val entry: ChoiceEntry<T>,32
val input: ComboboxProps<T>,33
val theme: Theme,34
val active: Binding<ViewKey?>,35
val highlight: (ViewKey) -> Unit,36
val select: (ViewKey, Boolean) -> SemanticActionResult37
)39
private class ComboRow<T: Equals<T>>(40
private val props: Props<ComboRowProps<T>>,41
scope: ComponentScope42
): Component(scope) {43
public override fun render(): View {44
val row = props.value45
val theme = row.theme46
val d = theme.tokens.typography.body.fontSize / 14.047
val fonts = row.input.input.fonts48
val ink = theme.tokens.palette49
val text =50
theme.tokens.typography.body.copy {51
fontSize = 14.0 * d;52
lineHeight = Some(20.0 / 14.0);53
weight = 400;54
color = ink.popover.foreground55
}56
when (row.entry) {57
case Label(_, val content) -> return panelFrame(58
panelCaption(59
content,60
text.copy {61
fontSize = 12.0 * d;62
lineHeight = Some(16.0 / 12.0);63
color = ink.muted.foreground64
},65
fonts66
),67
Insets(8.0 * d, 6.0 * d, 8.0 * d, 6.0 * d)68
)69
case Separator(_) -> return panelFrame(separator(), Insets(0.0, 4.0 * d, 0.0, 4.0 * d))70
else -> {}71
}72
val item = choiceEntryItem(row.entry)!!73
val active = row.active.value == Some(item.choice.key)74
val selected = comboContains(comboValues(row.input.selection), item.choice.value)75
val enabled = row.input.mutable && item.choice.enabled76
val color =77
if (active)78
ink.accent.foreground79
else80
ink.popover.foreground81
val direction = scope.layoutDirection.textDirection()82
val body =83
comboRow {84
when (item.icon) {85
case Some(val icon) -> this.item(panelSlot(icon, 0L)) { shrink = 0.0 };86
case None -> {}87
}88
this.item(89
panelSlot(90
item.content ?:91
comboLabel(92
styledText {93
style = text.copy { this.color = color };94
this.text(item.choice.label)95
}96
) {97
this.fonts = fonts98
settings =99
Some(100
paragraphStyle {101
this.direction = direction;102
maxLines = Some(1L);103
ellipsis = "…"104
}105
)106
},107
1L108
)109
) { grow = 1.0 }110
this.item(111
panelSlot(112
element(113
MenuCheckProps(selected, 16.0 * d, color),114
{ current, mount -> MenuCheck(current, scope = mount) }115
),116
2L117
)118
) { shrink = 0.0 }119
spacing = 8.0 * d;120
alignment = CrossAlignment.Center121
}122
return element(123
CandidateRowInkProps(124
item.choice.key,125
item.choice.label,126
enabled,127
row.highlight,128
row.select,129
if (row.input.multiple)130
SemanticRole.Checkbox131
else132
SemanticRole.RadioButton,133
Some(134
if (selected)135
SemanticChecked.On136
else137
SemanticChecked.Off138
)139
),140
{ current, mount -> CandidateRowInk(current, scope = mount) },141
[142
panelFrame(143
body,144
LogicalInsets(6.0 * d, 4.0 * d, 8.0 * d, 4.0 * d).resolve(145
scope.layoutDirection146
),147
styleOverride {148
if (active && enabled)149
background(Some(Paint(ink.accent.background)));150
corners(Corners.all(6.0 * d))151
}.then(item.style)152
)153
]154
)155
}156
}