Added tabbing to the main UI (Error currently occurs because of circular dependency on App object)

This commit is contained in:
Luke Else 2024-07-30 19:48:50 +01:00
parent 983a2eb7a4
commit fa919970e5
6 changed files with 75 additions and 18 deletions

View File

@ -9,3 +9,4 @@ edition = "2021"
egui = "0.28.1" egui = "0.28.1"
eframe = "0.28.1" eframe = "0.28.1"
tracing-subscriber = "0.3.16" tracing-subscriber = "0.3.16"
strum = { version = "0.26.3", features = ["derive"] }

View File

@ -3,7 +3,8 @@ mod ui;
/// Stuct to store the state of the running application /// Stuct to store the state of the running application
pub struct App<'a> { pub struct App<'a> {
name: &'a str, name: &'a str,
icaos: Vec<&'a str>, tab_state: ui::tabs::TabState,
icaos: Vec<String>,
metars: Vec<&'a str>, metars: Vec<&'a str>,
tafs: Vec<&'a str> tafs: Vec<&'a str>
} }
@ -13,6 +14,7 @@ impl <'a> Default for App<'a> {
fn default() -> Self { fn default() -> Self {
Self { Self {
name: "AvBag - Welcome", name: "AvBag - Welcome",
tab_state: ui::tabs::TabState::default(),
icaos: vec![], icaos: vec![],
metars: vec![], metars: vec![],
tafs: vec![] tafs: vec![]

View File

@ -1,3 +1,5 @@
use tabs::Tab;
use crate::app::App; use crate::app::App;
use crate::theme::onedark::ONE_DARK; use crate::theme::onedark::ONE_DARK;
@ -5,21 +7,14 @@ pub mod tabs;
impl<'a> eframe::App for App<'a> { impl<'a> eframe::App for App<'a> {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { 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() tabs::display_tabs(self, ctx);
.frame(background_frame)
.show(ctx, |ui| {
ui.heading(self.name);
egui::TopBottomPanel::top("wrap_app_top_bar").show(ctx, |ui| { match &mut self.tab_state.current_tab {
ui.horizontal_wrapped(|ui| { tabs::Tabs::METAR(tab) => tab.display_tab(self, ctx),
ui.visuals_mut().button_frame = false; _ => ()
}
// Insert Tabs
ui.selectable_label(false, format!("{:#?}", tabs::CURRENT_TAB))
});
});
});
} }
} }

View File

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

View File

@ -1,11 +1,48 @@
pub mod metar_tab; 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 { pub enum Tabs {
METAR, #[default]
Home,
METAR(metar_tab::MetarTab),
TAF, TAF,
NOTAMS, NOTAMS,
Routing, 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
}
}
});
});
}

View File

@ -10,7 +10,7 @@ fn main() -> Result<(), eframe::Error> {
// Setup the options for the default window // Setup the options for the default window
let options = eframe::NativeOptions { let options = eframe::NativeOptions {
viewport: ViewportBuilder::default().with_inner_size(egui::vec2(300.0, 200.0)), viewport: ViewportBuilder::default(),
..Default::default() ..Default::default()
}; };