| 1 |
//! Register global shortcuts |
| 2 |
|
| 3 |
use std::{collections::HashMap, fmt::Debug, time::Duration}; |
| 4 |
|
| 5 |
use futures_util::Stream; |
| 6 |
use serde::{Deserialize, Serialize}; |
| 7 |
use zbus::zvariant::{ |
| 8 |
ObjectPath, Optional, OwnedObjectPath, OwnedValue, Type, |
| 9 |
as_value::{self, optional}, |
| 10 |
}; |
| 11 |
|
| 12 |
use super::{HandleToken, Request, Session, session::SessionPortal}; |
| 13 |
use crate::{ |
| 14 |
ActivationToken, Error, WindowIdentifier, |
| 15 |
desktop::session::{CreateSessionOptions, CreateSessionResponse}, |
| 16 |
proxy::Proxy, |
| 17 |
}; |
| 18 |
|
| 19 |
#[derive(Clone, Serialize, Type, Debug, Default)] |
| 20 |
#[zvariant(signature = "dict")] |
| 21 |
struct NewShortcutInfo { |
| 22 |
/// User-readable text describing what the shortcut does. |
| 23 |
#[serde(with = "as_value")] |
| 24 |
description: String, |
| 25 |
/// The preferred shortcut trigger, defined as described by the "shortcuts" |
| 26 |
/// XDG specification. Optional. |
| 27 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 28 |
preferred_trigger: Option<String>, |
| 29 |
} |
| 30 |
|
| 31 |
/// Shortcut descriptor used to bind new shortcuts in |
| 32 |
/// [`GlobalShortcuts::bind_shortcuts`] |
| 33 |
#[derive(Clone, Serialize, Type, Debug)] |
| 34 |
pub struct NewShortcut(String, NewShortcutInfo); |
| 35 |
|
| 36 |
impl NewShortcut { |
| 37 |
/// Construct new shortcut |
| 38 |
pub fn new(id: impl Into<String>, description: impl Into<String>) -> Self { |
| 39 |
Self( |
| 40 |
id.into(), |
| 41 |
NewShortcutInfo { |
| 42 |
description: description.into(), |
| 43 |
preferred_trigger: None, |
| 44 |
}, |
| 45 |
) |
| 46 |
} |
| 47 |
|
| 48 |
/// Sets the preferred shortcut trigger, defined as described by the |
| 49 |
/// "shortcuts" XDG specification. |
| 50 |
#[must_use] |
| 51 |
pub fn preferred_trigger<'a>(mut self, preferred_trigger: impl Into<Option<&'a str>>) -> Self { |
| 52 |
self.1.preferred_trigger = preferred_trigger.into().map(ToOwned::to_owned); |
| 53 |
self |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
#[derive(Clone, Deserialize, Type, Debug, Default)] |
| 58 |
#[zvariant(signature = "dict")] |
| 59 |
struct ShortcutInfo { |
| 60 |
/// User-readable text describing what the shortcut does. |
| 61 |
#[serde(with = "as_value")] |
| 62 |
description: String, |
| 63 |
/// User-readable text describing how to trigger the shortcut for the client |
| 64 |
/// to render. |
| 65 |
#[serde(with = "as_value")] |
| 66 |
trigger_description: String, |
| 67 |
} |
| 68 |
|
| 69 |
/// Struct that contains information about existing binded shortcut. |
| 70 |
/// |
| 71 |
/// If you need to create a new shortcuts, take a look at [`NewShortcut`] |
| 72 |
/// instead. |
| 73 |
#[derive(Clone, Deserialize, Type, Debug)] |
| 74 |
pub struct Shortcut(String, ShortcutInfo); |
| 75 |
|
| 76 |
impl Shortcut { |
| 77 |
/// Shortcut id |
| 78 |
pub fn id(&self) -> &str { |
| 79 |
&self.0 |
| 80 |
} |
| 81 |
|
| 82 |
/// User-readable text describing what the shortcut does. |
| 83 |
pub fn description(&self) -> &str { |
| 84 |
&self.1.description |
| 85 |
} |
| 86 |
|
| 87 |
/// User-readable text describing how to trigger the shortcut for the client |
| 88 |
/// to render. |
| 89 |
pub fn trigger_description(&self) -> &str { |
| 90 |
&self.1.trigger_description |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
#[derive(Serialize, Type, Debug, Default)] |
| 95 |
#[zvariant(signature = "dict")] |
| 96 |
/// Specified options for a [`GlobalShortcuts::bind_shortcuts`] request. |
| 97 |
pub struct BindShortcutsOptions { |
| 98 |
/// A string that will be used as the last element of the handle. |
| 99 |
#[serde(with = "as_value")] |
| 100 |
handle_token: HandleToken, |
| 101 |
} |
| 102 |
|
| 103 |
/// A response to a [`GlobalShortcuts::bind_shortcuts`] request. |
| 104 |
#[derive(Deserialize, Type, Debug)] |
| 105 |
#[zvariant(signature = "dict")] |
| 106 |
pub struct BindShortcuts { |
| 107 |
#[serde(default, with = "as_value")] |
| 108 |
shortcuts: Vec<Shortcut>, |
| 109 |
} |
| 110 |
|
| 111 |
impl BindShortcuts { |
| 112 |
/// A list of shortcuts. |
| 113 |
pub fn shortcuts(&self) -> &[Shortcut] { |
| 114 |
&self.shortcuts |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
#[derive(Serialize, Type, Debug, Default)] |
| 119 |
#[zvariant(signature = "dict")] |
| 120 |
/// Specified options for a [`GlobalShortcuts::configure_shortcuts`] request. |
| 121 |
pub struct ConfigureShortcutsOptions { |
| 122 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 123 |
activation_token: Option<ActivationToken>, |
| 124 |
} |
| 125 |
|
| 126 |
impl ConfigureShortcutsOptions { |
| 127 |
/// Sets the token that can be used to activate the configuration dialog. |
| 128 |
#[must_use] |
| 129 |
pub fn set_activation_token( |
| 130 |
mut self, |
| 131 |
activation_token: impl Into<Option<ActivationToken>>, |
| 132 |
) -> Self { |
| 133 |
self.activation_token = activation_token.into(); |
| 134 |
self |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
#[derive(Serialize, Type, Debug, Default)] |
| 139 |
#[zvariant(signature = "dict")] |
| 140 |
/// Specified options for a [`GlobalShortcuts::list_shortcuts`] request. |
| 141 |
pub struct ListShortcutsOptions { |
| 142 |
/// A string that will be used as the last element of the handle. |
| 143 |
#[serde(with = "as_value")] |
| 144 |
handle_token: HandleToken, |
| 145 |
} |
| 146 |
|
| 147 |
/// A response to a [`GlobalShortcuts::list_shortcuts`] request. |
| 148 |
#[derive(Deserialize, Type, Debug)] |
| 149 |
#[zvariant(signature = "dict")] |
| 150 |
pub struct ListShortcuts { |
| 151 |
/// A list of shortcuts. |
| 152 |
#[serde(default, with = "as_value")] |
| 153 |
shortcuts: Vec<Shortcut>, |
| 154 |
} |
| 155 |
|
| 156 |
impl ListShortcuts { |
| 157 |
/// A list of shortcuts. |
| 158 |
pub fn shortcuts(&self) -> &[Shortcut] { |
| 159 |
&self.shortcuts |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/// Notifies about a shortcut becoming active. |
| 164 |
#[derive(Debug, Deserialize, Type)] |
| 165 |
pub struct Activated(OwnedObjectPath, String, u64, HashMap<String, OwnedValue>); |
| 166 |
|
| 167 |
impl Activated { |
| 168 |
/// Session that requested the shortcut. |
| 169 |
pub fn session_handle(&self) -> ObjectPath<'_> { |
| 170 |
self.0.as_ref() |
| 171 |
} |
| 172 |
|
| 173 |
/// The application-provided ID for the shortcut. |
| 174 |
pub fn shortcut_id(&self) -> &str { |
| 175 |
&self.1 |
| 176 |
} |
| 177 |
|
| 178 |
/// The timestamp, as seconds and microseconds since the Unix epoch. |
| 179 |
pub fn timestamp(&self) -> Duration { |
| 180 |
Duration::from_millis(self.2) |
| 181 |
} |
| 182 |
|
| 183 |
/// Optional information |
| 184 |
pub fn options(&self) -> &HashMap<String, OwnedValue> { |
| 185 |
&self.3 |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/// Notifies that a shortcut is not active anymore. |
| 190 |
#[derive(Debug, Deserialize, Type)] |
| 191 |
pub struct Deactivated(OwnedObjectPath, String, u64, HashMap<String, OwnedValue>); |
| 192 |
|
| 193 |
impl Deactivated { |
| 194 |
/// Session that requested the shortcut. |
| 195 |
pub fn session_handle(&self) -> ObjectPath<'_> { |
| 196 |
self.0.as_ref() |
| 197 |
} |
| 198 |
|
| 199 |
/// The application-provided ID for the shortcut. |
| 200 |
pub fn shortcut_id(&self) -> &str { |
| 201 |
&self.1 |
| 202 |
} |
| 203 |
|
| 204 |
/// The timestamp, as seconds and microseconds since the Unix epoch. |
| 205 |
pub fn timestamp(&self) -> Duration { |
| 206 |
Duration::from_millis(self.2) |
| 207 |
} |
| 208 |
|
| 209 |
/// Optional information |
| 210 |
pub fn options(&self) -> &HashMap<String, OwnedValue> { |
| 211 |
&self.3 |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/// Indicates that the information associated with some of the shortcuts has |
| 216 |
/// changed. |
| 217 |
#[derive(Debug, Deserialize, Type)] |
| 218 |
pub struct ShortcutsChanged(OwnedObjectPath, Vec<Shortcut>); |
| 219 |
|
| 220 |
impl ShortcutsChanged { |
| 221 |
/// Session that requested the shortcut. |
| 222 |
pub fn session_handle(&self) -> ObjectPath<'_> { |
| 223 |
self.0.as_ref() |
| 224 |
} |
| 225 |
|
| 226 |
/// Shortcuts that have been registered. |
| 227 |
pub fn shortcuts(&self) -> &[Shortcut] { |
| 228 |
&self.1 |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
/// Wrapper of the DBus interface: [`org.freedesktop.portal.GlobalShortcuts`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GlobalShortcuts.html). |
| 233 |
#[derive(Debug)] |
| 234 |
#[doc(alias = "org.freedesktop.portal.GlobalShortcuts")] |
| 235 |
pub struct GlobalShortcuts(Proxy<'static>); |
| 236 |
|
| 237 |
impl GlobalShortcuts { |
| 238 |
/// Create a new instance of [`GlobalShortcuts`]. |
| 239 |
pub async fn new() -> Result<GlobalShortcuts, Error> { |
| 240 |
let proxy = Proxy::new_desktop("org.freedesktop.portal.GlobalShortcuts").await?; |
| 241 |
Ok(Self(proxy)) |
| 242 |
} |
| 243 |
|
| 244 |
/// Create a new instance of [`GlobalShortcuts`]. |
| 245 |
pub async fn with_connection(connection: zbus::Connection) -> Result<GlobalShortcuts, Error> { |
| 246 |
let proxy = Proxy::new_desktop_with_connection( |
| 247 |
connection, |
| 248 |
"org.freedesktop.portal.GlobalShortcuts", |
| 249 |
) |
| 250 |
.await?; |
| 251 |
Ok(Self(proxy)) |
| 252 |
} |
| 253 |
|
| 254 |
/// Returns the version of the portal interface. |
| 255 |
pub fn version(&self) -> u32 { |
| 256 |
self.0.version() |
| 257 |
} |
| 258 |
|
| 259 |
/// Create a global shortcuts session. |
| 260 |
/// |
| 261 |
/// # Specifications |
| 262 |
/// |
| 263 |
/// See also [`CreateSession`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GlobalShortcuts.html#org-freedesktop-portal-globalshortcuts-createsession). |
| 264 |
#[doc(alias = "CreateSession")] |
| 265 |
pub async fn create_session( |
| 266 |
&self, |
| 267 |
options: CreateSessionOptions, |
| 268 |
) -> Result<Session<Self>, Error> { |
| 269 |
let (request, proxy) = futures_util::try_join!( |
| 270 |
self.0.request::<CreateSessionResponse>( |
| 271 |
&options.handle_token, |
| 272 |
"CreateSession", |
| 273 |
&options |
| 274 |
), |
| 275 |
Session::from_unique_name(self.0.connection().clone(), &options.session_handle_token), |
| 276 |
)?; |
| 277 |
assert_eq!(proxy.path(), &request.response()?.session_handle.as_ref()); |
| 278 |
Ok(proxy) |
| 279 |
} |
| 280 |
|
| 281 |
/// Bind the shortcuts. |
| 282 |
/// |
| 283 |
/// # Specifications |
| 284 |
/// |
| 285 |
/// See also [`BindShortcuts`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GlobalShortcuts.html#org-freedesktop-portal-globalshortcuts-bindshortcuts). |
| 286 |
#[doc(alias = "BindShortcuts")] |
| 287 |
pub async fn bind_shortcuts( |
| 288 |
&self, |
| 289 |
session: &Session<Self>, |
| 290 |
shortcuts: &[NewShortcut], |
| 291 |
identifier: Option<&WindowIdentifier>, |
| 292 |
options: BindShortcutsOptions, |
| 293 |
) -> Result<Request<BindShortcuts>, Error> { |
| 294 |
let identifier = Optional::from(identifier); |
| 295 |
self.0 |
| 296 |
.request( |
| 297 |
&options.handle_token, |
| 298 |
"BindShortcuts", |
| 299 |
&(session, shortcuts, identifier, &options), |
| 300 |
) |
| 301 |
.await |
| 302 |
} |
| 303 |
|
| 304 |
/// Lists all shortcuts. |
| 305 |
/// |
| 306 |
/// # Specifications |
| 307 |
/// |
| 308 |
/// See also [`ListShortcuts`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GlobalShortcuts.html#org-freedesktop-portal-globalshortcuts-listshortcuts). |
| 309 |
#[doc(alias = "ListShortcuts")] |
| 310 |
pub async fn list_shortcuts( |
| 311 |
&self, |
| 312 |
session: &Session<Self>, |
| 313 |
options: ListShortcutsOptions, |
| 314 |
) -> Result<Request<ListShortcuts>, Error> { |
| 315 |
self.0 |
| 316 |
.request(&options.handle_token, "ListShortcuts", &(session, &options)) |
| 317 |
.await |
| 318 |
} |
| 319 |
|
| 320 |
/// Request showing a configuration UI so the user is able to configure all |
| 321 |
/// shortcuts of this session. |
| 322 |
/// |
| 323 |
/// # Specifications |
| 324 |
/// |
| 325 |
/// See also [`ConfigureShortcuts`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GlobalShortcuts.html#org-freedesktop-portal-globalshortcuts-configureshortcuts). |
| 326 |
#[doc(alias = "ConfigureShortcuts")] |
| 327 |
pub async fn configure_shortcuts( |
| 328 |
&self, |
| 329 |
session: &Session<Self>, |
| 330 |
identifier: Option<&WindowIdentifier>, |
| 331 |
options: ConfigureShortcutsOptions, |
| 332 |
) -> Result<(), Error> { |
| 333 |
let identifier = Optional::from(identifier); |
| 334 |
|
| 335 |
self.0 |
| 336 |
.call_versioned::<()>("ConfigureShortcuts", &(session, identifier, options), 2) |
| 337 |
.await |
| 338 |
} |
| 339 |
|
| 340 |
/// Signal emitted when shortcut becomes active. |
| 341 |
/// |
| 342 |
/// # Specifications |
| 343 |
/// |
| 344 |
/// See also [`Activated`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GlobalShortcuts.html#org-freedesktop-portal-globalshortcuts-activated). |
| 345 |
#[doc(alias = "Activated")] |
| 346 |
pub async fn receive_activated(&self) -> Result<impl Stream<Item = Activated> + use<>, Error> { |
| 347 |
self.0.signal("Activated").await |
| 348 |
} |
| 349 |
|
| 350 |
/// Signal emitted when shortcut is not active anymore. |
| 351 |
/// |
| 352 |
/// # Specifications |
| 353 |
/// |
| 354 |
/// See also [`Deactivated`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GlobalShortcuts.html#org-freedesktop-portal-globalshortcuts-deactivated). |
| 355 |
#[doc(alias = "Deactivated")] |
| 356 |
pub async fn receive_deactivated( |
| 357 |
&self, |
| 358 |
) -> Result<impl Stream<Item = Deactivated> + use<>, Error> { |
| 359 |
self.0.signal("Deactivated").await |
| 360 |
} |
| 361 |
|
| 362 |
/// Signal emitted when information associated with some of the shortcuts |
| 363 |
/// has changed. |
| 364 |
/// |
| 365 |
/// # Specifications |
| 366 |
/// |
| 367 |
/// See also [`ShortcutsChanged`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.GlobalShortcuts.html#org-freedesktop-portal-globalshortcuts-shortcutschanged). |
| 368 |
#[doc(alias = "ShortcutsChanged")] |
| 369 |
pub async fn receive_shortcuts_changed( |
| 370 |
&self, |
| 371 |
) -> Result<impl Stream<Item = ShortcutsChanged> + use<>, Error> { |
| 372 |
self.0.signal("ShortcutsChanged").await |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
impl std::ops::Deref for GlobalShortcuts { |
| 377 |
type Target = zbus::Proxy<'static>; |
| 378 |
|
| 379 |
fn deref(&self) -> &Self::Target { |
| 380 |
&self.0 |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
impl crate::Sealed for GlobalShortcuts {} |
| 385 |
impl SessionPortal for GlobalShortcuts {} |