Skip to main content

ferroui_system/collections/specialized/
notify_collection_changed_event_args.rs

1//! `System.Collections.Specialized.NotifyCollectionChangedEventArgs`: the description of a change
2//! to a collection, borrowed by the handlers of the change and shareable as an owned handle.
3
4use std::rc::Rc;
5
6/// Describes the action that caused a collection changed notification.
7#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
8pub enum NotifyCollectionChangedAction {
9    Add,
10    Remove,
11    Replace,
12    Move,
13    Reset,
14}
15
16/// Describes a change to a collection.
17///
18/// Indexes are `-1` when not applicable to the action, as in the notification
19/// model this mirrors.
20pub struct NotifyCollectionChangedEventArgs<'a, T> {
21    pub action: NotifyCollectionChangedAction,
22    pub new_items: &'a [T],
23    pub old_items: &'a [T],
24    pub new_starting_index: i32,
25    pub old_starting_index: i32,
26}
27
28impl<T: Clone> NotifyCollectionChangedEventArgs<'_, T> {
29    /// The change as an owned, shareable value: what untyped code (an
30    /// event handler wired up by markup) is given, since the borrowed form
31    /// cannot be held in an untyped value.
32    pub fn share(&self) -> SharedNotifyCollectionChangedEventArgs<T> {
33        SharedNotifyCollectionChangedEventArgs(Rc::new(SharedNotifyCollectionChangedData {
34            action: self.action,
35            new_items: self.new_items.to_vec(),
36            old_items: self.old_items.to_vec(),
37            new_starting_index: self.new_starting_index,
38            old_starting_index: self.old_starting_index,
39        }))
40    }
41}
42
43struct SharedNotifyCollectionChangedData<T> {
44    action: NotifyCollectionChangedAction,
45    new_items: Vec<T>,
46    old_items: Vec<T>,
47    new_starting_index: i32,
48    old_starting_index: i32,
49}
50
51/// The owned form of [`NotifyCollectionChangedEventArgs`]: a shared handle
52/// to the description of a change. Clones refer to the same description and
53/// handles compare by identity, as the event args object this mirrors.
54pub struct SharedNotifyCollectionChangedEventArgs<T>(Rc<SharedNotifyCollectionChangedData<T>>);
55
56impl<T> Clone for SharedNotifyCollectionChangedEventArgs<T> {
57    fn clone(&self) -> Self {
58        Self(self.0.clone())
59    }
60}
61
62impl<T> PartialEq for SharedNotifyCollectionChangedEventArgs<T> {
63    fn eq(&self, other: &Self) -> bool {
64        Rc::ptr_eq(&self.0, &other.0)
65    }
66}
67
68impl<T> SharedNotifyCollectionChangedEventArgs<T> {
69    /// The action that caused the notification.
70    pub fn action(&self) -> NotifyCollectionChangedAction {
71        self.0.action
72    }
73
74    /// The items involved in the change that are new.
75    pub fn new_items(&self) -> &[T] {
76        &self.0.new_items
77    }
78
79    /// The items affected by a replace, remove or move.
80    pub fn old_items(&self) -> &[T] {
81        &self.0.old_items
82    }
83
84    /// The index at which the change occurred; `-1` when not applicable.
85    pub fn new_starting_index(&self) -> i32 {
86        self.0.new_starting_index
87    }
88
89    /// The index at which a move, remove or replace occurred; `-1` when not
90    /// applicable.
91    pub fn old_starting_index(&self) -> i32 {
92        self.0.old_starting_index
93    }
94
95    /// The change in the borrowed form handlers take.
96    pub fn as_args(&self) -> NotifyCollectionChangedEventArgs<'_, T> {
97        NotifyCollectionChangedEventArgs {
98            action: self.0.action,
99            new_items: &self.0.new_items,
100            old_items: &self.0.old_items,
101            new_starting_index: self.0.new_starting_index,
102            old_starting_index: self.0.old_starting_index,
103        }
104    }
105}