scoop:scoop.ui.component:0.1.0
src/toast/card_control.scoop
Selected production source · src/toast/card_control.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.*5
import scoop.surface.*6
import scoop.ui.*7
import scoop.window.*9
internal class ToastCardControl(val scope: ComponentScope, val read: () -> ToastItemInput) {10
val area =11
scope.createArea(focusable = true) {12
context,13
_ -> if (context.phase == RoutedPhase.Notification && active)14
read().host.refresh()15
}16
val focus = scope.focusTarget(area, FocusOptions(tabStop = false))17
val offset = scope.animation(0.0)18
val origin = scope.animation(Point(0.0, 0.0))19
val zoom = scope.animation(1.0)20
var geometry: ToastGeometry? = None21
var ready = false22
var dragging = false23
private var dragOrigin = 0.024
private var retired = false25
private val drag =26
scope.drag(area, options(), { _ -> available() }, { event -> dragged(event) })27
val active: Boolean get() = !retired && scope.isMounted28
init {29
scope.onDispose { retire() }30
scope.routeChildren(area)31
read().entry.card = Some(this)32
scope.resource {33
area.scope.observeActivity {34
if (active)35
read().host.refresh()36
}37
}38
scope.resource {39
area.scope.addDefaultAction {40
context,41
event -> when (event) {42
case Keyboard(val key) -> {43
if (44
sliderKeyAllowed(key) &&45
!key.isRepeat &&46
key.logicalKey ==47
LogicalKey.Named(NamedKey.Escape)48
) {49
if (dragging) {50
context.consume();51
cancel()52
}53
else54
if (read().entry.data.value.dismiss.escape) {55
context.consume();56
read().entry.model.close(57
read().entry.id,58
ToastCloseReason.Escape59
)60
}61
}62
}63
else -> {}64
}65
}66
}67
}68
private fun options(): DragOptions =69
DragOptions(70
if (71
toastSide(read().position, scope.layoutDirection) ==72
OverlaySide.Top ||73
toastSide(read().position, scope.layoutDirection) ==74
OverlaySide.Bottom75
)76
DragAxis.Vertical77
else78
DragAxis.Horizontal,79
4.0 * read().density,80
read().props.enabled &&81
read().entry.closing.value ==82
None &&83
read().entry.data.value.dismiss.swipe84
)85
private fun available(): Boolean =86
active &&87
ready &&88
read().props.enabled &&89
read().entry.closing.value ==90
None &&91
read().entry.data.value.dismiss.swipe &&92
area.scope.isInputAllowed93
fun update() {94
drag.update(options(), { _ -> available() }, { event -> dragged(event) })95
if (!panelPresent(toastTransition(read()))) {96
if (offset.state == AnimationState.Running)97
offset.snapTo(offset.target)98
if (origin.state == AnimationState.Running)99
origin.snapTo(origin.target)100
if (zoom.state == AnimationState.Running)101
zoom.snapTo(zoom.target)102
}103
read().host.refresh()104
}105
fun accept(shape: ToastGeometry, phase: PresencePhase) {106
if (!active)107
return108
val old = geometry109
val changed =110
when (old) {111
case Some(val value) -> !toastSameGeometry(value, shape);112
case None -> true113
}114
if (changed) {115
if (panelPresent(old))116
cancel()117
if (!active)118
return119
geometry = Some(shape)120
val spec =121
if (panelPresent(old))122
toastTransition(read())123
else124
None125
val next = Point(shape.bounds.x, shape.bounds.y)126
when (spec) {127
case Some(val value) -> {128
if (origin.target != next)129
origin.animateTo(next, value)130
if (zoom.target != shape.zoom)131
zoom.animateTo(shape.zoom, value)132
}133
case None -> {134
if (origin.value != next || origin.state == AnimationState.Running)135
origin.snapTo(next)136
if (zoom.value != shape.zoom || zoom.state == AnimationState.Running)137
zoom.snapTo(shape.zoom)138
}139
}140
}141
val entry = read().entry142
if (entry.height != shape.naturalHeight) {143
entry.height = shape.naturalHeight144
read().host.heightChanged(read().position)145
}146
ready =147
phase ==148
PresencePhase.Present &&149
shape.bounds.width >150
0.0 &&151
shape.bounds.height >152
0.0 &&153
area.visibleBounds !=154
None155
read().host.refresh()156
}157
private fun dragged(event: DragEvent) {158
when (event) {159
case Started(val sample) -> {160
if (!available()) {161
drag.cancel();162
return163
}164
dragging = true;165
dragOrigin = offset.value166
offset.cancel()167
focus.handle.requestFocus(FocusOrigin.Pointer)168
if (!active)169
return170
move(sample)171
read().host.refresh()172
}173
case Moved(val sample) -> move(sample)174
case Ended(val sample) -> {175
if (!dragging)176
return177
move(sample)178
dragging = false179
val shape = geometry ?: return180
val input = read()181
if (toastReleased(shape, offset.value, sample.velocity, input.density))182
input.entry.model.close(input.entry.id, ToastCloseReason.Swipe)183
else184
rebound()185
if (active)186
input.host.refresh()187
}188
case Cancelled(_, _) -> if (dragging) {189
dragging = false190
rebound()191
if (active)192
read().host.refresh()193
}194
}195
}196
private fun move(sample: DragSample) {197
if (!dragging || !active)198
return199
val shape = geometry ?: return200
var value = dragOrigin + sample.displacement201
if (value * shape.outward < 0.0)202
value /= 4.0203
if (value.isFinite() && offset.value != value)204
offset.snapTo(value)205
}206
private fun rebound() {207
if (!active)208
return209
when (toastTransition(read())) {210
case Some(val spec) -> if (offset.value != 0.0)211
offset.animateTo(0.0, spec)212
case None -> if (offset.value != 0.0 || offset.state == AnimationState.Running)213
offset.snapTo(0.0)214
}215
}216
fun cancel() {217
val wasDragging = dragging218
dragging = false219
drag.cancel()220
if (wasDragging)221
rebound()222
}223
fun retire() {224
if (retired)225
return226
retired = true227
ready = false;228
dragging = false229
when (read().entry.card) {230
case Some(val current) -> if (current === this)231
read().entry.card = None232
case None -> {}233
}234
read().host.refresh()235
}236
}