Message and alert dialog from main thread

This commit is contained in:
Piyush मिश्रः 2022-03-26 19:38:44 +05:30
parent 969c867cd4
commit dc5f7206dc
6 changed files with 43 additions and 18 deletions

View File

@ -24,7 +24,7 @@ use crate::{
use fltk::{ use fltk::{
app, app,
button::Button, button::Button,
dialog, enums, enums,
frame::Frame, frame::Frame,
input::{Input, MultilineInput}, input::{Input, MultilineInput},
menu, menu,
@ -274,14 +274,9 @@ fn load_image(
let read = match serde_json::from_str::<ImagePropertiesFile>(&read) { let read = match serde_json::from_str::<ImagePropertiesFile>(&read) {
Ok(r) => r, Ok(r) => r,
Err(e) => { Err(e) => {
warn!("Config is corrupt\n{:?}", e); Result::<(),_>::Err(e).warn_log("Config is corrupt");
match dialog::choice_default("Config is corrupt, fix??", "yes", "no", "") { fs::remove_file(&properties_file).warn_log("Failed to delete image properties file!");
1 => { ImagePropertiesFile::default()
fs::remove_file(&properties_file).warn_log("Failed to delete image properties file!");
ImagePropertiesFile::default()
}
_ => return,
}
} }
}; };
@ -377,7 +372,7 @@ fn show_images_details(images_list: Arc<RwLock<Vec<ImageInfo>>>) {
} }
} }
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 /// Flush the Buffer from image container to drawing buffer for fltk

View File

@ -29,6 +29,9 @@ lazy_static! {
pub(crate) static ref CONFIG: RwLock<config::ConfigFile> = pub(crate) static ref CONFIG: RwLock<config::ConfigFile> =
RwLock::new(config::ConfigFile::load()); RwLock::new(config::ConfigFile::load());
/// Main Sender
pub(crate) static ref MAIN_SENDER: RwLock<Option<fltk::app::Sender<crate::AppMessage>>> = RwLock::new(None);
/// TTF Font for Quote /// TTF Font for Quote
pub(crate) static ref FONT_QUOTE: Font<'static> = { pub(crate) static ref FONT_QUOTE: Font<'static> = {
let mut buffer = Vec::new(); let mut buffer = Vec::new();

View File

@ -42,6 +42,8 @@ use std::sync::{Arc, RwLock};
pub(crate) enum AppMessage { pub(crate) enum AppMessage {
/// Copy recived image buffer from draw_thread to Buffer for fltk frame /// Copy recived image buffer from draw_thread to Buffer for fltk frame
RedrawMainWindowImage(Option<Vec<u8>>), RedrawMainWindowImage(Option<Vec<u8>>),
Message(String),
Alert(String)
} }
fn main() { fn main() {
@ -67,7 +69,8 @@ fn main() {
let draw_buff: Arc<RwLock<Option<Vec<u8>>>> = Arc::new(RwLock::new(None)); let draw_buff: Arc<RwLock<Option<Vec<u8>>>> = Arc::new(RwLock::new(None));
let (main_sender, main_receiver) = channel::<AppMessage>(); let (main_sender, main_receiver) = channel::<AppMessage>();
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() { while app.wait() {
if let Some(msg) = main_receiver.recv() { if let Some(msg) = main_receiver.recv() {
@ -77,6 +80,12 @@ fn main() {
*buff = data; *buff = data;
main_window.win.redraw(); main_window.win.redraw();
} }
AppMessage::Message(msg) => {
dialog::message_default(&msg);
}
AppMessage::Alert(msg) => {
dialog::alert_default(&msg)
}
} }
} }
} }

View File

@ -23,7 +23,6 @@ use crate::utils::ImageType;
use crate::utils::ImageProperties; use crate::utils::ImageProperties;
use crate::{config_window::ConfigWindow, globals}; use crate::{config_window::ConfigWindow, globals};
use fltk::{ use fltk::{
app,
button::Button, button::Button,
dialog, dialog,
dialog::NativeFileChooser, dialog::NativeFileChooser,
@ -101,7 +100,7 @@ pub(crate) struct Page {
impl MainWindow { impl MainWindow {
pub(crate) fn new( pub(crate) fn new(
sender: app::Sender<crate::AppMessage>, // sender: app::Sender<crate::AppMessage>,
draw_buff: Arc<RwLock<Option<Vec<u8>>>>, draw_buff: Arc<RwLock<Option<Vec<u8>>>>,
) -> Self { ) -> Self {
let mut win = Window::new(0, 0, 1100, 700, "Post Maker").center_screen(); let mut win = Window::new(0, 0, 1100, 700, "Post Maker").center_screen();
@ -394,7 +393,11 @@ impl MainWindow {
}, },
sender: rx, 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.menu();
main_win.draw(); main_win.draw();
main_win.events(); main_win.events();

View File

@ -1,6 +1,7 @@
use std::fmt::Debug; use std::fmt::Debug;
use std::panic::Location; use std::panic::Location;
use fltk::dialog;
use crate::utils;
pub trait ResultExt<T, E> { pub trait ResultExt<T, E> {
fn expect_log(self, msg: &str) -> T; fn expect_log(self, msg: &str) -> T;
@ -14,7 +15,7 @@ impl<T, E: Debug> ResultExt<T, E> for Result<T, E> {
match self { match self {
Ok(v) => v, Ok(v) => v,
Err(e) => { Err(e) => {
dialog::alert_default(msg); utils::show_alert(msg);
error!("{}\n{:?}\n{}", msg, e, Location::caller()); error!("{}\n{:?}\n{}", msg, e, Location::caller());
std::process::exit(1); std::process::exit(1);
} }
@ -24,7 +25,7 @@ impl<T, E: Debug> ResultExt<T, E> for Result<T, E> {
#[track_caller] #[track_caller]
fn error_log(&self, msg: &str) { fn error_log(&self, msg: &str) {
if let Err(e) = self { if let Err(e) = self {
dialog::alert_default(msg); utils::show_alert(msg);
error!("{}\n{:?}\n{}", msg, e, Location::caller()); error!("{}\n{:?}\n{}", msg, e, Location::caller());
} }
} }
@ -32,7 +33,7 @@ impl<T, E: Debug> ResultExt<T, E> for Result<T, E> {
#[track_caller] #[track_caller]
fn warn_log(&self, msg: &str) { fn warn_log(&self, msg: &str) {
if let Err(e) = self { if let Err(e) = self {
dialog::alert_default(msg); utils::show_alert(msg);
warn!("{}\n{:?}", msg, e); warn!("{}\n{:?}", msg, e);
} }
} }

View File

@ -728,3 +728,17 @@ pub(crate) fn tag_from_height(height: f64) -> f64 {
pub(crate) fn tag2_from_height(height: f64) -> f64 { pub(crate) fn tag2_from_height(height: f64) -> f64 {
(height * globals::CONFIG.read().unwrap().tag2_font_ratio) / 5000.0 (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()));
}
}