diff options
| author | Joe Carstairs <me@joeac.net> | 2026-01-25 10:10:24 +0000 |
|---|---|---|
| committer | Joe Carstairs <me@joeac.net> | 2026-01-25 10:10:24 +0000 |
| commit | 1feae160bd184dcea618415c0e9e55289ca8b68a (patch) | |
| tree | 1cbbedd744468df30fa346bce4eeba9a60f42565 /schist_desktop_gui/src/gui/components/navigation/navigate_navigation.rs | |
| parent | d2fccfb4fe073eefc235595d255fcb6385bfa1f1 (diff) | |
factors out navigable trait
Diffstat (limited to 'schist_desktop_gui/src/gui/components/navigation/navigate_navigation.rs')
| -rw-r--r-- | schist_desktop_gui/src/gui/components/navigation/navigate_navigation.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/schist_desktop_gui/src/gui/components/navigation/navigate_navigation.rs b/schist_desktop_gui/src/gui/components/navigation/navigate_navigation.rs new file mode 100644 index 0000000..f399cd0 --- /dev/null +++ b/schist_desktop_gui/src/gui/components/navigation/navigate_navigation.rs @@ -0,0 +1,45 @@ +use iced::Element; + +use crate::traits::{Navigable, NavigationResult}; + +use super::{Message, Navigation}; + +impl<'a, TOption> Navigable<TOption> for Navigation<TOption> +where + TOption: Clone + PartialEq + 'a, + Element<'a, Message<TOption>>: From<&'a TOption>, +{ + fn move_next(&mut self) -> NavigationResult<TOption> { + if let Some(next) = self.options_after_active.clone().first() { + self.options_before_active = vec![ + self.options_before_active.clone(), + self.active_option.clone().map_or(vec![], |o| vec![o]), + ] + .concat(); + self.active_option = Some(next.clone()); + self.options_after_active = self.options_after_active.split_at(1).1.to_vec(); + NavigationResult::Moved(next.clone()) + } else { + NavigationResult::LastOption + } + } + + fn move_prev(&mut self) -> NavigationResult<TOption> { + if let Some(prev) = self.options_before_active.clone().last() { + self.options_after_active = vec![ + self.active_option.clone().map_or(vec![], |o| vec![o]), + self.options_after_active.clone(), + ] + .concat(); + self.active_option = Some(prev.clone()); + self.options_before_active = self + .options_before_active + .split_last() + .map(|split| split.1.to_vec()) + .unwrap_or_else(Vec::new); + NavigationResult::Moved(prev.clone()) + } else { + NavigationResult::LastOption + } + } +} |
