moly_kit/utils/makepad/
portal_list.rs

1//! Utilities to deal with [PortalList] widget.
2
3use makepad_widgets::*;
4use std::ops::RangeBounds;
5
6/// Iterator over a subset of portal list items.
7pub struct ItemsRangeIter<R: RangeBounds<usize>> {
8    list: PortalListRef,
9    range: R,
10    current: usize,
11}
12
13impl<R: RangeBounds<usize>> ItemsRangeIter<R> {
14    pub fn new(list: PortalListRef, range: R) -> Self {
15        let start = match range.start_bound() {
16            std::ops::Bound::Included(&start) => start,
17            std::ops::Bound::Excluded(&start) => start + 1,
18            std::ops::Bound::Unbounded => 0,
19        };
20
21        Self {
22            list,
23            range,
24            current: start,
25        }
26    }
27}
28
29impl<R: RangeBounds<usize>> Iterator for ItemsRangeIter<R> {
30    type Item = (usize, WidgetRef);
31
32    fn next(&mut self) -> Option<Self::Item> {
33        if !self.range.contains(&self.current) {
34            return None;
35        }
36
37        // Currently PortalList doesn't expose its children in an unconditional way,
38        // that why I'm creating this iterator on the first place, on top of `get_item`
39        // which esentialy does a hash map lookup.
40        let Some((_, item)) = self.list.get_item(self.current) else {
41            return None;
42        };
43
44        self.current += 1;
45        Some((self.current - 1, item))
46    }
47}