Scoop API

scoop:scoop.ui.component:0.1.0

src/tree/drag.scoop

Selected production source · src/tree/drag.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.platform.time.MonotonicTimeSource
5import scoop.surface.*
6import scoop.time.Duration
7import scoop.ui.*
8import scoop.window.*
10/** The source and receiver borrow the row's real input route, including sticky placement. */
11internal class TreeDragControl<T>(
12 private val scope: ComponentScope,
13 private val area: InputArea,
14 private val source: DragSource,
15 private val read: () -> TreeRowInput<T>
16) : Closeable {
17 private val hover = state<TreeDrop<T>?>(None)
18 private val expand = scope.createDeadline { expandHovered() }
19 private val scroll = scope.createDeadline { scrollHovered() }
20 private var offerValue: DropOffer? = None
21 private var hoverLeave: ((TreeDrop<T>) -> Unit)? = None
22 private var height = 0.0
23 private var ready = false
24 private var closed = false
25 private var enabled = false
26 private var sourceEnabled = false
27 private val handlers = DropAreaHandlers(
28 { value, point -> offer(value, point) },
29 { value, point -> drop(value, point) },
30 { leave() }
31 )
32 private val target = scope.dropTarget(area, handlers, false)
33 val position: TreeDropPosition? get() = hover.value?.position
34 private val activity = scope
35 .observeWindowActivity { value -> if (!value.visible || value.occluded) leave() }
36 fun update() {
37 val input = read()
38 val options = input.plan.input.drag
39 val next = input.plan.input.enabled && input.row.node.enabled && (options?.enabled ?: false)
40 if (enabled != next) {
41 enabled = next
42 target.update(handlers, next)
43 }
44 val draggable = next && input.row.node.draggable
45 if (sourceEnabled != draggable) {
46 sourceEnabled = draggable
47 source.update(DragSourceOptions(enabled = draggable)) { value -> prepare(value) }
48 }
49 if (!next)
50 leave()
51 else
52 when (offerValue) {
53 case Some(val value) -> {
54 val point = area.toLocal(Point(value.position.x, value.position.y))
55 if (!panelPresent(point))
56 leave()
57 else if (!panelPresent(resolve(value, point!!))) leave()
58 }
60 case None -> {}
61 }
62 }
63 private fun prepare(start: DragSourceStart): DragSourceData? {
64 if (closed || !enabled) return None
65 val input = read()
66 val options = input.plan.input.drag ?: return None
67 val model = input.plan.input.state
68 val node = model.data.node(input.row.key) ?: return None
69 if (!node.enabled || !node.draggable) return None
70 options.onStart(node, start)
71 return Some(
72 DragSourceData(
73 options.content(node), DragEffects(false, true, false),
74 Some(TreeDragPayload(model, node)), onCompleted = { result ->
75 options.onEnd(node, result)
76 }
77 )
78 )
79 }
80 private fun resolve(value: DropOffer, point: Point): TreeDrop<T>? {
81 if (closed || !enabled || !ready || !value.effects.move || !scope.isMounted) return None
82 val payload = when (value.localData) {
83 case Some(val data) -> data as? TreeDragPayload<T>
84 case None -> None
85 } ?:
86 return None
87 val input = read()
88 val model = input.plan.input.state
89 if (payload.state !== model) return None
90 val node = model.data.node(payload.node.key) ?: return None
91 if (node !== payload.node) return None
92 val destination = model.data.node(input.row.key) ?: return None
93 if (model.projection.indexOfKey(destination.key) == None) return None
94 val branch = when (destination.children) {
95 case Leaf -> false;
96 else -> true
97 }
98 val position = if (point.y < height * (if (branch) 0.25 else 0.5))
99 TreeDropPosition.Before
100 else if (point.y >= height * (if (branch) 0.75 else 0.5))
101 TreeDropPosition.After
102 else
103 TreeDropPosition.Inside
104 val result = model.dropTarget(node.key, destination.key, position) ?: return None
105 val options = input.plan.input.drag ?: return None
106 return if (options.allowDrop(result)) Some(result) else None
107 }
108 private fun offer(value: DropOffer, point: Point): DropDecision {
109 val next = resolve(value, point)
110 when (next) {
111 case None -> {
112 leave();
113 return DropDecision.Reject
114 }
116 case Some(val move) -> {
117 val options = read().plan.input.drag ?: return DropDecision.Reject
118 val previous = hover.value
119 val same = when (previous) {
120 case Some(val old) ->
121 old.source.key ==
122 move.source.key &&
123 old.target.key ==
124 move.target.key &&
125 old.position ==
126 move.position
127 case None -> false
128 }
129 if (!same) {
130 leave()
131 hover.value = Some(move)
132 hoverLeave = Some(options.onLeave)
133 scope.requestFrame()
134 options.onEnter(move)
135 if (move.position ==
136 TreeDropPosition.Inside &&
137 move.target.expandable &&
138 !read().plan.input.state.isExpanded(move.target.key)
139 )
140 expand.schedule(MonotonicTimeSource.now() + options.expandDelay)
141 }
142 offerValue = Some(value)
143 options.onOver(move)
144 scheduleScroll()
145 return DropDecision
146 .Accept(
147 if (value.formats.text)
148 DragFormat.Text
149 else if (value.formats.url) DragFormat.Url else DragFormat.Files,
150 DragEffect.Move
151 )
152 }
153 }
154 }
155 private fun drop(value: DropRequest, point: Point): Boolean {
156 val move = resolve(value.offer, point) ?: return false
157 val options = read().plan.input.drag ?: return false
158 try {
159 return value.effect == DragEffect.Move && options.onDrop(move)
160 } finally {
161 leave()
162 }
163 }
164 private fun expandHovered() {
165 val value = offerValue ?: return
166 val point = area.toLocal(Point(value.position.x, value.position.y)) ?: return
167 val move = resolve(value, point) ?: return
168 if (move.position == TreeDropPosition.Inside)
169 read().plan.input.state.setExpanded(move.target.key, true, TreeChangeOrigin.Pointer)
170 }
171 private fun scheduleScroll() {
172 val value = offerValue ?: return
173 val delta = read()
174 .plan
175 .controller
176 .ink
177 ?.dragDelta(Point(value.position.x, value.position.y)) ?:
178 0.0
179 if (delta == 0.0)
180 scroll.cancel()
181 else if (!scroll.isScheduled)
182 scroll.schedule(MonotonicTimeSource.now() + Duration.milliseconds(16L))
183 }
184 private fun scrollHovered() {
185 val value = offerValue ?: return
186 val point = Point(value.position.x, value.position.y)
187 if (closed ||
188 !scope.isMounted ||
189 scope.nativeWindow.state !=
190 WindowState.Open ||
191 !scope.nativeWindow.isVisible ||
192 scope.nativeWindow.isOccluded ||
193 !area.hitTest(point)
194 ) {
195 leave()
196 return
197 }
198 val plan = read().plan
199 val delta = plan.controller.ink?.dragDelta(point) ?: 0.0
200 val before = plan.vertical.offset
201 if (delta != 0.0) plan.vertical.scrollBy(Point(0.0, delta), ScrollOrigin.User)
202 if (plan.vertical.offset != before) scheduleScroll()
203 }
204 private fun leave() {
205 if (closed) return
206 expand.cancel()
207 scroll.cancel()
208 offerValue = None
209 val previous = hover.value
210 val callback = hoverLeave
211 hoverLeave = None
212 when (previous) {
213 case Some(_) -> {
214 hover.value = None;
215 if (scope.isMounted) scope.requestFrame()
216 }
218 case None -> {}
219 }
220 when (previous) {
221 case Some(val value) -> callback?.invoke(value)
222 case None -> {}
223 }
224 }
225 public override fun close() {
226 if (closed) return
227 enabled = false
228 ready = false
229 try {
230 leave()
231 } finally {
232 closed = true
233 activity.close()
234 source.close()
235 target.close()
236 expand.close()
237 scroll.close()
238 }
239 }
240 fun scene(context: SceneContext, bounds: Rect) {
241 val current = position
242 when (current) {
243 case Some(val value) ->
244 context
245 .draw { paint ->
246 val color = read().plan.appearance.theme.tokens.palette.ring
247 val thickness = 2.0 * read().plan.appearance.density
248 if (value == TreeDropPosition.Inside)
249 paint.canvas.drawRect(bounds, Paint(color.withAlpha(0.16)))
250 else
251 paint
252 .canvas
253 .drawRect(
254 Rect(
255 0.0,
256 if (value == TreeDropPosition.Before)
257 0.0
258 else
259 lowerBound(bounds.height - thickness, 0.0),
260 bounds.width,
261 thickness
262 ),
263 Paint(color)
264 )
265 }
266 case None -> {}
267 }
268 context
269 .onFrameComplete { result ->
270 if (result.submission == FrameSubmission.Accepted && scope.isMounted && !closed) {
271 height = bounds.height
272 ready = true
273 }
274 }
275 }