scoop:scoop.ui:0.1.0
src/text/drag_options.scoop
Selected production source · src/text/drag_options.scoop
1
package scoop.ui.text3
import scoop.ui.*4
import scoop.window.*6
/** Reports source deletion separately from the native destination's Move result. */7
public enum TextDragResult {8
Copied, Moved, SourceRetained, NotDropped, Failed(WindowException)9
}11
/** Selection export is independent of paste and drop acceptance. */12
public 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
}28
}30
/** Resolves one eligible text export for editor, selectable and custom text hosts. */31
public 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
}41
internal fun textDragAllowed(options: TextDragOptions, defaultCopy: Boolean): Boolean =42
options.enabled &&43
when (options.copyPolicy) {44
case Default -> defaultCopy45
case Allow -> true46
case Deny -> false47
}49
internal fun textDragResult(result: DragResult): TextDragResult =50
when (result) {51
case Dropped(DragEffect.Copy) -> TextDragResult.Copied52
case Dropped(DragEffect.Move) -> TextDragResult.Moved53
case Dropped(DragEffect.Link) -> TextDragResult.NotDropped54
case NotDropped -> TextDragResult.NotDropped55
case Failed(val error) -> TextDragResult.Failed(error)56
}