window: Add ToastOverlay and use toasts for feedback
This commit is contained in:
parent
084304cadc
commit
400672b6b6
3 changed files with 76 additions and 46 deletions
|
|
@ -38,6 +38,8 @@
|
|||
</object>
|
||||
</property>
|
||||
<property name="content">
|
||||
<object class="AdwToastOverlay" id="toast_overlay">
|
||||
<property name="child">
|
||||
<object class="GtkStack" id="stack">
|
||||
<property name="visible-child">status_page</property>
|
||||
<property name="vexpand">True</property>
|
||||
|
|
@ -68,6 +70,8 @@
|
|||
</child>
|
||||
</object>
|
||||
</property>
|
||||
</object>
|
||||
</property>
|
||||
<style>
|
||||
<class name="fullscreen-widget"/>
|
||||
</style>
|
||||
|
|
|
|||
|
|
@ -271,9 +271,27 @@ impl LpImageView {
|
|||
pub fn set_wallpaper(&self) -> anyhow::Result<()> {
|
||||
let wallpaper = self.uri().context("No URI for current file")?;
|
||||
let ctx = glib::MainContext::default();
|
||||
ctx.spawn_local(async move {
|
||||
set_wallpaper(wallpaper).await;
|
||||
});
|
||||
ctx.spawn_local(clone!(@weak self as view => async move {
|
||||
let status = match wallpaper::set_from_uri(
|
||||
&WindowIdentifier::default(),
|
||||
&wallpaper,
|
||||
true,
|
||||
wallpaper::SetOn::Background,
|
||||
)
|
||||
.await {
|
||||
Ok(_) => "Set as wallpaper.",
|
||||
Err(_) => "Could not set wallpaper.",
|
||||
};
|
||||
|
||||
view.activate_action(
|
||||
"win.show-toast",
|
||||
// We use `1` here because we can't pass enums directly as GVariants,
|
||||
// so we need to use the C int value of the enum.
|
||||
// `TOAST_PRIORITY_NORMAL = 0`, and `TOAST_PRIORITY_HIGH = 1`
|
||||
Some(&(status, 1).to_variant()),
|
||||
)
|
||||
.unwrap();
|
||||
}));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -338,19 +356,3 @@ impl LpImageView {
|
|||
imp.popover.popup();
|
||||
}
|
||||
}
|
||||
|
||||
async fn set_wallpaper(uri: String) {
|
||||
if let Err(e) = wallpaper::set_from_uri(
|
||||
&WindowIdentifier::default(),
|
||||
&uri,
|
||||
true,
|
||||
wallpaper::SetOn::Background,
|
||||
)
|
||||
.await
|
||||
{
|
||||
log::error!(
|
||||
"Failed to set the wallpaper using the freedesktop portal: {}",
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,7 +163,11 @@ mod imp {
|
|||
let file = list.files().get(0).unwrap().clone();
|
||||
let info = util::query_attributes(
|
||||
&file,
|
||||
vec![&gio::FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE])
|
||||
vec![
|
||||
&gio::FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
|
||||
&gio::FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
|
||||
],
|
||||
)
|
||||
.expect("Could not query file info");
|
||||
|
||||
if info
|
||||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue