gui: fix navigation

This commit is contained in:
ardocrat 2023-04-27 02:05:16 +03:00
parent a8a24281fd
commit d0246a7c44
5 changed files with 38 additions and 42 deletions

View file

@ -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!()
}
}

View file

@ -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
}
}

View file

@ -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
);
}

View file

@ -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<ScreenId>,
screens: Vec<Box<dyn Screen>>,
pub(crate) stack: BTreeSet<ScreenId>,
}
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<Box<dyn Screen>>) -> 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<dyn Screen>> {
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;
}
}

View file

@ -18,13 +18,14 @@ use crate::gui::screens::{Account, Accounts, Navigator, Screen, ScreenId};
pub struct Root {
navigator: Navigator,
// screens: Vec<Box<dyn Screen>>,
screens: Vec<Box<dyn Screen>>,
}
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;
}
}
}
}