From 166183aa9ad536f56e1a40f52a331da090247299 Mon Sep 17 00:00:00 2001 From: Penwing Date: Mon, 15 Jan 2024 09:34:41 +0100 Subject: [PATCH] added code editor --- src/main.rs | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5d4345b..62dbd40 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ mod tools; use eframe::egui; -use tools::Demo; +//use tools::Demo; fn main() -> Result<(), eframe::Error> { @@ -24,16 +24,21 @@ fn main() -> Result<(), eframe::Error> { //#[derive(Default)] struct MyApp { picked_path: Option, - code_editor: tools::code_editor::CodeEditor, - code_open: bool, + language: String, + code: String, } impl Default for MyApp { fn default() -> Self { Self { picked_path: None, - code_editor: tools::code_editor::CodeEditor::default(), // Initialize CodeEditor - code_open: true, + language: "rs".into(), + code: "// A very simple example\n\ +fn main() {\n\ +\tprintln!(\"Hello world!\");\n\ +}\n\ +" + .into(), } } } @@ -50,8 +55,6 @@ impl eframe::App for MyApp { self.picked_path = Some(path.display().to_string()); } } - - ui.label("Code"); if let Some(picked_path) = &self.picked_path { ui.horizontal(|ui| { @@ -60,12 +63,29 @@ impl eframe::App for MyApp { }); } - //~ let mut code_editor = CodeEditor::default(); - //~ code_editor.code = self.code.clone(); // Initialize code editor with MyApp's code - //~ code_editor.language = self.language.clone(); - //~ code_editor.show(ctx, ui); - //~ - self.code_editor.show(ctx, &mut self.code_open); + //self.code_editor.show(ctx, &mut self.code_open); + + let Self { language, code, .. } = self; + + let theme = egui_extras::syntax_highlighting::CodeTheme::from_memory(ui.ctx()); + let mut layouter = |ui: &egui::Ui, string: &str, wrap_width: f32| { + let mut layout_job = + egui_extras::syntax_highlighting::highlight(ui.ctx(), &theme, string, language); + layout_job.wrap.max_width = wrap_width; + ui.fonts(|f| f.layout_job(layout_job)) + }; + + egui::ScrollArea::vertical().show(ui, |ui| { + ui.add( + egui::TextEdit::multiline(code) + .font(egui::FontId::monospace(60.0)) // for cursor height + .code_editor() + .desired_rows(20) + .lock_focus(true) + .desired_width(f32::INFINITY) + .layouter(&mut layouter), + ); + }); }); egui::TopBottomPanel::bottom("terminal").show(ctx, |ui| { ui.label("Terminal ?");