Scoop API

scoop:scoop.ui:0.1.0

src/interaction/drop_area.scoop

Selected production source · src/interaction/drop_area.scoop

1package scoop.ui
3import scoop.graphics.Point
4import scoop.surface.InputArea
5import scoop.window.*
7public struct DropAreaHandlers(
8 val offer: (DropOffer, Point) -> DropDecision,
9 val drop: (DropRequest, Point) -> Boolean,
10 val leave: () -> Unit = {}
13public struct DropHover(val position: Point, val format: DragFormat, val effect: DragEffect)
15/** A scope-owned receiver that borrows an existing input area. */
16public class DropArea internal constructor(
17 private val scope: ComponentScope,
18 internal val controller: RootDrops,
19 internal val area: InputArea,
20 handlers: DropAreaHandlers,
21 enabled: Boolean
22): Closeable {
23 private val visual = state<DropHover?>(None)
24 private var current: DropAreaHandlers? = Some(handlers)
25 private var accepts = enabled
26 private var closed = false
27 internal var revision = 0uL
29 public val hover: DropHover? get() = visual.value
30 public val isClosed: Boolean get() {
31 val mounted = scope.isMounted
32 return closed || !mounted || area.isClosed
33 }
34 internal val available: Boolean get() = !isClosed && accepts && area.scope.isInputAllowed
36 public fun update(handlers: DropAreaHandlers, enabled: Boolean = true) {
37 checkDropMutation()
38 if (isClosed)
39 throw IllegalStateException(Some("drop area is no longer mounted"))
40 current = Some(handlers)
41 accepts = enabled
42 revision += 1uL
43 controller.updated(this)
44 }
45 internal fun offer(value: DropOffer, point: Point): DropDecision = current!!.offer(value, point)
46 internal fun receive(value: DropRequest, point: Point): Boolean = current!!.drop(value, point)
47 internal fun leave() {
48 current!!.leave()
49 }
50 internal fun publish(value: DropHover?) {
51 if (visual.value == value)
52 return
53 visual.value = value
54 if (scope.isMounted)
55 scope.requestFrame()
56 }
57 public override fun close() {
58 scope.isMounted
59 checkDropMutation()
60 if (closed)
61 return
62 closed = true
63 current = None
64 revision += 1uL
65 controller.retire(this)
66 publish(None)
67 }
70internal fun checkDropMutation() {
71 when (currentEvaluation()) {
72 case Some(_) -> throw IllegalStateException(
73 Some("data drag bindings cannot change during render, measure, scene or draw")
74 )
75 case None -> {}
76 }