Skip to main content

ferroui_base/interactivity/
cancel_routed_event_args.rs

1use super::{RoutedEvent, RoutedEventArgs};
2use crate::{ferro_routed_event_args, FerroObject, Nullable};
3use std::cell::Cell;
4use std::rc::Rc;
5
6/// Provides state information and data specific to a cancelable routed event.
7#[derive(Clone, Default)]
8pub struct CancelRoutedEventArgs {
9    base: RoutedEventArgs,
10    cancel: Rc<Cell<bool>>,
11}
12
13ferro_routed_event_args!(CancelRoutedEventArgs: RoutedEventArgs);
14
15impl CancelRoutedEventArgs {
16    /// Creates args with no routed event.
17    pub fn new() -> Self {
18        Self::default()
19    }
20
21    /// Creates args for a routed event.
22    pub fn with_event<T: ?Sized>(routed_event: &RoutedEvent<T>) -> Self {
23        Self { base: RoutedEventArgs::with_event(routed_event), cancel: Rc::new(Cell::new(false)) }
24    }
25
26    /// Creates args for a routed event with a source.
27    pub fn with_event_and_source<T: ?Sized>(
28        routed_event: &RoutedEvent<T>,
29        source: impl Into<Nullable<FerroObject>>,
30    ) -> Self {
31        Self { base: RoutedEventArgs::with_event_and_source(routed_event, source), cancel: Rc::new(Cell::new(false)) }
32    }
33
34    /// Whether the routed event should be canceled.
35    #[inline]
36    pub fn cancel(&self) -> bool {
37        self.cancel.get()
38    }
39
40    /// Sets whether the routed event should be canceled.
41    #[inline]
42    pub fn set_cancel(&self, value: bool) {
43        self.cancel.set(value)
44    }
45}