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.
This commit is contained in:
Michael Schimmel
2026-02-17 11:31:30 +01:00
parent 7042206ab6
commit b19fd4b097
+35 -12
View File
@@ -19,6 +19,7 @@ struct CompilerApp {
source_code: String, source_code: String,
output_log: String, output_log: String,
env: Environment, env: Environment,
is_first_frame: bool,
} }
impl Default for CompilerApp { impl Default for CompilerApp {
@@ -27,6 +28,24 @@ impl Default for CompilerApp {
source_code: String::new(), source_code: String::new(),
output_log: String::from("Ready to compile..."), output_log: String::from("Ready to compile..."),
env: Environment::new(), 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) // Compile Button (at the top of the bottom panel)
if ui.button("Compile").clicked() { if ui.button("Compile").clicked() {
match self.env.run_script(&self.source_code) { self.compile();
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);
}
}
} }
ui.add_space(5.0); ui.add_space(5.0);
@@ -80,12 +88,27 @@ impl eframe::App for CompilerApp {
ui.heading("Source Code Editor"); ui.heading("Source Code Editor");
ui.label("Input Source:"); 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() egui::ScrollArea::vertical()
.id_salt("source_code_scroll") .id_salt("source_code_scroll")
.show(ui, |ui| { .show(ui, |ui| {
ui.add_sized( ui.add_sized(
ui.available_size(), ui.available_size(),
egui::TextEdit::multiline(&mut self.source_code) egui::TextEdit::multiline(&mut self.source_code)
.id(editor_id)
.font(egui::TextStyle::Monospace) .font(egui::TextStyle::Monospace)
.desired_width(f32::INFINITY) .desired_width(f32::INFINITY)
.lock_focus(true), .lock_focus(true),