Scoop API

scoop:scoop.ui:0.1.0

src/focus/cursors.scoop

Selected production source · src/focus/cursors.scoop

1package scoop.ui
3/** One finite mounted sequence, optionally wrapping once at its scope boundary. */
4internal class NodeFocusCursor(
5 val node: MountedNode,
6 val reverse: Boolean,
7 private val loop: Boolean,
8 private val excluded: MountedNode? = None
9) {
10 val stops = tabOrder(node)
11 private var index =
12 if (reverse)
13 stops.size
14 else
15 -1L
16 private var remaining = stops.size
18 fun seekAfter(position: Long) {
19 index = position
20 remaining =
21 if (loop)
22 stops.size
23 else
24 if (reverse)
25 position
26 else
27 stops.size - position - 1L
28 }
29 fun seekAfterSubtree(boundary: MountedNode) {
30 var found: Long? = None
31 for (position in 0L ..< stops.size) {
32 if (!withinFocusScope(focusStopNode(stops[position]), boundary))
33 continue
34 if (!reverse || found == None)
35 found = Some(position)
36 }
37 when (found) {
38 case Some(val position) -> seekAfter(position)
39 case None -> {}
40 }
41 }
42 fun focusedPosition(): Long? {
43 for (position in 0L ..< stops.size) {
44 val focused =
45 when (stops[position]) {
46 case Target(val target) -> target.active && target.area.hasFocus
47 case Items(val owner) -> when (owner.focusedEntry()) {
48 case Some(_) -> true
49 case None -> false
50 }
51 }
52 if (focused)
53 return Some(position)
54 }
55 return None
56 }
57 fun next(): FocusStop? {
58 if (!node.active || !node.scope.input.isInputAllowed)
59 return None
60 while (remaining > 0L) {
61 remaining -= 1L
62 index += if (reverse)
63 -1L
64 else
65 1L
66 if (index < 0L || index >= stops.size) {
67 if (!loop)
68 return None
69 index =
70 if (reverse)
71 stops.size - 1L
72 else
73 0L
74 }
75 val stop = stops[index]
76 val skip =
77 when (excluded) {
78 case Some(val boundary) -> withinFocusScope(focusStopNode(stop), boundary)
79 case None -> false
80 }
81 if (!skip)
82 return Some(stop)
83 }
84 return None
85 }
88/** Stores only a stable current key and a finite scan length, never one slot per source item. */
89internal class ItemFocusCursor(
90 val owner: LazyFocus,
91 private val reverse: Boolean,
92 private val initialIndex: Long,
93 private var remaining: Long,
94 private val loop: Boolean,
95 val edge: ItemFocusEdge,
96 private var exact: ViewKey? = None
97) {
98 var current: ViewKey? = None
100 fun next(): ViewKey? {
101 if (!owner.available || remaining == 0L)
102 return None
103 when (exact) {
104 case Some(val key) -> {
105 exact = None
106 remaining = 0L
107 current = Some(key)
108 return Some(key)
109 }
110 case None -> {}
111 }
112 val count = owner.count
113 if (count == 0L)
114 return None
115 var index = initialIndex
116 when (current) {
117 case Some(val key) -> {
118 val previous = owner.indexOf(key) ?: return None
119 index =
120 previous +
121 if (reverse)
122 -1L
123 else
124 1L
125 }
126 case None -> {}
127 }
128 if (index < 0L || index >= count) {
129 if (!loop)
130 return None
131 index =
132 if (reverse)
133 count - 1L
134 else
135 0L
136 }
137 remaining -= 1L
138 val key = owner.keyAt(index)
139 current = Some(key)
140 return Some(key)
141 }
144internal enum FocusCursor {
145 Nodes(NodeFocusCursor), Items(ItemFocusCursor)
148internal class FocusItemVisit(val owner: LazyFocus, val key: ViewKey, val edge: ItemFocusEdge)
150internal enum FocusSearchStep {
151 Target(FocusTarget), Item(FocusItemVisit), End