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,34 +38,38 @@
|
||||||
</object>
|
</object>
|
||||||
</property>
|
</property>
|
||||||
<property name="content">
|
<property name="content">
|
||||||
<object class="GtkStack" id="stack">
|
<object class="AdwToastOverlay" id="toast_overlay">
|
||||||
<property name="visible-child">status_page</property>
|
<property name="child">
|
||||||
<property name="vexpand">True</property>
|
<object class="GtkStack" id="stack">
|
||||||
<child>
|
<property name="visible-child">status_page</property>
|
||||||
<object class="AdwStatusPage" id="status_page">
|
<property name="vexpand">True</property>
|
||||||
<property name="title" translatable="yes">Image Viewer</property>
|
|
||||||
<property name="description" translatable="yes">Click "Open File…" or drag an image here.</property>
|
|
||||||
<child>
|
<child>
|
||||||
<object class="GtkButton">
|
<object class="AdwStatusPage" id="status_page">
|
||||||
<property name="halign">center</property>
|
<property name="title" translatable="yes">Image Viewer</property>
|
||||||
<property name="label" translatable="yes">Open File…</property>
|
<property name="description" translatable="yes">Click "Open File…" or drag an image here.</property>
|
||||||
<property name="action-name">win.open</property>
|
<child>
|
||||||
<style>
|
<object class="GtkButton">
|
||||||
<class name="pill"/>
|
<property name="halign">center</property>
|
||||||
<class name="suggested-action"/>
|
<property name="label" translatable="yes">Open File…</property>
|
||||||
</style>
|
<property name="action-name">win.open</property>
|
||||||
|
<style>
|
||||||
|
<class name="pill"/>
|
||||||
|
<class name="suggested-action"/>
|
||||||
|
</style>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="LpImageView" id="image_view"/>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkDropTarget" id="drop_target">
|
||||||
|
<property name="actions">copy</property>
|
||||||
</object>
|
</object>
|
||||||
</child>
|
</child>
|
||||||
</object>
|
</object>
|
||||||
</child>
|
</property>
|
||||||
<child>
|
|
||||||
<object class="LpImageView" id="image_view"/>
|
|
||||||
</child>
|
|
||||||
<child>
|
|
||||||
<object class="GtkDropTarget" id="drop_target">
|
|
||||||
<property name="actions">copy</property>
|
|
||||||
</object>
|
|
||||||
</child>
|
|
||||||
</object>
|
</object>
|
||||||
</property>
|
</property>
|
||||||
<style>
|
<style>
|
||||||
|
|
|
||||||
|
|
@ -271,9 +271,27 @@ impl LpImageView {
|
||||||
pub fn set_wallpaper(&self) -> anyhow::Result<()> {
|
pub fn set_wallpaper(&self) -> anyhow::Result<()> {
|
||||||
let wallpaper = self.uri().context("No URI for current file")?;
|
let wallpaper = self.uri().context("No URI for current file")?;
|
||||||
let ctx = glib::MainContext::default();
|
let ctx = glib::MainContext::default();
|
||||||
ctx.spawn_local(async move {
|
ctx.spawn_local(clone!(@weak self as view => async move {
|
||||||
set_wallpaper(wallpaper).await;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -338,19 +356,3 @@ impl LpImageView {
|
||||||
imp.popover.popup();
|
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]
|
#[template_child]
|
||||||
pub fullscreen_button: TemplateChild<gtk::Button>,
|
pub fullscreen_button: TemplateChild<gtk::Button>,
|
||||||
#[template_child]
|
#[template_child]
|
||||||
|
pub toast_overlay: TemplateChild<adw::ToastOverlay>,
|
||||||
|
#[template_child]
|
||||||
pub stack: TemplateChild<gtk::Stack>,
|
pub stack: TemplateChild<gtk::Stack>,
|
||||||
#[template_child]
|
#[template_child]
|
||||||
pub status_page: TemplateChild<adw::StatusPage>,
|
pub status_page: TemplateChild<adw::StatusPage>,
|
||||||
|
|
@ -101,6 +103,12 @@ mod imp {
|
||||||
klass.install_action("win.print", None, move |win, _, _| {
|
klass.install_action("win.print", None, move |win, _, _| {
|
||||||
win.print();
|
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>) {
|
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
|
||||||
|
|
@ -155,8 +163,12 @@ mod imp {
|
||||||
let file = list.files().get(0).unwrap().clone();
|
let file = list.files().get(0).unwrap().clone();
|
||||||
let info = util::query_attributes(
|
let info = util::query_attributes(
|
||||||
&file,
|
&file,
|
||||||
vec![&gio::FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE])
|
vec![
|
||||||
.expect("Could not query file info");
|
&gio::FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
|
||||||
|
&gio::FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
.expect("Could not query file info");
|
||||||
|
|
||||||
if info
|
if info
|
||||||
.content_type()
|
.content_type()
|
||||||
|
|
@ -165,7 +177,10 @@ mod imp {
|
||||||
.is_some() {
|
.is_some() {
|
||||||
obj.set_image_from_file(&file, false);
|
obj.set_image_from_file(&file, false);
|
||||||
} else {
|
} 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
|
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) {
|
pub fn set_image_from_file(&self, file: &gio::File, resize: bool) {
|
||||||
let imp = imp::LpWindow::from_instance(self);
|
let imp = imp::LpWindow::from_instance(self);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue