generated from luke-else/egui-template
Fixed circular dependency on appstate
This commit is contained in:
parent
fa919970e5
commit
98d2c271ef
@ -2,22 +2,16 @@ mod ui;
|
||||
|
||||
/// Stuct to store the state of the running application
|
||||
pub struct App<'a> {
|
||||
name: &'a str,
|
||||
pub name: &'a str,
|
||||
tab_state: ui::tabs::TabState,
|
||||
icaos: Vec<String>,
|
||||
metars: Vec<&'a str>,
|
||||
tafs: Vec<&'a str>
|
||||
}
|
||||
|
||||
impl <'a> Default for App<'a> {
|
||||
impl<'a> Default for App<'a> {
|
||||
/// Creates the default startup state for the app;
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
name: "AvBag - Welcome",
|
||||
tab_state: ui::tabs::TabState::default(),
|
||||
icaos: vec![],
|
||||
metars: vec![],
|
||||
tafs: vec![]
|
||||
}
|
||||
}
|
||||
}
|
@ -12,9 +12,8 @@ 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(self, ctx),
|
||||
_ => ()
|
||||
tabs::Tabs::METAR(tab) => tab.display_tab(ctx),
|
||||
_ => (),
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,22 +1,21 @@
|
||||
use super::Tab;
|
||||
use crate::App;
|
||||
|
||||
#[derive(Debug, Default, PartialEq, Eq)]
|
||||
pub struct MetarTab {}
|
||||
pub struct MetarTab {
|
||||
pub icaos: Vec<String>,
|
||||
pub metars: Vec<String>,
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
});
|
||||
fn display_tab(&mut self, ctx: &egui::Context) {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
for icao in self.icaos.iter_mut() {
|
||||
ui.text_edit_singleline(icao);
|
||||
}
|
||||
|
||||
if ui.button("Add More").clicked() || ui.input(|i| i.key_pressed(egui::Key::Tab)) {
|
||||
self.icaos.push("".to_string());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
pub mod metar_tab;
|
||||
|
||||
use std::fmt::Display;
|
||||
|
||||
// Strum Package to make enum Itterable
|
||||
use strum::EnumIter;
|
||||
use strum::IntoEnumIterator;
|
||||
@ -16,14 +18,23 @@ pub enum Tabs {
|
||||
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
|
||||
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);
|
||||
fn display_tab(&mut self, ctx: &egui::Context);
|
||||
}
|
||||
|
||||
/// Function to draw main set of tabs on the window
|
||||
@ -34,10 +45,8 @@ pub fn display_tabs(app: &mut App, ctx: &egui::Context) {
|
||||
|
||||
// Insert Tabs
|
||||
for tab in Tabs::iter() {
|
||||
let tab_label = ui.selectable_label(
|
||||
tab == app.tab_state.current_tab,
|
||||
format!("{:#?}", tab)
|
||||
);
|
||||
let tab_label =
|
||||
ui.selectable_label(tab == app.tab_state.current_tab, format!("{}", tab));
|
||||
|
||||
if tab_label.clicked() {
|
||||
app.tab_state.current_tab = tab
|
||||
|
@ -14,10 +14,9 @@ fn main() -> Result<(), eframe::Error> {
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// Create a box to the app and then pass into app creator
|
||||
let app = Box::new(App::default());
|
||||
|
||||
//Start rendering and run the application
|
||||
eframe::run_native(
|
||||
"AvBag",
|
||||
options,
|
||||
Box::new(|_cc| Ok(Box::new(App::default()))),
|
||||
)
|
||||
eframe::run_native(app.name, options, Box::new(|_cc| Ok(app)))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user