Started adding logic to the tab handler

This commit is contained in:
Luke Else 2024-08-02 15:33:37 +01:00
parent 98d2c271ef
commit bfc34796b2
2 changed files with 37 additions and 17 deletions

View File

@ -12,7 +12,7 @@ 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(ctx),
tabs::TabTypes::METAR(tab) => tab.display_tab(ctx),
_ => (),
}
}

View File

@ -1,6 +1,6 @@
pub mod metar_tab;
use std::fmt::Display;
use std::collections::HashMap;
// Strum Package to make enum Itterable
use strum::EnumIter;
@ -8,28 +8,48 @@ use strum::IntoEnumIterator;
use crate::App;
#[derive(Debug, Default, EnumIter, PartialEq, Eq)]
pub enum Tabs {
#[derive(Debug, Default, EnumIter, PartialEq, Eq, Hash)]
pub enum TabTypes {
#[default]
Home,
METAR(metar_tab::MetarTab),
METAR,
TAF,
NOTAMS,
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,
current_tab: TabTypes,
tabs: HashMap<TabTypes, Box<dyn Tab>>,
}
impl TabState {
/// Get the Tab based on a given Tab Type
pub fn get_tab_state(&self, tab_type: &TabTypes) -> Option<&Box<dyn Tab>> {
match self.tabs.get(tab_type) {
Some(tab) => Some(tab),
None => None,
}
}
/// Set the state of a given tab
pub fn set_tab_state(&mut self, tab_type: &TabTypes) {}
/// Get the currently selected Tab
pub fn get_current_tab(&self) -> Option<&Box<dyn Tab>> {
self.get_tab_state(&self.current_tab)
}
/// Get the type of the current Tab
pub fn get_current_tab_type(&self) -> &TabTypes {
return &self.current_tab;
}
/// Change the current tab to a different tab type, saving the state of the current tab
pub fn change_tab(&mut self, new_tab_type: &TabTypes) {
self.current_tab = *new_tab_type;
}
}
/// Tab traits that allow tab display on the main application window
@ -44,9 +64,9 @@ pub fn display_tabs(app: &mut App, ctx: &egui::Context) {
ui.visuals_mut().button_frame = false;
// Insert Tabs
for tab in Tabs::iter() {
for tab in TabTypes::iter() {
let tab_label =
ui.selectable_label(tab == app.tab_state.current_tab, format!("{}", tab));
ui.selectable_label(tab == app.tab_state.current_tab, format!("{:?}", tab));
if tab_label.clicked() {
app.tab_state.current_tab = tab