summaryrefslogtreecommitdiff
path: root/schist_desktop_gui/src/gui/components/navigation/navigation_remove_options.rs
blob: 6320c99017c67d24639fba0d81903330741526e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use iced::Element;

use super::{Message, Navigation};

impl<'a, TOption> Navigation<TOption>
where
    TOption: Clone + PartialEq + 'a,
    Element<'a, Message<TOption>>: From<&'a TOption>,
{
    pub fn remove_options_where<P>(&mut self, pred: P)
    where
        P: Fn(&TOption) -> bool,
    {
        while let Some(index) = self.options_before_active.iter().position(&pred) {
            self.options_before_active.remove(index);
        }

        while let Some(index) = self.options_after_active.iter().position(&pred) {
            self.options_after_active.remove(index);
        }

        if self.active_option.as_ref().is_some_and(pred) {
            self.active_option = self
                .options_before_active
                .pop()
                .or_else(|| self.options_after_active.splice(0..1, Vec::new()).last());
        }
    }
}