From d9477626e184a1e5c1902e827750b4709a8c9a54 Mon Sep 17 00:00:00 2001 From: Hubald Verzijl Date: Sun, 7 Sep 2025 19:10:14 +0200 Subject: [PATCH] Included active tab variable to stop amplifier after tab change. --- src/app.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/src/app.rs b/src/app.rs index cfa9126..154269b 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,3 +1,5 @@ +use log::info; + use std::sync::{Arc, Mutex}; use std::sync::atomic::{AtomicBool, Ordering}; @@ -12,6 +14,13 @@ use crate::plot::TimeSeriesPlot; use crate::signals::SingleFrequencySignal; +#[derive(Clone, Copy,Debug, PartialEq, Eq)] +enum TabActive { + Single, + Multi, + Shortcuts, +} + pub struct App { tree: DockState, tab_viewer: TabViewer, @@ -22,6 +31,7 @@ pub struct App { pub phase_series: Arc>, pub connected: Arc, pub on: Arc>, + tab_active: TabActive, pub data_frequency: Arc, pub single_frequency: Arc> } @@ -146,6 +156,7 @@ impl App { let phase_series = Arc::new(Mutex::new(TimeSeriesPlot::new())); let single_frequency = Arc::new(Mutex::new(50000)); let on = Arc::new(Mutex::new(true)); + let tab_active = TabActive::Single; // Step 2: Now we can initialize tab_viewer let tab_viewer = TabViewer { @@ -170,6 +181,7 @@ impl App { phase_series, connected: Arc::new(AtomicBool::new(false)), on, + tab_active, data_frequency: Arc::new(AtomicF32::new(0.0)), single_frequency, }; @@ -179,17 +191,22 @@ impl App { } pub fn update_start_stop(&self) { - match *self.on.lock().unwrap() { - true => { + match (self.tab_active, *self.on.lock().unwrap()) { + (TabActive::Single, true) => { if let Err(e) = self.run_impedancemeter_tx.try_send(SingleFrequencySignal::Start(*self.single_frequency.lock().unwrap())) { eprintln!("Failed to send start command: {:?}", e); } }, - false => { + (TabActive::Multi, true) => { + // For future use + info!("Multi frequency mode not implemented yet"); + }, + (_, false) => { if let Err(e) = self.run_impedancemeter_tx.try_send(SingleFrequencySignal::Stop) { eprintln!("Failed to send stop command: {:?}", e); } }, + (_, _) => {} } } @@ -256,7 +273,39 @@ impl eframe::App for App { .show_close_buttons(false) .show_inside(ui, &mut self.tab_viewer); }); - + + // Do something when a tab is changed + if let Some((_, tab)) = self.tree.find_active_focused() { + match tab.as_str() { + "Single" => { + if self.tab_active != TabActive::Single { + self.tab_active = TabActive::Single; + *self.on.lock().unwrap() = false; + self.update_start_stop(); + info!("Switched to Single tab"); + } + } + "Multi" => { + if self.tab_active != TabActive::Multi { + self.tab_active = TabActive::Multi; + *self.on.lock().unwrap() = false; + self.update_start_stop(); + info!("Switched to Multi tab"); + } + } + "Shortcuts" => { + // if self.tab_active != TabActive::Shortcuts { + // self.tab_active = TabActive::Shortcuts; + // *self.on.lock().unwrap() = false; + // self.update_start_stop(); + // info!("Switched to Shortcuts tab"); + // } + } + _ => { + } + } + } + // CMD- or control-W to close window if ctx.input(|i| i.modifiers.cmd_ctrl_matches(Modifiers::COMMAND)) && ctx.input(|i| i.key_pressed(Key::W))