i3status_rs/blocks/
notify.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//! Display and toggle the state of notifications daemon
//!
//! Left-clicking on this block will enable/disable notifications.
//!
//! # Configuration
//!
//! Key | Values | Default
//! ----|--------|--------
//! `driver` | Which notifications daemon is running. Available drivers are: `"dunst"` and `"swaync"` | `"dunst"`
//! `format` | A string to customise the output of this block. See below for available placeholders. | `" $icon "`
//!
//! Placeholder                               | Value                                      | Type   | Unit
//! ------------------------------------------|--------------------------------------------|--------|-----
//! `icon`                                    | Icon based on notification's state         | Icon   | -
//! `notification_count`[^dunst_version_note] | The number of notification (omitted if 0)  | Number | -
//! `paused`                                  | Present only if notifications are disabled | Flag   | -
//!
//! Action          | Default button
//! ----------------|---------------
//! `toggle_paused` | Left
//! `show`          | -
//!
//! # Examples
//!
//! How to use `paused` flag
//!
//! ```toml
//! [[block]]
//! block = "notify"
//! format = " $icon {$paused{Off}|On} "
//! ```
//! How to use `notification_count`
//!
//! ```toml
//! [[block]]
//! block = "notify"
//! format = " $icon {($notification_count.eng(w:1)) |}"
//! ```
//! How to remap actions
//!
//! ```toml
//! [[block]]
//! block = "notify"
//! driver = "swaync"
//! [[block.click]]
//! button = "left"
//! action = "show"
//! [[block.click]]
//! button = "right"
//! action = "toggle_paused"
//! ```
//!
//! # Icons Used
//! - `bell`
//! - `bell-slash`
//!
//! [^dunst_version_note]: when using `notification_count` with the `dunst` driver use dunst > 1.9.0

use super::prelude::*;
use tokio::{join, try_join};
use zbus::proxy::PropertyStream;

const ICON_ON: &str = "bell";
const ICON_OFF: &str = "bell-slash";

#[derive(Deserialize, Debug, Default)]
#[serde(deny_unknown_fields, default)]
pub struct Config {
    pub driver: DriverType,
    pub format: FormatConfig,
}

#[derive(Deserialize, Debug, SmartDefault)]
#[serde(rename_all = "lowercase")]
pub enum DriverType {
    #[default]
    Dunst,
    SwayNC,
}

pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
    let mut actions = api.get_actions()?;
    api.set_default_actions(&[(MouseButton::Left, None, "toggle_paused")])?;

    let format = config.format.with_default(" $icon ")?;

    let mut driver: Box<dyn Driver> = match config.driver {
        DriverType::Dunst => Box::new(DunstDriver::new().await?),
        DriverType::SwayNC => Box::new(SwayNCDriver::new().await?),
    };

    loop {
        let (is_paused, notification_count) =
            try_join!(driver.is_paused(), driver.notification_count())?;

        let mut widget = Widget::new().with_format(format.clone());
        widget.set_values(map!(
            "icon" => Value::icon(if is_paused { ICON_OFF } else { ICON_ON }),
            [if notification_count != 0] "notification_count" => Value::number(notification_count),
            [if is_paused] "paused" => Value::flag(),
        ));
        widget.state = if notification_count == 0 {
            State::Idle
        } else {
            State::Info
        };
        api.set_widget(widget)?;

        select! {
            x = driver.wait_for_change() => x?,
            Some(action) = actions.recv() => match action.as_ref() {
                "toggle_paused" => {
                    driver.set_paused(!is_paused).await?;
                }
                "show" => {
                    driver.notification_show().await?;
                }
                _ => (),
            }
        }
    }
}

#[async_trait]
trait Driver {
    async fn is_paused(&self) -> Result<bool>;
    async fn set_paused(&self, paused: bool) -> Result<()>;
    async fn notification_show(&self) -> Result<()>;
    async fn notification_count(&self) -> Result<u32>;
    async fn wait_for_change(&mut self) -> Result<()>;
}

struct DunstDriver {
    proxy: DunstDbusProxy<'static>,
    paused_changes: PropertyStream<'static, bool>,
    displayed_length_changes: PropertyStream<'static, u32>,
    waiting_length_changes: PropertyStream<'static, u32>,
}

impl DunstDriver {
    async fn new() -> Result<Self> {
        let dbus_conn = new_dbus_connection().await?;
        let proxy = DunstDbusProxy::new(&dbus_conn)
            .await
            .error("Failed to create DunstDbusProxy")?;
        Ok(Self {
            paused_changes: proxy.receive_paused_changed().await,
            displayed_length_changes: proxy.receive_displayed_length_changed().await,
            waiting_length_changes: proxy.receive_waiting_length_changed().await,
            proxy,
        })
    }
}

#[async_trait]
impl Driver for DunstDriver {
    async fn is_paused(&self) -> Result<bool> {
        self.proxy.paused().await.error("Failed to get 'paused'")
    }

    async fn set_paused(&self, paused: bool) -> Result<()> {
        self.proxy
            .set_paused(paused)
            .await
            .error("Failed to set 'paused'")
    }

    async fn notification_show(&self) -> Result<()> {
        self.proxy
            .notification_show()
            .await
            .error("Could not call 'NotificationShow'")
    }

    async fn notification_count(&self) -> Result<u32> {
        let (displayed_length, waiting_length) =
            try_join!(self.proxy.displayed_length(), self.proxy.waiting_length())
                .error("Failed to get property")?;

        Ok(displayed_length + waiting_length)
    }

    async fn wait_for_change(&mut self) -> Result<()> {
        select! {
            _ = self.paused_changes.next() => {}
            _ = self.displayed_length_changes.next() => {}
            _ = self.waiting_length_changes.next() => {}
        }
        Ok(())
    }
}

#[zbus::proxy(
    interface = "org.dunstproject.cmd0",
    default_service = "org.freedesktop.Notifications",
    default_path = "/org/freedesktop/Notifications"
)]

trait DunstDbus {
    #[zbus(property, name = "paused")]
    fn paused(&self) -> zbus::Result<bool>;
    #[zbus(property, name = "paused")]
    fn set_paused(&self, value: bool) -> zbus::Result<()>;
    fn notification_show(&self) -> zbus::Result<()>;
    #[zbus(property, name = "displayedLength")]
    fn displayed_length(&self) -> zbus::Result<u32>;
    #[zbus(property, name = "waitingLength")]
    fn waiting_length(&self) -> zbus::Result<u32>;
}
struct SwayNCDriver {
    proxy: SwayNCDbusProxy<'static>,
    changes: SubscribeStream,
    changes_v2: SubscribeV2Stream,
}

impl SwayNCDriver {
    async fn new() -> Result<Self> {
        let dbus_conn = new_dbus_connection().await?;
        let proxy = SwayNCDbusProxy::new(&dbus_conn)
            .await
            .error("Failed to create SwayNCDbusProxy")?;
        Ok(Self {
            changes: proxy
                .receive_subscribe()
                .await
                .error("Failed to create SubscribeStream")?,
            changes_v2: proxy
                .receive_subscribe_v2()
                .await
                .error("Failed to create SubscribeV2Stream")?,
            proxy,
        })
    }
}

#[async_trait]
impl Driver for SwayNCDriver {
    async fn is_paused(&self) -> Result<bool> {
        let (is_dnd, is_inhibited) = join!(self.proxy.get_dnd(), self.proxy.is_inhibited());

        is_dnd
            .error("Failed to call 'GetDnd'")
            .map(|is_dnd| is_dnd || is_inhibited.unwrap_or_default())
    }

    async fn set_paused(&self, paused: bool) -> Result<()> {
        if paused {
            self.proxy.set_dnd(paused).await
        } else {
            join!(self.proxy.set_dnd(paused), self.proxy.clear_inhibitors()).0
        }
        .error("Failed to call 'SetDnd'")
    }

    async fn notification_show(&self) -> Result<()> {
        self.proxy
            .toggle_visibility()
            .await
            .error("Failed to call 'ToggleVisibility'")
    }

    async fn notification_count(&self) -> Result<u32> {
        self.proxy
            .notification_count()
            .await
            .error("Failed to call 'NotificationCount'")
    }

    async fn wait_for_change(&mut self) -> Result<()> {
        select! {
            _ = self.changes.next() => (),
            _ = self.changes_v2.next() => (),
        }
        Ok(())
    }
}

#[zbus::proxy(
    interface = "org.erikreider.swaync.cc",
    default_service = "org.freedesktop.Notifications",
    default_path = "/org/erikreider/swaync/cc"
)]
trait SwayNCDbus {
    fn get_dnd(&self) -> zbus::Result<bool>;
    fn set_dnd(&self, value: bool) -> zbus::Result<()>;
    fn toggle_visibility(&self) -> zbus::Result<()>;
    fn notification_count(&self) -> zbus::Result<u32>;
    #[zbus(signal)]
    fn subscribe(&self, count: u32, dnd: bool, cc_open: bool) -> zbus::Result<()>;

    // inhibitors were introduced in v0.8.0
    fn is_inhibited(&self) -> zbus::Result<bool>;
    fn clear_inhibitors(&self) -> zbus::Result<bool>;
    // subscribe_v2 replaced subscribe in v0.8.0
    #[zbus(signal)]
    fn subscribe_v2(
        &self,
        count: u32,
        dnd: bool,
        cc_open: bool,
        inhibited: bool,
    ) -> zbus::Result<()>;
}