From 142a14cddbc094ba6010f8b8a2b6ebf10e707ee4 Mon Sep 17 00:00:00 2001 From: Penwing Date: Mon, 15 Jan 2024 10:06:20 +0100 Subject: [PATCH] can open files --- src/main.rs | 17 ++++--- src/tools/code_editor.rs | 101 --------------------------------------- src/tools/mod.rs | 2 +- 3 files changed, 11 insertions(+), 109 deletions(-) delete mode 100644 src/tools/code_editor.rs diff --git a/src/main.rs b/src/main.rs index 62dbd40..2861785 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,17 @@ -mod tools; +//mod tools; use eframe::egui; -//use tools::Demo; +use std::fs; +use std::path::Path; fn main() -> Result<(), eframe::Error> { - tools::code_editor::linked(); + //tools::code_editor::linked(); env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`). let options = eframe::NativeOptions { viewport: egui::ViewportBuilder::default() - .with_inner_size([1200.0, 800.0]) // wide enough for the drag-drop overlay text + .with_inner_size([1200.0, 800.0]) .with_drag_and_drop(true), ..Default::default() }; @@ -21,7 +22,7 @@ fn main() -> Result<(), eframe::Error> { ) } -//#[derive(Default)] + struct MyApp { picked_path: Option, language: String, @@ -43,6 +44,7 @@ fn main() {\n\ } } + impl eframe::App for MyApp { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { egui::SidePanel::left("my_left_panel").show(ctx, |ui| { @@ -53,6 +55,8 @@ impl eframe::App for MyApp { if ui.button("Open file…").clicked() { if let Some(path) = rfd::FileDialog::new().pick_file() { self.picked_path = Some(path.display().to_string()); + let file_path = Path::new(self.picked_path.as_deref().unwrap_or_default()); + self.code = fs::read_to_string(file_path).expect("Should have been able to read the file"); } } @@ -62,8 +66,6 @@ impl eframe::App for MyApp { ui.monospace(picked_path); }); } - - //self.code_editor.show(ctx, &mut self.code_open); let Self { language, code, .. } = self; @@ -80,6 +82,7 @@ impl eframe::App for MyApp { egui::TextEdit::multiline(code) .font(egui::FontId::monospace(60.0)) // for cursor height .code_editor() + .lock_focus(true) .desired_rows(20) .lock_focus(true) .desired_width(f32::INFINITY) diff --git a/src/tools/code_editor.rs b/src/tools/code_editor.rs deleted file mode 100644 index 0db90d5..0000000 --- a/src/tools/code_editor.rs +++ /dev/null @@ -1,101 +0,0 @@ -use eframe::egui; - -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] -#[cfg_attr(feature = "serde", serde(default))] -pub struct CodeEditor { - language: String, - code: String, -} - -impl Default for CodeEditor { - fn default() -> Self { - Self { - language: "rs".into(), - code: "// A very simple example\n\ -fn main() {\n\ -\tprintln!(\"Hello world!\");\n\ -}\n\ -" - .into(), - } - } -} - -impl super::Demo for CodeEditor { - fn name(&self) -> &'static str { - "🖮 Code Editor" - } - - fn show(&mut self, ctx: &egui::Context, open: &mut bool) { - use super::View as _; - egui::Window::new(self.name()) - .open(open) - .default_height(500.0) - .show(ctx, |ui| self.ui(ui)); - } -} - -impl super::View for CodeEditor { - fn ui(&mut self, ui: &mut egui::Ui) { - let Self { language, code } = self; - - ui.horizontal(|ui| { - ui.set_height(0.0); - ui.label("An example of syntax highlighting in a TextEdit."); - //ui.add(crate::egui_github_link_file!()); - }); - - if cfg!(feature = "syntect") { - ui.horizontal(|ui| { - ui.label("Language:"); - ui.text_edit_singleline(language); - }); - ui.horizontal_wrapped(|ui| { - ui.spacing_mut().item_spacing.x = 0.0; - ui.label("Syntax highlighting powered by "); - ui.hyperlink_to("syntect", "https://github.com/trishume/syntect"); - ui.label("."); - }); - } else { - ui.horizontal_wrapped(|ui| { - ui.spacing_mut().item_spacing.x = 0.0; - ui.label("Compile the demo with the "); - ui.code("syntax_highlighting"); - ui.label(" feature to enable more accurate syntax highlighting using "); - ui.hyperlink_to("syntect", "https://github.com/trishume/syntect"); - ui.label("."); - }); - } - - let mut theme = egui_extras::syntax_highlighting::CodeTheme::from_memory(ui.ctx()); - ui.collapsing("Theme", |ui| { - ui.group(|ui| { - theme.ui(ui); - theme.clone().store_in_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::TextStyle::Monospace) // for cursor height - .code_editor() - .desired_rows(10) - .lock_focus(true) - .desired_width(f32::INFINITY) - .layouter(&mut layouter), - ); - }); - } -} - -pub fn linked() { - println!("Code Editor Linked"); -} diff --git a/src/tools/mod.rs b/src/tools/mod.rs index 6a7234a..4583d00 100644 --- a/src/tools/mod.rs +++ b/src/tools/mod.rs @@ -1,6 +1,6 @@ use eframe::egui; -pub mod code_editor; +//pub mod code_editor; // ----------------------------------------------------------------------------