wip: send dismiss requests and display notifications

This commit is contained in:
Ashley Wulber 2023-07-03 14:53:45 -04:00 • committed by Ashley Wulber
parent c4566730ab
commit 060b316365
5 changed files with 330 additions and 15 deletions

View file

@ -1 +1,83 @@
// TODO connect as a client and send actions / dismissals
use crate::subscriptions::dbus_proxy::NotificationsProxy;
use cosmic::{
iced::{
futures::{self, SinkExt},
subscription,
},
iced_futures::Subscription,
};
use tokio::sync::mpsc::{channel, Receiver, Sender};
use tracing::{error, warn};
use zbus::Connection;
#[derive(Debug)]
pub enum State {
Ready,
WaitingForNotificationEvent(Connection, Receiver<Input>),
Finished,
}
#[derive(Debug, Clone, Copy)]
pub enum Input {
Dismiss(u32),
}
#[derive(Debug, Clone)]
pub enum Output {
Ready(Sender<Input>),
}
pub fn proxy() -> Subscription<Output> {
struct SomeWorker;
subscription::channel(
std::any::TypeId::of::<SomeWorker>(),
50,
|mut output| async move {
let mut state = State::Ready;
loop {
match &mut state {
State::Ready => {
let (sender, receiver) = channel(10);
let Ok(conn) = Connection::session().await else {
error!("Failed to connect to session bus");
state = State::Finished;
continue;
};
if let Err(err) = output.send(Output::Ready(sender)).await {
error!("Failed to send sender: {}", err);
state = State::Finished;
continue;
}
state = State::WaitingForNotificationEvent(conn, receiver);
}
State::WaitingForNotificationEvent(conn, rx) => {
let Ok(proxy) = NotificationsProxy::new(&conn).await else {
error!("Failed to create proxy from session connection");
state = State::Finished;
continue;
};
match rx.recv().await {
Some(Input::Dismiss(id)) => {
if let Err(err) = proxy.close_notification(id).await {
error!("Failed to close notification: {}", err);
}
}
None => {
warn!("Notification event channel closed");
state = State::Finished;
continue;
}
}
}
State::Finished => {
let () = futures::future::pending().await;
}
}
}
},
)
}

View file

@ -0,0 +1,59 @@
//! # DBus interface proxy for: `org.freedesktop.Notifications`
//!
//! This code was generated by `zbus-xmlgen` `3.1.1` from DBus introspection data.
//! Source: `Interface '/org/freedesktop/Notifications' from service 'org.freedesktop.Notifications' on session bus`.
//!
//! You may prefer to adapt it, instead of using it verbatim.
//!
//! More information can be found in the
//! [Writing a client proxy](https://dbus.pages.freedesktop.org/zbus/client.html)
//! section of the zbus documentation.
//!
//! This DBus object implements
//! [standard DBus interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html),
//! (`org.freedesktop.DBus.*`) for which the following zbus proxies can be used:
//!
//! * [`zbus::fdo::PeerProxy`]
//! * [`zbus::fdo::IntrospectableProxy`]
//! * [`zbus::fdo::PropertiesProxy`]
//!
//! …consequently `zbus-xmlgen` did not generate code for the above interfaces.
use zbus::dbus_proxy;
#[dbus_proxy(
interface = "org.freedesktop.Notifications",
default_service = "org.freedesktop.Notifications",
default_path = "/org/freedesktop/Notifications"
)]
trait Notifications {
/// CloseNotification method
fn close_notification(&self, id: u32) -> zbus::Result<()>;
/// GetCapabilities method
fn get_capabilities(&self) -> zbus::Result<Vec<String>>;
/// GetServerInformation method
fn get_server_information(&self) -> zbus::Result<(String, String, String, String)>;
/// Notify method
fn notify(
&self,
app_name: &str,
replaces_id: u32,
app_icon: &str,
summary: &str,
body: &str,
actions: &[&str],
hints: std::collections::HashMap<&str, zbus::zvariant::Value<'_>>,
expire_timeout: i32,
) -> zbus::Result<u32>;
/// ActionInvoked signal
#[dbus_proxy(signal)]
fn action_invoked(&self, id: u32, action_key: &str) -> zbus::Result<()>;
/// NotificationClosed signal
#[dbus_proxy(signal)]
fn notification_closed(&self, id: u32, reason: u32) -> zbus::Result<()>;
}

View file

@ -1 +1,3 @@
pub mod dbus;
mod dbus_proxy;
pub mod notifications;