Scoop API

scoop:scoop.ui:0.1.0

src/text/drag_options.scoop

Selected production source · src/text/drag_options.scoop

1package scoop.ui.text
3import scoop.ui.*
4import scoop.window.*
6/** Reports source deletion separately from the native destination's Move result. */
7public enum TextDragResult {
8 Copied, Moved, SourceRetained, NotDropped, Failed(WindowException)
9}
11/** Selection export is independent of paste and drop acceptance. */
12public class TextDragOptions public constructor(
13 public val enabled: Boolean = true,
14 public val threshold: Double = 4.0,
15 public val effects: DragEffects = DragEffects(true, true, false),
16 public val copyPolicy: TextCopyPolicy = TextCopyPolicy.Default,
17 /** Uses the platform default when the callback returns None; absent draws the visible selection. */
18 public val preview: ((DragSourceStart, String) -> DragPreview?)? = None,
19 public val onCompleted: (TextDragResult) -> Unit = {}
20) {
21 internal fun check() {
22 requireUi(threshold.isFinite() && threshold >= 0.0, "invalid text drag threshold")
23 requireUi(
24 !effects.link && (effects.copy || effects.move),
25 "text dragging requires Copy or Move"
26 )
27 }
30/** Resolves one eligible text export for editor, selectable and custom text hosts. */
31public fun TextDragOptions.preparePreview(
32 start: DragSourceStart,
33 text: String,
34 selectionImage: () -> DragPreview?
35): DragPreview? =
36 when (preview) {
37 case Some(val custom) -> custom(start, text)
38 case None -> selectionImage()
39 }
41internal fun textDragAllowed(options: TextDragOptions, defaultCopy: Boolean): Boolean =
42 options.enabled &&
43 when (options.copyPolicy) {
44 case Default -> defaultCopy
45 case Allow -> true
46 case Deny -> false
47 }
49internal fun textDragResult(result: DragResult): TextDragResult =
50 when (result) {
51 case Dropped(DragEffect.Copy) -> TextDragResult.Copied
52 case Dropped(DragEffect.Move) -> TextDragResult.Moved
53 case Dropped(DragEffect.Link) -> TextDragResult.NotDropped
54 case NotDropped -> TextDragResult.NotDropped
55 case Failed(val error) -> TextDragResult.Failed(error)
56 }