Scoop API

scoop:scoop.window:0.1.0

src/drag_drop/native_target.scoop

Selected production source · src/drag_drop/native_target.scoop

1package scoop.window
3import scoop.platform.fs.Path
5/** Registers before or after window configuration. Payloads are read only during drop. */
6public fun Window.registerDropTarget(handlers: DropHandlers): DropTarget {
7 owner.requireOpen()
8 when (owner.dropTarget) {
9 case Some(_) -> throw IllegalStateException(Some("window already has a drop target"))
10 case None -> {}
11 }
12 val target = DropTarget(owner, handlers)
13 @Unsafe
14 {
15 val callback =
16 foreignCallback<
17 (Ptr<Unit>, Int, Int, Int, Int, Double, Double, Long, Int, Ptr<Unit>?) -> Int
18 >(
19 {
20 phase: Int,
21 formats: Int,
22 effects: Int,
23 local: Int,
24 x: Double,
25 y: Double,
26 modifiers: Long,
27 choice: Int,
28 payload: Ptr<Unit>? -> target.perform(
29 {
30 if (phase == 2)
31 return@perform target.leave()
32 val offer =
33 DropOffer(
34 DragFormats(bit(formats, 1), bit(formats, 2), bit(formats, 4)),
35 DragEffects(bit(effects, 1), bit(effects, 2), bit(effects, 4)),
36 LogicalPosition(x, y),
37 platformModifiers(modifiers),
38 local != 0,
39 if (bit(local, 2))
40 when (val source = owner.application.dragSource) {
41 case Some(val session) -> session.localData
42 case None -> None
43 }
44 else
45 None
46 )
47 if (phase == 0)
48 return@perform target.offer(offer)
49 if (phase != 1 || payload == None)
50 return@perform 0
51 val effect =
52 when (choice % 256) {
53 case 1 -> DragEffect.Copy
54 case 2 -> DragEffect.Move
55 case 4 -> DragEffect.Link
56 case _ -> return@perform 0
57 }
58 when (val content = dropContent(payload!!, choice / 256)) {
59 case Some(val value) -> target.drop(
60 DropRequest(offer, effect, value)
61 )
62 case None -> 0
63 }
64 },
65 leaving = phase == 2
66 )
67 },
68 0,
69 ForeignCallbackMode.Reusable
70 )
71 try {
72 val pointer =
73 created(
74 nativeDropCreate(owner.pointer!!, callback.function, callback.context),
75 "register drop target"
76 )
77 target.bind(PlatformDropTarget(owner.application, pointer, callback))
78 owner.dropTarget = Some(target)
79 return target
80 } catch (error: Throwable) {
81 releaseForeignCallback(callback)
82 throw error
83 }
84 }
87private fun bit(value: Int, mask: Int): Boolean = (value / mask) % 2 != 0
89private class PlatformDropTarget(
90 private val application: ApplicationOwner,
91 private val pointer: Ptr<Unit>,
92 private val callback: ForeignCallback<
93 (Ptr<Unit>, Int, Int, Int, Int, Double, Double, Long, Int, Ptr<Unit>?) -> Int
94 >
95): NativeDropTarget {
96 public override fun setPeriodicUpdates(value: Boolean) {
97 @Unsafe
98 {
99 nativeDropPeriodic(
100 pointer,
101 if (value)
102 1
103 else
104 0
105 )
106 }
107 }
108 public override fun update(enabled: Boolean) {
109 @Unsafe
110 {
111 nativeDropUpdate(
112 pointer,
113 if (enabled)
114 1
115 else
116 0
117 )
118 }
119 }
120 public override fun leave() {
121 @Unsafe
122 {
123 nativeDropLeave(pointer)
124 }
125 }
126 public override fun close() {
127 @Unsafe
128 {
129 nativeDropClose(pointer)
130 }
131 val owned = callback
132 application.mailbox.postControl {
133 @Unsafe
134 {
135 releaseForeignCallback(owned)
136 }
137 }
138 }
141private fun dropContent(pointer: Ptr<Unit>, format: Int): DragContent? {
142 @Unsafe
143 {
144 val count = nativeDropItemCount(pointer)
145 if (format == 1 || format == 2) {
146 if (count != 1L)
147 return None
148 when (val text = dropText(nativeDropText(pointer, 0L))) {
149 case Some(val value) -> return Some(
150 if (format == 1)
151 DragContent.Text(value)
152 else
153 DragContent.Url(value)
154 )
155 case None -> return None
156 }
157 }
158 if (format != 3 || count <= 0L)
159 return None
160 val paths = ArrayList<Path>()
161 for (index in 0L ..< count) {
162 val value = nativeDropPath(pointer, index)
163 if (value.data == None || value.length <= 0L)
164 return None
165 paths.add(Path.fromOsString(copyDialogString(value)))
166 }
167 return Some(DragContent.Files(paths.toArray()))
168 }
171@Unsafe
172private fun dropText(value: NativeDropText): String? {
173 if (value.length < 0L)
174 return None
175 if (value.length == 0L)
176 return Some("")
177 when (val data = value.data) {
178 case Some(val bytes) -> {
179 try {
180 return Some(String.fromUtf8(bytes, value.length))
181 } catch (error: CharacterCodingException) {
182 return None
183 }
184 }
185 case None -> return None
186 }
189@CLayout
190private struct NativeDropText(val data: Ptr<UInt8>?, val length: Long)
192@Extern(name = "scoop_window_drop_create")
193private fun nativeDropCreate(
194 window: Ptr<Unit>,
195 callback: FunPtr<(Ptr<Unit>, Int, Int, Int, Int, Double, Double, Long, Int, Ptr<Unit>?) -> Int>,
196 context: Ptr<Unit>
197): NativeCreateResult
199@Extern(name = "scoop_window_drop_update")
200private fun nativeDropUpdate(target: Ptr<Unit>, enabled: Int)
202@Extern(name = "scoop_window_drop_leave")
203private fun nativeDropLeave(target: Ptr<Unit>)
205@Extern(name = "scoop_window_drop_periodic")
206private fun nativeDropPeriodic(target: Ptr<Unit>, enabled: Int)
208@Extern(name = "scoop_window_drop_close")
209private fun nativeDropClose(target: Ptr<Unit>)
211@Extern(name = "scoop_window_drop_item_count")
212private fun nativeDropItemCount(payload: Ptr<Unit>): Long
214@Extern(name = "scoop_window_drop_text")
215private fun nativeDropText(payload: Ptr<Unit>, index: Long): NativeDropText
217@Extern(name = "scoop_window_drop_path")
218private fun nativeDropPath(payload: Ptr<Unit>, index: Long): NativeFilePath