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
This commit is contained in:
Huan Nguyen 2023-05-30 12:52:59 +00:00 • committed by Sophie Herold
parent 6dda1eeb50
commit 735451250c
3 changed files with 12 additions and 17 deletions

View file

@ -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());
}
}

View file

@ -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<gio::File>) {
// Add image to recently used file. Does not work in Flatpaks:
// <https://github.com/flatpak/xdg-desktop-portal/issues/215>
gtk::RecentManager::default().add_item(&file.uri());
self.load_file(file);
}
pub fn set_images_from_files(&self, files: Vec<gio::File>) {
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<gio::File>) {

View file

@ -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::<gdk::FileList>().unwrap()`.
let mut files = match value.get::<gdk::FileList>() {
let files = match value.get::<gdk::FileList>() {
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(