Add buttons on shell notification
This commit is contained in:
@ -10,7 +10,7 @@ use gtk::prelude::*;
|
||||
use gtk::{gdk, gio, glib};
|
||||
use ntfy_daemon::models;
|
||||
use ntfy_daemon::ntfy_capnp::system_notifier;
|
||||
use tracing::{debug, info, warn};
|
||||
use tracing::{debug, error, info, warn};
|
||||
|
||||
use crate::config::{APP_ID, PKGDATADIR, PROFILE, VERSION};
|
||||
use crate::widgets::*;
|
||||
@ -126,7 +126,59 @@ impl NotifyApplication {
|
||||
app.show_about_dialog();
|
||||
})
|
||||
.build();
|
||||
self.add_action_entries([action_quit, action_about]);
|
||||
|
||||
let message_action = gio::ActionEntry::builder("message-action")
|
||||
.parameter_type(Some(&glib::VariantTy::STRING))
|
||||
.activate(|app: &Self, _, params| {
|
||||
let Some(params) = params else {
|
||||
return;
|
||||
};
|
||||
let Some(s) = params.str() else {
|
||||
warn!("action is not a string");
|
||||
return;
|
||||
};
|
||||
let Ok(action) = serde_json::from_str(s) else {
|
||||
error!("invalid action json");
|
||||
return;
|
||||
};
|
||||
app.handle_message_action(action);
|
||||
})
|
||||
.build();
|
||||
self.add_action_entries([action_quit, action_about, message_action]);
|
||||
}
|
||||
|
||||
fn handle_message_action(&self, action: models::Action) {
|
||||
match action {
|
||||
models::Action::View { url, .. } => {
|
||||
gtk::UriLauncher::builder().uri(url.clone()).build().launch(
|
||||
gtk::Window::NONE,
|
||||
gio::Cancellable::NONE,
|
||||
|_| {},
|
||||
);
|
||||
}
|
||||
models::Action::Http {
|
||||
method,
|
||||
url,
|
||||
body,
|
||||
headers,
|
||||
..
|
||||
} => {
|
||||
gio::spawn_blocking(move || {
|
||||
let mut req = ureq::request(method.as_str(), url.as_str());
|
||||
for (k, v) in headers.iter() {
|
||||
req = req.set(&k, &v);
|
||||
}
|
||||
let res = req.send(body.as_bytes());
|
||||
match res {
|
||||
Err(e) => {
|
||||
error!(error = ?e, "Error sending request");
|
||||
}
|
||||
Ok(_) => {}
|
||||
}
|
||||
});
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Sets up keyboard shortcuts
|
||||
@ -192,6 +244,23 @@ impl NotifyApplication {
|
||||
rx.attach(None, move |n: models::Notification| {
|
||||
let gio_notif = gio::Notification::new(&n.title);
|
||||
gio_notif.set_body(Some(&n.body));
|
||||
|
||||
let action_name = |a| {
|
||||
let json = serde_json::to_string(a).unwrap();
|
||||
gio::Action::print_detailed_name("app.message-action", Some(&json.into()))
|
||||
};
|
||||
for a in n.actions.iter() {
|
||||
match a {
|
||||
models::Action::View { label, .. } => {
|
||||
gio_notif.add_button(&label, &action_name(a))
|
||||
}
|
||||
models::Action::Http { label, .. } => {
|
||||
gio_notif.add_button(&label, &action_name(a))
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
app.send_notification(None, &gio_notif);
|
||||
glib::ControlFlow::Continue
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user