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
|
/// Stuct to store the state of the running application
|
||||||
pub struct App<'a> {
|
pub struct App<'a> {
|
||||||
name: &'a str,
|
pub name: &'a str,
|
||||||
tab_state: ui::tabs::TabState,
|
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;
|
/// Creates the default startup state for the app;
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
name: "AvBag - Welcome",
|
name: "AvBag - Welcome",
|
||||||
tab_state: ui::tabs::TabState::default(),
|
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);
|
tabs::display_tabs(self, ctx);
|
||||||
|
|
||||||
match &mut self.tab_state.current_tab {
|
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 super::Tab;
|
||||||
use crate::App;
|
|
||||||
|
|
||||||
#[derive(Debug, Default, PartialEq, Eq)]
|
#[derive(Debug, Default, PartialEq, Eq)]
|
||||||
pub struct MetarTab {}
|
pub struct MetarTab {
|
||||||
|
pub icaos: Vec<String>,
|
||||||
|
pub metars: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
impl Tab for MetarTab {
|
impl Tab for MetarTab {
|
||||||
fn display_tab(&mut self, app: &mut App, ctx: &egui::Context) {
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
egui::CentralPanel::default()
|
if ui.button("Add More").clicked() || ui.input(|i| i.key_pressed(egui::Key::Tab)) {
|
||||||
.show(ctx, |ui| {
|
self.icaos.push("".to_string());
|
||||||
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());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
pub mod metar_tab;
|
pub mod metar_tab;
|
||||||
|
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
// Strum Package to make enum Itterable
|
// Strum Package to make enum Itterable
|
||||||
use strum::EnumIter;
|
use strum::EnumIter;
|
||||||
use strum::IntoEnumIterator;
|
use strum::IntoEnumIterator;
|
||||||
@ -16,14 +18,23 @@ pub enum Tabs {
|
|||||||
Routing,
|
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)]
|
#[derive(Default)]
|
||||||
pub struct TabState {
|
pub struct TabState {
|
||||||
pub current_tab: Tabs
|
pub current_tab: Tabs,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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, app: &mut App, ctx: &egui::Context);
|
fn display_tab(&mut self, ctx: &egui::Context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Function to draw main set of tabs on the window
|
/// 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
|
// Insert Tabs
|
||||||
for tab in Tabs::iter() {
|
for tab in Tabs::iter() {
|
||||||
let tab_label = ui.selectable_label(
|
let tab_label =
|
||||||
tab == app.tab_state.current_tab,
|
ui.selectable_label(tab == app.tab_state.current_tab, format!("{}", tab));
|
||||||
format!("{:#?}", tab)
|
|
||||||
);
|
|
||||||
|
|
||||||
if tab_label.clicked() {
|
if tab_label.clicked() {
|
||||||
app.tab_state.current_tab = tab
|
app.tab_state.current_tab = tab
|
||||||
@ -45,4 +54,4 @@ pub fn display_tabs(app: &mut App, ctx: &egui::Context) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -14,10 +14,9 @@ fn main() -> Result<(), eframe::Error> {
|
|||||||
..Default::default()
|
..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
|
//Start rendering and run the application
|
||||||
eframe::run_native(
|
eframe::run_native(app.name, options, Box::new(|_cc| Ok(app)))
|
||||||
"AvBag",
|
|
||||||
options,
|
|
||||||
Box::new(|_cc| Ok(Box::new(App::default()))),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user