From d0246a7c440d7446ab6af4e9c5bd2ea00d25fd1e Mon Sep 17 00:00:00 2001 From: ardocrat Date: Thu, 27 Apr 2023 02:05:16 +0300 Subject: [PATCH] gui: fix navigation --- src/gui/screens/account.rs | 5 ++--- src/gui/screens/accounts.rs | 10 +++------ src/gui/screens/mod.rs | 2 +- src/gui/screens/navigator.rs | 39 ++++++++++++------------------------ src/gui/screens/root.rs | 24 +++++++++++++++++----- 5 files changed, 38 insertions(+), 42 deletions(-) diff --git a/src/gui/screens/account.rs b/src/gui/screens/account.rs index 631b648..164996c 100644 --- a/src/gui/screens/account.rs +++ b/src/gui/screens/account.rs @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::gui::App; use crate::gui::platform::PlatformCallbacks; use crate::gui::screens::{Navigator, ScreenId}; @@ -35,8 +34,8 @@ impl super::Screen for Account { fn show(&mut self, ui: &mut egui::Ui, - nav: Option<&mut Navigator>, + nav: &mut Navigator, cb: &dyn PlatformCallbacks) { - todo!() + } } \ No newline at end of file diff --git a/src/gui/screens/accounts.rs b/src/gui/screens/accounts.rs index 3ed5143..b733ee1 100644 --- a/src/gui/screens/accounts.rs +++ b/src/gui/screens/accounts.rs @@ -12,9 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::gui::app::App; use crate::gui::platform::PlatformCallbacks; -use crate::gui::screens::{Account, Navigator, Screen, ScreenId}; +use crate::gui::screens::{Navigator, Screen, ScreenId}; use crate::gui::views::title_panel::TitlePanel; use crate::gui::views::View; @@ -38,17 +37,14 @@ impl Screen for Accounts { fn show(&mut self, ui: &mut egui::Ui, - nav: Option<&mut Navigator>, + nav: &mut Navigator, cb: &dyn PlatformCallbacks) { TitlePanel::default() .title(self.title.to_owned()) .ui(ui); if ui.button("test").clicked() { - nav.unwrap().to(ScreenId::Account) + nav.to(ScreenId::Account) }; - - - //TODO: content } } \ No newline at end of file diff --git a/src/gui/screens/mod.rs b/src/gui/screens/mod.rs index b3fe319..d2ca610 100644 --- a/src/gui/screens/mod.rs +++ b/src/gui/screens/mod.rs @@ -42,7 +42,7 @@ pub trait Screen { fn show( &mut self, ui: &mut egui::Ui, - navigator: Option<&mut Navigator>, + navigator: &mut Navigator, cb: &dyn PlatformCallbacks ); } diff --git a/src/gui/screens/navigator.rs b/src/gui/screens/navigator.rs index fd1694d..e12ae77 100644 --- a/src/gui/screens/navigator.rs +++ b/src/gui/screens/navigator.rs @@ -12,25 +12,25 @@ // See the License for the specific language governing permissions and // limitations under the License. - - - use std::collections::BTreeSet; -use crate::gui::platform::PlatformCallbacks; -use crate::gui::screens::{Accounts, Screen, ScreenId}; + +use crate::gui::screens::ScreenId; pub struct Navigator { - stack: BTreeSet, - screens: Vec>, + pub(crate) stack: BTreeSet, +} + +impl Default for Navigator { + fn default() -> Self { + let mut stack = BTreeSet::new(); + stack.insert(ScreenId::Accounts); + Self { + stack + } + } } impl Navigator { - pub fn new(screens: Vec>) -> Self { - let mut stack = BTreeSet::new(); - stack.insert(ScreenId::Accounts); - Self { stack, screens } - } - pub fn to(&mut self, id: ScreenId) { self.stack.insert(id); } @@ -38,17 +38,4 @@ impl Navigator { pub fn back(&mut self) { self.stack.pop_last(); } - - pub fn get_current_screen(&mut self) -> Option<&Box> { - let Self { stack, screens } = self; - let current = stack.last().unwrap(); - let mut result = screens.get(0); - for screen in screens.iter() { - if screen.id() == *current { - result = Some(screen); - break; - } - } - return result; - } } \ No newline at end of file diff --git a/src/gui/screens/root.rs b/src/gui/screens/root.rs index bca13bf..e53d5f3 100644 --- a/src/gui/screens/root.rs +++ b/src/gui/screens/root.rs @@ -18,13 +18,14 @@ use crate::gui::screens::{Account, Accounts, Navigator, Screen, ScreenId}; pub struct Root { navigator: Navigator, - // screens: Vec>, + screens: Vec>, } impl Default for Root { fn default() -> Self { Self { - navigator: Navigator::new(vec![ + navigator: Navigator::new(), + screens: (vec![ Box::new(Accounts::default()), Box::new(Account::default()) ]) @@ -32,12 +33,25 @@ impl Default for Root { } } -impl Screen for Root { +impl Root { fn id(&self) -> ScreenId { ScreenId::Root } - fn show(&mut self, ui: &mut Ui, navigator: Option<&mut Navigator>, cb: &dyn PlatformCallbacks) { - let screen = self.navigator.get_current_screen(); + pub fn ui(&mut self, ui: &mut Ui, cb: &dyn PlatformCallbacks) { + self.show_current_screen(ui, cb); + } + + pub fn show_current_screen(&mut self, + ui: &mut Ui, + cb: &dyn PlatformCallbacks) { + let Self { navigator, screens } = self; + let current = navigator.stack.last().unwrap(); + for screen in screens.iter_mut() { + if screen.id() == *current { + screen.show(ui, navigator, cb); + break; + } + } } } \ No newline at end of file