Replace smol with async-std in music player + notification timeouts

This commit is contained in:
mars 2022-12-07 18:11:10 -07:00
parent 9660ebb4dd
commit 508abcb0cc
2 changed files with 52 additions and 27 deletions

View File

@ -9,11 +9,11 @@ path = "src/main.rs"
required-features = ["bin"]
[dependencies]
async-std = { version = "1.12", optional = true, features = ["attributes"] }
canary-magpie = { path = "../magpie", optional = true, features = ["async"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
smol = { version = "1.2", optional = true }
zbus = { version = "3.5", optional = true }
[features]
bin = ["dep:canary-magpie", "dep:smol", "dep:zbus"]
bin = ["dep:async-std", "dep:canary-magpie", "dep:zbus"]

View File

@ -2,16 +2,17 @@ use std::collections::HashMap;
use std::future::pending;
use std::path::PathBuf;
use async_std::channel::{unbounded, Sender};
use async_std::os::unix::net::UnixStream;
use canary_magpie::protocol::*;
use canary_notifications::Contents;
use smol::net::unix::UnixStream;
use zbus::{dbus_interface, zvariant::Value, ConnectionBuilder, SignalContext};
pub type MagpieClient = ClientMessenger<UnixStream>;
pub struct Notifications {
module_path: PathBuf,
magpie: MagpieClient,
magpie_sender: Sender<MagpieServerMsg>,
next_id: u32,
}
@ -45,11 +46,17 @@ impl Notifications {
hints: HashMap<String, Value<'_>>,
timeout: i32,
) -> u32 {
let timeout = match timeout {
-1 => Some(5000), // default timeout
0 => None,
t => Some(t),
};
let contents = Contents {
app_name: Some(app_name).filter(|s| !s.is_empty()),
summary,
body: Some(body).filter(|s| !s.is_empty()),
timeout: Some(timeout).filter(|t| *t == 0),
timeout,
};
let id = self.next_id;
@ -62,8 +69,20 @@ impl Notifications {
init_msg: serde_json::to_vec(&contents).unwrap(),
};
self.magpie
.send_async(&MagpieServerMsg::CreatePanel(msg))
if let Some(delay_ms) = contents.timeout.clone() {
let delay = std::time::Duration::from_millis(delay_ms as _);
let magpie_sender = self.magpie_sender.to_owned();
async_std::task::spawn(async move {
async_std::task::sleep(delay).await;
magpie_sender
.send(MagpieServerMsg::ClosePanel(ClosePanel { id }))
.await
.unwrap();
});
}
self.magpie_sender
.send(MagpieServerMsg::CreatePanel(msg))
.await
.unwrap();
@ -84,7 +103,8 @@ impl Notifications {
) -> zbus::Result<()>;
}
pub fn main() {
#[async_std::main]
async fn main() {
let args: Vec<String> = std::env::args().collect();
let module_path = args
.get(1)
@ -92,27 +112,32 @@ pub fn main() {
.to_owned()
.into();
smol::block_on(async {
let sock_path = find_socket();
let socket = UnixStream::connect(sock_path).await.unwrap();
let magpie = MagpieClient::new(socket);
let sock_path = find_socket();
let socket = UnixStream::connect(sock_path).await.unwrap();
let mut magpie = MagpieClient::new(socket);
let (magpie_sender, magpie_receiver) = unbounded();
let notifications = Notifications {
magpie,
next_id: 0,
module_path,
};
let notifications = Notifications {
magpie_sender,
next_id: 0,
module_path,
};
let _ = ConnectionBuilder::session()
.unwrap()
.name("org.freedesktop.Notifications")
.unwrap()
.serve_at("/org/freedesktop/Notifications", notifications)
.unwrap()
.build()
.await
.unwrap();
let _ = ConnectionBuilder::session()
.unwrap()
.name("org.freedesktop.Notifications")
.unwrap()
.serve_at("/org/freedesktop/Notifications", notifications)
.unwrap()
.build()
.await
.unwrap();
pending::<()>().await;
async_std::task::spawn(async move {
while let Ok(msg) = magpie_receiver.recv().await {
magpie.send_async(&msg).await.unwrap();
}
});
pending::<()>().await;
}