Scoop API

scoop:scoop.ui:0.1.0

src/interaction/drag_source.scoop

Selected production source · src/interaction/drag_source.scoop

1package scoop.ui
3import scoop.graphics.Point
4import scoop.surface.*
5import scoop.window.*
7/** Borrows an area and hands one confirmed press sequence to the native data session. */
8public class DragSource internal constructor(
9 private val root: UiRoot,
10 private val scope: ComponentScope,
11 private val area: InputArea,
12 initial: DragSourceOptions,
13 prepare: (DragSourceStart) -> DragSourceData?,
14 begin: (DragSourceData, (DragResult) -> Unit) -> DragSession
15): Closeable {
16 private val visual = state(false)
17 private var options = initial
18 private var prepareData: ((DragSourceStart) -> DragSourceData?)? = Some(prepare)
19 private var beginSession: ((DragSourceData, (DragResult) -> Unit) -> DragSession)? = Some(begin)
20 private var session: DragSession? = None
21 private var completion: ((DragResult) -> Unit)? = None
22 private var press: PointerState? = None
23 private var input: DragInput? = None
24 private val contacts = ArrayList<ContactId>()
25 private var moved = false
26 private var began = false
27 private var preparing = false
28 private var closed = false
29 private var revision = 0uL
30 private val events = area.scope.addEventHandler { context, event -> observe(context, event) }
31 private val defaults = area.scope.addDefaultAction { context, event -> handle(context, event) }
32 private val finished = area.scope.addEventFinished { event -> finish(event) }
34 public val isDragging: Boolean get() = visual.value
35 public val isClosed: Boolean get() {
36 val mounted = scope.isMounted
37 return closed || !mounted || area.isClosed
38 }
39 private val available: Boolean get() = !isClosed && options.enabled && area.scope.isInputAllowed
41 public fun update(options: DragSourceOptions, prepare: (DragSourceStart) -> DragSourceData?) {
42 checkDropMutation()
43 requireUi(!isClosed, "drag source is no longer mounted")
44 if (
45 options.threshold !=
46 this.options.threshold ||
47 !options.enabled ||
48 options.mouse !=
49 this.options.mouse ||
50 options.contacts !=
51 this.options.contacts
52 )
53 clearCandidate()
54 this.options = options
55 prepareData = Some(prepare)
56 revision += 1uL
57 reconcile()
58 }
60 private fun observe(context: RoutedEventContext, event: InputEvent) {
61 if (closed)
62 return
63 if (context.phase == RoutedPhase.Notification) {
64 when (event) {
65 case Pointer(InputPointerEvent.Cancelled(_, _)) -> {
66 if (isMouse())
67 clearCandidate()
68 }
69 case Contact(InputContactEvent.Cancelled(val id, _, _)) -> {
70 forgetContact(contacts, id)
71 if (isContact(id))
72 clearCandidate()
73 }
74 else -> {}
75 }
76 reconcile()
77 return
78 }
79 when (event) {
80 case Pointer(
81 InputPointerEvent.Button(
82 val pointer,
83 MouseButton.Left,
84 ElementState.Pressed,
85 val count
86 )
87 ) -> {
88 clearCandidate()
89 if (
90 available &&
91 options.mouse &&
92 contacts.size ==
93 0L &&
94 !visual.value &&
95 count ==
96 1 &&
97 context.localPosition !=
98 None &&
99 !isContextMenuClick(MouseButton.Left, pointer.state.modifiers)
100 )
101 {
102 press = Some(pointer.state)
103 input = Some(DragInput.Mouse(MouseButton.Left))
104 }
105 }
106 case Contact(InputContactEvent.Began(val value)) -> {
107 rememberContact(contacts, value.id)
108 if (contacts.size > 1L) {
109 clearCandidate()
110 return
111 }
112 if (
113 available && options.contacts && !visual.value && context.localPosition != None
114 ) {
115 press = Some(value.pointer)
116 input = Some(DragInput.Contact(value.id, value.kind))
117 }
118 }
119 else -> {}
120 }
121 }
123 internal fun handle(context: RoutedEventContext, event: InputEvent) {
124 if (!available || visual.value || press == None)
125 return
126 when (event) {
127 case Pointer(InputPointerEvent.Moved(val pointer)) -> if (isMouse()) {
128 moved = true
129 advance(context, pointer.state)
130 }
131 case Pointer(
132 InputPointerEvent.Button(val pointer, MouseButton.Left, ElementState.Pressed, _)
133 ) -> {
134 began = true
135 if (isMouse() && options.threshold == 0.0)
136 advance(context, pointer.state)
137 }
138 case Contact(InputContactEvent.Began(val value)) -> {
139 began = true
140 if (isContact(value.id) && options.threshold == 0.0)
141 advance(context, value.pointer)
142 }
143 case Contact(InputContactEvent.Moved(val value)) -> {
144 if (isContact(value.id)) {
145 moved = true
146 advance(context, value.pointer)
147 }
148 }
149 else -> {}
150 }
151 }
153 private fun advance(context: RoutedEventContext, pointer: PointerState) {
154 val origin = press ?: return
155 val source = input ?: return
156 if (!dragSourceThreshold(origin, pointer, options.threshold))
157 return
158 // A declined or invalidated preparation is not retried until the next press.
159 clearCandidate()
160 val position = context.localPosition ?: return
161 val start =
162 area.toLocal(Point(origin.logicalPosition.x, origin.logicalPosition.y)) ?: return
163 val version = revision
164 preparing = true
165 val data =
166 try {
167 prepareData!!.invoke(DragSourceStart(start, position, origin, pointer, source)) ?:
168 return
169 } finally {
170 preparing = false
171 }
172 if (!available || revision != version || visual.value)
173 return
174 val claimed =
175 when (source) {
176 case Mouse(val button) -> area.scope.claimPointer(button)
177 case Contact(val id, _) -> area.scope.claimContacts([id])
178 }
179 if (!claimed)
180 return
181 try {
182 if (!available || revision != version) {
183 releaseClaim(source)
184 return
185 }
186 val value = beginSession!!.invoke(data) { result -> completed(result) }
187 session = Some(value)
188 completion = Some(data.onCompleted)
189 root.dragSource = Some(this)
190 publish(true)
191 context.consume()
192 } catch (error: Throwable) {
193 var failure = cleanupUi(Some(error)) { releaseSession() }
194 failure = cleanupUi(failure) { releaseClaim(source) }
195 throw failure!!
196 }
197 }
199 private fun finish(event: InputEvent) {
200 when (event) {
201 case Contact(InputContactEvent.Began(val value)) -> {
202 if (isContact(value.id) && !began)
203 clearCandidate()
204 began = false
205 }
206 case Pointer(
207 InputPointerEvent.Button(_, MouseButton.Left, ElementState.Pressed, _)
208 ) -> {
209 if (isMouse() && !began)
210 clearCandidate()
211 began = false
212 }
213 case Pointer(InputPointerEvent.Moved(_)) -> {
214 if (isMouse() && !moved)
215 clearCandidate()
216 moved = false
217 }
218 case Contact(InputContactEvent.Moved(val value)) -> {
219 if (isContact(value.id) && !moved)
220 clearCandidate()
221 moved = false
222 }
223 case Contact(InputContactEvent.Ended(val value)) -> {
224 forgetContact(contacts, value.id)
225 if (isContact(value.id))
226 clearCandidate()
227 }
228 case Pointer(
229 InputPointerEvent.Button(_, MouseButton.Left, ElementState.Released, _)
230 ) -> {
231 if (isMouse())
232 clearCandidate()
233 }
234 else -> {}
235 }
236 }
238 private fun isMouse(): Boolean =
239 when (input) {
240 case Some(DragInput.Mouse(_)) -> true
241 else -> false
242 }
243 private fun isContact(id: ContactId): Boolean =
244 when (input) {
245 case Some(DragInput.Contact(val current, _)) -> current == id
246 else -> false
247 }
248 internal fun hasContactCandidate(id: ContactId): Boolean = press != None && isContact(id)
249 private fun clearCandidate() {
250 press = None
251 input = None
252 }
253 private fun releaseClaim(source: DragInput) {
254 if (area.isClosed)
255 return
256 when (source) {
257 case Mouse(_) -> area.scope.cancelPointerClaim()
258 case Contact(val id, _) -> area.scope.cancelContactClaim(id)
259 }
260 }
262 private fun completed(result: DragResult) {
263 if (closed)
264 return
265 if (!scope.isMounted) {
266 releaseSession()
267 return
268 }
269 scope.dispatch {
270 val action = completion
271 val allowed = available
272 releaseSession()
273 if (allowed)
274 action?.invoke(result)
275 }
276 }
278 internal fun reconcile() {
279 if (!available) {
280 clearCandidate()
281 releaseSession()
282 }
283 }
285 internal fun updateEffects(effects: DragEffects) {
286 val current = session ?: return
287 if (current.isClosed)
288 return
289 when (current.result) {
290 case Some(_) -> {}
291 case None -> current.effects = effects
292 }
293 }
295 public fun cancelPending(): Boolean {
296 scope.isMounted
297 checkDropMutation()
298 if (closed)
299 return false
300 val candidate = press != None || preparing
301 clearCandidate()
302 preparing = false
303 revision += 1uL
304 return when (val current = session) {
305 case Some(val value) -> value.cancelPending()
306 case None -> candidate
307 }
308 }
310 private fun publish(value: Boolean) {
311 if (visual.value == value)
312 return
313 visual.value = value
314 if (scope.isMounted)
315 scope.requestFrame()
316 }
318 private fun releaseSession() {
319 val previous = session
320 session = None
321 completion = None
322 when (val current = root.dragSource) {
323 case Some(val value) -> if (value === this)
324 root.dragSource = None
325 case None -> {}
326 }
327 try {
328 previous?.close()
329 } finally {
330 publish(false)
331 }
332 }
334 public override fun close() {
335 scope.isMounted
336 checkDropMutation()
337 if (closed)
338 return
339 closed = true
340 revision += 1uL
341 clearCandidate()
342 contacts.clear()
343 preparing = false
344 prepareData = None
345 beginSession = None
346 var failure = cleanupUi(None) { releaseSession() }
347 failure = cleanupUi(failure) { events.close() }
348 failure = cleanupUi(failure) { defaults.close() }
349 failure = cleanupUi(failure) { finished.close() }
350 throwUi(failure)
351 }