1use crate::StateAccess;
2
3pub struct Unmount {
4 pub activated: bool,
5 pub on_unmount: Box<dyn Fn() -> ()>,
6}
7
8impl Unmount {
9 pub fn new(on_unmount: impl Fn() -> () + 'static) -> Self {
10 Self {
11 activated: true,
12 on_unmount: Box::new(on_unmount),
13 }
14 }
15
16 pub fn execute_if_activated(&self) {
17 if self.activated {
18 (self.on_unmount)();
19 }
20 }
21
22 pub fn activate(&mut self) {
23 self.activated = true;
24 }
25 pub fn deactivate(&mut self) {
26 self.activated = false;
27 }
28}
29
30pub trait StateAccessUnmount {
31 fn activate(&self);
32 fn deactivate(&self);
33 fn execute_and_remove(self);
34}
35
36impl StateAccessUnmount for StateAccess<Unmount> {
37 fn execute_and_remove(self) {
38 self.update(|dt| {
39 dt.execute_if_activated();
40 });
41 self.remove();
42 }
43
44 fn activate(&self) {
45 self.update(|dt| dt.activate());
46 }
47
48 fn deactivate(&self) {
49 self.update(|dt| dt.deactivate());
50 }
51}