Scoop API

scoop:scoop.ui.component:0.1.0

src/command/component.scoop

Selected production source · src/command/component.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.FocusOrigin
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.accessibility.*
9public class Command public constructor(
10 private val props: Props<CommandProps>,
11 scope: ComponentScope
12): Component(scope) {
13 private val themes = ThemeAccess(scope)
14 private val localActive = state<ViewKey?>(None)
15 private val ownFocus = FocusHandle()
16 private val ownScroll = ScrollState()
17 private val ownList = VirtualListHandle()
18 private var cached: CommandProps? = None
19 private var cachedQuery = ""
20 private var result: CommandSource? = None
21 private var rowSource: CommandRows? = None
22 private val active =
23 binding(
24 { (props.value.active ?: localActive.binding()).value },
25 { key -> (props.value.active ?: localActive.binding()).value = key }
26 )
27 public override fun onMount() {
28 themes.mount(props.value.input.theme)
29 }
30 private fun results(): CommandSource {
31 val input = props.value
32 val query = input.query.committedText
33 when (cached) {
34 case Some(val previous) -> if (previous === input && query == cachedQuery)
35 return result!!
36 case None -> {}
37 }
38 val next = commandResults(input.source, query, input.filter)
39 cached = Some(input);
40 cachedQuery = query;
41 result = Some(next)
42 return next
43 }
44 private fun highlight(key: ViewKey) {
45 val input = props.value
46 if (!input.input.enabled || input.query.isComposing)
47 return
48 val source = results()
49 val index = source.indexOfKey(key) ?: return
50 if (source.enabledAt(index))
51 active.value = Some(key)
52 }
53 private fun select(key: ViewKey, requestFocus: Boolean = true): SemanticActionResult {
54 val input = props.value
55 if (!input.input.enabled || input.query.isComposing)
56 return SemanticActionResult.Rejected
57 val source = results()
58 val index = source.indexOfKey(key) ?: return SemanticActionResult.Unavailable
59 val item = commandItem(source.entryAt(index)) ?: return SemanticActionResult.Unsupported
60 if (!item.enabled)
61 return SemanticActionResult.Rejected
62 val focus = input.input.focus.handle ?: ownFocus
63 if (requestFocus && !focus.hasFocus)
64 focus.requestFocus(FocusOrigin.Pointer)
65 item.onSelect()
66 props.value.onSelect(key)
67 return SemanticActionResult.Completed
68 }
69 private fun rows(source: CommandSource, input: CommandProps, theme: Theme): CommandRows {
70 when (rowSource) {
71 case Some(val previous) -> if (
72 previous.source === source && previous.input === input && previous.theme === theme
73 )
74 return previous
75 case None -> {}
76 }
77 val next =
78 CommandRows(
79 source,
80 input,
81 theme,
82 active,
83 { key -> highlight(key) },
84 { key, focus -> select(key, focus) }
85 )
86 rowSource = Some(next)
87 return next
88 }
89 public override fun render(): View {
90 active.value
91 val input = props.value
92 val theme = themes.read(input.input.theme)
93 val density = theme.tokens.typography.body.fontSize / 14.0
94 val source = results()
95 val focus = input.input.focus.handle ?: ownFocus
96 val list = input.listHandle ?: ownList
97 val scrollState = input.scroll ?: ownScroll
98 val children = ArrayList<View>()
99 children.add(
100 commandInput(
101 input,
102 focus,
103 theme,
104 list.semanticId,
105 when (val key = active.value) {
106 case Some(val value) -> if (source.indexOfKey(value) != None)
107 key
108 else
109 None
110 case None -> None
111 }
112 )
113 )
114 var height = 0.0
115 if (source.itemCount == 0L) {
116 children.add(
117 panelFrame(
118 panelCaption(
119 input.empty,
120 theme.tokens.typography.body.copy {
121 fontSize = 14.0 * density;
122 color = theme.tokens.palette.muted.foreground
123 },
124 input.input.fonts,
125 true
126 ),
127 Insets(8.0 * density, 24.0 * density, 8.0 * density, 24.0 * density)
128 )
129 )
130 height = input.maxHeight * density
131 } else {
132 val rows = rows(source, input, theme)
133 children.add(
134 virtualList(rows, ItemExtent.Variable(32.0 * density)) {
135 itemSemantics = Some(ItemSemantics(rows, { index -> rows.describe(index) }))
136 state = Some(scrollState)
137 handle = Some(list)
138 this.theme = input.input.theme
139 this.enabled = input.input.enabled
140 this.focus = FocusOptions(tabStop = false)
141 scrollbars = ScrollbarVisibility.Hidden
142 lineStep = 32.0 * density
143 chain = ScrollChain.Contain
144 overscan = 32.0 * density
145 }
146 )
147 height = upperBound(input.maxHeight * density, source.count.toDouble() * 32.0 * density)
148 }
149 val body =
150 element(
151 CommandLayoutProps(input.width * density, height, source.itemCount == 0L),
152 { current, mount -> CommandLayout(current, scope = mount) },
153 children.toArray()
154 )
155 return element(
156 CandidateNavigationProps(
157 { results() },
158 input.query,
159 active,
160 focus,
161 input.input.enabled,
162 input.loop,
163 input.autoHighlight,
164 { key -> (props.value.listHandle ?: ownList).scrollToItem(key) },
165 { key -> select(key) }
166 ),
167 { current, mount -> CandidateNavigation(current, scope = mount) },
168 [
169 panelFrame(
170 body,
171 Insets.all(4.0 * density),
172 styleOverride {
173 background(Some(Paint(theme.tokens.palette.popover.background)))
174 corners(Corners.all(12.0 * density))
175 }.then(input.panelStyle),
176 Some(input.width * density)
177 )
178 ]
179 )
180 }
183private class CommandLayoutProps(val width: Double, val listHeight: Double, val empty: Boolean)
185private class CommandLayout(
186 private val props: Props<CommandLayoutProps>,
187 scope: ComponentScope
188): Element<Unit>(scope) {
189 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
190 Unit
191 > {
192 val width = constraints.constrain(Size(props.value.width, 0.0)).width
193 val head =
194 context.measureChild(
195 0L,
196 Constraints(width, Some(width), maxHeight = constraints.maxHeight)
197 )
198 val available =
199 when (constraints.maxHeight) {
200 case Some(val height) -> lowerBound(0.0, height - head.size.height)
201 case None -> props.value.listHeight
202 }
203 val height = upperBound(props.value.listHeight, available)
204 val list =
205 context.measureChild(
206 1L,
207 if (props.value.empty)
208 Constraints(width, Some(width), maxHeight = Some(height))
209 else
210 Constraints.tight(Size(width, height))
211 )
212 context.placeChild(0L, Point(0.0, 0.0))
213 context.placeChild(1L, Point(0.0, head.size.height))
214 return LayoutResult(
215 LayoutMetrics(constraints.constrain(Size(width, head.size.height + list.size.height))),
216 Unit
217 )
218 }
219 public override fun scene(context: SceneContext, measured: Unit) {
220 context.children()
221 }