Scoop API

scoop:scoop.ui.component:0.1.0

src/toast/control.scoop

Selected production source · src/toast/control.scoop

1package scoop.ui.component
3import scoop.platform.time.MonotonicTimeSource
4import scoop.surface.observePointer
5import scoop.platform.time.MonotonicInstant
6import scoop.ui.*
7import scoop.window.WindowState
9/** Admission, stack activity and one earliest deadline belong to this explicit host. */
10internal 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().state
16 private var windowActivity = scope.nativeWindow.activity
17 private var scheduled: MonotonicInstant? = None
18 private var sequence = 0L
19 private var refreshing = false
20 private var again = false
21 private var retired = false
22 val active: Boolean get() =
23 !retired && scope.isMounted && scope.nativeWindow.state == WindowState.Open
24 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 = value
36 refresh()
37 }
38 }
39 refresh()
40 }
41 fun heightChanged(position: ToastPosition) {
42 for (entry in group(position)) {
43 val card = entry.card ?: continue
44 if (card.active)
45 card.scope.requestLayout()
46 }
47 }
48 fun position(entry: ToastEntry): ToastPosition = entry.data.value.position ?: read().position
49 fun group(position: ToastPosition): Array<ToastEntry> {
50 val result = ArrayList<ToastEntry>()
51 val all = visible.value
52 var at = all.size
53 while (at > 0L) {
54 at -= 1L
55 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 true
65 return false
66 }
67 fun refresh() {
68 if (!active)
69 return
70 if (refreshing) {
71 again = true;
72 return
73 }
74 refreshing = true
75 try {
76 do {
77 again = false
78 if (bound !== read().state) {
79 val previous = bound
80 val next = read().state
81 next.attach(this)
82 bound = next
83 previous.detach(this)
84 }
85 scope.input.enabled = read().enabled
86 admit()
87 activity()
88 deadlines()
89 } while (again && active)
90 } finally {
91 refreshing = false
92 }
93 }
94 private fun admit() {
95 val input = read()
96 val records = bound.entries.value
97 val previous = visible.value
98 val chosen = ArrayList<ToastEntry>()
99 for (position in toastPositions) {
100 var used = 0L
101 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 None
110 ) {
111 chosen.add(entry);
112 used += 1L
113 }
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 None
125 ) {
126 chosen.add(entry);
127 used += 1L
128 }
129 for (at in 0L ..< records.size) {
130 val index =
131 if (input.order == ToastOrder.OldestFirst)
132 at
133 else
134 records.size - 1L - at
135 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 += 1L
147 }
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 = false
157 }
158 for (entry in wanted)
159 if (!entry.admitted.value) {
160 entry.sequence = sequence;
161 sequence += 1L
162 entry.admitted.value = true
163 }
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 = next
175 }
176 private fun activity() {
177 val positions = ArrayList<ToastPosition>()
178 for (position in toastPositions) {
179 var busy = false
180 for (region in regions)
181 if (
182 region.position ==
183 position &&
184 !region.area.isClosed &&
185 region.area.scope.isHovered
186 )
187 busy = true
188 for (entry in group(position)) {
189 val card = entry.card ?: continue
190 if (
191 card.active &&
192 (
193 card.area.scope.isHovered ||
194 card.area.scope.activity.hasFocus ||
195 card.dragging
196 )
197 )
198 busy = true
199 }
200 if (busy)
201 positions.add(position)
202 }
203 val next = positions.toArray()
204 val old = interacting.value
205 var same = old.size == next.size
206 if (same)
207 for (at in 0L ..< old.size)
208 if (old[at] != next[at])
209 same = false
210 if (!same)
211 interacting.value = next
212 }
213 private fun deadlines() {
214 val now = MonotonicTimeSource.now()
215 val windowReady =
216 windowActivity.focused && windowActivity.visible && !windowActivity.occluded
217 var first: MonotonicInstant? = None
218 for (entry in visible.value) {
219 val card = entry.card
220 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 else
231 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 return
240 scheduled = first
241 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 return
249 scheduled = None
250 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 return
263 retired = true
264 scheduled = None
265 timer.use { _ -> bound.detach(this) }
266 }
269private fun toastIncludes(values: Array<ToastEntry>, entry: ToastEntry): Boolean {
270 for (value in values)
271 if (value === entry)
272 return true
273 return false
276private fun toastSameEntries(a: Array<ToastEntry>, b: Array<ToastEntry>): Boolean {
277 if (a.size != b.size)
278 return false
279 for (at in 0L ..< a.size)
280 if (a[at] !== b[at])
281 return false
282 return true