scoop:scoop.ui.component:0.1.0
src/toast/control.scoop
Selected production source · src/toast/control.scoop
1
package scoop.ui.component3
import scoop.platform.time.MonotonicTimeSource4
import scoop.surface.observePointer5
import scoop.platform.time.MonotonicInstant6
import scoop.ui.*7
import scoop.window.WindowState9
/** Admission, stack activity and one earliest deadline belong to this explicit host. */10
internal class ToasterControl(val scope: ComponentScope, val read: () -> ToasterProps) {11
val visible = state<Array<ToastEntry>>([])12
val regions = ArrayList<ToastRegion>()13
val interacting = state<Array<ToastPosition>>([])14
private val timer = scope.createDeadline { expired() }15
private var bound = read().state16
private var windowActivity = scope.nativeWindow.activity17
private var scheduled: MonotonicInstant? = None18
private var sequence = 0L19
private var refreshing = false20
private var again = false21
private var retired = false22
val active: Boolean get() =23
!retired && scope.isMounted && scope.nativeWindow.state == WindowState.Open24
init {25
scope.onDispose { retire() }26
bound.attach(this)27
scope.resource {28
scope.input.observePointer {29
_ -> if (active)30
refresh()31
}32
}33
scope.observeWindowActivity {34
value -> if (active) {35
windowActivity = value36
refresh()37
}38
}39
refresh()40
}41
fun heightChanged(position: ToastPosition) {42
for (entry in group(position)) {43
val card = entry.card ?: continue44
if (card.active)45
card.scope.requestLayout()46
}47
}48
fun position(entry: ToastEntry): ToastPosition = entry.data.value.position ?: read().position49
fun group(position: ToastPosition): Array<ToastEntry> {50
val result = ArrayList<ToastEntry>()51
val all = visible.value52
var at = all.size53
while (at > 0L) {54
at -= 1L55
if (this.position(all[at]) == position)56
result.add(all[at])57
}58
return result.toArray()59
}60
fun expanded(position: ToastPosition): Boolean = read().expanded || busy(position)61
private fun busy(position: ToastPosition): Boolean {62
for (value in interacting.value)63
if (value == position)64
return true65
return false66
}67
fun refresh() {68
if (!active)69
return70
if (refreshing) {71
again = true;72
return73
}74
refreshing = true75
try {76
do {77
again = false78
if (bound !== read().state) {79
val previous = bound80
val next = read().state81
next.attach(this)82
bound = next83
previous.detach(this)84
}85
scope.input.enabled = read().enabled86
admit()87
activity()88
deadlines()89
} while (again && active)90
} finally {91
refreshing = false92
}93
}94
private fun admit() {95
val input = read()96
val records = bound.entries.value97
val previous = visible.value98
val chosen = ArrayList<ToastEntry>()99
for (position in toastPositions) {100
var used = 0L101
for (entry in previous)102
if (103
entry.model ===104
bound &&105
panelPresent(bound.find(entry.id)) &&106
this.position(entry) ==107
position &&108
entry.closing.value !=109
None110
) {111
chosen.add(entry);112
used += 1L113
}114
for (entry in previous)115
if (116
used <117
input.visibleCount &&118
entry.model ===119
bound &&120
panelPresent(bound.find(entry.id)) &&121
this.position(entry) ==122
position &&123
entry.closing.value ==124
None125
) {126
chosen.add(entry);127
used += 1L128
}129
for (at in 0L ..< records.size) {130
val index =131
if (input.order == ToastOrder.OldestFirst)132
at133
else134
records.size - 1L - at135
val entry = records[index]136
if (137
used <138
input.visibleCount &&139
this.position(entry) ==140
position &&141
entry.closing.value ==142
None &&143
!toastIncludes(chosen.toArray(), entry)144
) {145
chosen.add(entry);146
used += 1L147
}148
}149
}150
val wanted = chosen.toArray()151
val now = MonotonicTimeSource.now()152
for (entry in previous)153
if (!toastIncludes(wanted, entry)) {154
entry.clock.pause(now)155
if (entry.admitted.value)156
entry.admitted.value = false157
}158
for (entry in wanted)159
if (!entry.admitted.value) {160
entry.sequence = sequence;161
sequence += 1L162
entry.admitted.value = true163
}164
// Existing cards stay before later admissions, so the newest card paints last.165
val ordered = ArrayList<ToastEntry>()166
for (entry in previous)167
if (toastIncludes(wanted, entry))168
ordered.add(entry)169
for (entry in wanted)170
if (!toastIncludes(previous, entry))171
ordered.add(entry)172
val next = ordered.toArray()173
if (!toastSameEntries(previous, next))174
visible.value = next175
}176
private fun activity() {177
val positions = ArrayList<ToastPosition>()178
for (position in toastPositions) {179
var busy = false180
for (region in regions)181
if (182
region.position ==183
position &&184
!region.area.isClosed &&185
region.area.scope.isHovered186
)187
busy = true188
for (entry in group(position)) {189
val card = entry.card ?: continue190
if (191
card.active &&192
(193
card.area.scope.isHovered ||194
card.area.scope.activity.hasFocus ||195
card.dragging196
)197
)198
busy = true199
}200
if (busy)201
positions.add(position)202
}203
val next = positions.toArray()204
val old = interacting.value205
var same = old.size == next.size206
if (same)207
for (at in 0L ..< old.size)208
if (old[at] != next[at])209
same = false210
if (!same)211
interacting.value = next212
}213
private fun deadlines() {214
val now = MonotonicTimeSource.now()215
val windowReady =216
windowActivity.focused && windowActivity.visible && !windowActivity.occluded217
var first: MonotonicInstant? = None218
for (entry in visible.value) {219
val card = entry.card220
val allowed =221
read().enabled &&222
windowReady &&223
entry.closing.value ==224
None &&225
!busy(position(entry)) &&226
(card?.ready ?: false) &&227
(card?.area?.scope?.isInputAllowed ?: false)228
if (allowed)229
entry.clock.resume(now)230
else231
entry.clock.pause(now)232
when (entry.clock.deadline) {233
case Some(val end) -> if (first == None || end < first!!)234
first = Some(end)235
case None -> {}236
}237
}238
if (first == scheduled)239
return240
scheduled = first241
when (first) {242
case Some(val end) -> timer.schedule(end)243
case None -> timer.cancel()244
}245
}246
private fun expired() {247
if (!active)248
return249
scheduled = None250
refresh()251
val now = MonotonicTimeSource.now()252
for (entry in visible.value)253
when (entry.clock.deadline) {254
case Some(val end) -> if (end <= now)255
entry.model.close(entry.id, ToastCloseReason.Timeout)256
case None -> {}257
}258
refresh()259
}260
fun retire() {261
if (retired)262
return263
retired = true264
scheduled = None265
timer.use { _ -> bound.detach(this) }266
}267
}269
private fun toastIncludes(values: Array<ToastEntry>, entry: ToastEntry): Boolean {270
for (value in values)271
if (value === entry)272
return true273
return false274
}276
private fun toastSameEntries(a: Array<ToastEntry>, b: Array<ToastEntry>): Boolean {277
if (a.size != b.size)278
return false279
for (at in 0L ..< a.size)280
if (a[at] !== b[at])281
return false282
return true283
}