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::theme::onedark::ONE_DARK;
@ -7,12 +5,16 @@ 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 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 {
tabs::TabTypes::METAR(tab) => tab.display_tab(ctx),
// Display tabs for main UI
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 {
fn display_tab(&mut self, ctx: &egui::Context) {
egui::CentralPanel::default().show(ctx, |ui| {
fn display_tab(&mut self, ctx: &egui::Context, frame: &egui::Frame) {
egui::CentralPanel::default().frame(*frame).show(ctx, |ui| {
for icao in self.icaos.iter_mut() {
ui.text_edit_singleline(icao);
}

View File

@ -6,9 +6,9 @@ use std::collections::HashMap;
use strum::EnumIter;
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 {
#[default]
Home,
@ -18,32 +18,43 @@ pub enum TabTypes {
Routing,
}
#[derive(Default)]
pub struct TabState {
current_tab: TabTypes,
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 {
/// 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) {
pub fn get_tab_state(&mut self, tab_type: TabTypes) -> Option<&mut Box<dyn Tab>> {
match self.tabs.get_mut(&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)
pub fn get_current_tab(&mut self) -> Option<&mut 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;
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
@ -54,24 +65,28 @@ impl TabState {
/// Tab traits that allow tab display on the main application window
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
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;
pub fn display_tabs(tab_state: &mut TabState, ctx: &egui::Context, frame: &egui::Frame) {
egui::TopBottomPanel::top("app_top_bar")
.frame(*frame)
.show(ctx, |ui| {
ui.horizontal_wrapped(|ui| {
ui.visuals_mut().button_frame = false;
// Insert Tabs
for tab in TabTypes::iter() {
let tab_label =
ui.selectable_label(tab == app.tab_state.current_tab, format!("{:?}", tab));
// Insert Tabs
for tab in TabTypes::iter() {
let tab_label = ui.selectable_label(
tab == tab_state.get_current_tab_type(),
format!("{:?}", tab),
);
if tab_label.clicked() {
app.tab_state.current_tab = tab
if tab_label.clicked() {
tab_state.change_tab(&tab);
}
}
}
});
});
});
}