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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//! Register global shortcuts

use std::{collections::HashMap, fmt::Debug, time::Duration};

use futures_util::{Stream, TryFutureExt};
use serde::{Deserialize, Serialize};
use zbus::zvariant::{
    DeserializeDict, ObjectPath, OwnedObjectPath, OwnedValue, SerializeDict, Type,
};

use super::{HandleToken, Request, Session};
use crate::{proxy::Proxy, Error, WindowIdentifier};

#[derive(Clone, SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct NewShortcutInfo {
    /// User-readable text describing what the shortcut does.
    description: String,
    /// The preferred shortcut trigger, defined as described by the "shortcuts"
    /// XDG specification. Optional.
    preferred_trigger: Option<String>,
}

/// Shortcut descriptor used to bind new shortcuts in
/// [`GlobalShortcuts::bind_shortcuts`]
#[derive(Clone, Serialize, Type, Debug)]
pub struct NewShortcut(String, NewShortcutInfo);

impl NewShortcut {
    /// Construct new shortcut
    pub fn new(id: impl Into<String>, description: impl Into<String>) -> Self {
        Self(
            id.into(),
            NewShortcutInfo {
                description: description.into(),
                preferred_trigger: None,
            },
        )
    }

    /// Sets the preferred shortcut trigger, defined as described by the
    /// "shortcuts" XDG specification.
    #[must_use]
    pub fn preferred_trigger<'a>(mut self, preferred_trigger: impl Into<Option<&'a str>>) -> Self {
        self.1.preferred_trigger = preferred_trigger.into().map(ToOwned::to_owned);
        self
    }
}

#[derive(Clone, DeserializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct ShortcutInfo {
    /// User-readable text describing what the shortcut does.
    description: String,
    /// User-readable text describing how to trigger the shortcut for the client
    /// to render.
    trigger_description: String,
}

/// Struct that contains information about existing binded shortcut.
///
/// If you need to create a new shortcuts, take a look at [`NewShortcut`]
/// instead.
#[derive(Clone, Deserialize, Type, Debug)]
pub struct Shortcut(String, ShortcutInfo);

impl Shortcut {
    /// Shortcut id
    pub fn id(&self) -> &str {
        &self.0
    }

    /// User-readable text describing what the shortcut does.
    pub fn description(&self) -> &str {
        &self.1.description
    }

    /// User-readable text describing how to trigger the shortcut for the client
    /// to render.
    pub fn trigger_description(&self) -> &str {
        &self.1.trigger_description
    }
}

/// Specified options for a [`GlobalShortcuts::create_session`] request.
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct CreateSessionOptions {
    /// A string that will be used as the last element of the handle.
    handle_token: HandleToken,
    /// A string that will be used as the last element of the session handle.
    session_handle_token: HandleToken,
}

/// A response to a [`GlobalShortcuts::create_session`] request.
#[derive(DeserializeDict, Type, Debug)]
#[zvariant(signature = "dict")]
struct CreateSession {
    session_handle: OwnedObjectPath,
}

/// Specified options for a [`GlobalShortcuts::bind_shortcuts`] request.
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct BindShortcutsOptions {
    /// A string that will be used as the last element of the handle.
    handle_token: HandleToken,
}

/// A response to a [`GlobalShortcuts::bind_shortcuts`] request.
#[derive(DeserializeDict, Type, Debug)]
#[zvariant(signature = "dict")]
pub struct BindShortcuts {
    shortcuts: Vec<Shortcut>,
}

impl BindShortcuts {
    /// A list of shortcuts.
    pub fn shortcuts(&self) -> &[Shortcut] {
        &self.shortcuts
    }
}

/// Specified options for a [`GlobalShortcuts::list_shortcuts`] request.
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct ListShortcutsOptions {
    /// A string that will be used as the last element of the handle.
    handle_token: HandleToken,
}

/// A response to a [`GlobalShortcuts::list_shortcuts`] request.
#[derive(DeserializeDict, Type, Debug)]
#[zvariant(signature = "dict")]
pub struct ListShortcuts {
    /// A list of shortcuts.
    shortcuts: Vec<Shortcut>,
}

impl ListShortcuts {
    /// A list of shortcuts.
    pub fn shortcuts(&self) -> &[Shortcut] {
        &self.shortcuts
    }
}

/// Notifies about a shortcut becoming active.
#[derive(Debug, Deserialize, Type)]
pub struct Activated(OwnedObjectPath, String, u64, HashMap<String, OwnedValue>);

impl Activated {
    /// Session that requested the shortcut.
    pub fn session_handle(&self) -> ObjectPath<'_> {
        self.0.as_ref()
    }

    /// The application-provided ID for the shortcut.
    pub fn shortcut_id(&self) -> &str {
        &self.1
    }

    /// The timestamp, as seconds and microseconds since the Unix epoch.
    pub fn timestamp(&self) -> Duration {
        Duration::from_millis(self.2)
    }

    /// Optional information
    pub fn options(&self) -> &HashMap<String, OwnedValue> {
        &self.3
    }
}

/// Notifies that a shortcut is not active anymore.
#[derive(Debug, Deserialize, Type)]
pub struct Deactivated(OwnedObjectPath, String, u64, HashMap<String, OwnedValue>);

impl Deactivated {
    /// Session that requested the shortcut.
    pub fn session_handle(&self) -> ObjectPath<'_> {
        self.0.as_ref()
    }

    /// The application-provided ID for the shortcut.
    pub fn shortcut_id(&self) -> &str {
        &self.1
    }

    /// The timestamp, as seconds and microseconds since the Unix epoch.
    pub fn timestamp(&self) -> Duration {
        Duration::from_millis(self.2)
    }

    /// Optional information
    pub fn options(&self) -> &HashMap<String, OwnedValue> {
        &self.3
    }
}

/// Indicates that the information associated with some of the shortcuts has
/// changed.
#[derive(Debug, Deserialize, Type)]
pub struct ShortcutsChanged(OwnedObjectPath, Vec<Shortcut>);

impl ShortcutsChanged {
    /// Session that requested the shortcut.
    pub fn session_handle(&self) -> ObjectPath<'_> {
        self.0.as_ref()
    }

    /// Shortcuts that have been registered.
    pub fn shortcuts(&self) -> &[Shortcut] {
        &self.1
    }
}

/// Wrapper of the DBus interface: [`org.freedesktop.portal.GlobalShortcuts`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GlobalShortcuts.html).
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.GlobalShortcuts")]
pub struct GlobalShortcuts<'a>(Proxy<'a>);

impl<'a> GlobalShortcuts<'a> {
    /// Create a new instance of [`GlobalShortcuts`].
    pub async fn new() -> Result<GlobalShortcuts<'a>, Error> {
        let proxy = Proxy::new_desktop("org.freedesktop.portal.GlobalShortcuts").await?;
        Ok(Self(proxy))
    }

    /// Create a global shortcuts session.
    ///
    /// # Specifications
    ///
    /// See also [`CreateSession`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GlobalShortcuts.html#org-freedesktop-portal-globalshortcuts-createsession).
    #[doc(alias = "CreateSession")]
    pub async fn create_session(&self) -> Result<Session<'a>, Error> {
        let options = CreateSessionOptions::default();
        let (request, proxy) = futures_util::try_join!(
            self.0
                .request::<CreateSession>(&options.handle_token, "CreateSession", &options)
                .into_future(),
            Session::from_unique_name(&options.session_handle_token).into_future(),
        )?;
        assert_eq!(proxy.path(), &request.response()?.session_handle.as_ref());
        Ok(proxy)
    }

    /// Bind the shortcuts.
    ///
    /// # Specifications
    ///
    /// See also [`BindShortcuts`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GlobalShortcuts.html#org-freedesktop-portal-globalshortcuts-bindshortcuts).
    #[doc(alias = "BindShortcuts")]
    pub async fn bind_shortcuts(
        &self,
        session: &Session<'_>,
        shortcuts: &[NewShortcut],
        parent_window: &WindowIdentifier,
    ) -> Result<Request<BindShortcuts>, Error> {
        let options = BindShortcutsOptions::default();
        self.0
            .request(
                &options.handle_token,
                "BindShortcuts",
                &(session, shortcuts, parent_window, &options),
            )
            .await
    }

    /// Lists all shortcuts.
    ///
    /// # Specifications
    ///
    /// See also [`ListShortcuts`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GlobalShortcuts.html#org-freedesktop-portal-globalshortcuts-listshortcuts).
    #[doc(alias = "ListShortcuts")]
    pub async fn list_shortcuts(
        &self,
        session: &Session<'_>,
    ) -> Result<Request<ListShortcuts>, Error> {
        let options = ListShortcutsOptions::default();
        self.0
            .request(&options.handle_token, "ListShortcuts", &(session, &options))
            .await
    }

    /// Signal emitted when shortcut becomes active.
    ///
    /// # Specifications
    ///
    /// See also [`Activated`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GlobalShortcuts.html#org-freedesktop-portal-globalshortcuts-activated).
    #[doc(alias = "Activated")]
    pub async fn receive_activated(&self) -> Result<impl Stream<Item = Activated>, Error> {
        self.0.signal("Activated").await
    }

    /// Signal emitted when shortcut is not active anymore.
    ///
    /// # Specifications
    ///
    /// See also [`Deactivated`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GlobalShortcuts.html#org-freedesktop-portal-globalshortcuts-deactivated).
    #[doc(alias = "Deactivated")]
    pub async fn receive_deactivated(&self) -> Result<impl Stream<Item = Deactivated>, Error> {
        self.0.signal("Deactivated").await
    }

    /// Signal emitted when information associated with some of the shortcuts
    /// has changed.
    ///
    /// # Specifications
    ///
    /// See also [`ShortcutsChanged`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GlobalShortcuts.html#org-freedesktop-portal-globalshortcuts-shortcutschanged).
    #[doc(alias = "ShortcutsChanged")]
    pub async fn receive_shortcuts_changed(
        &self,
    ) -> Result<impl Stream<Item = ShortcutsChanged>, Error> {
        self.0.signal("ShortcutsChanged").await
    }
}

impl<'a> std::ops::Deref for GlobalShortcuts<'a> {
    type Target = zbus::Proxy<'a>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}