Skip to content
Snippets Groups Projects
Commit 6f5760d2 authored by Martin Morin's avatar Martin Morin
Browse files

Change from enum to trait objects for subapps

parent b2c73725
No related branches found
No related tags found
No related merge requests found
...@@ -6,13 +6,13 @@ use basic_print::basic_print; // basic print for print-debugging ...@@ -6,13 +6,13 @@ use basic_print::basic_print; // basic print for print-debugging
pub struct ControlApp { pub struct ControlApp {
cur_app_idx: Option<usize>, cur_app_idx: Option<usize>,
apps: Vec<(String,SubApp)>, apps: Vec<Box<dyn CentralApp>>,
} }
#[derive(PartialEq)] trait CentralApp {
enum SubApp { fn draw_app(&mut self, ui: &mut egui::Ui);
PolePos,
FreqResp, fn get_label(&self) -> &str;
} }
impl ControlApp { impl ControlApp {
...@@ -20,12 +20,19 @@ impl ControlApp { ...@@ -20,12 +20,19 @@ impl ControlApp {
// This is also where you can customized the look at feel of egui using // This is also where you can customized the look at feel of egui using
// `cc.egui_ctx.set_visuals` and `cc.egui_ctx.set_fonts`. // `cc.egui_ctx.set_visuals` and `cc.egui_ctx.set_fonts`.
let pp = ("Pole Positioning".to_string(), SubApp::PolePos); let apps: Vec<Box<dyn CentralApp>> = vec![
let fr = ("Frequency Response".to_string(), SubApp::FreqResp); Box::new(PolePos{
label: "Pole Positioning".to_string(),
}),
Box::new(FreqResp{
label: "Frequency Response".to_string(),
}),
];
ControlApp{cur_app_idx: None, apps: vec![pp,fr] } ControlApp{cur_app_idx: None, apps }
} }
fn top_bar(&mut self, ui: &mut egui::Ui) -> bool { fn top_bar(&mut self, ui: &mut egui::Ui) -> bool {
#[allow(unused_mut)] #[allow(unused_mut)]
let mut quit = false; let mut quit = false;
...@@ -34,12 +41,12 @@ impl ControlApp { ...@@ -34,12 +41,12 @@ impl ControlApp {
ui.heading("Control Apps"); ui.heading("Control Apps");
ui.separator(); ui.separator();
for (idx, (label, _app)) in self.apps.iter().enumerate() { for (idx, app) in self.apps.iter().enumerate() {
let checked = match self.cur_app_idx { let checked = match self.cur_app_idx {
Some(cur) => cur == idx, Some(cur) => cur == idx,
None => false, None => false,
}; };
if ui.selectable_label(checked, label).clicked() { if ui.selectable_label(checked, app.get_label()).clicked() {
if checked { if checked {
self.cur_app_idx = None; self.cur_app_idx = None;
} else { } else {
...@@ -67,6 +74,7 @@ impl ControlApp { ...@@ -67,6 +74,7 @@ impl ControlApp {
} }
impl eframe::App for ControlApp { impl eframe::App for ControlApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
...@@ -80,8 +88,8 @@ impl eframe::App for ControlApp { ...@@ -80,8 +88,8 @@ impl eframe::App for ControlApp {
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
ui.centered_and_justified( |ui| { ui.centered_and_justified( |ui| {
match self.cur_app_idx { match self.cur_app_idx {
Some(idx) => ui.label(self.apps[idx].0.to_string()), None => {ui.label("Select an application in the bar above.");},
None => ui.label("Select an application in the bar above."), Some(idx) => {self.apps[idx].draw_app(ui);},
}; };
}); });
}); });
...@@ -89,6 +97,40 @@ impl eframe::App for ControlApp { ...@@ -89,6 +97,40 @@ impl eframe::App for ControlApp {
} }
struct PolePos{
label: String,
}
impl CentralApp for PolePos {
fn draw_app(&mut self, ui: &mut egui::Ui) {
ui.label("In app, Pole Pos");
}
fn get_label(&self) -> &str {
&self.label
}
}
struct FreqResp{
label: String,
}
impl CentralApp for FreqResp {
fn draw_app(&mut self, ui: &mut egui::Ui) {
ui.label("Frequency response app currently not implemented.");
}
fn get_label(&self) -> &str {
&self.label
}
}
// impl eframe::App for TemplateApp { // impl eframe::App for TemplateApp {
// fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { // fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
// let Self { label, value } = self; // let Self { label, value } = self;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment