Skip to main content

notifications/
settings.rs

1use crate::private_prelude::*;
2use std::sync::RwLock;
3
4#[derive(Debug)]
5struct SettingsInner {
6    follow_xdg_timeout: bool,
7    default_timeout: u32,
8    expire_by_default: bool,
9}
10
11impl Default for SettingsInner {
12    fn default() -> Self {
13        Self {
14            follow_xdg_timeout: true,
15            default_timeout: 3000,
16            expire_by_default: false,
17        }
18    }
19}
20
21/// Settings for [`NotificationService`].
22///
23/// This struct provides methods to change behavior of some parts of the service.
24/// [`Settings`] uses shared ownership for the data, so cloning it is a cheap operation.
25#[derive(Default, Debug, Clone)]
26pub struct Settings {
27    inner: Arc<RwLock<SettingsInner>>,
28}
29
30impl Settings {
31    /// Returns whether to respect XDG Specification for timeout.
32    ///
33    /// If set to `false`, notifications never expire despite the value of [`NotificationHandle::timeout()`].
34    /// Otherwise, behavior is based on notification's timeout:
35    /// * `-1` - timeout value is taken from [`Settings::default_timeout()`].
36    /// * `0` - the notification never expire
37    /// * `>=0` - this timeout is used to expire the notification
38    ///
39    /// Default: `True`.
40    pub fn follow_xdg_timeout(&self) -> bool {
41        self.inner.read().unwrap().follow_xdg_timeout
42    }
43
44    /// Returns the default timeout which is used when a notification doesn't specify timeout (-1).
45    ///
46    /// Has effect only if [`Settings::follow_xdg_timeout()`] and [`Settings::expire_by_default()`] are both `true`.
47    ///
48    /// Default: `3000`.
49    pub fn default_timeout(&self) -> u32 {
50        self.inner.read().unwrap().default_timeout
51    }
52
53    /// Returns whether to expire notifications if the timeout is not specified (when timeout is -1).
54    ///
55    /// If `true`, notifications expire after the timeout defined in [`Settings::default_timeout()`].
56    ///
57    /// Default: `false`.
58    pub fn expire_by_default(&self) -> bool {
59        self.inner.read().unwrap().expire_by_default
60    }
61
62    /// Sets [`Settings::follow_xdg_timeout()`] setting.
63    pub fn set_follow_xdg_timeout(&self, value: bool) {
64        self.inner.write().unwrap().follow_xdg_timeout = value;
65    }
66
67    /// Sets [`Settings::default_timeout()`] setting.
68    pub fn set_default_timeout(&self, value: u32) {
69        self.inner.write().unwrap().default_timeout = value;
70    }
71
72    /// Sets [`Settings::expire_by_default()`] setting.
73    pub fn set_expire_by_default(&self, value: bool) {
74        self.inner.write().unwrap().expire_by_default = value;
75    }
76}