Scoop API

scoop:scoop.window:0.1.0

src/drag_drop/native_source.scoop

Selected production source · src/drag_drop/native_source.scoop

1package scoop.window
3/** Begins native data dragging from the currently dispatched mouse or direct-contact input. */
4public fun Window.beginDrag(
5 content: DragContent,
6 effects: DragEffects = DragEffects(true, false, false),
7 localData: Any? = None,
8 preview: DragPreview? = None,
9 onCompleted: (DragResult) -> Unit = {}
10): DragSession {
11 owner.requireInteractive()
12 val input =
13 owner.currentInput ?: throw WindowException("begin data drag", WindowErrorKind.InvalidState)
14 val pointer =
15 input.pointer ?: throw WindowException("begin data drag", WindowErrorKind.InvalidState)
16 if (owner.systemOperationPending)
17 throw WindowException("begin data drag", WindowErrorKind.ResourceBusy)
18 when (owner.application.dragSource) {
19 case Some(_) -> throw WindowException("begin data drag", WindowErrorKind.ResourceBusy)
20 case None -> {}
21 }
22 checkDragContent(content, effects)
23 val session = DragSession(owner, effects, localData, onCompleted)
24 session.bind(prepareDragSource(owner, pointer, content, effects, preview, session))
25 session.schedule(input.time)
26 return session
29private fun prepareDragSource(
30 owner: WindowOwner,
31 input: Ptr<Unit>,
32 content: DragContent,
33 effects: DragEffects,
34 preview: DragPreview?,
35 session: DragSession
36): NativeDragSource {
37 @Unsafe
38 {
39 val callback =
40 foreignCallback<(Ptr<Unit>, Int, Int) -> Int>(
41 { phase: Int, effect: Int -> session.nativeEvent(phase, effect) },
42 0,
43 ForeignCallbackMode.Reusable
44 )
45 var allocated: Ptr<Unit>? = None
46 try {
47 val mask =
48 (
49 if (effects.copy)
50 1
51 else
52 0
53 ) +
54 (
55 if (effects.move)
56 2
57 else
58 0
59 ) +
60 (
61 if (effects.link)
62 4
63 else
64 0
65 )
66 val pointer =
67 created(
68 nativeDragCreate(
69 owner.pointer!!,
70 input,
71 mask,
72 callback.function,
73 callback.context
74 ),
75 "prepare data drag"
76 )
77 allocated = Some(pointer)
78 when (content) {
79 case Text(val value) -> addDragText(pointer, 1, value)
80 case Url(val value) -> addDragText(pointer, 2, value)
81 case Files(val paths) -> for (path in paths) {
82 val status =
83 withDialogString(path.toOsString()) {
84 bytes,
85 count -> @Unsafe
86 {
87 return@withDialogString nativeDragAddPath(pointer, bytes, count)
88 }
89 }
90 checkDragContent(status)
91 }
92 }
93 when (preview) {
94 case Some(val value) -> {
95 val metrics =
96 platformObservedMetrics(owner) ?:
97 throw WindowException(
98 "data drag preview metrics",
99 WindowErrorKind.InvalidState
100 )
101 prepareDragPreview(pointer, value, metrics.scaleFactor)
102 }
103 case None -> {}
104 }
105 checkNativeStatus(nativeDragReady(pointer), "prepare data drag")
106 return PlatformDragSource(owner.application, pointer, callback)
107 } catch (error: Throwable) {
108 when (allocated) {
109 case Some(val value) -> nativeDragClose(value)
110 case None -> {}
111 }
112 releaseForeignCallback(callback)
113 throw error
114 }
115 }
118private fun addDragText(pointer: Ptr<Unit>, format: Int, text: String) {
119 @Unsafe
120 {
121 val status =
122 text.withUtf8Bytes {
123 bytes,
124 count -> @Unsafe
125 {
126 return@withUtf8Bytes nativeDragAdd(pointer, format, bytes, count)
127 }
128 }
129 checkDragContent(status)
130 }
133private fun checkDragContent(status: Long) {
134 if (status == -8L)
135 throw IllegalArgumentException(Some("drag content cannot be represented by the platform"))
136 checkNativeStatus(status, "copy drag content")
139private class PlatformDragSource(
140 private val application: ApplicationOwner,
141 private val pointer: Ptr<Unit>,
142 private val callback: ForeignCallback<(Ptr<Unit>, Int, Int) -> Int>
143): NativeDragSource {
144 public override fun setEffects(value: DragEffects) {
145 val mask =
146 (
147 if (value.copy)
148 1
149 else
150 0
151 ) +
152 (
153 if (value.move)
154 2
155 else
156 0
157 ) +
158 (
159 if (value.link)
160 4
161 else
162 0
163 )
164 @Unsafe
165 {
166 checkNativeStatus(nativeDragEffects(pointer, mask), "update drag effects")
167 }
168 }
169 public override fun start() {
170 @Unsafe
171 {
172 checkNativeStatus(nativeDragStart(pointer), "start data drag")
173 }
174 }
175 public override fun close() {
176 @Unsafe
177 {
178 nativeDragClose(pointer)
179 }
180 val owned = callback
181 application.mailbox.postControl {
182 @Unsafe
183 {
184 releaseForeignCallback(owned)
185 }
186 }
187 }
190@Extern(name = "scoop_window_drag_create")
191private fun nativeDragCreate(
192 window: Ptr<Unit>,
193 input: Ptr<Unit>,
194 effects: Int,
195 callback: FunPtr<(Ptr<Unit>, Int, Int) -> Int>,
196 context: Ptr<Unit>
197): NativeCreateResult
199@Extern(name = "scoop_window_drag_add")
200private fun nativeDragAdd(source: Ptr<Unit>, format: Int, bytes: Ptr<UInt8>, length: Long): Long
202@Extern(name = "scoop_window_drag_add_path")
203private fun nativeDragAddPath(source: Ptr<Unit>, bytes: Ptr<Byte>, length: Long): Long
205@Extern(name = "scoop_window_drag_ready")
206private fun nativeDragReady(source: Ptr<Unit>): Long
208@Extern(name = "scoop_window_drag_start")
209private fun nativeDragStart(source: Ptr<Unit>): Long
211@Extern(name = "scoop_window_drag_effects")
212private fun nativeDragEffects(source: Ptr<Unit>, effects: Int): Long
214@Extern(name = "scoop_window_drag_close")
215private fun nativeDragClose(source: Ptr<Unit>)