Scoop API

scoop:scoop.ui.component:0.1.0

src/tree/ink.scoop

Selected production source · src/tree/ink.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.FrameSubmission
5import scoop.ui.*
6import scoop.window.accessibility.*
8internal class TreeInk<T>(private val props: Props<TreePlan<T>>, scope: ComponentScope) :
9 LazyElement<TreeFrame<T>>(scope) {
10 private val control = ScrollControl(scope) { props.value.controls }
11 private val collection = createComponentSemantics(scope, SemanticRole.Tree, control.semantics)
12 private val focus = scope.lazyFocus(control.viewport)
13 private val measurements = VirtualMeasurements()
14 private val dragArea = scope.createArea()
15 private val typeahead = scope
16 .typeahead(
17 props.value.source,
18 { props.value.controller.focusedKey() },
19 { key -> props.value.controller.focus(key, None, false) },
20 TypeaheadOptions(keyboard = false, matches = { text, query -> menuPrefix(text, query) })
21 )
22 init {
23 dragArea.ignoreSelf = true
24 }
25 private var handle: TreeHandle? = None
26 private var updated: TreePlan<T>? = None
27 private var committed: TreeReadingPosition<T>? = None
28 private var drawing: TreeFrame<T>? = None
29 private var requested: TreeScrollRequest? = None
30 val active: Boolean get() = scope.isMounted
31 val pending: Boolean get() = active && (panelPresent(requestValue) || focus.isPending)
32 val requestValue: TreeScrollRequest?
33 get() = when (requested) {
34 case Some(val value) ->
35 if (value.revision == props.value.vertical.requestRevision) Some(value) else None
36 case None -> None
37 }
38 val gutter: Size get() = control.gutter
39 fun bodyViewport(size: Size, direction: LayoutDirection): Rect = control
40 .contentViewport(size, direction)
41 fun isSticky(key: ViewKey): Boolean {
42 val current = drawing ?: return false
43 for (row in current.pinning.rows)
44 if (current.plan.projection.keyAt(row.index) == key) return true
45 return false
46 }
47 fun dragDelta(point: Point): Double {
48 val frame = drawing ?: return 0.0
49 val local = dragArea.toLocal(point) ?: return 0.0
50 val viewport = control.contentViewport(frame.size, scope.layoutDirection)
51 val top = viewport.y + frame.pinning.height
52 val bottom = viewport.y + viewport.height
53 if (local.x <
54 viewport.x ||
55 local.x >=
56 viewport.x +
57 viewport.width ||
58 local.y <
59 top ||
60 local.y >=
61 bottom
62 )
63 return 0.0
64 val edge = upperBound(24.0 * frame.plan.appearance.density, (bottom - top) / 3.0)
65 val step = 12.0 * frame.plan.appearance.density
66 return if (local.y < top + edge) -step else if (local.y >= bottom - edge) step else 0.0
67 }
68 fun type(text: String) {
69 typeahead.push(text)
70 }
71 fun page(index: Long, forward: Boolean): Long {
72 val current = drawing ?: return index
73 val extents = current.extents
74 val step = lowerBound(current.reading.snapshot.bodyHeight, itemEstimate(props.value.extent))
75 val next = extents
76 .indexAt(lowerBound(extents.prefix(index) + if (forward) step else -step, 0.0))
77 return if (next < extents.count) next else extents.count - 1L
78 }
79 fun request(
80 key: ViewKey,
81 column: String?,
82 alignment: ScrollAlignment,
83 focusItem: Boolean
84 ): Boolean {
85 if (!active) return false
86 val plan = props.value
87 val revision = plan.vertical.beginLayoutRequest()
88 val horizontalRevision = if (column != None)
89 plan.horizontal.beginLayoutRequest()
90 else
91 plan.horizontal.requestRevision
92 if (!active || plan.vertical.requestRevision != revision) return false
93 requested = Some(
94 TreeScrollRequest(key, column, alignment, revision, horizontalRevision, focusItem)
95 )
96 scope.requestLayout()
97 return true
98 }
99 public override fun onMount() {
100 props.value.controller.ink = Some(this);
101 scope.routeSemanticsChildren(collection)
102 }
103 public override fun update(): UiInvalidation {
104 val plan = props.value
105 if (plan.input.enabled) plan.input.state.synchronizeLoading(interactive = true)
106 control.update()
107 plan.controller.plan = Some(plan)
108 plan.controller.ink = Some(this)
109 focus.options = LazyFocusOptions(
110 ItemNavigation.Roving(),
111 NavigationLayout.Vertical,
112 plan.input.focus.order
113 )
114 val changed = when (updated) {
115 case Some(val old) -> old !== plan;
116 case None -> true
117 }
118 if (changed) {
119 typeahead
120 .update(
121 plan.source,
122 { plan.controller.focusedKey() },
123 { key -> plan.controller.focus(key, None, false) },
124 Some(
125 TypeaheadOptions(
126 enabled = plan.input.enabled,
127 keyboard = false,
128 matches = { text, query -> menuPrefix(text, query) }
129 )
130 )
131 )
132 when (updated) {
133 case Some(val old) ->
134 if (old.vertical !== plan.vertical) {
135 committed = None;
136 requested = None
137 };
138 case None -> {}
139 }
140 updated = Some(plan)
141 }
142 val sameHandle = when ((handle, plan.input.handle)) {
143 case(Some(val a), Some(val b)) -> a === b;
144 case(None, None) -> true;
145 else -> false
146 }
147 if (!sameHandle) {
148 plan.input.handle?.bind(plan.controller);
149 handle?.unbind(plan.controller);
150 handle = plan.input.handle
151 }
152 when (collection) {
153 case Some(val value) -> {
154 val references = ArrayList<SemanticsReference>()
155 when (handle) {
156 case Some(val target) -> references.add(target.semanticTarget);
157 case None -> {}
158 }
159 when (plan.input.semantics.reference) {
160 case Some(val target) -> references.add(target);
161 case None -> {}
162 }
163 value.bindReferences(references.toArray())
164 value
165 .update(
166 plan.input.semantics.properties(enabled = plan.input.enabled),
167 relations = plan.input.semantics.relations
168 )
169 value
170 .updateCollection(
171 plan.source,
172 Some(plan.semantics),
173 Some(control.viewport)
174 )
175 }
177 case None -> {}
178 }
179 if (changed)
180 when (plan.restoreFocus) {
181 case Some(val key) -> request(key, None, ScrollAlignment.Nearest, true);
182 case None -> {}
183 }
184 return UiInvalidation.Layout
185 }
186 public override fun measure(
187 context: LazyMeasureContext,
188 constraints: Constraints
189 ): LayoutResult<TreeFrame<T>> {
190 val plan = props.value
191 val request = when (focus.pendingItem) {
192 case Some(val key) ->
193 Some(
194 TreeScrollRequest(
195 key,
196 None,
197 ScrollAlignment.Nearest,
198 plan.vertical.requestRevision,
199 plan.horizontal.requestRevision,
200 false
201 )
202 )
203 case None -> requestValue
204 }
205 val frame = TreeLayout(
206 context,
207 constraints,
208 plan,
209 control,
210 measurements,
211 committed,
212 drawing?.regular,
213 request
214 )
215 .measure()
216 return LayoutResult(LayoutMetrics(frame.size), frame)
217 }
218 public override fun scene(context: SceneContext, frame: TreeFrame<T>) {
219 val viewport = frame.layout.snapshot.viewport
220 context.place(dragArea, Rect(0.0, 0.0, context.size.width, context.size.height))
221 when (collection) {
222 case Some(val value) ->
223 context
224 .semantics(value, control.contentViewport(frame.size, context.layoutDirection));
225 case None -> {}
226 }
227 control
228 .scene(
229 context,
230 frame.layout,
231 Point(0.0, frame.layout.snapshot.offset.y),
232 { content ->
233 val shift = frame.layout.overscroll.y + frame.layout.snapshot.padding.top
234 val left = -frame.layout.snapshot.padding.left - frame.layout.overscroll.x
235 val body = Rect(
236 0.0,
237 frame.pinning.height,
238 viewport.width,
239 lowerBound(viewport.height - frame.pinning.height, 0.0)
240 )
241 content
242 .withScrollReveal(control.viewport, body, ScrollAxes.Vertical) { normal ->
243 normal
244 .clipRect(Rect(left, body.y - shift, body.width, body.height))
245 normal
246 .translate(
247 0.0,
248 frame.regularOrigin - frame.layout.snapshot.offset.y
249 )
250 for (key in frame.regular.keys)
251 normal.item(key)
252 }
253 for (row in frame.pinning.rows)
254 content
255 .withScrollReveal(
256 control.viewport,
257 Rect(0.0, row.top, viewport.width, row.bottom - row.top),
258 ScrollAxes.Vertical
259 ) { sticky ->
260 sticky
261 .clipRect(
262 Rect(
263 left,
264 row.top - shift,
265 viewport.width,
266 row.bottom - row.top
267 )
268 )
269 sticky.item(frame.plan.projection.keyAt(row.index))
270 }
271 }
272 )
273 if (frame.pinning.height > 0.0) {
274 val body = control.contentViewport(frame.size, context.layoutDirection)
275 val line = lowerBound(frame.plan.appearance.density, 1.0)
276 context
277 .draw { paint ->
278 paint
279 .canvas
280 .drawRect(
281 Rect(body.x, body.y + frame.pinning.height - line, body.width, line),
282 Paint(frame.plan.appearance.theme.tokens.palette.border)
283 )
284 }
285 }
286 context
287 .onFrameComplete { result ->
288 if (result.submission ==
289 FrameSubmission.Accepted &&
290 active &&
291 props.value ===
292 frame.plan &&
293 frame.reading.revision ==
294 props.value.vertical.requestRevision
295 ) {
296 committed = Some(frame.reading)
297 drawing = Some(frame)
298 frame.plan.controller.presented = Some(frame.plan.projection)
299 handle?.publish(frame.plan.controller, frame.reading.snapshot)
300 val same = when ((requested, frame.request)) {
301 case(Some(val current), Some(val accepted)) -> current === accepted;
302 else -> false
303 }
304 if (same && frame.requestDone) {
305 val completed = requested!!
306 requested = None
307 frame.plan.controller.finishFocus(completed)
308 }
309 if (frame.recheck) scope.requestLayout()
310 }
311 }
312 }
313 public override fun onUnmount() {
314 handle?.unbind(props.value.controller)
315 props.value.controller.ink = None
316 props.value.controller.plan = None
317 props.value.controller.presented = None
318 requested = None
319 committed = None
320 drawing = None
321 }