Fixed build error. Generic TabStates are now functional

This commit is contained in:
Luke Else 2024-08-02 16:18:03 +01:00
parent bfc34796b2
commit 566603e961
3 changed files with 50 additions and 33 deletions

View File

@ -1,5 +1,3 @@
use tabs::Tab;
use crate::app::App; use crate::app::App;
use crate::theme::onedark::ONE_DARK; use crate::theme::onedark::ONE_DARK;
@ -7,12 +5,16 @@ 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 frame = egui::Frame::default().fill(ONE_DARK.bg);
tabs::display_tabs(self, ctx); // Display the top bar of tabs
tabs::display_tabs(&mut self.tab_state, ctx, &frame);
match &mut self.tab_state.current_tab { // Display tabs for main UI
tabs::TabTypes::METAR(tab) => tab.display_tab(ctx), match self.tab_state.get_current_tab() {
Some(tab) => tab.display_tab(ctx, &frame),
// What to do if the tab is not available
_ => (), _ => (),
} }
} }

View File

@ -7,8 +7,8 @@ pub struct MetarTab {
} }
impl Tab for MetarTab { impl Tab for MetarTab {
fn display_tab(&mut self, ctx: &egui::Context) { fn display_tab(&mut self, ctx: &egui::Context, frame: &egui::Frame) {
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().frame(*frame).show(ctx, |ui| {
for icao in self.icaos.iter_mut() { for icao in self.icaos.iter_mut() {
ui.text_edit_singleline(icao); ui.text_edit_singleline(icao);
} }

View File

@ -6,9 +6,9 @@ use std::collections::HashMap;
use strum::EnumIter; use strum::EnumIter;
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
use crate::App; use self::metar_tab::MetarTab;
#[derive(Debug, Default, EnumIter, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, Default, EnumIter, Eq, Hash, PartialEq)]
pub enum TabTypes { pub enum TabTypes {
#[default] #[default]
Home, Home,
@ -18,32 +18,43 @@ pub enum TabTypes {
Routing, Routing,
} }
#[derive(Default)]
pub struct TabState { pub struct TabState {
current_tab: TabTypes, current_tab: TabTypes,
tabs: HashMap<TabTypes, Box<dyn Tab>>, tabs: HashMap<TabTypes, Box<dyn Tab>>,
} }
impl Default for TabState {
fn default() -> Self {
let mut ts = Self {
current_tab: TabTypes::default(),
tabs: HashMap::new(),
};
// Add each tab to the map
ts.tabs
.insert(TabTypes::METAR, Box::new(MetarTab::default()));
ts
}
}
impl TabState { impl TabState {
/// Get the Tab based on a given Tab Type /// Get the Tab based on a given Tab Type
pub fn get_tab_state(&self, tab_type: &TabTypes) -> Option<&Box<dyn Tab>> { pub fn get_tab_state(&mut self, tab_type: TabTypes) -> Option<&mut Box<dyn Tab>> {
match self.tabs.get(tab_type) { match self.tabs.get_mut(&tab_type) {
Some(tab) => Some(tab), Some(tab) => Some(tab),
None => None, None => None,
} }
} }
/// Set the state of a given tab
pub fn set_tab_state(&mut self, tab_type: &TabTypes) {}
/// Get the currently selected Tab /// Get the currently selected Tab
pub fn get_current_tab(&self) -> Option<&Box<dyn Tab>> { pub fn get_current_tab(&mut self) -> Option<&mut Box<dyn Tab>> {
self.get_tab_state(&self.current_tab) self.get_tab_state(self.current_tab)
} }
/// Get the type of the current Tab /// Get the type of the current Tab
pub fn get_current_tab_type(&self) -> &TabTypes { pub fn get_current_tab_type(&self) -> TabTypes {
return &self.current_tab; return self.current_tab;
} }
/// Change the current tab to a different tab type, saving the state of the current tab /// Change the current tab to a different tab type, saving the state of the current tab
@ -54,24 +65,28 @@ impl TabState {
/// Tab traits that allow tab display on the main application window /// Tab traits that allow tab display on the main application window
pub trait Tab { pub trait Tab {
fn display_tab(&mut self, ctx: &egui::Context); fn display_tab(&mut self, ctx: &egui::Context, frame: &egui::Frame);
} }
/// Function to draw main set of tabs on the window /// Function to draw main set of tabs on the window
pub fn display_tabs(app: &mut App, ctx: &egui::Context) { pub fn display_tabs(tab_state: &mut TabState, ctx: &egui::Context, frame: &egui::Frame) {
egui::TopBottomPanel::top("app_top_bar").show(ctx, |ui| { egui::TopBottomPanel::top("app_top_bar")
ui.horizontal_wrapped(|ui| { .frame(*frame)
ui.visuals_mut().button_frame = false; .show(ctx, |ui| {
ui.horizontal_wrapped(|ui| {
ui.visuals_mut().button_frame = false;
// Insert Tabs // Insert Tabs
for tab in TabTypes::iter() { for tab in TabTypes::iter() {
let tab_label = let tab_label = ui.selectable_label(
ui.selectable_label(tab == app.tab_state.current_tab, format!("{:?}", tab)); tab == tab_state.get_current_tab_type(),
format!("{:?}", tab),
);
if tab_label.clicked() { if tab_label.clicked() {
app.tab_state.current_tab = tab tab_state.change_tab(&tab);
}
} }
} });
}); });
});
} }