Compare commits
2 Commits
e944caa358
...
508abcb0cc
Author | SHA1 | Date |
---|---|---|
mars | 508abcb0cc | |
mars | 9660ebb4dd |
|
@ -29,6 +29,12 @@ pub struct CreatePanel {
|
|||
pub init_msg: Vec<u8>,
|
||||
}
|
||||
|
||||
/// Closes a Magpie panel with a given ID.
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct ClosePanel {
|
||||
pub id: PanelId,
|
||||
}
|
||||
|
||||
/// Sends a panel a message.
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct SendMessage {
|
||||
|
@ -41,6 +47,7 @@ pub struct SendMessage {
|
|||
#[serde(tag = "kind")]
|
||||
pub enum MagpieServerMsg {
|
||||
CreatePanel(CreatePanel),
|
||||
ClosePanel(ClosePanel),
|
||||
SendMessage(SendMessage),
|
||||
}
|
||||
|
||||
|
|
|
@ -151,6 +151,12 @@ impl Client {
|
|||
};
|
||||
let _ = self.window_sender.send_event(msg);
|
||||
}
|
||||
MagpieServerMsg::ClosePanel(ClosePanel { id }) => {
|
||||
if let Some(id) = self.id_to_window.get(&id).copied() {
|
||||
let msg = WindowMessage::CloseWindow { id };
|
||||
let _ = self.window_sender.send_event(msg);
|
||||
}
|
||||
}
|
||||
MagpieServerMsg::SendMessage(SendMessage { id, msg }) => {
|
||||
if let Some(id) = self.id_to_window.get(&id).cloned() {
|
||||
let msg = WindowMessage::SendMessage { id, msg };
|
||||
|
|
|
@ -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"]
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue