Scoop API

scoop:scoop.ui:0.1.0

src/interaction/presence.scoop

Selected production source · src/interaction/presence.scoop

1package scoop.ui
3import scoop.surface.*
5public enum PresencePhase {
6 Absent, Entering, Present, Exiting
7}
9public struct PresenceOptions(val enter: AnimationSpec? = None, val exit: AnimationSpec? = None)
11public struct PresenceSnapshot(val phase: PresencePhase, val progress: Double)
13/** A finite presentation transition on the mount's existing animation timeline. */
14public class Presence internal constructor(
15 private val scope: ComponentScope,
16 initial: Boolean,
17 private var options: PresenceOptions,
18 private val changed: () -> Unit = {}
19): Closeable {
20 private val phaseState = state(PresencePhase.Absent)
21 private val motion = scope.animation(0.0)
22 private var desired = false
23 private var closed = false
25 init {
26 update(initial, Some(options))
27 }
29 public val isClosed: Boolean get() {
30 val mounted = scope.isMounted;
31 return closed || !mounted
32 }
33 public val phase: PresencePhase get() = phaseState.value
34 public val isPresent: Boolean get() = phase != PresencePhase.Absent
35 public val snapshot: PresenceSnapshot get() {
36 val current = phase
37 if (closed)
38 return PresenceSnapshot(PresencePhase.Absent, 0.0)
39 val value = motion.value
40 return PresenceSnapshot(
41 current,
42 if (value < 0.0)
43 0.0
44 else
45 if (value > 1.0)
46 1.0
47 else
48 value
49 )
50 }
51 public fun update(open: Boolean, options: PresenceOptions? = None) {
52 checkOverlayMutation()
53 if (isClosed)
54 throw IllegalStateException(Some("presence is no longer mounted"))
55 this.options = options ?: this.options
56 val end =
57 if (open)
58 PresencePhase.Present
59 else
60 PresencePhase.Absent
61 val value =
62 if (open)
63 1.0
64 else
65 0.0
66 val transition =
67 if (open)
68 this.options.enter
69 else
70 this.options.exit
71 if (desired == open) {
72 when (transition) {
73 case None -> if (
74 phase == PresencePhase.Entering || phase == PresencePhase.Exiting
75 ) {
76 motion.snapTo(value)
77 setPhase(end)
78 }
79 case Some(_) -> {}
80 }
81 return
82 }
83 desired = open
84 when (transition) {
85 case Some(val spec) -> {
86 setPhase(
87 if (open)
88 PresencePhase.Entering
89 else
90 PresencePhase.Exiting
91 )
92 motion.animateTo(
93 value,
94 spec,
95 onFinished =
96 Some(
97 {
98 _ -> if (!closed)
99 setPhase(end)
100 }
101 )
102 )
103 }
104 case None -> {
105 motion.snapTo(value)
106 setPhase(end)
107 }
108 }
109 }
110 private fun setPhase(value: PresencePhase) {
111 if (phaseState.value == value)
112 return
113 phaseState.value = value
114 changed()
115 }
116 internal fun finishImmediately() {
117 if (closed || phase == PresencePhase.Absent)
118 return
119 desired = false
120 if (motion.state != AnimationState.Closed)
121 motion.snapTo(0.0)
122 prepareUi { setPhase(PresencePhase.Absent) }
123 }
124 public override fun close() {
125 scope.isMounted
126 checkOverlayMutation()
127 if (closed)
128 return
129 closed = true
130 phaseState.value = PresencePhase.Absent
131 motion.close()
132 }
135public fun ComponentScope.presence(
136 open: Boolean = false,
137 options: PresenceOptions = PresenceOptions()
138): Presence = resource { Presence(this, open, options) }