From 83b2c7cae1f818fe2b4aae3a7645176ec7e816c4 Mon Sep 17 00:00:00 2001 From: Sophie Herold Date: Tue, 9 Dec 2025 22:04:58 +0100 Subject: [PATCH] util/root: Add window_show_error() to avoid unwrap Fixes #553 --- news.d/49.2/fixed-crashes-when-closing-window | 1 + src/util/root.rs | 13 +++++++------ src/widgets/edit/crop.rs | 2 +- src/widgets/edit_window.rs | 18 +++++++++--------- 4 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 news.d/49.2/fixed-crashes-when-closing-window diff --git a/news.d/49.2/fixed-crashes-when-closing-window b/news.d/49.2/fixed-crashes-when-closing-window new file mode 100644 index 0000000..c518e6e --- /dev/null +++ b/news.d/49.2/fixed-crashes-when-closing-window @@ -0,0 +1 @@ +Fix panics, probably occuring when using an action like 'copy', and then closing the window. The crash causes all other windows to close. #553 diff --git a/src/util/root.rs b/src/util/root.rs index a5774b1..9b116da 100644 --- a/src/util/root.rs +++ b/src/util/root.rs @@ -52,16 +52,12 @@ pub trait ParentWindow: WidgetExt { #[track_caller] fn window_show_toast(&self, text: &str, priority: adw::ToastPriority) { - if let Some(window) = self.try_window() { - window.show_toast(text, priority); - } + self.window_inspect(|w| w.show_toast(text, priority)); } #[track_caller] fn window_add_toast(&self, toast: adw::Toast) { - if let Some(window) = self.try_window() { - window.add_toast(toast); - } + self.window_inspect(|w| w.add_toast(toast)); } #[track_caller] @@ -69,6 +65,11 @@ pub trait ParentWindow: WidgetExt { self.try_window().inspect(f); } + #[track_caller] + fn window_show_error(&self, stub: &str, details: &str, error_type: super::ErrorType) { + self.window_inspect(|w| w.show_error(stub, details, error_type)); + } + fn edit_window(&self) -> LpEditWindow { let parent = self.parent().unwrap(); diff --git a/src/widgets/edit/crop.rs b/src/widgets/edit/crop.rs index 7e8f8ae..bfeea90 100644 --- a/src/widgets/edit/crop.rs +++ b/src/widgets/edit/crop.rs @@ -359,7 +359,7 @@ impl LpEditCrop { fn handle_error(&self, res: Result<(), EditingError>) { if let Err(err) = res { - self.window().show_error( + self.window_show_error( &gettext("Failed to Edit Image"), &format!("Failed to edit image: {err}"), ErrorType::General, diff --git a/src/widgets/edit_window.rs b/src/widgets/edit_window.rs index e0a9cac..aa5af84 100644 --- a/src/widgets/edit_window.rs +++ b/src/widgets/edit_window.rs @@ -167,7 +167,7 @@ mod imp { if let Some(cancellable) = obj.imp().save_cancellable.replace(None) { cancellable.cancel(); } - obj.window().show_image(); + obj.window_inspect(|w| w.show_image()); } async fn save_copy(&self) { @@ -232,7 +232,7 @@ mod imp { file_dialog.set_initial_file(Some(&suggested_file)); - match file_dialog.save_future(Some(&obj.window())).await { + match file_dialog.save_future(obj.try_window().as_ref()).await { Err(err) => { log::error!("{}", err); } @@ -241,7 +241,7 @@ mod imp { .save(current_file, new_file.clone(), cancellable.clone()) .await { - obj.window().show_specific_image(new_file); + obj.window_inspect(|w| w.show_specific_image(new_file)); } } } @@ -290,7 +290,7 @@ mod imp { log::debug!("Trashing image canceled"); return; } else if let Ok(Err(err)) = trash_result { - obj.window().show_error( + obj.window_show_error( &gettext("Failed to save image."), &format!("Failed to move image {current_path:?} to trash and therefore couldn't save image: {err}"), ErrorType::General, @@ -316,7 +316,7 @@ mod imp { log::debug!("Moving image canceled"); return; } else if let Ok(Err(err)) = move_result { - obj.window().show_error( + obj.window_show_error( &gettext("Failed to save image."), &format!( "Failed to move image image to {current_path:?}: {err} move {:?} to {:?}", tmp_file.path().unwrap(), original_file.path().unwrap() @@ -326,7 +326,7 @@ mod imp { return; } - obj.window().show_specific_image(original_file); + obj.window_inspect(|w| w.show_specific_image(original_file)); } } } @@ -363,7 +363,7 @@ mod imp { } Err(err) => { log::warn!("Failed to edit image: {err}"); - obj.window().show_error( + obj.window_show_error( &gettext("Failed to edit image."), &format!("Failed to edit image:\n\n{err}\n\n{operations:#?}"), ErrorType::Loader, @@ -373,7 +373,7 @@ mod imp { log::debug!("Saving edited image to '{}'", new_file.uri()); match edit.data().get() { Err(err) => { - obj.window().show_error( + obj.window_show_error( &gettext("Failed to Save Image"), &format!("Failed to get binary data: {err}"), ErrorType::General, @@ -396,7 +396,7 @@ mod imp { if save_result.is_err() { log::debug!("Saving image canceled"); } else if let Ok(Err(err)) = save_result { - obj.window().show_error( + obj.window_show_error( &gettext("Failed to Save Image"), &format!("Failed to write file:\n\n{err:?}"), ErrorType::General,