util/root: Use specific function to show toasts

This commit is contained in:
Sophie Herold 2025-12-09 15:55:30 +01:00
parent 3e69dc4c6f
commit 7b45009eed
5 changed files with 40 additions and 65 deletions

View file

@ -39,15 +39,28 @@ pub trait ParentWindow: WidgetExt {
let result = self.root().and_downcast();
if result.is_none() {
log::debug!(
"Couldn't find LpWindow for {self:?}. mapped={}",
self.is_mapped()
log::error!(
"Couldn't find LpWindow for {self:?}. mapped={}. caller={}",
self.is_mapped(),
std::panic::Location::caller(),
);
}
result
}
fn window_show_toast(&self, text: &str, priority: adw::ToastPriority) {
if let Some(window) = self.try_window() {
window.show_toast(text, priority);
}
}
fn window_add_toast(&self, toast: adw::Toast) {
if let Some(window) = self.try_window() {
window.add_toast(toast);
}
}
fn edit_window(&self) -> LpEditWindow {
let parent = self.parent().unwrap();

View file

@ -37,7 +37,6 @@ use adw::subclass::prelude::*;
use anyhow::Context;
use ashpd::desktop::{ResponseError, wallpaper};
use ashpd::{Error, WindowIdentifier};
use glib::translate::IntoGlib;
use glib::{Properties, clone};
use gtk::CompositeTemplate;
@ -45,6 +44,7 @@ use crate::decoder::DecoderError;
use crate::deps::*;
use crate::file_model::{FileEvent, LpFileModel};
use crate::util::gettext::*;
use crate::util::root::ParentWindow;
use crate::util::{self, Direction, Position};
use crate::widgets::{LpImage, LpImagePage, LpPrint, LpSlidingView};
@ -483,17 +483,10 @@ impl LpImageView {
if let Some(directory) = directory {
if let Err(err) = obj.model().load_directory(directory.clone()).await {
log::warn!("Failed to load directory: {}", err.root_cause());
obj.activate_action(
"win.show-toast",
Some(
&(
format!("{} {}", err, err.root_cause()),
adw::ToastPriority::High.into_glib(),
)
.to_variant(),
),
)
.unwrap();
obj.window_show_toast(
&format!("{} {}", err, err.root_cause()),
adw::ToastPriority::High,
);
return;
}
@ -835,33 +828,18 @@ impl LpImageView {
.await
.and_then(|req| req.response())
{
Ok(_) => self
.activate_action(
"win.show-toast",
Some(
&(
// Translators: This is a toast notification, informing the user that
// an image has been set as background.
gettext("Set as background."),
adw::ToastPriority::High.into_glib(),
)
.to_variant(),
),
)
.unwrap(),
Ok(_) => self.window_show_toast(
// Translators: This is a toast notification, informing the user that
// an image has been set as background.
&gettext("Set as background."),
adw::ToastPriority::High,
),
Err(err) => {
if !matches!(err, Error::Response(ResponseError::Cancelled)) {
self.activate_action(
"win.show-toast",
Some(
&(
gettext("Could not set background."),
adw::ToastPriority::High.into_glib(),
)
.to_variant(),
),
)
.unwrap();
self.window_show_toast(
&gettext("Could not set background."),
adw::ToastPriority::High,
);
}
}
};

View file

@ -610,7 +610,7 @@ impl LpImageWindow {
if let Err(e) = imp.image_view.copy() {
log::error!("Failed to copy to clipboard: {}", e);
} else {
self.window().show_toast(
self.window_show_toast(
&gettext("Image copied to clipboard"),
adw::ToastPriority::High,
);
@ -647,7 +647,7 @@ impl LpImageWindow {
let result = crate::util::untrash(&path).await;
if let Err(err) = result {
log::error!("Failed to untrash {path:?}: {err}");
obj.window().show_toast(
obj.window_show_toast(
&gettext("Failed to restore image from trash"),
adw::ToastPriority::High,
);
@ -655,14 +655,14 @@ impl LpImageWindow {
});
}
));
self.window().add_toast(toast);
self.window_add_toast(toast);
}
Err(err) => {
if Some(gio::IOErrorEnum::NotSupported) == err.kind::<gio::IOErrorEnum>() {
self.delete_future(&path).await;
} else {
log::error!("Failed to delete file {path:?}: {err}");
self.window().show_toast(
self.window_show_toast(
&gettext("Failed to move image to trash"),
adw::ToastPriority::Normal,
);
@ -708,7 +708,7 @@ impl LpImageWindow {
if let Err(err) = result {
log::error!("Failed to delete file {path:?}: {err}");
self.window().show_toast(
self.window_show_toast(
&gettext("Failed to delete image"),
adw::ToastPriority::Normal,
);

View file

@ -23,11 +23,11 @@ use std::cell::{OnceCell, RefCell};
use adw::prelude::*;
use adw::subclass::prelude::*;
use glib::Properties;
use glib::translate::IntoGlib;
use gtk::CompositeTemplate;
use crate::deps::*;
use crate::util::gettext::*;
use crate::util::root::ParentWindow;
use crate::util::{self};
use crate::widgets::image::LpImage;
@ -322,15 +322,9 @@ mod imp {
if let Some(location) = obj.image().and_then(|x| x.metadata().gps_location()) {
let clipboard = obj.display().clipboard();
clipboard.set_text(&location.iso_6709());
let _ = obj.activate_action(
"win.show-toast",
Some(
&(
gettext("Image Coordinates Copied"),
adw::ToastPriority::Normal.into_glib(),
)
.to_variant(),
),
obj.window_show_toast(
&gettext("Image Coordinates Copied"),
adw::ToastPriority::Normal,
);
}
}

View file

@ -91,16 +91,6 @@ mod imp {
"win.show-help-overlay",
);
klass.install_action(
"win.show-toast",
Some(VariantTy::TUPLE),
move |win, _, var| {
if let Some((ref toast, i)) = var.and_then(|v| v.get::<(String, i32)>()) {
win.show_toast(toast, adw::ToastPriority::__Unknown(i));
}
},
);
WindowAction::init_actions_and_bindings(klass);
ActionPartGlobal::init_actions_and_bindings(klass);