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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//! Register global shortcuts
use std::{collections::HashMap, fmt::Debug, time::Duration};
use futures_util::Stream;
use serde::{Deserialize, Serialize};
use zbus::zvariant::{
ObjectPath, Optional, OwnedObjectPath, OwnedValue, Type,
as_value::{self, optional},
};
use super::{HandleToken, Request, Session, session::SessionPortal};
use crate::{
ActivationToken, Error, WindowIdentifier,
desktop::session::{CreateSessionOptions, CreateSessionResponse},
proxy::Proxy,
};
#[derive(Clone, Serialize, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct NewShortcutInfo {
/// User-readable text describing what the shortcut does.
#[serde(with = "as_value")]
description: String,
/// The preferred shortcut trigger, defined as described by the "shortcuts"
/// XDG specification. Optional.
#[serde(with = "optional", skip_serializing_if = "Option::is_none")]
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, Deserialize, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct ShortcutInfo {
/// User-readable text describing what the shortcut does.
#[serde(with = "as_value")]
description: String,
/// User-readable text describing how to trigger the shortcut for the client
/// to render.
#[serde(with = "as_value")]
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
}
}
#[derive(Serialize, Type, Debug, Default)]
#[zvariant(signature = "dict")]
/// Specified options for a [`GlobalShortcuts::bind_shortcuts`] request.
pub struct BindShortcutsOptions {
/// A string that will be used as the last element of the handle.
#[serde(with = "as_value")]
handle_token: HandleToken,
}
/// A response to a [`GlobalShortcuts::bind_shortcuts`] request.
#[derive(Deserialize, Type, Debug)]
#[zvariant(signature = "dict")]
pub struct BindShortcuts {
#[serde(default, with = "as_value")]
shortcuts: Vec<Shortcut>,
}
impl BindShortcuts {
/// A list of shortcuts.
pub fn shortcuts(&self) -> &[Shortcut] {
&self.shortcuts
}
}
#[derive(Serialize, Type, Debug, Default)]
#[zvariant(signature = "dict")]
/// Specified options for a [`GlobalShortcuts::configure_shortcuts`] request.
pub struct ConfigureShortcutsOptions {
#[serde(with = "optional", skip_serializing_if = "Option::is_none")]
activation_token: Option<ActivationToken>,
}
impl ConfigureShortcutsOptions {
/// Sets the token that can be used to activate the configuration dialog.
#[must_use]
pub fn set_activation_token(
mut self,
activation_token: impl Into<Option<ActivationToken>>,
) -> Self {
self.activation_token = activation_token.into();
self
}
}
#[derive(Serialize, Type, Debug, Default)]
#[zvariant(signature = "dict")]
/// Specified options for a [`GlobalShortcuts::list_shortcuts`] request.
pub struct ListShortcutsOptions {
/// A string that will be used as the last element of the handle.
#[serde(with = "as_value")]
handle_token: HandleToken,
}
/// A response to a [`GlobalShortcuts::list_shortcuts`] request.
#[derive(Deserialize, Type, Debug)]
#[zvariant(signature = "dict")]
pub struct ListShortcuts {
/// A list of shortcuts.
#[serde(default, with = "as_value")]
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(Proxy<'static>);
impl GlobalShortcuts {
/// Create a new instance of [`GlobalShortcuts`].
pub async fn new() -> Result<GlobalShortcuts, Error> {
let proxy = Proxy::new_desktop("org.freedesktop.portal.GlobalShortcuts").await?;
Ok(Self(proxy))
}
/// Create a new instance of [`GlobalShortcuts`].
pub async fn with_connection(connection: zbus::Connection) -> Result<GlobalShortcuts, Error> {
let proxy = Proxy::new_desktop_with_connection(
connection,
"org.freedesktop.portal.GlobalShortcuts",
)
.await?;
Ok(Self(proxy))
}
/// Returns the version of the portal interface.
pub fn version(&self) -> u32 {
self.0.version()
}
/// 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,
options: CreateSessionOptions,
) -> Result<Session<Self>, Error> {
let (request, proxy) = futures_util::try_join!(
self.0.request::<CreateSessionResponse>(
&options.handle_token,
"CreateSession",
&options
),
Session::from_unique_name(self.0.connection().clone(), &options.session_handle_token),
)?;
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<Self>,
shortcuts: &[NewShortcut],
identifier: Option<&WindowIdentifier>,
options: BindShortcutsOptions,
) -> Result<Request<BindShortcuts>, Error> {
let identifier = Optional::from(identifier);
self.0
.request(
&options.handle_token,
"BindShortcuts",
&(session, shortcuts, identifier, &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<Self>,
options: ListShortcutsOptions,
) -> Result<Request<ListShortcuts>, Error> {
self.0
.request(&options.handle_token, "ListShortcuts", &(session, &options))
.await
}
/// Request showing a configuration UI so the user is able to configure all
/// shortcuts of this session.
///
/// # Specifications
///
/// See also [`ConfigureShortcuts`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GlobalShortcuts.html#org-freedesktop-portal-globalshortcuts-configureshortcuts).
#[doc(alias = "ConfigureShortcuts")]
pub async fn configure_shortcuts(
&self,
session: &Session<Self>,
identifier: Option<&WindowIdentifier>,
options: ConfigureShortcutsOptions,
) -> Result<(), Error> {
let identifier = Optional::from(identifier);
self.0
.call_versioned::<()>("ConfigureShortcuts", &(session, identifier, options), 2)
.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> + use<>, 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> + use<>, 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> + use<>, Error> {
self.0.signal("ShortcutsChanged").await
}
}
impl std::ops::Deref for GlobalShortcuts {
type Target = zbus::Proxy<'static>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl crate::Sealed for GlobalShortcuts {}
impl SessionPortal for GlobalShortcuts {}