|
1 | | -struct CosmicWorkspaces; |
| 1 | +use cosmic::iced::{self, futures::StreamExt}; |
| 2 | +use tokio::sync::broadcast; |
| 3 | +use tokio_stream::wrappers::BroadcastStream; |
| 4 | + |
| 5 | +#[derive(Clone, Debug)] |
| 6 | +pub enum Event { |
| 7 | + Show, |
| 8 | + Hide, |
| 9 | +} |
| 10 | + |
| 11 | +struct CosmicWorkspaces { |
| 12 | + event_sender: broadcast::Sender<Event>, |
| 13 | +} |
2 | 14 |
|
3 | 15 | #[zbus::interface(name = "com.system76.CosmicWorkspaces")] |
4 | 16 | impl CosmicWorkspaces { |
| 17 | + fn show(&self) { |
| 18 | + let _ = self.event_sender.send(Event::Show); |
| 19 | + } |
| 20 | + |
| 21 | + fn hide(&self) { |
| 22 | + let _ = self.event_sender.send(Event::Hide); |
| 23 | + } |
| 24 | + |
5 | 25 | #[zbus(signal)] |
6 | 26 | async fn shown(&self, _emitter: &zbus::object_server::SignalEmitter<'_>) -> zbus::Result<()>; |
| 27 | + |
7 | 28 | #[zbus(signal)] |
8 | 29 | async fn hidden(&self, _emitter: &zbus::object_server::SignalEmitter<'_>) -> zbus::Result<()>; |
9 | 30 | } |
10 | 31 |
|
11 | 32 | #[derive(Clone, Debug)] |
12 | 33 | pub struct Interface { |
13 | 34 | emitter: zbus::object_server::SignalEmitter<'static>, |
| 35 | + event_sender: broadcast::Sender<Event>, |
14 | 36 | } |
15 | 37 |
|
16 | 38 | impl Interface { |
17 | 39 | pub async fn new(conn: zbus::Connection) -> zbus::Result<Self> { |
| 40 | + let event_sender = broadcast::Sender::new(8); |
18 | 41 | conn.object_server() |
19 | | - .at("/com/system76/CosmicWorkspaces", CosmicWorkspaces) |
| 42 | + .at( |
| 43 | + "/com/system76/CosmicWorkspaces", |
| 44 | + CosmicWorkspaces { |
| 45 | + event_sender: event_sender.clone(), |
| 46 | + }, |
| 47 | + ) |
20 | 48 | .await?; |
21 | 49 | Ok(Interface { |
22 | 50 | emitter: zbus::object_server::SignalEmitter::new( |
23 | 51 | &conn, |
24 | 52 | "/com/system76/CosmicWorkspaces", |
25 | 53 | ) |
26 | 54 | .unwrap(), |
| 55 | + event_sender, |
27 | 56 | }) |
28 | 57 | } |
29 | 58 |
|
30 | 59 | pub async fn shown(&self) -> zbus::Result<()> { |
31 | | - CosmicWorkspaces.shown(&self.emitter).await |
| 60 | + self.emitter.shown(&self.emitter).await |
32 | 61 | } |
33 | 62 |
|
34 | 63 | pub async fn hidden(&self) -> zbus::Result<()> { |
35 | | - CosmicWorkspaces.hidden(&self.emitter).await |
| 64 | + self.emitter.hidden(&self.emitter).await |
| 65 | + } |
| 66 | + |
| 67 | + pub fn subscription(&self) -> iced::Subscription<Event> { |
| 68 | + iced::Subscription::run_with_id( |
| 69 | + "workspaces-dbus-sun", |
| 70 | + BroadcastStream::new(self.event_sender.subscribe()).filter_map(|x| async { x.ok() }), |
| 71 | + ) |
36 | 72 | } |
37 | 73 | } |
0 commit comments