Scoop API

scoop:scoop.ui:0.1.0

src/interaction/drop_root.scoop

Selected production source · src/interaction/drop_root.scoop

1package scoop.ui
3import scoop.graphics.Point
4import scoop.surface.InputArea
5import scoop.window.*
7/** Coordinates are frozen from one accepted input path before asking receivers. */
8internal struct DropCandidate(val target: DropArea, val position: Point)
10private class DropReply<T>(var value: T)
12private struct DropSelection(
13 val target: DropArea,
14 val position: Point,
15 val windowPosition: LogicalPosition,
16 val format: DragFormat,
17 val effect: DragEffect,
18 val revision: ULong
21internal class RootDrops(
22 private val root: UiRoot,
23 create: (DropHandlers) -> DropTarget
24): Closeable {
25 private val areas = ArrayList<DropArea>()
26 private val leaves = ArrayList<DropArea>()
27 private var selected: DropSelection? = None
28 private var closed = false
29 private val native =
30 create(
31 DropHandlers(
32 offer = { value -> nativeOffer(value) },
33 drop = { value -> nativeDrop(value) },
34 leave =
35 {
36 dispatch { endSelection(true, false) };
37 Unit
38 }
39 )
40 )
42 fun create(
43 scope: ComponentScope,
44 area: InputArea,
45 handlers: DropAreaHandlers,
46 enabled: Boolean
47 ): DropArea {
48 for (current in areas)
49 requireUi(current.area !== area, "input area already has a drop receiver")
50 val value = DropArea(scope, this, area, handlers, enabled)
51 areas.add(value)
52 native.enabled = true
53 return value
54 }
56 private fun candidates(position: LogicalPosition): Array<DropCandidate> {
57 val result = ArrayList<DropCandidate>()
58 for (hit in root.surface.hitInput(Point(position.x, position.y))) {
59 val local = hit.localPosition ?: continue
60 for (area in areas)
61 if (area.area.scope === hit.scope) {
62 result.add(DropCandidate(area, local))
63 break
64 }
65 }
66 return result.toArray()
67 }
68 private fun dispatch(flush: Boolean = true, action: () -> Unit): Boolean {
69 if (closed || !root.isOpen || root.host.isDispatching)
70 return false
71 when (currentEvaluation()) {
72 case Some(_) -> return false
73 case None -> {}
74 }
75 return root.surface.dispatch {
76 flushLeaves()
77 action()
78 if (flush && root.isOpen)
79 root.flush()
80 }
81 }
82 private fun nativeOffer(value: DropOffer): DropDecision {
83 val result = DropReply(DropDecision.Reject)
84 val alive =
85 dispatch(false) { result.value = offerCandidates(value, candidates(value.position)) }
86 val proposal = selected ?: return DropDecision.Reject
87 return if (
88 alive &&
89 !closed &&
90 proposal.target.available &&
91 proposal.revision ==
92 proposal.target.revision
93 )
94 result.value
95 else
96 DropDecision.Reject
97 }
98 private fun nativeDrop(value: DropRequest): Boolean {
99 val result = DropReply(false)
100 dispatch { result.value = dropCandidates(value, candidates(value.offer.position)) }
101 return result.value
102 }
104 internal fun offerCandidates(value: DropOffer, route: Array<DropCandidate>): DropDecision {
105 if (closed || !root.isOpen)
106 return DropDecision.Reject
107 var next: DropSelection? = None
108 for (candidate in route) {
109 val area = candidate.target
110 if (!area.available)
111 continue
112 val revision = area.revision
113 val decision = area.offer(value, candidate.position)
114 if (closed || !root.isOpen)
115 return DropDecision.Reject
116 if (!area.available || revision != area.revision)
117 continue
118 when (decision) {
119 case Accept(val format, val effect) -> {
120 if (value.formats.contains(format) && value.effects.allows(effect)) {
121 next =
122 Some(
123 DropSelection(
124 area,
125 candidate.position,
126 value.position,
127 format,
128 effect,
129 revision
130 )
131 )
132 break
133 }
134 }
135 case Reject -> {}
136 }
137 }
138 val previous = selected
139 val same =
140 when ((previous, next)) {
141 case(Some(val a), Some(val b)) -> a.target === b.target
142 case(None, None) -> true
143 else -> false
144 }
145 if (!same)
146 endSelection(true, false)
147 when (next) {
148 case Some(val value) -> {
149 if (
150 closed ||
151 !root.isOpen ||
152 !value.target.available ||
153 value.target.revision !=
154 value.revision
155 ) {
156 endSelection(true, false)
157 return DropDecision.Reject
158 }
159 selected = Some(value)
160 value.target.publish(Some(DropHover(value.position, value.format, value.effect)))
161 return DropDecision.Accept(value.format, value.effect)
162 }
163 case None -> return DropDecision.Reject
164 }
165 }
167 internal fun dropCandidates(value: DropRequest, route: Array<DropCandidate>): Boolean {
168 if (closed || !root.isOpen)
169 return false
170 val proposal = selected ?: return false
171 val area = proposal.target
172 if (
173 !area.available ||
174 proposal.revision !=
175 area.revision ||
176 proposal.windowPosition !=
177 value.offer.position ||
178 proposal.effect !=
179 value.effect ||
180 !value.offer.formats.contains(proposal.format) ||
181 !value.offer.effects.allows(proposal.effect) ||
182 !matchesFormat(value.content, proposal.format)
183 )
184 return false
185 var current = false
186 for (candidate in route)
187 if (candidate.target === area && candidate.position == proposal.position) {
188 current = true
189 break
190 }
191 if (!current)
192 return false
193 // Consume before invoking user code, including a callback that closes this root.
194 selected = None
195 area.publish(None)
196 val result = area.receive(value, proposal.position)
197 if (!area.isClosed)
198 leaves.add(area)
199 return result
200 }
202 fun updated(area: DropArea) {
203 when (selected) {
204 case Some(val value) -> if (value.target === area && !area.available)
205 endSelection(true, true)
206 case None -> {}
207 }
208 }
209 private fun endSelection(notify: Boolean, deferred: Boolean) {
210 val value = selected ?: return
211 selected = None
212 value.target.publish(None)
213 if (notify && !value.target.isClosed) {
214 if (deferred) {
215 leaves.add(value.target)
216 root.host.enqueue(root)
217 } else {
218 value.target.leave()
219 }
220 }
221 }
222 fun flushLeaves() {
223 val work = leaves.toArray()
224 leaves.clear()
225 for (area in work)
226 if (!closed && root.isOpen && !area.isClosed)
227 area.leave()
228 }
229 fun retire(area: DropArea) {
230 when (selected) {
231 case Some(val value) -> if (value.target === area)
232 endSelection(false, false)
233 case None -> {}
234 }
235 for (index in 0L ..< areas.size)
236 if (areas[index] === area) {
237 areas.removeAt(index)
238 break
239 }
240 if (!closed && !native.isClosed && areas.size == 0L)
241 native.enabled = false
242 }
243 public override fun close() {
244 root.checkThread()
245 if (closed)
246 return
247 closed = true
248 selected = None
249 leaves.clear()
250 var failure = cleanupUi(None) { native.close() }
251 val work = areas.toArray()
252 areas.clear()
253 for (area in work)
254 failure = cleanupUi(failure) { area.close() }
255 throwUi(failure)
256 }
259private fun matchesFormat(content: DragContent, format: DragFormat): Boolean =
260 when (content) {
261 case Text(_) -> format == DragFormat.Text
262 case Url(_) -> format == DragFormat.Url
263 case Files(_) -> format == DragFormat.Files
264 }