Scoop API

scoop:scoop.window:0.1.0

src/drag_drop/source.scoop

Selected production source · src/drag_drop/source.scoop

1package scoop.window
3internal interface NativeDragSource: Closeable {
4 fun start()
5 fun setEffects(value: DragEffects)
6}
8/** Closing disconnects callbacks; it does not claim to cancel an active system drag. */
9public class DragSession internal constructor(
10 private val owner: WindowOwner,
11 private val initialEffects: DragEffects,
12 localData: Any?,
13 onCompleted: (DragResult) -> Unit
14): Closeable {
15 private var native: NativeDragSource? = None
16 private var completion: ((DragResult) -> Unit)? = Some(onCompleted)
17 private var pending = true
18 private var requested = false
19 private var started = false
20 private var queued = false
21 private var closed = false
22 private var outcome: DragResult? = None
23 internal var localData = localData
24 private var allowedEffects = initialEffects
26 /** Changes future negotiation without rewriting a destination result already completed. */
27 public var effects: DragEffects
28 get() {
29 owner.application.checkThread()
30 return allowedEffects
31 }
32 set(value) {
33 owner.application.checkThread()
34 if (closed || queued)
35 throw IllegalStateException(Some("drag session no longer accepts effect updates"))
36 requireWindow(
37 (!value.copy || initialEffects.copy) &&
38 (!value.move || initialEffects.move) &&
39 (!value.link || initialEffects.link),
40 "drag effects must have been offered at creation"
41 )
42 if (value != allowedEffects) {
43 native!!.setEffects(value)
44 allowedEffects = value
45 }
46 }
48 public val isStarted: Boolean get() {
49 owner.application.checkThread();
50 return started
51 }
52 public val isClosed: Boolean get() {
53 owner.application.checkThread();
54 return closed
55 }
56 public val result: DragResult? get() {
57 owner.application.checkThread();
58 return outcome
59 }
61 internal fun bind(value: NativeDragSource) {
62 native = Some(value)
63 }
65 internal fun schedule(time: InputTime) {
66 owner.dragSource = Some(this)
67 owner.application.dragSource = Some(this)
68 owner.systemOperationPending = true
69 owner.systemOperationCancelled = false
70 owner.pendingSystemCancellation = Some(time)
71 owner.leases += 1L
72 owner.application.deferNativeOperation {
73 try {
74 start()
75 } finally {
76 owner.systemOperationPending = false
77 owner.leases -= 1L
78 owner.application.mailbox.wake()
79 }
80 }
81 }
83 private fun start() {
84 if (closed || !pending || requested)
85 return
86 if (
87 owner.phase !=
88 WindowState.Open ||
89 owner.application.exiting ||
90 owner.inputBlocked ||
91 owner.systemOperationCancelled ||
92 !owner.requestedVisible ||
93 owner.visibilitySuppressed
94 ) {
95 cancelPending()
96 return
97 }
98 requested = true
99 try {
100 native!!.start()
101 } catch (error: WindowException) {
102 complete(DragResult.Failed(error))
103 }
104 }
106 internal fun nativeEvent(phase: Int, effect: Int): Int {
107 if (!owner.application.isCurrentThread() || closed || queued)
108 return 0
109 if (phase == 0) {
110 pending = false
111 started = true
112 } else
113 if (phase == 1) {
114 complete(
115 when (effect) {
116 case 1 -> DragResult.Dropped(DragEffect.Copy)
117 case 2 -> DragResult.Dropped(DragEffect.Move)
118 case 4 -> DragResult.Dropped(DragEffect.Link)
119 else -> DragResult.NotDropped
120 }
121 )
122 } else
123 if (phase == 2) {
124 complete(
125 DragResult.Failed(
126 WindowException(
127 "start data drag",
128 WindowErrorKind.NativeFailure,
129 nativeCode = Some(effect.toLong())
130 )
131 )
132 )
133 }
134 return 0
135 }
137 private fun complete(value: DragResult) {
138 if (closed || queued)
139 return
140 outcome = Some(value)
141 pending = false
142 queued = true
143 if (
144 !owner.application.mailbox.post {
145 if (!closed) {
146 val action = completion
147 close()
148 action?.invoke(value)
149 }
150 }
151 )
152 close()
153 }
155 /** Cancels only work that has not entered the system drag session. */
156 public fun cancelPending(): Boolean {
157 owner.application.checkThread()
158 if (closed || !pending || queued)
159 return false
160 pending = false
161 releaseNative()
162 complete(DragResult.NotDropped)
163 return true
164 }
166 private fun releaseNative() {
167 val value = native
168 native = None
169 value?.close()
170 }
172 public override fun close() {
173 owner.application.checkThread()
174 if (closed)
175 return
176 closed = true
177 pending = false
178 completion = None
179 localData = None
180 when (val current = owner.dragSource) {
181 case Some(val value) -> if (value === this)
182 owner.dragSource = None
183 case None -> {}
184 }
185 when (val current = owner.application.dragSource) {
186 case Some(val value) -> if (value === this)
187 owner.application.dragSource = None
188 case None -> {}
189 }
190 releaseNative()
191 }