summaryrefslogtreecommitdiff
path: root/schist_desktop_gui/src/gui/components/navigation/navigation_select_option.rs
blob: 5aa96cdac1a1363ce0176ca87f470d056d3ddd80 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use iced::Element;

use crate::traits::Focusable;

use super::{Message, Navigation};

pub enum SelectOptionResult<TOption> {
    AlreadySelected,
    SelectedExistingOption(TOption),
    SelectedNewOption(TOption),
}

impl<'a, TOption> Navigation<TOption>
where
    TOption: Clone + Focusable + PartialEq + 'a,
    Element<'a, Message<TOption>>: From<&'a TOption>,
{
    pub fn select_option(&mut self, option: TOption) -> SelectOptionResult<TOption>
    where
        TOption: Clone + PartialEq + 'a,
    {
        if self
            .active_option
            .as_ref()
            .is_some_and(|active_option| TOption::eq(active_option, &option))
        {
            SelectOptionResult::AlreadySelected
        } else if let Some(index) = self
            .options_before_active
            .iter()
            .position(|o| TOption::eq(o, &option))
        {
            let split = self.options_before_active.split_at(index);
            self.options_after_active = vec![
                self.active_option.as_mut().map_or(vec![], |active_option| {
                    active_option.unfocus();
                    vec![active_option.clone()]
                }),
                split.1.split_at(1).1.to_vec(),
                self.options_after_active.clone(),
            ]
            .concat();
            self.options_before_active = split.0.to_vec();
            self.active_option = Some(option.clone());
            self.active_option.iter_mut().for_each(TOption::focus);
            SelectOptionResult::SelectedExistingOption(option)
        } else if let Some(index) = self
            .options_after_active
            .iter()
            .position(|o| TOption::eq(o, &option))
        {
            let split = self.options_after_active.split_at(index);
            self.options_before_active = vec![
                self.options_before_active.clone(),
                split.0.to_vec(),
                self.active_option.as_mut().map_or(vec![], |active_option| {
                    active_option.unfocus();
                    vec![active_option.clone()]
                }),
            ]
            .concat();
            self.active_option = Some(option.clone());
            self.active_option.iter_mut().for_each(TOption::focus);
            self.options_after_active = split.1.split_at(1).1.to_vec();
            SelectOptionResult::SelectedExistingOption(option)
        } else {
            if let Some(active_option) = &mut self.active_option {
                active_option.unfocus();
                self.options_before_active.push(active_option.clone());
            }
            self.options_before_active
                .append(&mut self.options_after_active);
            self.active_option = Some(option.clone());
            self.active_option.iter_mut().for_each(TOption::focus);
            self.options_after_active = vec![];
            SelectOptionResult::SelectedNewOption(option)
        }
    }
}