Scoop API

scoop:scoop.window:0.1.0

src/drag_drop/target.scoop

Selected production source · src/drag_drop/target.scoop

1package scoop.window
3internal interface NativeDropTarget: Closeable {
4 fun update(enabled: Boolean)
5 fun setPeriodicUpdates(value: Boolean)
6 fun leave()
7}
9/** One native destination owned by a window; the renderer supplies its own hit routing. */
10public class DropTarget internal constructor(
11 internal val owner: WindowOwner,
12 initial: DropHandlers
13): Closeable {
14 private var current: DropHandlers? = Some(initial)
15 private var accepts = true
16 private var periodic = false
17 private var closed = false
18 private var native: NativeDropTarget? = None
20 internal fun bind(value: NativeDropTarget) {
21 native = Some(value)
22 }
23 public val isClosed: Boolean get() {
24 owner.application.checkThread();
25 return closed
26 }
27 public var handlers: DropHandlers
28 get() {
29 checkOpen();
30 return current!!
31 }
32 set(value) {
33 checkOpen()
34 current = Some(value)
35 native!!.update(accepts)
36 }
37 public var enabled: Boolean
38 get() {
39 checkOpen();
40 return accepts
41 }
42 set(value) {
43 checkOpen()
44 if (value == accepts)
45 return
46 accepts = value
47 native!!.update(value)
48 if (!value)
49 owner.application.deferNativeOperation {
50 if (!closed && !accepts)
51 native!!.leave()
52 }
53 }
54 /** Requests native updates during an active drag even while the pointer is stationary. */
55 public var periodicUpdates: Boolean
56 get() {
57 checkOpen();
58 return periodic
59 }
60 set(value) {
61 checkOpen()
62 if (periodic != value) {
63 periodic = value
64 native!!.setPeriodicUpdates(value)
65 }
66 }
67 private fun checkOpen() {
68 owner.requireOpen()
69 if (closed)
70 throw IllegalStateException(Some("drop target is closed"))
71 }
72 internal fun perform(action: () -> Int, leaving: Boolean = false): Int {
73 val application = owner.application
74 if (
75 !application.isCurrentThread() ||
76 closed ||
77 application.exiting ||
78 owner.phase !=
79 WindowState.Open
80 )
81 return 0
82 if (
83 !leaving &&
84 (
85 !accepts ||
86 owner.inputBlocked ||
87 !owner.requestedVisible ||
88 owner.visibilitySuppressed
89 )
90 )
91 return 0
92 val nativeOperation =
93 application.nativeOperationDepth == 1 && application.nativeCallbackDepth == 1
94 if (
95 application.dispatchDepth !=
96 0 ||
97 application.pumping ||
98 (application.nativeCallbackDepth != 0 && !nativeOperation)
99 )
100 return 0
101 application.dispatchDepth += 1
102 try {
103 return action()
104 } catch (error: Throwable) {
105 application.recordFailure(error)
106 application.beginExit()
107 return 0
108 } finally {
109 application.dispatchDepth -= 1
110 if (application.exiting)
111 application.mailbox.wake()
112 }
113 }
114 internal fun offer(value: DropOffer): Int = dropDecisionCode(value, current!!.offer(value))
115 internal fun drop(value: DropRequest): Int =
116 if (current!!.drop(value))
117 1
118 else
119 0
120 internal fun leave(): Int {
121 current!!.leave();
122 return 0
123 }
124 public override fun close() {
125 owner.application.checkThread()
126 if (closed)
127 return
128 closed = true
129 current = None
130 owner.dropTarget = None
131 native?.close()
132 native = None
133 }