From 98d2c271efdeee130670ce493e7523224ea80126 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Fri, 2 Aug 2024 10:42:30 +0100 Subject: [PATCH] Fixed circular dependency on appstate --- src/app/mod.rs | 12 +++--------- src/app/ui/mod.rs | 5 ++--- src/app/ui/tabs/metar_tab.rs | 29 ++++++++++++++--------------- src/app/ui/tabs/mod.rs | 23 ++++++++++++++++------- src/main.rs | 9 ++++----- 5 files changed, 39 insertions(+), 39 deletions(-) diff --git a/src/app/mod.rs b/src/app/mod.rs index 605cf71..cd5bbcb 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -2,22 +2,16 @@ mod ui; /// Stuct to store the state of the running application pub struct App<'a> { - name: &'a str, + pub name: &'a str, tab_state: ui::tabs::TabState, - icaos: Vec, - metars: Vec<&'a str>, - tafs: Vec<&'a str> } -impl <'a> Default for App<'a> { +impl<'a> Default for App<'a> { /// Creates the default startup state for the app; fn default() -> Self { Self { name: "AvBag - Welcome", tab_state: ui::tabs::TabState::default(), - icaos: vec![], - metars: vec![], - tafs: vec![] } } -} \ No newline at end of file +} diff --git a/src/app/ui/mod.rs b/src/app/ui/mod.rs index 8788c54..45551b7 100644 --- a/src/app/ui/mod.rs +++ b/src/app/ui/mod.rs @@ -12,9 +12,8 @@ impl<'a> eframe::App for App<'a> { tabs::display_tabs(self, ctx); match &mut self.tab_state.current_tab { - tabs::Tabs::METAR(tab) => tab.display_tab(self, ctx), - _ => () + tabs::Tabs::METAR(tab) => tab.display_tab(ctx), + _ => (), } - } } diff --git a/src/app/ui/tabs/metar_tab.rs b/src/app/ui/tabs/metar_tab.rs index fa9b9a3..29635ca 100644 --- a/src/app/ui/tabs/metar_tab.rs +++ b/src/app/ui/tabs/metar_tab.rs @@ -1,22 +1,21 @@ use super::Tab; -use crate::App; #[derive(Debug, Default, PartialEq, Eq)] -pub struct MetarTab {} +pub struct MetarTab { + pub icaos: Vec, + pub metars: Vec, +} impl Tab for MetarTab { - fn display_tab(&mut self, app: &mut App, ctx: &egui::Context) { + fn display_tab(&mut self, ctx: &egui::Context) { + egui::CentralPanel::default().show(ctx, |ui| { + for icao in self.icaos.iter_mut() { + ui.text_edit_singleline(icao); + } - 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()); - } - }); - + if ui.button("Add More").clicked() || ui.input(|i| i.key_pressed(egui::Key::Tab)) { + self.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 599156e..f3ffbc5 100644 --- a/src/app/ui/tabs/mod.rs +++ b/src/app/ui/tabs/mod.rs @@ -1,5 +1,7 @@ pub mod metar_tab; +use std::fmt::Display; + // Strum Package to make enum Itterable use strum::EnumIter; use strum::IntoEnumIterator; @@ -16,14 +18,23 @@ pub enum Tabs { Routing, } +impl Display for Tabs { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::METAR(_) => write!(f, "METAR"), + _ => write!(f, "{}", format!("{:?}", self)), + } + } +} + #[derive(Default)] pub struct TabState { - pub current_tab: Tabs + 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); + fn display_tab(&mut self, ctx: &egui::Context); } /// Function to draw main set of tabs on the window @@ -34,10 +45,8 @@ pub fn display_tabs(app: &mut App, ctx: &egui::Context) { // Insert Tabs for tab in Tabs::iter() { - let tab_label = ui.selectable_label( - tab == app.tab_state.current_tab, - format!("{:#?}", tab) - ); + let tab_label = + ui.selectable_label(tab == app.tab_state.current_tab, format!("{}", tab)); if tab_label.clicked() { app.tab_state.current_tab = tab @@ -45,4 +54,4 @@ pub fn display_tabs(app: &mut App, ctx: &egui::Context) { } }); }); -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 71192ec..7d87cb8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,10 +14,9 @@ fn main() -> Result<(), eframe::Error> { ..Default::default() }; + // Create a box to the app and then pass into app creator + let app = Box::new(App::default()); + //Start rendering and run the application - eframe::run_native( - "AvBag", - options, - Box::new(|_cc| Ok(Box::new(App::default()))), - ) + eframe::run_native(app.name, options, Box::new(|_cc| Ok(app))) }