From cacbc89a01d2169ff94a5216a84c89ea3fab5cb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristo=CC=81fer=20R?= Date: Mon, 25 Mar 2024 18:34:11 +0000 Subject: [PATCH] add working open file param --- src/app.rs | 48 ++++++++++++++++++++++++++++++++++++++++-------- src/main.rs | 5 ++++- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/app.rs b/src/app.rs index 4648dda..0cf072d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -86,16 +86,10 @@ impl Default for TemplateApp { impl TemplateApp { /// Called once before the first frame. - pub fn new(cc: &eframe::CreationContext<'_>) -> Self { + pub fn new(cc: &eframe::CreationContext<'_>, file: Option) -> Self { // This is also where you can customize the look and feel of egui using // `cc.egui_ctx.set_visuals` and `cc.egui_ctx.set_fonts`. - // Load previous app state (if any). - // Note that you must enable the `persistence` feature for this to work. - if let Some(storage) = cc.storage { - return eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default(); - } - // FIXME: Merge JetBrains Mono font variants into one .ttf file to support // bolds/italic/boldi-italic etc. // See https://github.com/emilk/egui/discussions/1862. @@ -116,7 +110,45 @@ impl TemplateApp { cc.egui_ctx.set_fonts(fonts); - Default::default() + // FIXME: This may be a slightly wonky way to open the file? The branching seems excessive at least. + let config = if let Some(file) = file { + // FIXME: Using .as_str() here to satisfy the borrow checker seems odd. + // I'm probably doing something wrong here. + let metadata = std::fs::metadata(file.as_str()) + .unwrap_or_else(|_| panic!("File not found: {}", file)); + + if metadata.is_file() { + // FIXME: Inform user of file read error more gracefully than with a panic. + let text = std::fs::read_to_string(file.as_str()) + .unwrap_or_else(|_| panic!("Could not read file {}", file)); + + let file_cannel = channel(); + + let _ = file_cannel.0.send(text.clone()); + + Self { + text: text.to_owned(), + ..Default::default() + } + } else { + // FXIME: we should create the file here. + Default::default() + } + } else { + Default::default() + }; + + // Load previous app state (if any). + // Note that you must enable the `persistence` feature for this to work. + if let Some(storage) = cc.storage { + // FIXME: Is there a better way to merge these structs? This seems slightly off. + return Self { + text: config.text, + ..eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default() + }; + } + + config } } diff --git a/src/main.rs b/src/main.rs index b291978..0aff179 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,9 @@ fn main() -> eframe::Result<()> { env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`). + let mut args = std::env::args(); + let file = args.nth(1); + let native_options = eframe::NativeOptions { viewport: egui::ViewportBuilder::default() .with_app_id("com.thorlaksson.egui_edit") @@ -19,6 +22,6 @@ fn main() -> eframe::Result<()> { eframe::run_native( "egui_edit", native_options, - Box::new(|cc| Box::new(egui_edit::TemplateApp::new(cc))), + Box::new(|cc| Box::new(egui_edit::TemplateApp::new(cc, file))), ) }