From 735451250c427d9c11c27ff1ed6b3459183ed3de Mon Sep 17 00:00:00 2001 From: Huan Nguyen Date: Tue, 30 May 2023 12:52:59 +0000 Subject: [PATCH] imageView: Merge set_image_from_file with set_images_from_files Also changing behavior when opening with multiple command line arguments or when multiple images were selected from other applications. Old behavior: Spawn an instance of Loupe for each argument. New behavior: One Loupe instance containing all arguments. Closes #195 --- src/application.rs | 6 ++---- src/widgets/image_view.rs | 15 +++++++-------- src/window.rs | 8 +++----- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/application.rs b/src/application.rs index cb9bd82..494cbfd 100644 --- a/src/application.rs +++ b/src/application.rs @@ -71,10 +71,8 @@ mod imp { // Handles opening files from the command line or other applications fn open(&self, files: &[gio::File], _hint: &str) { let application = self.obj(); - for file in files { - let win = LpWindow::new(&*application); - win.image_view().set_image_from_file(file.clone()); - } + let win = LpWindow::new(&*application); + win.image_view().set_images_from_files(files.to_vec()); } } diff --git a/src/widgets/image_view.rs b/src/widgets/image_view.rs index c6dcdae..c16d2a7 100644 --- a/src/widgets/image_view.rs +++ b/src/widgets/image_view.rs @@ -241,20 +241,19 @@ glib::wrapper! { #[gtk::template_callbacks] impl LpImageView { - pub fn set_image_from_file(&self, file: gio::File) { + pub fn set_images_from_files(&self, files: Vec) { // Add image to recently used file. Does not work in Flatpaks: // - gtk::RecentManager::default().add_item(&file.uri()); - - self.load_file(file); - } - - pub fn set_images_from_files(&self, files: Vec) { let recent_manager = gtk::RecentManager::default(); for file in files.iter() { recent_manager.add_item(&file.uri()); } - self.load_files(files); + + if files.len() == 1 { + self.load_file(files[0].clone()); + } else { + self.load_files(files); + } } fn load_files(&self, files: Vec) { diff --git a/src/window.rs b/src/window.rs index bc5e346..f2658da 100644 --- a/src/window.rs +++ b/src/window.rs @@ -388,7 +388,7 @@ mod imp { // We've added type annotations here, and written it as `let list: gdk::FileList = ...`, // but you might also see places where type arguments are used. // This line could have been written as `let list = value.get::().unwrap()`. - let mut files = match value.get::() { + let files = match value.get::() { Ok(list) => list.files(), Err(err) => { log::error!("Issue with drop value: {err}"); @@ -396,10 +396,8 @@ mod imp { } }; - if files.len() > 1 { + if !files.is_empty() { obj.image_view().set_images_from_files(files); - } else if let Some(file) = files.pop() { - obj.image_view().set_image_from_file(file); } else { log::error!("Dropped FileList was empty"); return false; @@ -566,7 +564,7 @@ impl LpWindow { spawn(async move { let result = crate::util::untrash(&path).await; match result { - Ok(()) => win.image_view().set_image_from_file(gio::File::for_path(&path)), + Ok(()) => win.image_view().set_images_from_files(vec![gio::File::for_path(&path)]), Err(err) => { log::error!("Failed to untrash {path:?}: {err}"); win.show_toast(