Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/drop_transfer.scoop

Selected production source · src/text/editor/drop_transfer.scoop

1package scoop.ui.text
3import scoop.surface.FocusOrigin
4import scoop.platform.system.OperatingSystem
5import scoop.platform.system.operatingSystem
6import scoop.window.*
8internal fun editorDropSource(data: Any?): EditorTextTransfer? =
9 when (data) {
10 case Some(val value) -> value as? EditorTextTransfer
11 case None -> None
12 }
14internal fun sameEditorDropSource(a: EditorTextTransfer?, b: EditorTextTransfer?): Boolean =
15 when ((a, b)) {
16 case(Some(val first), Some(val second)) -> first === second
17 case(None, None) -> true
18 else -> false
19 }
21internal fun editorDropEffect(offer: DropOffer, source: EditorTextTransfer?): DragEffect? {
22 val moving =
23 offer.effects.move &&
24 when (source) {
25 case Some(val value) -> value.canMove
26 case None -> true
27 }
28 val copyRequested =
29 if (operatingSystem == OperatingSystem.Darwin)
30 offer.modifiers.alt
31 else
32 offer.modifiers.control
33 if (moving && !(copyRequested && offer.effects.copy))
34 return Some(DragEffect.Move)
35 return if (offer.effects.copy)
36 Some(DragEffect.Copy)
37 else
38 None
41internal 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) -> value
50 case Url(val value) -> value
51 case Files(_) -> return false
52 }
53 val editor = host.current
54 var sameDocument = false
55 if (request.effect == DragEffect.Move) {
56 when (source) {
57 case Some(val value) -> {
58 if (!value.canMove || value.text != text)
59 return false
60 sameDocument = value.editor === editor
61 }
62 case None -> {}
63 }
64 }
65 val prepared =
66 if (sameDocument)
67 prepareEditorMove(source!!, position, host.options.policy, !host.options.multiline)
68 else
69 prepareEditorDrop(
70 editor,
71 position,
72 text,
73 host.options.policy,
74 !host.options.multiline,
75 request.effect == DragEffect.Move
76 )
77 val edit = prepared ?: return false
78 val movedHere = sameDocument
79 host.perform(
80 {
81 val changed = edit.publish(editor, false)
82 if (movedHere)
83 source!!.moved = true
84 changed
85 },
86 endComposition = true
87 )
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.isInputAllowed
97 )
98 host.area.requestFocus(FocusOrigin.Pointer)
99 return true