diff --git a/src/draw_thread.rs b/src/draw_thread.rs index 3c45749..e402fcb 100644 --- a/src/draw_thread.rs +++ b/src/draw_thread.rs @@ -24,7 +24,7 @@ use crate::{ use fltk::{ app, button::Button, - dialog, enums, + enums, frame::Frame, input::{Input, MultilineInput}, menu, @@ -274,14 +274,9 @@ fn load_image( let read = match serde_json::from_str::(&read) { Ok(r) => r, Err(e) => { - warn!("Config is corrupt\n{:?}", e); - match dialog::choice_default("Config is corrupt, fix??", "yes", "no", "") { - 1 => { - fs::remove_file(&properties_file).warn_log("Failed to delete image properties file!"); - ImagePropertiesFile::default() - } - _ => return, - } + Result::<(),_>::Err(e).warn_log("Config is corrupt"); + fs::remove_file(&properties_file).warn_log("Failed to delete image properties file!"); + ImagePropertiesFile::default() } }; @@ -377,7 +372,7 @@ fn show_images_details(images_list: Arc>>) { } } - dialog::message_default(&format!("With Quote: {}\nWithout Quote: {}", image_with_quote, image_without_quote)); + utils::show_message(&format!("With Quote: {}\nWithout Quote: {}", image_with_quote, image_without_quote)); } /// Flush the Buffer from image container to drawing buffer for fltk diff --git a/src/globals.rs b/src/globals.rs index 9481c3c..91b336b 100644 --- a/src/globals.rs +++ b/src/globals.rs @@ -29,6 +29,9 @@ lazy_static! { pub(crate) static ref CONFIG: RwLock = RwLock::new(config::ConfigFile::load()); + /// Main Sender + pub(crate) static ref MAIN_SENDER: RwLock>> = RwLock::new(None); + /// TTF Font for Quote pub(crate) static ref FONT_QUOTE: Font<'static> = { let mut buffer = Vec::new(); diff --git a/src/main.rs b/src/main.rs index b98796b..58590d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,6 +42,8 @@ use std::sync::{Arc, RwLock}; pub(crate) enum AppMessage { /// Copy recived image buffer from draw_thread to Buffer for fltk frame RedrawMainWindowImage(Option>), + Message(String), + Alert(String) } fn main() { @@ -67,7 +69,8 @@ fn main() { let draw_buff: Arc>>> = Arc::new(RwLock::new(None)); let (main_sender, main_receiver) = channel::(); - let mut main_window = MainWindow::new(main_sender, Arc::clone(&draw_buff)); + *globals::MAIN_SENDER.write().unwrap() = Some(main_sender); + let mut main_window = MainWindow::new(Arc::clone(&draw_buff)); while app.wait() { if let Some(msg) = main_receiver.recv() { @@ -77,6 +80,12 @@ fn main() { *buff = data; main_window.win.redraw(); } + AppMessage::Message(msg) => { + dialog::message_default(&msg); + } + AppMessage::Alert(msg) => { + dialog::alert_default(&msg) + } } } } diff --git a/src/main_window.rs b/src/main_window.rs index ca3d6a4..c498cfa 100644 --- a/src/main_window.rs +++ b/src/main_window.rs @@ -23,7 +23,6 @@ use crate::utils::ImageType; use crate::utils::ImageProperties; use crate::{config_window::ConfigWindow, globals}; use fltk::{ - app, button::Button, dialog, dialog::NativeFileChooser, @@ -101,7 +100,7 @@ pub(crate) struct Page { impl MainWindow { pub(crate) fn new( - sender: app::Sender, + // sender: app::Sender, draw_buff: Arc>>>, ) -> Self { let mut win = Window::new(0, 0, 1100, 700, "Post Maker").center_screen(); @@ -394,7 +393,11 @@ impl MainWindow { }, sender: rx, }; - spawn_image_thread(tx, sender, Arc::clone(&properties), &main_win); + + let a = globals::MAIN_SENDER.read().unwrap(); + if let Some(a) = &*a { + spawn_image_thread(tx, a.to_owned(), Arc::clone(&properties), &main_win); + } main_win.menu(); main_win.draw(); main_win.events(); diff --git a/src/result_ext.rs b/src/result_ext.rs index dadb603..e518bd6 100644 --- a/src/result_ext.rs +++ b/src/result_ext.rs @@ -1,6 +1,7 @@ use std::fmt::Debug; use std::panic::Location; -use fltk::dialog; + +use crate::utils; pub trait ResultExt { fn expect_log(self, msg: &str) -> T; @@ -14,7 +15,7 @@ impl ResultExt for Result { match self { Ok(v) => v, Err(e) => { - dialog::alert_default(msg); + utils::show_alert(msg); error!("{}\n{:?}\n{}", msg, e, Location::caller()); std::process::exit(1); } @@ -24,7 +25,7 @@ impl ResultExt for Result { #[track_caller] fn error_log(&self, msg: &str) { if let Err(e) = self { - dialog::alert_default(msg); + utils::show_alert(msg); error!("{}\n{:?}\n{}", msg, e, Location::caller()); } } @@ -32,7 +33,7 @@ impl ResultExt for Result { #[track_caller] fn warn_log(&self, msg: &str) { if let Err(e) = self { - dialog::alert_default(msg); + utils::show_alert(msg); warn!("{}\n{:?}", msg, e); } } diff --git a/src/utils.rs b/src/utils.rs index febd2fc..32e2253 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -728,3 +728,17 @@ pub(crate) fn tag_from_height(height: f64) -> f64 { pub(crate) fn tag2_from_height(height: f64) -> f64 { (height * globals::CONFIG.read().unwrap().tag2_font_ratio) / 5000.0 } + +pub(crate) fn show_message(msg: &str) { + let a = globals::MAIN_SENDER.read().unwrap(); + if let Some(a) = &*a { + a.send(crate::AppMessage::Message(msg.to_owned())); + } +} + +pub(crate) fn show_alert(msg: &str) { + let a = globals::MAIN_SENDER.read().unwrap(); + if let Some(a) = &*a { + a.send(crate::AppMessage::Alert(msg.to_owned())); + } +}