-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathapp.rs
More file actions
78 lines (70 loc) · 2.78 KB
/
Copy pathapp.rs
File metadata and controls
78 lines (70 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use eframe::egui;
use eframe::egui::Response;
use egui_plot::Legend;
use egui_plot::Line;
use egui_plot::Plot;
use egui_plot::PlotPoints;
#[derive(Default)]
pub struct SavePlotExample {
plot_rect: Option<egui::Rect>,
}
impl SavePlotExample {
pub fn show_plot(&mut self, ui: &mut egui::Ui) -> Response {
let my_plot = Plot::new("My Plot").legend(Legend::default());
// let's create a dummy line in the plot
let graph: Vec<[f64; 2]> = vec![[0.0, 1.0], [2.0, 3.0], [3.0, 2.0]];
let inner = my_plot.show(ui, |plot_ui| {
plot_ui.line(Line::new("curve", PlotPoints::from(graph)));
});
// Remember the position of the plot
self.plot_rect = Some(inner.response.rect);
#[cfg(not(target_arch = "wasm32"))]
{
// Check for returned screenshot:
let ctx = ui.ctx();
let screenshot = ctx.input(|i| {
for event in &i.raw.events {
if let egui::Event::Screenshot { image, .. } = event {
return Some(image.clone());
}
}
None
});
if let (Some(screenshot), Some(plot_location)) = (screenshot, self.plot_rect) {
if let Some(mut path) = rfd::FileDialog::new().save_file() {
path.set_extension("png");
// for a full size application, we should put this in a different thread,
// so that the GUI doesn't lag during saving
let pixels_per_point = ctx.pixels_per_point();
let plot = screenshot.region(&plot_location, Some(pixels_per_point));
// save the plot to png
let result = image::save_buffer(
&path,
plot.as_raw(),
plot.width() as u32,
plot.height() as u32,
image::ColorType::Rgba8,
);
match result {
Ok(()) => eprintln!("Image saved to {}", path.display()),
Err(err) => eprintln!("Failed to save image to {}: {err}", path.display()),
}
}
}
}
#[cfg(target_arch = "wasm32")]
{
eprintln!("File saving is not supported on WASM targets");
}
inner.response
}
#[expect(clippy::unused_self, reason = "required by the example template")]
pub fn show_controls(&self, ui: &mut egui::Ui) -> Response {
let response = ui.button("Save Plot");
if response.clicked() {
ui.ctx()
.send_viewport_cmd(egui::ViewportCommand::Screenshot(Default::default()));
}
response
}
}