Scoop API

scoop:scoop.ui.component:0.1.0

src/combobox/row.scoop

Selected production source · src/combobox/row.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.ui.style.Theme
8import scoop.window.accessibility.*
9import scoop.ui.component.row as comboRow
10import scoop.ui.component.label as comboLabel
12internal 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) -> SemanticActionResult
19): ItemSource {
20 public override val count: Long get() = source.count
21 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 )
30private 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) -> SemanticActionResult
39private class ComboRow<T: Equals<T>>(
40 private val props: Props<ComboRowProps<T>>,
41 scope: ComponentScope
42): Component(scope) {
43 public override fun render(): View {
44 val row = props.value
45 val theme = row.theme
46 val d = theme.tokens.typography.body.fontSize / 14.0
47 val fonts = row.input.input.fonts
48 val ink = theme.tokens.palette
49 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.foreground
55 }
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.foreground
64 },
65 fonts
66 ),
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.enabled
76 val color =
77 if (active)
78 ink.accent.foreground
79 else
80 ink.popover.foreground
81 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 = fonts
98 settings =
99 Some(
100 paragraphStyle {
101 this.direction = direction;
102 maxLines = Some(1L);
103 ellipsis = "…"
104 }
105 )
106 },
107 1L
108 )
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 2L
117 )
118 ) { shrink = 0.0 }
119 spacing = 8.0 * d;
120 alignment = CrossAlignment.Center
121 }
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.Checkbox
131 else
132 SemanticRole.RadioButton,
133 Some(
134 if (selected)
135 SemanticChecked.On
136 else
137 SemanticChecked.Off
138 )
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.layoutDirection
146 ),
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 }