scoop:scoop.window:0.1.0
src/drag_drop/native_source.scoop
Selected production source · src/drag_drop/native_source.scoop
1
package scoop.window3
/** Begins native data dragging from the currently dispatched mouse or direct-contact input. */4
public 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 session27
}29
private fun prepareDragSource(30
owner: WindowOwner,31
input: Ptr<Unit>,32
content: DragContent,33
effects: DragEffects,34
preview: DragPreview?,35
session: DragSession36
): NativeDragSource {37
@Unsafe38
{39
val callback =40
foreignCallback<(Ptr<Unit>, Int, Int) -> Int>(41
{ phase: Int, effect: Int -> session.nativeEvent(phase, effect) },42
0,43
ForeignCallbackMode.Reusable44
)45
var allocated: Ptr<Unit>? = None46
try {47
val mask =48
(49
if (effects.copy)50
151
else52
053
) +54
(55
if (effects.move)56
257
else58
059
) +60
(61
if (effects.link)62
463
else64
065
)66
val pointer =67
created(68
nativeDragCreate(69
owner.pointer!!,70
input,71
mask,72
callback.function,73
callback.context74
),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 -> @Unsafe86
{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.InvalidState100
)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 error114
}115
}116
}118
private fun addDragText(pointer: Ptr<Unit>, format: Int, text: String) {119
@Unsafe120
{121
val status =122
text.withUtf8Bytes {123
bytes,124
count -> @Unsafe125
{126
return@withUtf8Bytes nativeDragAdd(pointer, format, bytes, count)127
}128
}129
checkDragContent(status)130
}131
}133
private 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")137
}139
private 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
1149
else150
0151
) +152
(153
if (value.move)154
2155
else156
0157
) +158
(159
if (value.link)160
4161
else162
0163
)164
@Unsafe165
{166
checkNativeStatus(nativeDragEffects(pointer, mask), "update drag effects")167
}168
}169
public override fun start() {170
@Unsafe171
{172
checkNativeStatus(nativeDragStart(pointer), "start data drag")173
}174
}175
public override fun close() {176
@Unsafe177
{178
nativeDragClose(pointer)179
}180
val owned = callback181
application.mailbox.postControl {182
@Unsafe183
{184
releaseForeignCallback(owned)185
}186
}187
}188
}190
@Extern(name = "scoop_window_drag_create")191
private 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
): NativeCreateResult199
@Extern(name = "scoop_window_drag_add")200
private fun nativeDragAdd(source: Ptr<Unit>, format: Int, bytes: Ptr<UInt8>, length: Long): Long202
@Extern(name = "scoop_window_drag_add_path")203
private fun nativeDragAddPath(source: Ptr<Unit>, bytes: Ptr<Byte>, length: Long): Long205
@Extern(name = "scoop_window_drag_ready")206
private fun nativeDragReady(source: Ptr<Unit>): Long208
@Extern(name = "scoop_window_drag_start")209
private fun nativeDragStart(source: Ptr<Unit>): Long211
@Extern(name = "scoop_window_drag_effects")212
private fun nativeDragEffects(source: Ptr<Unit>, effects: Int): Long214
@Extern(name = "scoop_window_drag_close")215
private fun nativeDragClose(source: Ptr<Unit>)