scoop:scoop.ui:0.1.0
src/text/editor/drop_transfer.scoop
Selected production source · src/text/editor/drop_transfer.scoop
1
package scoop.ui.text3
import scoop.surface.FocusOrigin4
import scoop.platform.system.OperatingSystem5
import scoop.platform.system.operatingSystem6
import scoop.window.*8
internal fun editorDropSource(data: Any?): EditorTextTransfer? =9
when (data) {10
case Some(val value) -> value as? EditorTextTransfer11
case None -> None12
}14
internal fun sameEditorDropSource(a: EditorTextTransfer?, b: EditorTextTransfer?): Boolean =15
when ((a, b)) {16
case(Some(val first), Some(val second)) -> first === second17
case(None, None) -> true18
else -> false19
}21
internal fun editorDropEffect(offer: DropOffer, source: EditorTextTransfer?): DragEffect? {22
val moving =23
offer.effects.move &&24
when (source) {25
case Some(val value) -> value.canMove26
case None -> true27
}28
val copyRequested =29
if (operatingSystem == OperatingSystem.Darwin)30
offer.modifiers.alt31
else32
offer.modifiers.control33
if (moving && !(copyRequested && offer.effects.copy))34
return Some(DragEffect.Move)35
return if (offer.effects.copy)36
Some(DragEffect.Copy)37
else38
None39
}41
internal fun receiveEditorDrop(42
host: TextEditHost,43
request: DropRequest,44
position: Long,45
source: EditorTextTransfer?46
): Boolean {47
val text =48
when (request.content) {49
case Text(val value) -> value50
case Url(val value) -> value51
case Files(_) -> return false52
}53
val editor = host.current54
var sameDocument = false55
if (request.effect == DragEffect.Move) {56
when (source) {57
case Some(val value) -> {58
if (!value.canMove || value.text != text)59
return false60
sameDocument = value.editor === editor61
}62
case None -> {}63
}64
}65
val prepared =66
if (sameDocument)67
prepareEditorMove(source!!, position, host.options.policy, !host.options.multiline)68
else69
prepareEditorDrop(70
editor,71
position,72
text,73
host.options.policy,74
!host.options.multiline,75
request.effect == DragEffect.Move76
)77
val edit = prepared ?: return false78
val movedHere = sameDocument79
host.perform(80
{81
val changed = edit.publish(editor, false)82
if (movedHere)83
source!!.moved = true84
changed85
},86
endComposition = true87
)88
if (89
!host.closed &&90
host.scope.isMounted &&91
host.current ===92
editor &&93
host.options.enabled &&94
host.options.acceptDrops &&95
!host.options.readOnly &&96
host.area.scope.isInputAllowed97
)98
host.area.requestFocus(FocusOrigin.Pointer)99
return true100
}