generated from luke-else/egui-template
	Added tabbing to the main UI (Error currently occurs because of circular dependency on App object)
This commit is contained in:
		@@ -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<String>,
 | 
			
		||||
    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![]
 | 
			
		||||
 
 | 
			
		||||
@@ -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))
 | 
			
		||||
                    });
 | 
			
		||||
                });
 | 
			
		||||
            });
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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());
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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;
 | 
			
		||||
#[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
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
@@ -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()
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user