scoop:scoop.window:0.1.0
src/drag_drop/source.scoop
Selected production source · src/drag_drop/source.scoop
1
package scoop.window3
internal 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. */9
public class DragSession internal constructor(10
private val owner: WindowOwner,11
private val initialEffects: DragEffects,12
localData: Any?,13
onCompleted: (DragResult) -> Unit14
): Closeable {15
private var native: NativeDragSource? = None16
private var completion: ((DragResult) -> Unit)? = Some(onCompleted)17
private var pending = true18
private var requested = false19
private var started = false20
private var queued = false21
private var closed = false22
private var outcome: DragResult? = None23
internal var localData = localData24
private var allowedEffects = initialEffects26
/** Changes future negotiation without rewriting a destination result already completed. */27
public var effects: DragEffects28
get() {29
owner.application.checkThread()30
return allowedEffects31
}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 = value45
}46
}48
public val isStarted: Boolean get() {49
owner.application.checkThread();50
return started51
}52
public val isClosed: Boolean get() {53
owner.application.checkThread();54
return closed55
}56
public val result: DragResult? get() {57
owner.application.checkThread();58
return outcome59
}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 = true69
owner.systemOperationCancelled = false70
owner.pendingSystemCancellation = Some(time)71
owner.leases += 1L72
owner.application.deferNativeOperation {73
try {74
start()75
} finally {76
owner.systemOperationPending = false77
owner.leases -= 1L78
owner.application.mailbox.wake()79
}80
}81
}83
private fun start() {84
if (closed || !pending || requested)85
return86
if (87
owner.phase !=88
WindowState.Open ||89
owner.application.exiting ||90
owner.inputBlocked ||91
owner.systemOperationCancelled ||92
!owner.requestedVisible ||93
owner.visibilitySuppressed94
) {95
cancelPending()96
return97
}98
requested = true99
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 0109
if (phase == 0) {110
pending = false111
started = true112
} else113
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.NotDropped120
}121
)122
} else123
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 0135
}137
private fun complete(value: DragResult) {138
if (closed || queued)139
return140
outcome = Some(value)141
pending = false142
queued = true143
if (144
!owner.application.mailbox.post {145
if (!closed) {146
val action = completion147
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 false160
pending = false161
releaseNative()162
complete(DragResult.NotDropped)163
return true164
}166
private fun releaseNative() {167
val value = native168
native = None169
value?.close()170
}172
public override fun close() {173
owner.application.checkThread()174
if (closed)175
return176
closed = true177
pending = false178
completion = None179
localData = None180
when (val current = owner.dragSource) {181
case Some(val value) -> if (value === this)182
owner.dragSource = None183
case None -> {}184
}185
when (val current = owner.application.dragSource) {186
case Some(val value) -> if (value === this)187
owner.application.dragSource = None188
case None -> {}189
}190
releaseNative()191
}192
}