From bb051eec61e249478217ba6fcbcd19508c5b03b7 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Fri, 21 Feb 2025 14:13:26 +0000 Subject: [PATCH] Added async traits crate to code to allow dynamic dispatch with async functions in traits --- Cargo.toml | 2 ++ src/app/ui/mod.rs | 14 +++++++++++--- src/app/ui/tabs/metar_tab.rs | 4 +++- src/app/ui/tabs/mod.rs | 6 +++++- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 79c3886..2bdf209 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,5 @@ tracing-subscriber = "0.3.16" strum = { version = "0.26.3", features = ["derive"] } tokio = { version = "1.39.2", features = ["full"] } tokio-macros = { version = "2.4.0" } +async-trait = "0.1.86" +futures = "0.3.31" diff --git a/src/app/ui/mod.rs b/src/app/ui/mod.rs index 4ea2c3d..054e44d 100644 --- a/src/app/ui/mod.rs +++ b/src/app/ui/mod.rs @@ -1,4 +1,5 @@ use crate::app::App; +use futures::executor::block_on; use crate::theme::onedark::ONE_DARK; pub mod tabs; @@ -11,10 +12,17 @@ impl<'a> eframe::App for App<'a> { // Display tabs for main UI 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 - _ => (), - } + None => { + println!("No tab available"); + }, + }; + + () } } diff --git a/src/app/ui/tabs/metar_tab.rs b/src/app/ui/tabs/metar_tab.rs index 60316de..48f3648 100644 --- a/src/app/ui/tabs/metar_tab.rs +++ b/src/app/ui/tabs/metar_tab.rs @@ -1,4 +1,5 @@ use egui::Key; +use async_trait::async_trait; use crate::app::metar::Metar; @@ -21,8 +22,10 @@ impl Default for MetarTab { } } +#[async_trait] impl Tab for MetarTab { async fn display_tab(&mut self, ctx: &egui::Context, frame: &egui::Frame) { + println!("Displaying METAR Tab"); egui::CentralPanel::default().frame(*frame).show(ctx, |ui| { // Given Key pressed, place focus on next item 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 { self.icaos.push("".to_string()); - self.metar.api.make_request(String::from("google.com")); } }); } diff --git a/src/app/ui/tabs/mod.rs b/src/app/ui/tabs/mod.rs index 305a915..9a4a69f 100644 --- a/src/app/ui/tabs/mod.rs +++ b/src/app/ui/tabs/mod.rs @@ -6,6 +6,9 @@ use std::collections::HashMap; use strum::EnumIter; use strum::IntoEnumIterator; +// Allow Async Traits +use async_trait::async_trait; + use self::metar_tab::MetarTab; #[derive(Clone, Copy, Debug, Default, EnumIter, Eq, Hash, PartialEq)] @@ -48,7 +51,7 @@ impl TabState { } /// Get the currently selected Tab - pub fn get_current_tab(&mut self) -> Option<&mut Box> { + pub fn get_current_tab(&mut self) -> Option<&mut Box> { self.get_tab_state(self.current_tab) } @@ -64,6 +67,7 @@ impl TabState { } /// Tab traits that allow tab display on the main application window +#[async_trait] pub trait Tab { async fn display_tab(&mut self, ctx: &egui::Context, frame: &egui::Frame); }