window: Add ToastOverlay and use toasts for feedback

This commit is contained in:
Christopher Davis 2021-12-28 16:38:36 -08:00
parent 084304cadc
commit 400672b6b6
3 changed files with 76 additions and 46 deletions

View file

@ -61,6 +61,8 @@ mod imp {
#[template_child]
pub fullscreen_button: TemplateChild<gtk::Button>,
#[template_child]
pub toast_overlay: TemplateChild<adw::ToastOverlay>,
#[template_child]
pub stack: TemplateChild<gtk::Stack>,
#[template_child]
pub status_page: TemplateChild<adw::StatusPage>,
@ -101,6 +103,12 @@ mod imp {
klass.install_action("win.print", None, move |win, _, _| {
win.print();
});
klass.install_action("win.show-toast", Some("(si)"), move |win, _, var| {
if let Some((ref toast, i)) = var.map(|v| v.get::<(String, i32)>()).flatten() {
win.show_toast(toast, adw::ToastPriority::__Unknown(i));
}
});
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
@ -155,8 +163,12 @@ mod imp {
let file = list.files().get(0).unwrap().clone();
let info = util::query_attributes(
&file,
vec![&gio::FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE])
.expect("Could not query file info");
vec![
&gio::FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
&gio::FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
],
)
.expect("Could not query file info");
if info
.content_type()
@ -165,7 +177,10 @@ mod imp {
.is_some() {
obj.set_image_from_file(&file, false);
} else {
log::debug!("{} was not a valid image.", file.uri().to_string());
obj.show_toast(
&format!("\"{}\" is not a valid image.", info.display_name().to_string()),
adw::ToastPriority::High,
);
}
true
@ -275,6 +290,15 @@ impl LpWindow {
}
}
fn show_toast(&self, text: &impl AsRef<str>, priority: adw::ToastPriority) {
let imp = imp::LpWindow::from_instance(self);
let toast = adw::Toast::new(text.as_ref());
toast.set_priority(priority);
imp.toast_overlay.add_toast(&toast);
}
pub fn set_image_from_file(&self, file: &gio::File, resize: bool) {
let imp = imp::LpWindow::from_instance(self);