waft_winui/controls/items_view/
selection.rs1use super::selection_model::SelectionModel;
6use super::selector::{ItemsViewSelectionMode, Selector};
7use waft_foundation::{App, ChangeNotifier, ChangeNotifierData, Handle, Ref};
8
9pub struct ItemsViewSource {
13 notifier: ChangeNotifierData,
14 model: SelectionModel,
15 selector: Selector,
16 current_item_index: Option<usize>,
18 revision: u64,
20}
21
22impl ItemsViewSource {
23 pub fn new(app: &mut App, item_count: usize) -> Ref<Self> {
25 let mut model = SelectionModel::default();
26 model.set_source(item_count);
27 let (selector, _) = Selector::for_mode(ItemsViewSelectionMode::Single, &mut model);
28 app.create(Self {
29 notifier: ChangeNotifierData::new(),
30 model,
31 selector,
32 current_item_index: None,
33 revision: 0,
34 })
35 }
36
37 pub fn dispose(self: Handle<Self>, app: &mut App) {
39 ChangeNotifierData::dispose(app, self);
40 }
41
42 pub fn is_selected(self: Handle<Self>, app: &App, index: usize) -> bool {
44 app.get(self).model.is_selected(index)
45 }
46
47 pub fn selected_indices(self: Handle<Self>, app: &App) -> Vec<usize> {
49 app.get(self).model.selected_indices().collect()
50 }
51
52 pub fn selected_index(self: Handle<Self>, app: &App) -> Option<usize> {
54 app.get(self).model.selected_index()
55 }
56
57 pub fn current_item_index(self: Handle<Self>, app: &App) -> Option<usize> {
59 app.get(self).current_item_index
60 }
61
62 pub fn select(self: Handle<Self>, app: &mut App, index: usize) {
64 self.change(app, |model| model.select(index));
65 }
66
67 pub fn deselect(self: Handle<Self>, app: &mut App, index: usize) {
69 self.change(app, |model| model.deselect(index));
70 }
71
72 pub fn select_all(self: Handle<Self>, app: &mut App) {
74 self.change(app, SelectionModel::select_all);
75 }
76
77 pub fn deselect_all(self: Handle<Self>, app: &mut App) {
79 self.change(app, SelectionModel::clear_selection);
80 }
81
82 pub fn invert_selection(self: Handle<Self>, app: &mut App) {
84 self.change(app, |model| {
85 let selected: Vec<usize> = model.selected_indices().collect();
86 let mut index_end = model.item_count() as isize - 1;
87 for index in selected.into_iter().rev() {
88 if (index as isize) < index_end {
89 model.select_range(index + 1, index_end as usize);
90 }
91 model.deselect_at(index);
92 index_end = index as isize - 1;
93 }
94 if index_end >= 0 {
95 model.select_range(0, index_end as usize);
96 }
97 true
98 });
99 }
100
101 pub(crate) fn selector(self: Handle<Self>, app: &App) -> Selector {
102 app.get(self).selector
103 }
104
105 pub(crate) fn set_selection_mode(
107 self: Handle<Self>,
108 app: &mut App,
109 mode: ItemsViewSelectionMode,
110 ) {
111 let mut selector = Selector::Null;
112 self.change(app, |model| {
113 let (made, changed) = Selector::for_mode(mode, model);
114 selector = made;
115 changed
116 });
117 app.get_mut(self).selector = selector;
118 }
119
120 pub fn item_count(self: Handle<Self>, app: &App) -> usize {
122 app.get(self).model.item_count()
123 }
124
125 pub fn set_items(self: Handle<Self>, app: &mut App, count: usize) {
127 self.change(app, |model| model.set_source(count));
128 }
129
130 pub(crate) fn set_current_item_index(self: Handle<Self>, app: &mut App, index: Option<usize>) {
132 if app.get(self).current_item_index != index {
133 app.get_mut(self).current_item_index = index;
134 self.notify_listeners(app);
135 }
136 }
137
138 pub(crate) fn interacted(
140 self: Handle<Self>,
141 app: &mut App,
142 index: usize,
143 ctrl: bool,
144 shift: bool,
145 ) {
146 let selector = self.selector(app);
147 self.change(app, |model| {
148 selector.on_interacted_action(model, index, ctrl, shift)
149 });
150 }
151
152 pub(crate) fn focused(
154 self: Handle<Self>,
155 app: &mut App,
156 index: usize,
157 ctrl: bool,
158 shift: bool,
159 ) {
160 let selector = self.selector(app);
161 self.change(app, |model| {
162 selector.on_focused_action(model, index, ctrl, shift)
163 });
164 }
165
166 pub(crate) fn deselect_with_anchor_preservation(
168 self: Handle<Self>,
169 app: &mut App,
170 index: usize,
171 ) {
172 self.change(app, |model| {
173 Selector::deselect_with_anchor_preservation(model, index)
174 });
175 }
176
177 pub(crate) fn revision(self: Handle<Self>, app: &App) -> u64 {
179 app.get(self).revision
180 }
181
182 pub(crate) fn notify_items(self: Handle<Self>, app: &mut App) {
184 self.notify_listeners(app);
185 }
186
187 fn change(self: Handle<Self>, app: &mut App, change: impl FnOnce(&mut SelectionModel) -> bool) {
189 if change(&mut app.get_mut(self).model) {
190 app.get_mut(self).revision += 1;
191 self.notify_listeners(app);
192 }
193 }
194}
195
196impl ChangeNotifier for ItemsViewSource {
197 fn change_notifier_data(&self) -> &ChangeNotifierData {
198 &self.notifier
199 }
200
201 fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
202 &mut self.notifier
203 }
204}
205
206#[cfg(test)]
207mod tests {
208 use super::*;
209 use waft_foundation::AppCell;
210
211 #[test]
214 fn a_selection_taken_after_the_items_are_given_is_kept() {
215 let cell = AppCell::new();
216 let mut app = cell.borrow_mut();
217 let source = ItemsViewSource::new(&mut app, 5);
218 source.select(&mut app, 3);
219 assert_eq!(source.selected_indices(&app), [3]);
220 assert_eq!(source.item_count(&app), 5);
221 }
222
223 #[test]
225 fn giving_the_items_clears_the_selection_and_notifies_once() {
226 let cell = AppCell::new();
227 let mut app = cell.borrow_mut();
228 let source = ItemsViewSource::new(&mut app, 5);
229 source.select(&mut app, 3);
230 let revision = source.revision(&app);
231
232 source.set_items(&mut app, 7);
233 assert_eq!(source.selected_indices(&app), Vec::<usize>::new());
234 assert_eq!(source.item_count(&app), 7);
235 assert_eq!(source.revision(&app), revision + 1);
236 }
237
238 #[test]
241 fn a_select_past_the_items_is_refused_and_raised() {
242 let cell = AppCell::new();
243 let mut app = cell.borrow_mut();
244 let source = ItemsViewSource::new(&mut app, 5);
245 let revision = source.revision(&app);
246
247 source.select(&mut app, 5);
248 assert_eq!(source.selected_indices(&app), Vec::<usize>::new());
249 assert_eq!(source.revision(&app), revision + 1, "SelectionChanged");
250 }
251}