ultimate_nag52/config_app/src/window.rs

96 lines
2.9 KiB
Rust
Raw Normal View History

2022-01-27 13:53:52 -08:00
use std::{borrow::BorrowMut, collections::VecDeque};
use crate::ui::status_bar::{self};
2022-08-01 08:08:19 -07:00
use eframe::egui;
2022-01-27 13:53:52 -08:00
pub struct MainWindow {
pages: VecDeque<Box<dyn InterfacePage>>,
curr_title: String,
bar: Option<Box<dyn StatusBar>>,
show_back: bool,
}
impl MainWindow {
pub fn new() -> Self {
Self {
pages: VecDeque::new(),
curr_title: "OpenVehicleDiag".into(),
bar: None,
show_back: true,
}
}
pub fn add_new_page(&mut self, p: Box<dyn InterfacePage>) {
if let Some(bar) = p.get_status_bar() {
self.bar = Some(bar)
}
self.pages.push_front(p)
}
pub fn pop_page(&mut self) {
self.pages.pop_front();
if let Some(bar) = self.pages.get_mut(0).map(|x| x.get_status_bar()) {
self.bar = bar
}
}
}
2022-08-01 08:08:19 -07:00
impl eframe::App for MainWindow {
fn update(&mut self, ctx: &eframe::egui::Context, frame: &mut eframe::Frame) {
2022-01-27 13:53:52 -08:00
let stack_size = self.pages.len();
if stack_size > 0 {
let mut pop_page = false;
if let Some(status_bar) = self.bar.borrow_mut() {
egui::TopBottomPanel::bottom("NAV")
.show(ctx, |nav| {
nav.horizontal(|row| {
2022-04-06 21:34:52 -07:00
status_bar.draw(row, ctx);
2022-01-27 13:53:52 -08:00
if stack_size > 1 && self.show_back {
if row.button("Back").clicked() {
pop_page = true;
}
}
});
});
}
if pop_page {
self.pop_page();
}
egui::CentralPanel::default().show(ctx, |main_win_ui| {
match self.pages[0].make_ui(main_win_ui, frame) {
PageAction::None => {}
PageAction::Destroy => self.pop_page(),
PageAction::Add(p) => self.add_new_page(p),
PageAction::Overwrite(p) => {
self.pages[0] = p;
self.bar = self.pages[0].get_status_bar();
}
PageAction::RePaint => ctx.request_repaint(),
PageAction::SetBackButtonState(state) => {
self.show_back = state;
}
}
});
}
2022-04-03 07:17:11 -07:00
//ctx.request_repaint(); // Continuous mode
2022-01-27 13:53:52 -08:00
}
}
pub enum PageAction {
None,
Destroy,
Add(Box<dyn InterfacePage>),
Overwrite(Box<dyn InterfacePage>),
SetBackButtonState(bool),
RePaint,
}
pub trait InterfacePage {
2022-08-01 08:08:19 -07:00
fn make_ui(&mut self, ui: &mut egui::Ui, frame: &eframe::Frame) -> PageAction;
2022-01-27 13:53:52 -08:00
fn get_title(&self) -> &'static str;
fn get_status_bar(&self) -> Option<Box<dyn StatusBar>>;
}
pub trait StatusBar {
2022-04-06 21:34:52 -07:00
fn draw(&mut self, ui: &mut egui::Ui, ctx: &egui::Context);
2022-01-27 13:53:52 -08:00
}