mirror of
https://github.com/hubaldv/bioz-host-rs.git
synced 2025-12-06 05:11:17 +00:00
Add egui dock.
This commit is contained in:
173
src/app.rs
173
src/app.rs
@@ -6,12 +6,15 @@ use tokio::{sync::mpsc::{Sender}};
|
||||
|
||||
use eframe::egui::{self, Button, Color32, DragValue, Key, Label, Layout, Modifiers};
|
||||
use egui_plot::{Corner, Legend, Line, Plot, PlotPoints, Points, PlotBounds};
|
||||
use egui_dock::tab_viewer::OnCloseResponse;
|
||||
use egui_dock::{DockArea, DockState, Style};
|
||||
|
||||
use crate::plot::TimeSeriesPlot;
|
||||
|
||||
use crate::signals::SingleFrequencySignal;
|
||||
|
||||
pub struct App {
|
||||
tree: DockState<String>,
|
||||
run_impedancemeter_tx: Sender<SingleFrequencySignal>,
|
||||
pub magnitude: Arc<Mutex<f32>>,
|
||||
pub phase: Arc<Mutex<f32>>,
|
||||
@@ -23,9 +26,94 @@ pub struct App {
|
||||
pub single_frequency: u32
|
||||
}
|
||||
|
||||
struct TabViewer {
|
||||
magnitude: Arc<Mutex<f32>>,
|
||||
phase: Arc<Mutex<f32>>,
|
||||
magnitude_series: Arc<Mutex<TimeSeriesPlot>>,
|
||||
phase_series: Arc<Mutex<TimeSeriesPlot>>,
|
||||
}
|
||||
|
||||
impl egui_dock::TabViewer for TabViewer {
|
||||
type Tab = String;
|
||||
|
||||
fn title(&mut self, tab: &mut Self::Tab) -> egui::WidgetText {
|
||||
(&*tab).into()
|
||||
}
|
||||
|
||||
fn ui(&mut self, ui: &mut egui::Ui, tab: &mut Self::Tab) {
|
||||
egui::Frame::default().inner_margin(5).show(ui, |ui| {
|
||||
let available_height = ui.available_height();
|
||||
let half_height = available_height / 2.0;
|
||||
|
||||
let point_pos = vec![[*self.magnitude.lock().unwrap() as f64, *self.phase.lock().unwrap() as f64]];
|
||||
|
||||
let point_pos = PlotPoints::new(point_pos);
|
||||
let point_pos = Points::new("pos", point_pos)
|
||||
.radius(20.0)
|
||||
.color(Color32::LIGHT_RED);
|
||||
|
||||
let bounds = PlotBounds::from_min_max([-1.5, -1.5], [1.5, 1.5]);
|
||||
|
||||
// Use a vertical layout to stack the plots
|
||||
ui.with_layout(Layout::top_down(egui::Align::Min), |ui| {
|
||||
// Plot pressure
|
||||
ui.allocate_ui_with_layout(
|
||||
egui::vec2(ui.available_width(), half_height),
|
||||
Layout::top_down(egui::Align::Min),
|
||||
|ui| {
|
||||
// Magnitude
|
||||
let magnitude = self.magnitude_series.lock().unwrap();
|
||||
Plot::new("magnitude")
|
||||
.allow_scroll(false)
|
||||
.allow_drag(false)
|
||||
// .center_y_axis(true)
|
||||
.legend(Legend::default().position(Corner::LeftTop))
|
||||
.y_axis_label("Magnitude [Ω]")
|
||||
.y_axis_min_width(2.0)
|
||||
.show(ui, |plot_ui| {
|
||||
plot_ui.line(
|
||||
Line::new("Magnitude", magnitude.plot_values())
|
||||
.color(Color32::BLUE)
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
// Plot pressure
|
||||
ui.allocate_ui_with_layout(
|
||||
egui::vec2(ui.available_width(), half_height),
|
||||
Layout::top_down(egui::Align::Min),
|
||||
|ui| {
|
||||
// Phase
|
||||
let phase = self.phase_series.lock().unwrap();
|
||||
Plot::new("phase")
|
||||
.allow_scroll(false)
|
||||
.allow_drag(false)
|
||||
// .center_y_axis(true)
|
||||
.legend(Legend::default().position(Corner::LeftTop))
|
||||
.y_axis_label("Phase [rad]")
|
||||
.y_axis_min_width(2.0)
|
||||
.show(ui, |plot_ui| {
|
||||
plot_ui.line(
|
||||
Line::new("Phase", phase.plot_values())
|
||||
.color(Color32::RED)
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn on_close(&mut self, _tab: &mut Self::Tab) -> OnCloseResponse {
|
||||
println!("Closed tab: {_tab}");
|
||||
OnCloseResponse::Close
|
||||
}
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new(run_impedancemeter_tx: Sender<SingleFrequencySignal>) -> Self {
|
||||
let app = App {
|
||||
tree: DockState::new(vec!["Single".to_string()]),
|
||||
run_impedancemeter_tx,
|
||||
magnitude: Arc::new(Mutex::new(0.0)),
|
||||
phase: Arc::new(Mutex::new(0.0)),
|
||||
@@ -118,77 +206,19 @@ impl eframe::App for App {
|
||||
});
|
||||
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
let available_height = ui.available_height();
|
||||
let half_height = available_height / 2.0;
|
||||
|
||||
let point_pos = vec![[*self.magnitude.lock().unwrap() as f64, *self.phase.lock().unwrap() as f64]];
|
||||
|
||||
let point_pos = PlotPoints::new(point_pos);
|
||||
let point_pos = Points::new("pos", point_pos)
|
||||
.radius(20.0)
|
||||
.color(Color32::LIGHT_RED);
|
||||
|
||||
let bounds = PlotBounds::from_min_max([-1.5, -1.5], [1.5, 1.5]);
|
||||
|
||||
// Use a vertical layout to stack the plots
|
||||
ui.with_layout(Layout::top_down(egui::Align::Min), |ui| {
|
||||
// Plot pressure
|
||||
ui.allocate_ui_with_layout(
|
||||
egui::vec2(ui.available_width(), half_height),
|
||||
Layout::top_down(egui::Align::Min),
|
||||
|ui| {
|
||||
// Magnitude
|
||||
let magnitude = self.magnitude_series.lock().unwrap();
|
||||
Plot::new("magnitude")
|
||||
.allow_scroll(false)
|
||||
.allow_drag(false)
|
||||
// .center_y_axis(true)
|
||||
.legend(Legend::default().position(Corner::LeftTop))
|
||||
.y_axis_label("Magnitude [Ω]")
|
||||
.y_axis_min_width(2.0)
|
||||
.show(ui, |plot_ui| {
|
||||
plot_ui.line(
|
||||
Line::new("Magnitude", magnitude.plot_values())
|
||||
.color(Color32::BLUE)
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
// Plot pressure
|
||||
ui.allocate_ui_with_layout(
|
||||
egui::vec2(ui.available_width(), half_height),
|
||||
Layout::top_down(egui::Align::Min),
|
||||
|ui| {
|
||||
// Phase
|
||||
let phase = self.phase_series.lock().unwrap();
|
||||
Plot::new("phase")
|
||||
.allow_scroll(false)
|
||||
.allow_drag(false)
|
||||
// .center_y_axis(true)
|
||||
.legend(Legend::default().position(Corner::LeftTop))
|
||||
.y_axis_label("Phase [rad]")
|
||||
.y_axis_min_width(2.0)
|
||||
.show(ui, |plot_ui| {
|
||||
plot_ui.line(
|
||||
Line::new("Phase", phase.plot_values())
|
||||
.color(Color32::RED)
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
// Plot::new("State")
|
||||
// .allow_scroll(false)
|
||||
// .allow_drag(false)
|
||||
// .data_aspect(1.0)
|
||||
// .center_y_axis(true)
|
||||
// .show(ui, |plot_ui| {
|
||||
// plot_ui.points(point_pos);
|
||||
// plot_ui.set_plot_bounds(bounds);
|
||||
// });
|
||||
DockArea::new(&mut self.tree)
|
||||
.style(Style::from_egui(ctx.style().as_ref()))
|
||||
.show_leaf_close_all_buttons(false)
|
||||
.show_leaf_collapse_buttons(false)
|
||||
.show_close_buttons(false)
|
||||
.show_inside(ui, &mut TabViewer {
|
||||
magnitude: self.magnitude.clone(),
|
||||
phase: self.phase.clone(),
|
||||
magnitude_series: self.magnitude_series.clone(),
|
||||
phase_series: self.phase_series.clone(),
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// 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))
|
||||
@@ -215,7 +245,8 @@ impl eframe::App for App {
|
||||
}
|
||||
|
||||
ctx.request_repaint();
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn toggle_ui_start_stop(ui: &mut egui::Ui, on: &mut bool) -> egui::Response {
|
||||
|
||||
Reference in New Issue
Block a user