1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use super::*;
/// Unified overlay stack management operations for `EuvOverlayStack`.
impl EuvOverlayStack {
/// Publishes the current active overlay depth into the reactive count signal if alive.
fn sync_active_count_signal() {
let count: usize = Self::active_count();
OVERLAY_STACK_COUNT_SIG.with(|slot: &RefCell<Option<Signal<usize>>>| {
if let Ok(guard) = slot.try_borrow()
&& let Some(sig) = *guard
&& untracked(|| sig.try_get().is_some())
{
let _: bool = sig.set_if_changed(count);
}
});
}
/// Returns a reactive signal tracking the number of currently open overlays.
///
/// # Returns
///
/// - `Signal<usize>` - Live reactive active overlay depth signal.
#[must_use]
pub fn active_count_signal() -> Signal<usize> {
let reset_detected: bool =
OVERLAY_STACK_COUNT_SIG.with(|slot: &RefCell<Option<Signal<usize>>>| {
slot.try_borrow()
.ok()
.and_then(|guard: Ref<'_, Option<Signal<usize>>>| *guard)
.is_some_and(|sig: Signal<usize>| untracked(|| sig.try_get().is_none()))
});
if reset_detected {
OVERLAY_STACK_ENTRIES.with(|stack: &RefCell<Vec<OverlayStackEntry>>| {
if let Ok(mut list) = stack.try_borrow_mut() {
list.clear();
}
});
}
let count: usize = Self::active_count();
OVERLAY_STACK_COUNT_SIG.with(|slot: &RefCell<Option<Signal<usize>>>| {
if let Ok(guard) = slot.try_borrow()
&& let Some(sig) = *guard
&& untracked(|| sig.try_get().is_some())
{
let _: bool = sig.set_if_changed(count);
return sig;
}
let fresh: Signal<usize> = Signal::new(count);
if let Ok(mut guard) = slot.try_borrow_mut() {
*guard = Some(fresh);
}
fresh
})
}
/// Registers an open overlay onto the unified dismissal stack and returns its unique entry ID.
///
/// # Arguments
///
/// - `&str` - Human-readable overlay label.
/// - `i32` - Layer stacking z-index (`Z_INDEX_MODAL`, `Z_INDEX_DRAWER`, etc.).
/// - `F` - Closure closing the overlay when dismissed by system back.
///
/// # Returns
///
/// - `u64` - Unique registration ID used to remove the entry upon manual dismissal.
pub fn push<F>(label: &str, z_index: i32, on_dismiss: F) -> u64
where
F: FnMut() + 'static,
{
let id: u64 = OVERLAY_STACK_NEXT_ID.with(|seq: &Cell<u64>| {
let cur: u64 = seq.get();
seq.set(cur.wrapping_add(1).max(1));
cur
});
let entry: OverlayStackEntry = OverlayStackEntry {
id,
label: label.to_string(),
z_index,
on_dismiss: Rc::new(RefCell::new(on_dismiss)),
};
OVERLAY_STACK_ENTRIES.with(|stack: &RefCell<Vec<OverlayStackEntry>>| {
if let Ok(mut list) = stack.try_borrow_mut() {
list.push(entry);
}
});
Self::sync_active_count_signal();
id
}
/// Removes an overlay entry by `id` when closed outside the back-stack path (e.g. close button or scrim tap).
///
/// # Arguments
///
/// - `u64` - Registration ID returned by `push`.
pub fn remove(id: u64) {
if id == 0 {
return;
}
OVERLAY_STACK_ENTRIES.with(|stack: &RefCell<Vec<OverlayStackEntry>>| {
if let Ok(mut list) = stack.try_borrow_mut() {
list.retain(|entry: &OverlayStackEntry| entry.get_id() != id);
}
});
Self::sync_active_count_signal();
}
/// Dismisses ONLY the highest-priority (`z_index`, then most recent `id`) active overlay in LIFO order.
///
/// # Returns
///
/// - `bool` - `true` if an active overlay was dismissed; `false` if the overlay stack is empty.
pub fn dismiss_top() -> bool {
let top_cb: Option<Rc<RefCell<dyn FnMut()>>> =
OVERLAY_STACK_ENTRIES.with(|stack: &RefCell<Vec<OverlayStackEntry>>| {
let Ok(mut list) = stack.try_borrow_mut() else {
return None;
};
if list.is_empty() {
return None;
}
let mut best_idx: usize = 0;
for i in 1..list.len() {
let a: &OverlayStackEntry = &list[best_idx];
let b: &OverlayStackEntry = &list[i];
if b.get_z_index() > a.get_z_index()
|| (b.get_z_index() == a.get_z_index() && b.get_id() > a.get_id())
{
best_idx = i;
}
}
let removed: OverlayStackEntry = list.remove(best_idx);
let OverlayStackEntry { on_dismiss, .. } = removed;
Some(on_dismiss)
});
if let Some(cb) = top_cb {
Self::sync_active_count_signal();
if let Ok(mut func) = cb.try_borrow_mut() {
func();
}
return true;
}
false
}
/// Dismisses every active overlay currently open in top-to-bottom LIFO order.
///
/// # Returns
///
/// - `usize` - Number of overlays dismissed.
pub fn dismiss_all() -> usize {
let mut closed: usize = 0;
while Self::dismiss_top() {
closed += 1;
}
closed
}
/// Returns the number of currently open overlays registered in the stack.
///
/// # Returns
///
/// - `usize` - Active overlay depth.
#[must_use]
pub fn active_count() -> usize {
OVERLAY_STACK_ENTRIES.with(|stack: &RefCell<Vec<OverlayStackEntry>>| {
stack
.try_borrow()
.map(|list: Ref<'_, Vec<OverlayStackEntry>>| list.len())
.unwrap_or(0)
})
}
/// Returns the ordered labels of all currently open overlays (bottom to top).
///
/// # Returns
///
/// - `Vec<String>` - Active overlay labels.
#[must_use]
pub fn active_labels() -> Vec<String> {
OVERLAY_STACK_ENTRIES.with(|stack: &RefCell<Vec<OverlayStackEntry>>| {
stack
.try_borrow()
.map(|list: Ref<'_, Vec<OverlayStackEntry>>| {
list.iter()
.map(|entry: &OverlayStackEntry| entry.get_label().clone())
.collect()
})
.unwrap_or_default()
})
}
/// Clears all registered entries without invoking callbacks (useful for test reset).
pub fn clear() {
OVERLAY_STACK_ENTRIES.with(|stack: &RefCell<Vec<OverlayStackEntry>>| {
if let Ok(mut list) = stack.try_borrow_mut() {
list.clear();
}
});
Self::sync_active_count_signal();
}
/// Binds a boolean open signal (`EuvModal`, `EuvDrawer`, `EuvSheet`, `EuvAlertDialog`, etc.) to the unified overlay stack.
///
/// # Arguments
///
/// - `Signal<bool>` - Controlled overlay visibility signal.
/// - `&str` - Diagnostic overlay label.
/// - `i32` - Layer z-index priority.
///
/// # Returns
///
/// - `Effect` - Reactive effect keeping the stack synchronized with `open`.
pub fn bind_signal(open: Signal<bool>, label: &str, z_index: i32) -> Effect {
let entry_id: Rc<Cell<u64>> = Rc::new(Cell::new(0));
let cleanup_id: Rc<Cell<u64>> = Rc::clone(&entry_id);
on_cleanup(move || {
let stale_id: u64 = cleanup_id.replace(0);
if stale_id != 0 {
Self::remove(stale_id);
}
});
let label_owned: String = label.to_string();
Effect::new(move || {
let is_open: bool = open.get();
let cur_id: u64 = entry_id.get();
if is_open && cur_id == 0 {
let id_holder: Rc<Cell<u64>> = Rc::clone(&entry_id);
let new_id: u64 = Self::push(&label_owned, z_index, move || {
id_holder.set(0);
let _: bool = open.set_if_changed(false);
});
entry_id.set(new_id);
} else if !is_open && cur_id != 0 {
entry_id.set(0);
Self::remove(cur_id);
}
})
}
}