Added async traits crate to code to allow dynamic dispatch with async functions in traits
Some checks failed
Continuous integration / Check (push) Failing after 1m53s
Continuous integration / Test Suite (push) Failing after 2m23s
Continuous integration / Rustfmt (push) Failing after 36s
Continuous integration / Clippy (push) Failing after 1m53s
Continuous integration / build (push) Failing after 2m16s

This commit is contained in:
Luke Else 2025-02-21 14:13:26 +00:00
parent 02178d83a2
commit bb051eec61
4 changed files with 21 additions and 5 deletions

View File

@ -12,3 +12,5 @@ tracing-subscriber = "0.3.16"
strum = { version = "0.26.3", features = ["derive"] } strum = { version = "0.26.3", features = ["derive"] }
tokio = { version = "1.39.2", features = ["full"] } tokio = { version = "1.39.2", features = ["full"] }
tokio-macros = { version = "2.4.0" } tokio-macros = { version = "2.4.0" }
async-trait = "0.1.86"
futures = "0.3.31"

View File

@ -1,4 +1,5 @@
use crate::app::App; use crate::app::App;
use futures::executor::block_on;
use crate::theme::onedark::ONE_DARK; use crate::theme::onedark::ONE_DARK;
pub mod tabs; pub mod tabs;
@ -11,10 +12,17 @@ impl<'a> eframe::App for App<'a> {
// Display tabs for main UI // Display tabs for main UI
match self.tab_state.get_current_tab() { match self.tab_state.get_current_tab() {
Some(tab) => tab.display_tab(ctx, &frame), Some(tab) => {
println!("Displaying tab");
block_on(tab.display_tab(ctx, &frame));
},
// What to do if the tab is not available // What to do if the tab is not available
_ => (), None => {
} println!("No tab available");
},
};
()
} }
} }

View File

@ -1,4 +1,5 @@
use egui::Key; use egui::Key;
use async_trait::async_trait;
use crate::app::metar::Metar; use crate::app::metar::Metar;
@ -21,8 +22,10 @@ impl Default for MetarTab {
} }
} }
#[async_trait]
impl Tab for MetarTab { impl Tab for MetarTab {
async fn display_tab(&mut self, ctx: &egui::Context, frame: &egui::Frame) { async fn display_tab(&mut self, ctx: &egui::Context, frame: &egui::Frame) {
println!("Displaying METAR Tab");
egui::CentralPanel::default().frame(*frame).show(ctx, |ui| { egui::CentralPanel::default().frame(*frame).show(ctx, |ui| {
// Given Key pressed, place focus on next item // Given Key pressed, place focus on next item
let new_icao_focus: bool = ui.input(|i| i.key_pressed(Key::Space)); let new_icao_focus: bool = ui.input(|i| i.key_pressed(Key::Space));
@ -33,7 +36,6 @@ impl Tab for MetarTab {
if ui.button("Add More").clicked() || new_icao_focus { if ui.button("Add More").clicked() || new_icao_focus {
self.icaos.push("".to_string()); self.icaos.push("".to_string());
self.metar.api.make_request(String::from("google.com"));
} }
}); });
} }

View File

@ -6,6 +6,9 @@ use std::collections::HashMap;
use strum::EnumIter; use strum::EnumIter;
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
// Allow Async Traits
use async_trait::async_trait;
use self::metar_tab::MetarTab; use self::metar_tab::MetarTab;
#[derive(Clone, Copy, Debug, Default, EnumIter, Eq, Hash, PartialEq)] #[derive(Clone, Copy, Debug, Default, EnumIter, Eq, Hash, PartialEq)]
@ -48,7 +51,7 @@ impl TabState {
} }
/// Get the currently selected Tab /// Get the currently selected Tab
pub fn get_current_tab(&mut self) -> Option<&mut Box<dyn Tab + Send>> { 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)
} }
@ -64,6 +67,7 @@ impl TabState {
} }
/// Tab traits that allow tab display on the main application window /// Tab traits that allow tab display on the main application window
#[async_trait]
pub trait Tab { pub trait Tab {
async fn display_tab(&mut self, ctx: &egui::Context, frame: &egui::Frame); async fn display_tab(&mut self, ctx: &egui::Context, frame: &egui::Frame);
} }