Scoop API

scoop:scoop.ui.component:0.1.0

src/tree/row.scoop

Selected production source · src/tree/row.scoop

1package scoop.ui.component
3import scoop.collections.HashMap
4import scoop.graphics.*
5import scoop.icons.lucide.file as fileIcon
6import scoop.icons.lucide.folder as folderIcon
7import scoop.surface.*
8import scoop.ui.*
9import scoop.ui.style.*
10import scoop.window.*
11import scoop.ui.component.row as makeTreeRow
12import scoop.ui.component.frame as treeSlot
13import scoop.ui.component.spinner as treeSpinner
14import scoop.ui.component.label as treeLabel
15import scoop.ui.component.checkbox as treeCheckbox
16import scoop.ui.component.icon as treeIcon
18internal class TreeRowInput<T>(val plan: TreePlan<T>, val index: Long) {
19 val row: TreeRow<T> get() = plan.projection.rowAt(index)
22internal class TreeCellTarget(val scope: ComponentScope, val focus: FocusScope)
24internal class TreeRowView<T>(private val props: Props<TreeRowInput<T>>, scope: ComponentScope) :
25 Component(scope),
26 TreeMountedRow {
27 public override val main = FocusHandle()
28 val area = scope.createArea(focusable = true)
29 private val focus = scope.focusTarget(area, FocusOptions(Some(main)))
30 // Keep drag admission ahead of Press, including when drag is enabled after mounting.
31 private val source = scope.dragSource(area, DragSourceOptions(enabled = false)) { _ -> None }
32 private val press = scope.press(area, PressOptions()) { activate() }
33 val cells = HashMap<String, TreeCellTarget>()
34 var cellGroup: FocusScope? = None
35 private var pointer: PointerState? = None
36 private var clicks = 1
37 var inputOrigin = TreeChangeOrigin.Pointer
38 var drag: TreeDragControl<T>? = None
39 public override val focused: Boolean get() = scope.input.activity.hasFocus
40 public override val pending: Boolean
41 get() {
42 if (main.isPending || (cellGroup?.isPending ?: false)) return true
43 for (entry in cells.entries)
44 if (entry.value.focus.isPending) return true
45 return false
46 }
47 public override val focusedColumn: String?
48 get() {
49 for (entry in cells.entries)
50 if (entry.value.scope.input.activity.hasFocus) return Some(entry.key)
51 return None
52 }
53 init {
54 scope.routeChildren(area)
55 area.scope.defaultAction = { context, event -> handle(context, event) }
56 scope
57 .resource {
58 area
59 .scope
60 .addEventHandler { context, event ->
61 if (context.phase ==
62 RoutedPhase.Capture ||
63 context.phase ==
64 RoutedPhase.Target
65 )
66 when (event) {
67 case Keyboard(_) -> inputOrigin = TreeChangeOrigin.Keyboard
69 case Pointer(
70 InputPointerEvent.Button(val value, _, _, val count)
71 ) -> {
72 inputOrigin = TreeChangeOrigin.Pointer
73 pointer = Some(value.state);
74 clicks = count
75 }
77 case Contact(InputContactEvent.Began(val value)) -> {
78 inputOrigin = TreeChangeOrigin.Pointer
79 pointer = Some(value.pointer);
80 clicks = 1
81 }
83 else -> {}
84 }
85 }
86 }
87 scope
88 .resource {
89 scope
90 .input
91 .observeActivity {
92 if (scope.isMounted) {
93 updateTabStop();
94 scope.requestFrame()
95 }
96 }
97 }
98 }
99 public override fun onMount() {
100 props.value.plan.controller.register(props.value.row.key, this)
101 }
102 public override fun onUnmount() {
103 drag?.close()
104 drag = None
105 props.value.plan.controller.unregister(props.value.row.key, this)
106 }
107 fun update() {
108 val input = props.value
109 val enabled = input.plan.input.enabled && input.row.node.enabled
110 area.scope.enabled = enabled
111 press.update(PressOptions(enabled, false), { activate() })
112 if (panelPresent(input.plan.input.drag) && !panelPresent(drag))
113 drag = Some(TreeDragControl(scope, area, source, { props.value }))
114 drag?.update()
115 updateTabStop()
116 }
117 private fun updateTabStop() {
118 focus.options = FocusOptions(
119 Some(main),
120 props.value.plan.input.focus.tabStop && (!focused || main.hasFocus)
121 )
122 }
123 public override fun focusCell(column: String): Boolean = cells[column]?.focus?.requestFocus() ?:
124 false
125 public override fun hasCell(column: String): Boolean = panelPresent(cells[column])
126 public override fun enter(): Boolean = cellGroup?.requestFocus() ?: false
127 private fun handle(context: RoutedEventContext, event: InputEvent) {
128 val input = props.value
129 if (!input.plan.input.enabled || !input.row.node.enabled) return
130 when (event) {
131 case Keyboard(val key) -> {
132 if (key.logicalKey ==
133 LogicalKey.Named(NamedKey.Escape) &&
134 key.state ==
135 ElementState.Pressed &&
136 !main.hasFocus
137 ) {
138 context.consume();
139 main.requestFocus(FocusOrigin.Keyboard)
140 } else if (main.hasFocus) {
141 if (treeKeyboard(input.plan, input.index, key))
142 context.consume()
143 else if (key.state ==
144 ElementState.Pressed &&
145 !key.synthetic &&
146 !key.modifiers.control &&
147 !key.modifiers.superKey &&
148 !key.modifiers.alt
149 )
150 when (key.logicalKey) {
151 case Character(val text) -> {
152 input.plan.controller.ink?.type(text);
153 context.consume()
154 }
156 else -> {}
157 }
158 }
159 }
161 case Pointer(
162 InputPointerEvent.Button(val value, val button, ElementState.Pressed, _)
163 ) -> {
164 if (isContextMenuClick(button, value.state.modifiers)) {
165 context.consume()
166 main.requestFocus(FocusOrigin.Pointer)
167 input
168 .plan
169 .input
170 .onContextMenu(
171 TreeActivation(
172 input.row.node,
173 TreeChangeOrigin.Pointer,
174 Some(value.state)
175 )
176 )
177 }
178 }
180 else -> {}
181 }
182 }
183 private fun activate() {
184 val input = props.value
185 val plan = input.plan
186 val row = input.row
187 if (!plan.input.enabled || !row.node.enabled) return
188 if (plan.controller.ink?.isSticky(row.key) ?: false)
189 plan.controller.scroll(row.key, None, ScrollAlignment.Start, false)
190 treeSelect(plan, row.key, pointer?.modifiers, TreeChangeOrigin.Pointer)
191 val activation = TreeActivation(row.node, TreeChangeOrigin.Pointer, pointer)
192 plan.input.onActivate(activation)
193 if (clicks == 2) plan.input.onDoubleClick(activation)
194 if (plan.input.expandAction ==
195 TreeExpandAction.Click ||
196 (plan.input.expandAction == TreeExpandAction.DoubleClick && clicks == 2)
197 )
198 plan.input.state.setExpanded(row.key, !row.expanded, TreeChangeOrigin.Pointer)
199 }
200 public override fun render(): View {
201 val input = props.value
202 val plan = input.plan
203 val row = input.row
204 val selected = plan.input.state.isSelected(row.key)
205 val interaction = press.interaction
206 val content = ArrayList<View>()
207 if (!plan.explicit)
208 content
209 .add(
210 treeCell(input, this, 0L, "title", treeTitle(input, this), AxisAlignment.Start)
211 )
212 else
213 for (index in 0L..<plan.columns.size) {
214 val column = plan.columns[index]
215 content
216 .add(
217 treeCell(
218 input, this, index, column.id,
219 if (column.id == plan.treeColumn)
220 treeTitle(input, this, Some(column.cell(row.node)))
221 else
222 column.cell(row.node), column.alignment
223 )
224 )
225 }
226 return element(
227 TreeRowDrawing(input, this, selected, interaction),
228 { value, mount -> TreeRowInk(value, mount) },
229 content.toArray()
230 )
231 }
234private fun <T> treeTitle(
235 input: TreeRowInput<T>,
236 owner: TreeRowView<T>,
237 custom: View? = None
238): View {
239 val row = input.row
240 val plan = input.plan
241 val options = plan.input
242 val density = plan.appearance.density
243 val slot = treeRowSlotSize * density
244 val title = when (options.title) {
245 case Some(val build) -> build(row)
246 case None ->
247 custom ?:
248 label(row.node.label) {
249 this.theme = options.theme
250 this.fonts = Some(plan.appearance.fonts)
251 this.settings = Some(plan.appearance.paragraph)
252 this
253 .style {
254 this.weight = Some(if (row.matched) 500 else 400)
255 this.color = Some(
256 if (!options.enabled || !row.node.enabled)
257 plan.appearance.theme.tokens.palette.muted.foreground
258 else if (row.matched)
259 plan.appearance.theme.tokens.palette.primary.background
260 else
261 plan.appearance.body.color
262 )
263 }
264 }
265 }
266 return makeTreeRow {
267 this.spacing = 0.0
268 this.alignment = CrossAlignment.Center
269 this
270 .add(
271 treeSlot(empty()) {
272 this.width = Some(row.depth.toDouble() * options.indent * density);
273 this.height = Some(1.0)
274 }
275 )
276 val status = options.state.loadStatus(row.key)
277 val failed = when (status) {
278 case Failed(_) -> true;
279 else -> false
280 }
281 val loading = when (status) {
282 case Loading -> true;
283 else -> false
284 }
285 val disclosure = if (!row.node.expandable)
286 empty()
287 else
288 treeActionButton(
289 if (loading)
290 when (options.loadingIcon) {
291 case Some(val build) -> build(row)
292 case None ->
293 treeSpinner {
294 this.size = Some(14.0 * density);
295 this.theme = options.theme
296 }
297 }
298 else if (failed)
299 treeLabel("↻") {
300 this.theme = options.theme;
301 this.fonts = Some(plan.appearance.fonts)
302 }
303 else
304 when (options.switcher) {
305 case Some(val build) -> build(row);
306 case None -> treeArrow(input)
307 },
308 (if (failed) "Retry loading " else if (row.expanded) "Collapse " else "Expand ") +
309 row.node.label,
310 options.enabled && row.node.enabled, options.theme,
311 { origin ->
312 if (failed)
313 options.state.retryLoad(row.key)
314 else
315 options.state.setExpanded(row.key, !row.expanded, origin)
316 }
317 )
318 this
319 .add(
320 treeSlot(disclosure) {
321 this.width = Some(slot);
322 this.height = Some(slot)
323 }
324 )
325 if (treeCheckingEnabled(options.state.checking)) {
326 val check = if (!row.node.checkable)
327 empty()
328 else
329 treeCheckbox(
330 binding(
331 { options.state.checkState(row.key) },
332 { next ->
333 options
334 .state
335 .setChecked(
336 row.key,
337 next == CheckState.Checked,
338 owner.inputOrigin
339 )
340 }
341 )
342 ) {
343 this.enabled = options.enabled && row.node.canCheck
344 this.theme = options.theme
345 this.focus = FocusOptions(tabStop = false)
346 this.style { this.padding = Some(Insets.all(0.0)) }
347 this.semantics = ComponentSemantics(
348 name = Some("Check " + row.node.label),
349 ignored = true
350 )
351 }
352 this
353 .add(
354 treeSlot(check) {
355 this.width = Some(slot);
356 this.height = Some(slot)
357 }
358 )
359 }
360 val decoration: View? = when (options.icon) {
361 case Some(val build) -> build(row)
362 case None ->
363 if (!options.showIcons)
364 None
365 else
366 Some(
367 treeIcon(
368 when (row.node.children) {
369 case Leaf -> fileIcon;
370 else -> folderIcon
371 }
372 ) {
373 this.size = 16.0 * density;
374 this.theme = options.theme
375 }
376 )
377 }
378 when (decoration) {
379 case Some(val view) ->
380 this
381 .add(
382 treeSlot(view) {
383 this.width = Some(slot);
384 this.height = Some(slot)
385 }
386 );
387 case None -> {}
388 }
389 this.item(title) { this.shrink = 1.0 }
390 }