From b19fd4b0974960c9d877769fb4471d59196f9420 Mon Sep 17 00:00:00 2001 From: Michael Schimmel Date: Tue, 17 Feb 2026 11:31:30 +0100 Subject: [PATCH] Add `is_first_frame` flag to CompilerApp Introduces a new `is_first_frame` boolean flag to the `CompilerApp` struct to manage initial UI state, specifically for requesting focus on the source code editor on the first frame. This commit also moves the compilation logic into a dedicated `compile` method for better organization and introduces a Shift+Enter keyboard shortcut for compilation. --- src/main.rs | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 41fa359..fde51a2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ struct CompilerApp { source_code: String, output_log: String, env: Environment, + is_first_frame: bool, } impl Default for CompilerApp { @@ -27,6 +28,24 @@ impl Default for CompilerApp { source_code: String::new(), output_log: String::from("Ready to compile..."), env: Environment::new(), + is_first_frame: true, + } + } +} + +impl CompilerApp { + fn compile(&mut self) { + match self.env.run_script(&self.source_code) { + Ok(result) => { + self.output_log = format!( + "Execution Successful.\nResult: {}\n\nFinished at {:?}", + result, + std::time::SystemTime::now(), + ); + } + Err(e) => { + self.output_log = format!("Error: {}", e); + } } } } @@ -43,18 +62,7 @@ impl eframe::App for CompilerApp { // Compile Button (at the top of the bottom panel) if ui.button("Compile").clicked() { - match self.env.run_script(&self.source_code) { - Ok(result) => { - self.output_log = format!( - "Execution Successful.\nResult: {}\n\nFinished at {:?}", - result, - std::time::SystemTime::now(), - ); - } - Err(e) => { - self.output_log = format!("Error: {}", e); - } - } + self.compile(); } ui.add_space(5.0); @@ -80,12 +88,27 @@ impl eframe::App for CompilerApp { ui.heading("Source Code Editor"); ui.label("Input Source:"); + let editor_id = ui.id().with("source_code_editor"); + + // Handle Shift+Enter shortcut BEFORE the editor handles it + let shortcut = egui::KeyboardShortcut::new(egui::Modifiers::SHIFT, egui::Key::Enter); + if ui.input_mut(|i| i.consume_shortcut(&shortcut)) { + self.compile(); + } + + // Set initial focus + if self.is_first_frame { + ui.ctx().memory_mut(|m| m.request_focus(editor_id)); + self.is_first_frame = false; + } + egui::ScrollArea::vertical() .id_salt("source_code_scroll") .show(ui, |ui| { ui.add_sized( ui.available_size(), egui::TextEdit::multiline(&mut self.source_code) + .id(editor_id) .font(egui::TextStyle::Monospace) .desired_width(f32::INFINITY) .lock_focus(true),