util/root: Add window_show_error() to avoid unwrap

Fixes #553
This commit is contained in:
Sophie Herold 2025-12-09 22:04:58 +01:00
parent cfd33fa2dd
commit 83b2c7cae1
4 changed files with 18 additions and 16 deletions

View file

@ -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

View file

@ -52,16 +52,12 @@ pub trait ParentWindow: WidgetExt {
#[track_caller] #[track_caller]
fn window_show_toast(&self, text: &str, priority: adw::ToastPriority) { fn window_show_toast(&self, text: &str, priority: adw::ToastPriority) {
if let Some(window) = self.try_window() { self.window_inspect(|w| w.show_toast(text, priority));
window.show_toast(text, priority);
}
} }
#[track_caller] #[track_caller]
fn window_add_toast(&self, toast: adw::Toast) { fn window_add_toast(&self, toast: adw::Toast) {
if let Some(window) = self.try_window() { self.window_inspect(|w| w.add_toast(toast));
window.add_toast(toast);
}
} }
#[track_caller] #[track_caller]
@ -69,6 +65,11 @@ pub trait ParentWindow: WidgetExt {
self.try_window().inspect(f); 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 { fn edit_window(&self) -> LpEditWindow {
let parent = self.parent().unwrap(); let parent = self.parent().unwrap();

View file

@ -359,7 +359,7 @@ impl LpEditCrop {
fn handle_error(&self, res: Result<(), EditingError>) { fn handle_error(&self, res: Result<(), EditingError>) {
if let Err(err) = res { if let Err(err) = res {
self.window().show_error( self.window_show_error(
&gettext("Failed to Edit Image"), &gettext("Failed to Edit Image"),
&format!("Failed to edit image: {err}"), &format!("Failed to edit image: {err}"),
ErrorType::General, ErrorType::General,

View file

@ -167,7 +167,7 @@ mod imp {
if let Some(cancellable) = obj.imp().save_cancellable.replace(None) { if let Some(cancellable) = obj.imp().save_cancellable.replace(None) {
cancellable.cancel(); cancellable.cancel();
} }
obj.window().show_image(); obj.window_inspect(|w| w.show_image());
} }
async fn save_copy(&self) { async fn save_copy(&self) {
@ -232,7 +232,7 @@ mod imp {
file_dialog.set_initial_file(Some(&suggested_file)); 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) => { Err(err) => {
log::error!("{}", err); log::error!("{}", err);
} }
@ -241,7 +241,7 @@ mod imp {
.save(current_file, new_file.clone(), cancellable.clone()) .save(current_file, new_file.clone(), cancellable.clone())
.await .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"); log::debug!("Trashing image canceled");
return; return;
} else if let Ok(Err(err)) = trash_result { } else if let Ok(Err(err)) = trash_result {
obj.window().show_error( obj.window_show_error(
&gettext("Failed to save image."), &gettext("Failed to save image."),
&format!("Failed to move image {current_path:?} to trash and therefore couldn't save image: {err}"), &format!("Failed to move image {current_path:?} to trash and therefore couldn't save image: {err}"),
ErrorType::General, ErrorType::General,
@ -316,7 +316,7 @@ mod imp {
log::debug!("Moving image canceled"); log::debug!("Moving image canceled");
return; return;
} else if let Ok(Err(err)) = move_result { } else if let Ok(Err(err)) = move_result {
obj.window().show_error( obj.window_show_error(
&gettext("Failed to save image."), &gettext("Failed to save image."),
&format!( &format!(
"Failed to move image image to {current_path:?}: {err} move {:?} to {:?}", tmp_file.path().unwrap(), original_file.path().unwrap() "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; 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) => { Err(err) => {
log::warn!("Failed to edit image: {err}"); log::warn!("Failed to edit image: {err}");
obj.window().show_error( obj.window_show_error(
&gettext("Failed to edit image."), &gettext("Failed to edit image."),
&format!("Failed to edit image:\n\n{err}\n\n{operations:#?}"), &format!("Failed to edit image:\n\n{err}\n\n{operations:#?}"),
ErrorType::Loader, ErrorType::Loader,
@ -373,7 +373,7 @@ mod imp {
log::debug!("Saving edited image to '{}'", new_file.uri()); log::debug!("Saving edited image to '{}'", new_file.uri());
match edit.data().get() { match edit.data().get() {
Err(err) => { Err(err) => {
obj.window().show_error( obj.window_show_error(
&gettext("Failed to Save Image"), &gettext("Failed to Save Image"),
&format!("Failed to get binary data: {err}"), &format!("Failed to get binary data: {err}"),
ErrorType::General, ErrorType::General,
@ -396,7 +396,7 @@ mod imp {
if save_result.is_err() { if save_result.is_err() {
log::debug!("Saving image canceled"); log::debug!("Saving image canceled");
} else if let Ok(Err(err)) = save_result { } else if let Ok(Err(err)) = save_result {
obj.window().show_error( obj.window_show_error(
&gettext("Failed to Save Image"), &gettext("Failed to Save Image"),
&format!("Failed to write file:\n\n{err:?}"), &format!("Failed to write file:\n\n{err:?}"),
ErrorType::General, ErrorType::General,