Scoop API

scoop:scoop.ui.component:0.1.0

src/command/row.scoop

Selected production source · src/command/row.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.surface.*
6import scoop.ui.*
7import scoop.ui.style.*
8import scoop.ui.style.Theme
9import scoop.window.*
10import scoop.window.accessibility.*
11import scoop.ui.component.row as commandRow
12import scoop.ui.component.label as commandLabel
13import scoop.ui.direction as commandDirection
15internal 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) -> SemanticActionResult
22): ItemSource {
23 public override val count: Long get() = source.count
24 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.enabled
32 )
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 )
46internal 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) -> SemanticActionResult
55private class CommandRow(
56 private val props: Props<CommandRowProps>,
57 scope: ComponentScope
58): Component(scope) {
59 public override fun render(): View {
60 val input = props.value
61 val theme = input.theme
62 val density = theme.tokens.typography.body.fontSize / 14.0
63 val text =
64 theme.tokens.typography.body.copy {
65 fontSize = 14.0 * density;
66 lineHeight = Some(20.0 / 14.0);
67 weight = 400
68 color = theme.tokens.palette.popover.foreground
69 }
70 val fonts = input.input.input.fonts
71 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 = 500
79 color = theme.tokens.palette.muted.foreground
80 },
81 fonts
82 ),
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.enabled
94 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 = fonts
112 settings =
113 Some(
114 paragraphStyle {
115 direction = textDirection
116 maxLines = Some(1L);
117 ellipsis = "…"
118 }
119 )
120 },
121 1L
122 )
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.foreground
137 }
138 this.text(shortcutCaption(window.shortcutLabel(chord)))
139 }
140 ) {
141 this.fonts = fonts;
142 settings = Some(paragraphStyle { maxLines = Some(1L) })
143 },
144 LayoutDirection.Ltr
145 ),
146 2L
147 )
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 2L
158 )
159 ) { shrink = 0.0 }
160 }
161 spacing = 8.0 * density
162 alignment = CrossAlignment.Center
163 }
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 }