egui_pop_api/system_keys.rs
1//! Shortcuts in the system scope: keys bound with
2//! `BindOptions::system()` (`KeyScope::System`, `system` in keymap
3//! files) also run their command while the application is not focused,
4//! when a plugin registers them with the desktop (the platform plugin of
5//! `egui-pop-platform`). Inside the window they fire as global bindings
6//! do.
7//!
8//! <div class="warning">Experimental: may change in any release
9//! (docs/stability.md).</div>
10//!
11//! Whoever registers them says how it went through the [`SYSTEM_KEYS`]
12//! service: whether the desktop offers global shortcuts at all
13//! ([`Support`]), and each binding's [`KeyStatus`]. The shortcuts editor
14//! shows it next to each command, so a shortcut that cannot work outside
15//! the window says so instead of failing silently; without the service,
16//! the editor says nothing registers them.
17//!
18//! ```
19//! use egui_pop_api::system_keys::{KeyStatus, Support, SystemKeys};
20//!
21//! let keys = SystemKeys::default();
22//! assert_eq!(keys.support(), Support::Unknown);
23//! keys.set_support(Support::Available { backend: "x11".into() });
24//! keys.set_status("Ctrl+Alt+P", KeyStatus::Registered);
25//! assert_eq!(keys.status("Ctrl+Alt+P"), Some(KeyStatus::Registered));
26//! ```
27
28use std::collections::BTreeMap;
29use std::sync::Mutex;
30
31use egui_pop_core::lock;
32use serde::Serialize;
33
34use crate::services::ServiceToken;
35
36/// The system shortcuts' state, as a service of the plugin registering
37/// them.
38pub const SYSTEM_KEYS: ServiceToken<SystemKeys> = ServiceToken::new("app.system_keys");
39
40/// Whether the desktop takes global shortcuts.
41#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
42#[serde(tag = "support", rename_all = "lowercase")]
43pub enum Support {
44 /// Not known yet (the backend is starting).
45 #[default]
46 Unknown,
47 /// Through this backend (`x11`, `windows`, `macos`, `portal`).
48 Available { backend: String },
49 /// Not here, and why (a Wayland compositor without the portal, the
50 /// web).
51 Unavailable { reason: String },
52}
53
54/// One binding's registration.
55#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
56#[serde(tag = "status", content = "detail", rename_all = "lowercase")]
57pub enum KeyStatus {
58 /// The desktop delivers it.
59 Registered,
60 /// Asked, not answered yet (a portal asking the user).
61 Pending,
62 /// Refused, and why (taken by another application, keys the
63 /// desktop cannot grab, declined by the user).
64 Failed(String),
65}
66
67#[derive(Debug, Default)]
68struct State {
69 support: Support,
70 keys: BTreeMap<String, KeyStatus>,
71}
72
73/// The system shortcuts' state: shared, written by the registering
74/// plugin from any thread, read by the shortcuts editor.
75#[derive(Debug, Default)]
76pub struct SystemKeys(Mutex<State>);
77
78impl SystemKeys {
79 pub fn support(&self) -> Support {
80 lock(&self.0).support.clone()
81 }
82
83 pub fn set_support(&self, support: Support) {
84 lock(&self.0).support = support;
85 }
86
87 /// A binding's registration, by its keys as shown (`Ctrl+Alt+P`).
88 pub fn status(&self, keys: &str) -> Option<KeyStatus> {
89 lock(&self.0).keys.get(keys).cloned()
90 }
91
92 pub fn set_status(&self, keys: &str, status: KeyStatus) {
93 lock(&self.0).keys.insert(keys.into(), status);
94 }
95
96 /// Forgets every binding's registration (before registering again).
97 pub fn clear(&self) {
98 lock(&self.0).keys.clear();
99 }
100
101 /// Every binding's registration, by keys.
102 pub fn statuses(&self) -> BTreeMap<String, KeyStatus> {
103 lock(&self.0).keys.clone()
104 }
105}