Clean up ui

This commit is contained in:
Lucas Schumacher 2024-06-03 22:20:36 -04:00
parent 9f18bb6920
commit d878f66bd8

View File

@ -16,15 +16,12 @@ const FFT_SIZE: usize = 1024;
pub struct TemplateApp { pub struct TemplateApp {
plots: DebugPlots, plots: DebugPlots,
// Example stuff:
label: String,
value: f32,
/// Behind an `Arc<Mutex<…>>` so we can pass it to [`egui::PaintCallback`] and paint later. /// Behind an `Arc<Mutex<…>>` so we can pass it to [`egui::PaintCallback`] and paint later.
waterfall: Arc<Mutex<Waterfall>>, waterfall: Arc<Mutex<Waterfall>>,
_fft: Fft, fft: Fft,
_backends: backend::Backends, backends: backend::Backends,
_selected_backend: usize, selected_backend: usize,
_open_device: Option<Box<dyn backend::Device>>, open_device: Option<Box<dyn backend::Device>>,
device_window_open: bool, device_window_open: bool,
} }
@ -49,15 +46,11 @@ impl TemplateApp {
Self { Self {
plots, plots,
// Example stuff:
label: "Hello World!".to_owned(),
value: 2.7,
waterfall: Arc::new(Mutex::new(Waterfall::new(gl, wf_size, wf_size, rx))), waterfall: Arc::new(Mutex::new(Waterfall::new(gl, wf_size, wf_size, rx))),
//_stream: stream, fft,
_fft: fft, backends: Backends::default(),
_backends: Backends::default(), selected_backend: 0,
_selected_backend: 0, open_device: None,
_open_device: None,
device_window_open: true, device_window_open: true,
} }
} }
@ -77,15 +70,11 @@ impl eframe::App for TemplateApp {
/// Called each time the UI needs repainting, which may be many times per second. /// Called each time the UI needs repainting, which may be many times per second.
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
// Put your widgets into a `SidePanel`, `TopBottomPanel`, `CentralPanel`, `Window` or `Area`.
// For inspiration and more examples, go to https://emilk.github.io/egui
ctx.request_repaint(); ctx.request_repaint();
self.plots.update_plots(); self.plots.update_plots();
// Menu bar
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| { egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
// The top panel is often a good place for a menu bar:
egui::menu::bar(ui, |ui| { egui::menu::bar(ui, |ui| {
// NOTE: no File->Quit on web pages! // NOTE: no File->Quit on web pages!
let is_web = cfg!(target_arch = "wasm32"); let is_web = cfg!(target_arch = "wasm32");
@ -106,111 +95,99 @@ impl eframe::App for TemplateApp {
}); });
}); });
egui::CentralPanel::default().show(ctx, |ui| {
egui::TopBottomPanel::top("Plot")
.resizable(true)
.show_inside(ui, |_ui| {
// TODO: Add plot
});
egui::CentralPanel::default().show_inside(ui, |ui| {
ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| {
powered_by_egui_and_eframe(ui);
egui::warn_if_debug_build(ui);
egui::Frame::canvas(ui.style()).show(ui, |ui| {
let available_space = ui.available_size();
let (rect, response) =
ui.allocate_exact_size(available_space, egui::Sense::drag());
let _angle = response.drag_motion().x * 0.01;
// Clone locals so we can move them into the paint callback:
let waterfall = self.waterfall.clone();
let callback = egui::PaintCallback {
rect,
callback: std::sync::Arc::new(egui_glow::CallbackFn::new(
move |_info, painter| {
waterfall.lock().paint(painter.gl(), _angle);
},
)),
};
ui.painter().add(callback);
});
});
});
});
// Update debug plot windows
self.plots.render_plot_windows(ctx); self.plots.render_plot_windows(ctx);
if self._open_device.is_none() { // Update device selection window
self.device_window_open = true; let mut device_window = egui::Window::new("Select Device")
}
let mut close_device_window = false;
egui::Window::new("Select Device")
.open(&mut self.device_window_open)
.default_width(600.0) .default_width(600.0)
.default_height(400.0) .default_height(400.0)
.vscroll(false) .vscroll(false)
.resizable(true) .resizable(true)
.show(ctx, |ui| { .collapsible(false);
egui::SidePanel::left("Select Driver") if self.open_device.is_some() {
.resizable(true) device_window = device_window.open(&mut self.device_window_open);
.default_width(150.0) } else {
.width_range(80.0..=200.0) device_window = device_window.anchor(egui::Align2::CENTER_CENTER, [0., 0.]);
.show_inside(ui, |ui| { }
ScrollArea::vertical().show(ui, |ui| { let mut close_device_window = false;
ui.with_layout( device_window.show(ctx, |ui| {
egui::Layout::top_down_justified(egui::Align::LEFT), egui::SidePanel::left("Select Driver")
|ui| { .resizable(true)
for (i, b) in self._backends.0.iter().enumerate() { .default_width(150.0)
ui.selectable_value( .width_range(80.0..=200.0)
&mut self._selected_backend, .show_inside(ui, |ui| {
i, ScrollArea::vertical().show(ui, |ui| {
b.display_text(), ui.with_layout(egui::Layout::top_down_justified(egui::Align::LEFT), |ui| {
); for (i, b) in self.backends.0.iter().enumerate() {
} ui.selectable_value(
}, &mut self.selected_backend,
); i,
b.display_text(),
);
}
}); });
}); });
ui.vertical_centered(|ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
//if self._selected_backend < self._backends.0.len() {
if let Some(b) = self._backends.0.get_mut(self._selected_backend) {
//let mut b = &self._backends.0[self._selected_backend];
b.show_device_selection(ui);
if ui.add(egui::Button::new("Apply")).clicked() {
drop(self._open_device.take());
if let Ok(device) =
b.build_device(self._fft.tx.clone(), self.plots.get_sender())
{
self._open_device = Some(device);
close_device_window = true;
}
}
} else {
ui.add(egui::Label::new("Select a Device Driver"));
}
});
}); });
}); ui.vertical_centered(|ui| {
if close_device_window { egui::ScrollArea::vertical().show(ui, |ui| {
self.device_window_open = false; //if self._selected_backend < self._backends.0.len() {
} if let Some(b) = self.backends.0.get_mut(self.selected_backend) {
//let mut b = &self._backends.0[self._selected_backend];
egui::CentralPanel::default().show(ctx, |ui| { b.show_device_selection(ui);
// The central panel the region left after adding TopPanel's and SidePanel's if ui.add(egui::Button::new("Apply")).clicked() {
ui.heading("eframe template"); drop(self.open_device.take());
if let Ok(device) =
ui.horizontal(|ui| { b.build_device(self.fft.tx.clone(), self.plots.get_sender())
ui.label("Write something: "); {
ui.text_edit_singleline(&mut self.label); self.open_device = Some(device);
}); close_device_window = true;
}
ui.add(egui::Slider::new(&mut self.value, 0.0..=10.0).text("value")); }
if ui.button("Increment").clicked() { } else {
self.value += 1.0; ui.add(egui::Label::new("Select a Device Driver"));
} }
ui.separator();
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
ui.label("The texture is being painted using ");
ui.hyperlink_to("glow", "https://github.com/grovesNL/glow");
ui.label(" (OpenGL).");
});
ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| {
powered_by_egui_and_eframe(ui);
egui::warn_if_debug_build(ui);
egui::Frame::canvas(ui.style()).show(ui, |ui| {
let available_space = ui.available_size();
let (rect, response) =
ui.allocate_exact_size(available_space, egui::Sense::drag());
let _angle = response.drag_motion().x * 0.01;
// Clone locals so we can move them into the paint callback:
let waterfall = self.waterfall.clone();
let callback = egui::PaintCallback {
rect,
callback: std::sync::Arc::new(egui_glow::CallbackFn::new(
move |_info, painter| {
waterfall.lock().paint(painter.gl(), _angle);
},
)),
};
ui.painter().add(callback);
}); });
}); });
}); });
if close_device_window {
self.device_window_open = false;
}
} }
} }
@ -219,11 +196,13 @@ fn powered_by_egui_and_eframe(ui: &mut egui::Ui) {
ui.spacing_mut().item_spacing.x = 0.0; ui.spacing_mut().item_spacing.x = 0.0;
ui.label("Powered by "); ui.label("Powered by ");
ui.hyperlink_to("egui", "https://github.com/emilk/egui"); ui.hyperlink_to("egui", "https://github.com/emilk/egui");
ui.label(" and "); ui.label(", ");
ui.hyperlink_to( ui.hyperlink_to(
"eframe", "eframe",
"https://github.com/emilk/egui/tree/master/crates/eframe", "https://github.com/emilk/egui/tree/master/crates/eframe",
); );
ui.label(" and ");
ui.hyperlink_to("glow", "https://github.com/grovesNL/glow");
ui.label("."); ui.label(".");
}); });
} }