ferroui_system/events/
event.rs1use super::{HandlerList, HandlerSnapshot};
2use std::rc::Rc;
3
4pub struct Event<A: ?Sized> {
10 handlers: HandlerList<dyn Fn(&A)>,
11}
12
13impl<A: ?Sized> Default for Event<A> {
14 fn default() -> Self {
15 Self::new()
16 }
17}
18
19impl<A: ?Sized> Event<A> {
20 pub fn new() -> Self {
21 Self { handlers: HandlerList::new() }
22 }
23
24 pub fn with_shared_ids(ids: std::rc::Rc<std::cell::Cell<u64>>) -> Self {
27 Self { handlers: HandlerList::with_shared_ids(ids) }
28 }
29
30 pub fn with_ordered_ids() -> Self {
33 Self { handlers: HandlerList::with_ordered_ids() }
34 }
35
36 pub fn snapshot(&self) -> HandlerSnapshot<dyn Fn(&A)> {
39 self.handlers.snapshot()
40 }
41
42 pub fn add(&self, handler: Rc<dyn Fn(&A)>) -> u64 {
44 self.handlers.add(handler)
45 }
46
47 pub fn remove(&self, token: u64) -> bool {
49 self.handlers.remove(token)
50 }
51
52 pub fn has_handlers(&self) -> bool {
54 !self.handlers.is_empty()
55 }
56
57 pub fn handler_count(&self) -> usize {
59 self.handlers.len()
60 }
61
62 pub fn raise(&self, args: &A) {
64 if self.handlers.is_empty() {
65 return;
66 }
67 for (_, handler) in self.handlers.snapshot().iter() {
68 handler(args);
69 }
70 }
71}