Scoop API

scoop:scoop.ui.component:0.1.0

src/tree/handle.scoop

Selected production source · src/tree/handle.scoop

1package scoop.ui.component
3import scoop.platform.concurrent.currentThreadId
4import scoop.ui.*
5import scoop.window.accessibility.SemanticNodeId
7public class TreeReadingSnapshot internal constructor(
8 public val visible: VirtualRange,
9 public val anchor: VirtualReadingAnchor?,
10 public val stickyKeys: Array<ViewKey>,
11 public val bodyHeight: Double,
12 public val offset: Double,
13 public val atEnd: Boolean
16internal interface TreeTarget {
17 val active: Boolean
18 val pending: Boolean
19 fun scroll(key: ViewKey, column: String?, alignment: ScrollAlignment, expand: Boolean): Boolean
20 fun focus(key: ViewKey, column: String?, expand: Boolean): Boolean
23/** Requests use the mounted tree's existing scroll and focus resources. */
24public class TreeHandle public constructor() {
25 private val thread = currentThreadId()
26 private var target: TreeTarget? = None
27 private val snapshot = state<TreeReadingSnapshot?>(None)
28 internal val semanticTarget = SemanticsReference()
29 public val semanticId: SemanticNodeId?
30 get() {
31 checkThread();
32 return semanticTarget.id
33 }
34 public val isAttached: Boolean
35 get() {
36 checkThread();
37 return target?.active ?: false
38 }
39 public val isPending: Boolean
40 get() {
41 checkThread();
42 return target?.pending ?: false
43 }
44 public val reading: TreeReadingSnapshot?
45 get() {
46 checkThread();
47 return snapshot.value
48 }
49 public fun scrollToItem(
50 key: ViewKey,
51 alignment: ScrollAlignment = ScrollAlignment.Nearest,
52 expandAncestors: Boolean = false
53 ): Boolean {
54 snapshot.checkMutation()
55 requireComponent(key != ViewKey.Position, "tree positioning requires an explicit key")
56 return target?.scroll(key, None, alignment, expandAncestors) ?: false
57 }
58 public fun scrollToCell(
59 key: ViewKey,
60 column: String,
61 alignment: ScrollAlignment = ScrollAlignment.Nearest,
62 expandAncestors: Boolean = false
63 ): Boolean {
64 snapshot.checkMutation()
65 requireComponent(
66 key != ViewKey.Position && column != "",
67 "tree cell positioning requires a key and column"
68 )
69 return target?.scroll(key, Some(column), alignment, expandAncestors) ?: false
70 }
71 public fun focusItem(key: ViewKey, expandAncestors: Boolean = false): Boolean {
72 snapshot.checkMutation()
73 requireComponent(key != ViewKey.Position, "tree positioning requires an explicit key")
74 return target?.focus(key, None, expandAncestors) ?: false
75 }
76 public fun focusCell(key: ViewKey, column: String, expandAncestors: Boolean = false): Boolean {
77 snapshot.checkMutation()
78 requireComponent(
79 key != ViewKey.Position && column != "",
80 "tree cell positioning requires a key and column"
81 )
82 return target?.focus(key, Some(column), expandAncestors) ?: false
83 }
84 private fun checkThread() {
85 requireComponent(currentThreadId() == thread, "tree handle used from another thread")
86 }
87 internal fun bind(next: TreeTarget) {
88 checkThread()
89 when (target) {
90 case Some(val old) ->
91 requireComponent(old === next || !old.active, "tree handle already has a target");
92 case None -> {}
93 }
94 target = Some(next)
95 }
96 internal fun unbind(owner: TreeTarget) {
97 checkThread()
98 when (target) {
99 case Some(val current) ->
100 if (current === owner) {
101 target = None;
102 snapshot.value = None
103 };
104 case None -> {}
105 }
106 }
107 internal fun publish(owner: TreeTarget, value: TreeReadingSnapshot) {
108 checkThread()
109 val current = target ?: return
110 if (current !== owner || !owner.active) return
111 when (snapshot.value) {
112 case Some(val old) ->
113 if (old.visible ==
114 value.visible &&
115 old.anchor ==
116 value.anchor &&
117 treeKeysEqual(old.stickyKeys, value.stickyKeys) &&
118 old.bodyHeight ==
119 value.bodyHeight &&
120 old.offset ==
121 value.offset &&
122 old.atEnd ==
123 value.atEnd
124 )
125 return
126 case None -> {}
127 }
128 snapshot.value = Some(value)
129 }