Skip to main content

ferroui_base/utilities/
cancel_event_args.rs

1use std::cell::Cell;
2use std::rc::Rc;
3
4/// The data of a cancellable event (the counterpart of the platform's
5/// cancel event args).
6///
7/// The args are a reference object: a copy shares the cancel flag with the
8/// args it was made from, so that a handler given a copy cancels the event,
9/// and copies compare by identity.
10#[derive(Clone, Debug, Default)]
11pub struct CancelEventArgs {
12    cancel: Rc<Cell<bool>>,
13}
14
15impl PartialEq for CancelEventArgs {
16    fn eq(&self, other: &Self) -> bool {
17        Rc::ptr_eq(&self.cancel, &other.cancel)
18    }
19}
20
21impl CancelEventArgs {
22    /// Creates args of an event that is not cancelled.
23    pub fn new() -> Self {
24        Self::default()
25    }
26
27    /// Whether the event should be cancelled.
28    pub fn cancel(&self) -> bool {
29        self.cancel.get()
30    }
31
32    pub fn set_cancel(&self, value: bool) {
33        self.cancel.set(value)
34    }
35}