From fa919970e560d7e89150504ffbd98486772fd23e Mon Sep 17 00:00:00 2001 From: Luke Else Date: Tue, 30 Jul 2024 19:48:50 +0100 Subject: [PATCH] Added tabbing to the main UI (Error currently occurs because of circular dependency on App object) --- Cargo.toml | 1 + src/app/mod.rs | 4 +++- src/app/ui/mod.rs | 21 +++++++----------- src/app/ui/tabs/metar_tab.rs | 22 ++++++++++++++++++ src/app/ui/tabs/mod.rs | 43 +++++++++++++++++++++++++++++++++--- src/main.rs | 2 +- 6 files changed, 75 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1c756de..3773528 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ edition = "2021" egui = "0.28.1" eframe = "0.28.1" tracing-subscriber = "0.3.16" +strum = { version = "0.26.3", features = ["derive"] } diff --git a/src/app/mod.rs b/src/app/mod.rs index c988d10..605cf71 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -3,7 +3,8 @@ mod ui; /// Stuct to store the state of the running application pub struct App<'a> { name: &'a str, - icaos: Vec<&'a str>, + tab_state: ui::tabs::TabState, + icaos: Vec, metars: Vec<&'a str>, tafs: Vec<&'a str> } @@ -13,6 +14,7 @@ impl <'a> Default for App<'a> { fn default() -> Self { Self { name: "AvBag - Welcome", + tab_state: ui::tabs::TabState::default(), icaos: vec![], metars: vec![], tafs: vec![] diff --git a/src/app/ui/mod.rs b/src/app/ui/mod.rs index 21dd67d..8788c54 100644 --- a/src/app/ui/mod.rs +++ b/src/app/ui/mod.rs @@ -1,3 +1,5 @@ +use tabs::Tab; + use crate::app::App; use crate::theme::onedark::ONE_DARK; @@ -5,21 +7,14 @@ pub mod tabs; impl<'a> eframe::App for App<'a> { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { - let background_frame = egui::Frame::default().fill(ONE_DARK.bg); + let _background_frame = egui::Frame::default().fill(ONE_DARK.bg); - egui::CentralPanel::default() - .frame(background_frame) - .show(ctx, |ui| { - ui.heading(self.name); + tabs::display_tabs(self, ctx); - egui::TopBottomPanel::top("wrap_app_top_bar").show(ctx, |ui| { - ui.horizontal_wrapped(|ui| { - ui.visuals_mut().button_frame = false; + match &mut self.tab_state.current_tab { + tabs::Tabs::METAR(tab) => tab.display_tab(self, ctx), + _ => () + } - // Insert Tabs - ui.selectable_label(false, format!("{:#?}", tabs::CURRENT_TAB)) - }); - }); - }); } } diff --git a/src/app/ui/tabs/metar_tab.rs b/src/app/ui/tabs/metar_tab.rs index e69de29..fa9b9a3 100644 --- a/src/app/ui/tabs/metar_tab.rs +++ b/src/app/ui/tabs/metar_tab.rs @@ -0,0 +1,22 @@ +use super::Tab; +use crate::App; + +#[derive(Debug, Default, PartialEq, Eq)] +pub struct MetarTab {} + +impl Tab for MetarTab { + fn display_tab(&mut self, app: &mut App, ctx: &egui::Context) { + + egui::CentralPanel::default() + .show(ctx, |ui| { + for icao in app.icaos.iter_mut() { + ui.text_edit_singleline(icao); + } + + if ui.button("Add More").clicked() || ui.input(|i| i.key_pressed(egui::Key::Tab)) { + app.icaos.push("".to_string()); + } + }); + + } +} \ No newline at end of file diff --git a/src/app/ui/tabs/mod.rs b/src/app/ui/tabs/mod.rs index f2008a2..599156e 100644 --- a/src/app/ui/tabs/mod.rs +++ b/src/app/ui/tabs/mod.rs @@ -1,11 +1,48 @@ pub mod metar_tab; -#[derive(Debug)] +// Strum Package to make enum Itterable +use strum::EnumIter; +use strum::IntoEnumIterator; + +use crate::App; + +#[derive(Debug, Default, EnumIter, PartialEq, Eq)] pub enum Tabs { - METAR, + #[default] + Home, + METAR(metar_tab::MetarTab), TAF, NOTAMS, Routing, } -pub static CURRENT_TAB: Tabs = Tabs::METAR; \ No newline at end of file +#[derive(Default)] +pub struct TabState { + pub current_tab: Tabs +} + +/// Tab traits that allow tab display on the main application window +pub trait Tab { + fn display_tab(&mut self, app: &mut App, ctx: &egui::Context); +} + +/// Function to draw main set of tabs on the window +pub fn display_tabs(app: &mut App, ctx: &egui::Context) { + egui::TopBottomPanel::top("app_top_bar").show(ctx, |ui| { + ui.horizontal_wrapped(|ui| { + ui.visuals_mut().button_frame = false; + + // Insert Tabs + for tab in Tabs::iter() { + let tab_label = ui.selectable_label( + tab == app.tab_state.current_tab, + format!("{:#?}", tab) + ); + + if tab_label.clicked() { + app.tab_state.current_tab = tab + } + } + }); + }); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index ad92573..71192ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ fn main() -> Result<(), eframe::Error> { // Setup the options for the default window let options = eframe::NativeOptions { - viewport: ViewportBuilder::default().with_inner_size(egui::vec2(300.0, 200.0)), + viewport: ViewportBuilder::default(), ..Default::default() };