scoop:scoop.ui:0.1.0
src/interaction/presence.scoop
Selected production source · src/interaction/presence.scoop
1
package scoop.ui3
import scoop.surface.*5
public enum PresencePhase {6
Absent, Entering, Present, Exiting7
}9
public struct PresenceOptions(val enter: AnimationSpec? = None, val exit: AnimationSpec? = None)11
public struct PresenceSnapshot(val phase: PresencePhase, val progress: Double)13
/** A finite presentation transition on the mount's existing animation timeline. */14
public 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 = false23
private var closed = false25
init {26
update(initial, Some(options))27
}29
public val isClosed: Boolean get() {30
val mounted = scope.isMounted;31
return closed || !mounted32
}33
public val phase: PresencePhase get() = phaseState.value34
public val isPresent: Boolean get() = phase != PresencePhase.Absent35
public val snapshot: PresenceSnapshot get() {36
val current = phase37
if (closed)38
return PresenceSnapshot(PresencePhase.Absent, 0.0)39
val value = motion.value40
return PresenceSnapshot(41
current,42
if (value < 0.0)43
0.044
else45
if (value > 1.0)46
1.047
else48
value49
)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.options56
val end =57
if (open)58
PresencePhase.Present59
else60
PresencePhase.Absent61
val value =62
if (open)63
1.064
else65
0.066
val transition =67
if (open)68
this.options.enter69
else70
this.options.exit71
if (desired == open) {72
when (transition) {73
case None -> if (74
phase == PresencePhase.Entering || phase == PresencePhase.Exiting75
) {76
motion.snapTo(value)77
setPhase(end)78
}79
case Some(_) -> {}80
}81
return82
}83
desired = open84
when (transition) {85
case Some(val spec) -> {86
setPhase(87
if (open)88
PresencePhase.Entering89
else90
PresencePhase.Exiting91
)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
return113
phaseState.value = value114
changed()115
}116
internal fun finishImmediately() {117
if (closed || phase == PresencePhase.Absent)118
return119
desired = false120
if (motion.state != AnimationState.Closed)121
motion.snapTo(0.0)122
prepareUi { setPhase(PresencePhase.Absent) }123
}124
public override fun close() {125
scope.isMounted126
checkOverlayMutation()127
if (closed)128
return129
closed = true130
phaseState.value = PresencePhase.Absent131
motion.close()132
}133
}135
public fun ComponentScope.presence(136
open: Boolean = false,137
options: PresenceOptions = PresenceOptions()138
): Presence = resource { Presence(this, open, options) }