UI for recording
This commit is contained in:
+80
-8
@@ -1,14 +1,19 @@
|
||||
use egui::{CentralPanel, ViewportBuilder, ViewportId};
|
||||
use std::{fs, time::Duration};
|
||||
|
||||
use anyhow::Result;
|
||||
use egui::{Button, CentralPanel, Checkbox, ViewportBuilder, ViewportId};
|
||||
use egui_notify::{Anchor, Toast, Toasts};
|
||||
|
||||
use crate::{
|
||||
emulator::{EmulatorClient, SimId},
|
||||
profiler::Profiler,
|
||||
profiler::{Profiler, ProfilerStatus},
|
||||
window::AppWindow,
|
||||
};
|
||||
|
||||
pub struct ProfileWindow {
|
||||
sim_id: SimId,
|
||||
profiler: Profiler,
|
||||
toasts: Toasts,
|
||||
}
|
||||
|
||||
impl ProfileWindow {
|
||||
@@ -16,11 +21,47 @@ impl ProfileWindow {
|
||||
Self {
|
||||
sim_id,
|
||||
profiler: Profiler::new(sim_id, client),
|
||||
toasts: Toasts::new()
|
||||
.with_anchor(Anchor::BottomLeft)
|
||||
.with_margin((10.0, 10.0).into())
|
||||
.reverse(true),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn launch(&mut self) {
|
||||
self.profiler.start();
|
||||
self.profiler.enable();
|
||||
}
|
||||
|
||||
fn finish_recording(&mut self) {
|
||||
match self.try_finish_recording() {
|
||||
Ok(Some(path)) => {
|
||||
let mut toast = Toast::info(format!("Saved to {path}"));
|
||||
toast.duration(Some(Duration::from_secs(5)));
|
||||
self.toasts.add(toast);
|
||||
}
|
||||
Ok(None) => {}
|
||||
Err(error) => {
|
||||
let mut toast = Toast::error(format!("{error:#}"));
|
||||
toast.duration(Some(Duration::from_secs(5)));
|
||||
self.toasts.add(toast);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn try_finish_recording(&mut self) -> Result<Option<String>> {
|
||||
let bytes_receiver = self.profiler.finish_recording();
|
||||
let file = rfd::FileDialog::new()
|
||||
.add_filter("Profiler files", &["json"])
|
||||
.set_file_name("profile.json")
|
||||
.save_file();
|
||||
if let Some(path) = file {
|
||||
let bytes = pollster::block_on(bytes_receiver)?;
|
||||
fs::write(&path, bytes)?;
|
||||
Ok(Some(path.display().to_string()))
|
||||
} else {
|
||||
self.profiler.cancel_recording();
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,15 +81,46 @@ impl AppWindow for ProfileWindow {
|
||||
}
|
||||
|
||||
fn show(&mut self, ctx: &egui::Context) {
|
||||
let status = self.profiler.status();
|
||||
let recording = matches!(status, ProfilerStatus::Recording);
|
||||
CentralPanel::default().show(ctx, |ui| {
|
||||
let mut started = self.profiler.started();
|
||||
if ui.checkbox(&mut started, "Profiling enabled?").changed() {
|
||||
if started {
|
||||
self.profiler.start();
|
||||
let mut enabled = status.enabled();
|
||||
let enabled_checkbox = Checkbox::new(&mut enabled, "Profiling enabled?");
|
||||
if ui.add_enabled(!recording, enabled_checkbox).changed() {
|
||||
if enabled {
|
||||
self.profiler.enable();
|
||||
} else {
|
||||
self.profiler.stop();
|
||||
self.profiler.disable();
|
||||
}
|
||||
}
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
if !recording {
|
||||
let record_button = Button::new("Record");
|
||||
let can_record = matches!(status, ProfilerStatus::Enabled);
|
||||
if ui.add_enabled(can_record, record_button).clicked() {
|
||||
self.profiler.start_recording();
|
||||
}
|
||||
} else {
|
||||
if ui.button("Finish recording").clicked() {
|
||||
self.finish_recording();
|
||||
}
|
||||
if ui.button("Cancel recording").clicked() {
|
||||
self.profiler.cancel_recording();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
match &status {
|
||||
ProfilerStatus::Recording => {
|
||||
ui.label("Recording...");
|
||||
}
|
||||
ProfilerStatus::Error(message) => {
|
||||
ui.label(message);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
self.toasts.show(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user