|
| 1 | +use anyhow::Result; |
| 2 | +use eframe::egui; |
| 3 | + |
| 4 | +use crate::ui::config_panel::render_config_panel; |
| 5 | +use crate::ui::modal::show_modal; |
| 6 | +use crate::ui::results_panel::render_results_panel; |
| 7 | +use crate::ui::test_management::handle_stats_update; |
| 8 | +use crate::ui::types::{ |
| 9 | + ConfigParams, PlotControls, ResultsPanelParams, StatsUpdateParams, TestState, |
| 10 | +}; |
| 11 | + |
| 12 | +use super::state::SpeedTestApp; |
| 13 | + |
| 14 | +impl SpeedTestApp { |
| 15 | + pub fn run(self) -> Result<()> { |
| 16 | + let options = eframe::NativeOptions { |
| 17 | + window_builder: Some(Box::new(|builder| { |
| 18 | + builder |
| 19 | + .with_inner_size(egui::vec2( |
| 20 | + super::super::constants::CONFIG_WINDOW_MIN_WIDTH, |
| 21 | + super::super::constants::CONFIG_WINDOW_MIN_HEIGHT, |
| 22 | + )) |
| 23 | + .with_min_inner_size(egui::vec2( |
| 24 | + super::super::constants::CONFIG_WINDOW_MIN_WIDTH, |
| 25 | + super::super::constants::CONFIG_WINDOW_MIN_HEIGHT, |
| 26 | + )) |
| 27 | + .with_max_inner_size(egui::vec2( |
| 28 | + super::super::constants::CONFIG_WINDOW_MAX_WIDTH, |
| 29 | + super::super::constants::CONFIG_WINDOW_MAX_HEIGHT, |
| 30 | + )) |
| 31 | + .with_resizable(true) |
| 32 | + .with_decorations(true) |
| 33 | + .with_title("DMA Speed Test") |
| 34 | + })), |
| 35 | + ..Default::default() |
| 36 | + }; |
| 37 | + |
| 38 | + eframe::run_native( |
| 39 | + "DMA Speed Test", |
| 40 | + options, |
| 41 | + Box::new(|cc| { |
| 42 | + let mut fonts = egui::FontDefinitions::default(); |
| 43 | + egui_phosphor::add_to_fonts(&mut fonts, egui_phosphor::Variant::Regular); |
| 44 | + cc.egui_ctx.set_fonts(fonts); |
| 45 | + |
| 46 | + Ok::<Box<dyn eframe::App>, Box<dyn std::error::Error + Send + Sync>>(Box::new(self)) |
| 47 | + }), |
| 48 | + ) |
| 49 | + .map_err(|e| anyhow::anyhow!("Failed to run GUI: {e}"))?; |
| 50 | + |
| 51 | + Ok(()) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl eframe::App for SpeedTestApp { |
| 56 | + fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { |
| 57 | + ctx.set_pixels_per_point(self.ui_scale * 1.3); |
| 58 | + |
| 59 | + if self.is_running && !self.was_running { |
| 60 | + ctx.send_viewport_cmd(egui::ViewportCommand::MinInnerSize(egui::vec2( |
| 61 | + super::super::constants::TEST_WINDOW_MIN_WIDTH, |
| 62 | + super::super::constants::TEST_WINDOW_MIN_HEIGHT, |
| 63 | + ))); |
| 64 | + ctx.send_viewport_cmd(egui::ViewportCommand::MaxInnerSize(egui::vec2( |
| 65 | + super::super::constants::TEST_WINDOW_MAX_WIDTH, |
| 66 | + super::super::constants::TEST_WINDOW_MAX_HEIGHT, |
| 67 | + ))); |
| 68 | + ctx.send_viewport_cmd(egui::ViewportCommand::InnerSize(egui::vec2( |
| 69 | + super::super::constants::TEST_WINDOW_MIN_WIDTH, |
| 70 | + super::super::constants::TEST_WINDOW_MIN_HEIGHT, |
| 71 | + ))); |
| 72 | + } |
| 73 | + self.was_running = self.is_running; |
| 74 | + |
| 75 | + self.console.show(ctx); |
| 76 | + |
| 77 | + if self.show_error_modal { |
| 78 | + let message = self.error_modal_message.clone(); |
| 79 | + let mut on_close = || { |
| 80 | + self.show_error_modal = false; |
| 81 | + }; |
| 82 | + show_modal(ctx, &message, &mut on_close); |
| 83 | + if !self.show_error_modal { |
| 84 | + self.error_modal_message.clear(); |
| 85 | + } |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + if self.is_running |
| 90 | + && let Some(rx) = &mut self.stats_rx |
| 91 | + { |
| 92 | + let mut stats_params = StatsUpdateParams { |
| 93 | + current_throughput: &mut self.current_throughput, |
| 94 | + current_reads: &mut self.current_reads, |
| 95 | + current_latency: &mut self.current_latency, |
| 96 | + current_test_size: &mut self.current_test_size, |
| 97 | + test_start_time: &mut self.test_start_time, |
| 98 | + max_throughput: &mut self.max_throughput, |
| 99 | + completed_chunks: &mut self.completed_chunks, |
| 100 | + }; |
| 101 | + |
| 102 | + let stats_closed = |
| 103 | + handle_stats_update(rx, &mut stats_params, &self.results, &self.console); |
| 104 | + |
| 105 | + if stats_closed { |
| 106 | + self.stop_test_impl(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if let Some(rx) = &self.modal_rx |
| 111 | + && let Ok(error_msg) = rx.try_recv() |
| 112 | + { |
| 113 | + self.show_error_modal = true; |
| 114 | + self.error_modal_message = error_msg; |
| 115 | + self.stop_test_impl(); |
| 116 | + self.show_config = true; |
| 117 | + } |
| 118 | + |
| 119 | + if self.show_config { |
| 120 | + egui::CentralPanel::default().show(ctx, |ui| { |
| 121 | + let mut should_start_test = false; |
| 122 | + let mut config_params = ConfigParams { |
| 123 | + connector: &mut self.connector, |
| 124 | + pcileech_device: &mut self.pcileech_device, |
| 125 | + duration: &mut self.duration, |
| 126 | + ui_scale: &mut self.ui_scale, |
| 127 | + ui_scale_text: &mut self.ui_scale_text, |
| 128 | + test_sizes: &mut self.test_sizes, |
| 129 | + show_error_modal: &mut self.show_error_modal, |
| 130 | + error_modal_message: &mut self.error_modal_message, |
| 131 | + show_config: &mut self.show_config, |
| 132 | + }; |
| 133 | + render_config_panel(ui, &mut config_params, || should_start_test = true); |
| 134 | + if should_start_test { |
| 135 | + self.start_test_impl(); |
| 136 | + } |
| 137 | + }); |
| 138 | + } else { |
| 139 | + egui::CentralPanel::default().show(ctx, |ui| { |
| 140 | + let mut should_stop_test = false; |
| 141 | + let mut should_start_test = false; |
| 142 | + let mut should_toggle_console = false; |
| 143 | + |
| 144 | + let plot_controls = PlotControls { |
| 145 | + custom_plot_width: &mut self.custom_plot_width, |
| 146 | + custom_plot_height: &mut self.custom_plot_height, |
| 147 | + plot_resize_start_time: &mut self.plot_resize_start_time, |
| 148 | + plot_resize_direction: &mut self.plot_resize_direction, |
| 149 | + plot_resize_last_repeat: &mut self.plot_resize_last_repeat, |
| 150 | + }; |
| 151 | + |
| 152 | + let test_state = TestState { |
| 153 | + is_running: self.is_running, |
| 154 | + current_throughput: self.current_throughput, |
| 155 | + current_reads: self.current_reads, |
| 156 | + current_latency: self.current_latency, |
| 157 | + current_test_size: self.current_test_size, |
| 158 | + test_start_time: self.test_start_time, |
| 159 | + test_end_time: self.test_end_time, |
| 160 | + completed_chunks: &self.completed_chunks, |
| 161 | + }; |
| 162 | + |
| 163 | + let mut results_params = ResultsPanelParams { |
| 164 | + results: &self.results, |
| 165 | + duration: self.duration, |
| 166 | + plot_controls, |
| 167 | + console: &self.console, |
| 168 | + ui_scale: &mut self.ui_scale, |
| 169 | + ui_scale_text: &mut self.ui_scale_text, |
| 170 | + test_state, |
| 171 | + test_sizes: &self.test_sizes, |
| 172 | + show_config: &mut self.show_config, |
| 173 | + }; |
| 174 | + |
| 175 | + render_results_panel( |
| 176 | + ui, |
| 177 | + &mut results_params, |
| 178 | + || should_stop_test = true, |
| 179 | + || should_start_test = true, |
| 180 | + &mut should_toggle_console, |
| 181 | + ); |
| 182 | + |
| 183 | + if should_stop_test { |
| 184 | + self.stop_test_impl(); |
| 185 | + } |
| 186 | + if should_start_test { |
| 187 | + self.start_test_impl(); |
| 188 | + } |
| 189 | + if should_toggle_console { |
| 190 | + self.console.toggle(); |
| 191 | + } |
| 192 | + }); |
| 193 | + } |
| 194 | + |
| 195 | + if self.is_running { |
| 196 | + ctx.request_repaint(); |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments