Skip to main content

ferroui_controls/
size_changed_event_args.rs

1use ferroui_base::interactivity::{RoutedEvent, RoutedEventArgs};
2use ferroui_base::layout::LayoutHelper;
3use ferroui_base::utilities::MathUtilities;
4use ferroui_base::{ferro_routed_event_args, FerroObject, Nullable, Size};
5
6/// Provides data specific to a `SizeChanged` event.
7#[derive(Clone, Default)]
8pub struct SizeChangedEventArgs {
9    base: RoutedEventArgs,
10    new_size: Size,
11    previous_size: Size,
12}
13
14ferro_routed_event_args!(SizeChangedEventArgs: RoutedEventArgs);
15
16impl SizeChangedEventArgs {
17    /// Creates args for a routed event.
18    pub fn with_event<T: ?Sized>(routed_event: &RoutedEvent<T>) -> Self {
19        Self { base: RoutedEventArgs::with_event(routed_event), ..Self::default() }
20    }
21
22    /// Creates args for a routed event with a source.
23    pub fn with_event_and_source<T: ?Sized>(
24        routed_event: &RoutedEvent<T>,
25        source: impl Into<Nullable<FerroObject>>,
26    ) -> Self {
27        Self { base: RoutedEventArgs::with_event_and_source(routed_event, source), ..Self::default() }
28    }
29
30    /// Creates args for a routed event with a source and the previous and
31    /// new sizes.
32    pub fn new<T: ?Sized>(
33        routed_event: &RoutedEvent<T>,
34        source: impl Into<Nullable<FerroObject>>,
35        previous_size: Size,
36        new_size: Size,
37    ) -> Self {
38        Self { base: RoutedEventArgs::with_event_and_source(routed_event, source), new_size, previous_size }
39    }
40
41    /// Whether the height of the new size is considered different to the
42    /// previous size height.
43    ///
44    /// This will take into account layout epsilon and will not be true if
45    /// both heights are considered equivalent for layout purposes. Remember
46    /// there can be small variations in the calculations between layout
47    /// cycles due to rounding and precision even when the size has not
48    /// otherwise changed.
49    pub fn height_changed(&self) -> bool {
50        !MathUtilities::are_close_eps(self.new_size.height, self.previous_size.height, LayoutHelper::LAYOUT_EPSILON)
51    }
52
53    /// The new size (or bounds) of the object.
54    pub fn new_size(&self) -> Size {
55        self.new_size
56    }
57
58    /// The previous size (or bounds) of the object.
59    pub fn previous_size(&self) -> Size {
60        self.previous_size
61    }
62
63    /// Whether the width of the new size is considered different to the
64    /// previous size width.
65    ///
66    /// See [`height_changed`](Self::height_changed).
67    pub fn width_changed(&self) -> bool {
68        !MathUtilities::are_close_eps(self.new_size.width, self.previous_size.width, LayoutHelper::LAYOUT_EPSILON)
69    }
70}