From 578bdb3d4789d9cd13e21440a66f413808144152 Mon Sep 17 00:00:00 2001 From: Piyush Mishra Date: Sun, 16 Jan 2022 17:21:42 +0530 Subject: [PATCH] changed delete and clone --- src/draw_thread.rs | 46 ++++++++++++++++++++++++---------------------- src/utils.rs | 17 ++++++++++++----- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/draw_thread.rs b/src/draw_thread.rs index aaf08d9..386dc94 100644 --- a/src/draw_thread.rs +++ b/src/draw_thread.rs @@ -47,9 +47,8 @@ pub(crate) fn spawn_image_thread( properties: Arc>, main_win: &MainWindow, ) { + let mut win = main_win.win.clone(); let mut file_choice = main_win.file_choice.clone(); - let mut next_btn = main_win.next_btn.clone(); - let mut back_btn = main_win.back_btn.clone(); let mut quote = main_win.quote.clone(); let mut tag = main_win.tag.clone(); let mut layer_red = main_win.layer_red.clone(); @@ -72,6 +71,7 @@ pub(crate) fn spawn_image_thread( match val { DrawMessage::Open => { status.set_label("Loading..."); + // win.deactivate(); load_image( &mut file_choice, Arc::clone(&images_path), @@ -93,13 +93,13 @@ pub(crate) fn spawn_image_thread( &properties, &mut _container, ); + // win.activate(); + status.set_label(""); status.set_label(""); } DrawMessage::ChangeCrop((x, y)) => { status.set_label("Loading..."); - file_choice.deactivate(); - next_btn.deactivate(); - back_btn.deactivate(); + // win.deactivate(); load_image( &mut file_choice, Arc::clone(&images_path), @@ -121,9 +121,7 @@ pub(crate) fn spawn_image_thread( &properties, &mut _container, ); - file_choice.activate(); - next_btn.activate(); - back_btn.activate(); + // win.activate(); status.set_label(""); } DrawMessage::Recalc => { @@ -137,44 +135,48 @@ pub(crate) fn spawn_image_thread( DrawMessage::Save => { if let Some(cont) = &mut _container { status.set_label("Saving..."); + win.deactivate(); cont.save(); + win.activate(); status.set_label(""); } } DrawMessage::Clone => { if let Some(cont) = &mut _container { status.set_label("Cloning..."); + win.deactivate(); if let Some(path) = cont.clone_img() { - images_path - .write() - .unwrap() - .insert(file_choice.value() as usize, path.clone()); + let idx = file_choice.value(); + let mut imgs = images_path.write().unwrap(); + imgs.insert(idx as usize, path.clone()); file_choice.insert( - file_choice.value(), + idx, path.file_name().unwrap().to_str().unwrap(), enums::Shortcut::None, menu::MenuFlag::Normal, - |_| {}, + |a| a.do_callback(), ); + file_choice.set_value(idx); } status.set_label(""); + win.activate(); } } DrawMessage::Delete => { if let Some(cont) = &mut _container { - status.set_label("Deleting to trash..."); + status.set_label("Deleting..."); + win.deactivate(); cont.delete(); - images_path - .write() - .unwrap() - .remove(file_choice.value() as usize); + let mut imgs = images_path.write().unwrap(); + imgs.remove(file_choice.value() as usize); file_choice.remove(file_choice.value()); - if file_choice.value() > 0 { - file_choice.set_value(file_choice.value() - 1); + if file_choice.value() != imgs.len() as i32 { + file_choice.set_value(file_choice.value()); } else { - file_choice.set_value(0); + file_choice.set_value(file_choice.value() - 1); } status.set_label(""); + win.activate(); } } } diff --git a/src/utils.rs b/src/utils.rs index 47fee0b..1a4a45c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -208,18 +208,25 @@ impl ImageContainer { Some(path) => { let name = path.file_stem().unwrap().to_string_lossy(); let ext = path.extension().unwrap().to_string_lossy(); - let new_file = format!("{}_copy.{}", name, ext); - let new_path = path.with_file_name(&new_file); + let mut i = 1; + let mut new_path = path.clone(); + while new_path.exists() { + let new_file = format!("{}{}.{}", name, "-copy".repeat(i), ext); + new_path = path.with_file_name(&new_file); + i += 1; + } + let path_conf = path.with_extension("conf"); let path_conf_new = new_path.with_extension("conf"); - if fs::copy(path, &new_path).is_err() { + if path.exists() && fs::copy(path, &new_path).is_err() { dialog::message_default("Failed to clone image!"); return None; } - if fs::copy(path_conf, &path_conf_new).is_err() { - dialog::message_default("Failed to clone image conf!"); + if path_conf.exists() && fs::copy(path_conf, &path_conf_new).is_err() { + dialog::message_default("Failed to clone image!"); + return None; } Some(new_path) }