Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ jobs:
- os: windows-latest
updates: updates-velopack
runs-on: ${{ matrix.os }}
# Cold Windows builds and inspection fixtures need time for the remaining view tests.
timeout-minutes: ${{ matrix.os == 'windows-latest' && 90 || 60 }}
env:
RUST_BACKTRACE: "1"
Expand Down Expand Up @@ -174,6 +175,12 @@ jobs:
if: runner.os == 'Windows'
run: |
cargo test -p rake-app-cli --bin rake project_build:: --locked
- name: Global shortcut lifecycle (Windows)
if: runner.os == 'Windows'
run: cargo run -p rake-shortcuts --example shortcut-probe --locked
- name: Global shortcut lifecycle (X11)
if: runner.os == 'Linux'
run: xvfb-run -a env -u WAYLAND_DISPLAY XDG_SESSION_TYPE=x11 cargo run -p rake-shortcuts --example shortcut-probe --locked
- name: Default desktop lints
run: >-
cargo clippy -p rake-app -p rake-desktop-example -p rake-notes
Expand Down Expand Up @@ -347,6 +354,7 @@ jobs:
- run: cargo clippy -p rake-native --features native-tests --all-targets --locked -- -D warnings
- run: cargo test -p rake-native --doc --locked
- run: cargo test -p rake-native --features native-tests --test appkit --locked
- run: cargo run -p rake-shortcuts --example shortcut-probe --locked
- run: cargo test -p rake-tray --locked
- run: cargo test --workspace --locked --no-fail-fast
- run: cargo test -p rake-files --lib --locked
Expand All @@ -355,6 +363,7 @@ jobs:
- run: cargo test -p rake-app --features tray,view-test-support --lib --locked
- run: cargo check -p rake-app --features tray,gpui-fast --all-targets --locked
- run: cargo check -p renamed-facade --features tray --locked
- run: cargo check -p rake-tray-example --locked
- run: cargo test -p rake-app --features view-test-support --lib --test view_state --test actions --test inspection_compiled_out --locked
- run: cargo test -p rake-view --features view-test-support --lib --locked
- run: cargo test -p rake-app --features view-test-support,devtools --lib --test view_state --test element_inspection --locked
Expand Down
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["vendor/blaze-live", "crates/rake-autostart", "crates/rake-view-syntax", "crates/rake-tray", "crates/rake-native", "crates/rake-dist-linux", "crates/rake-updates", "crates/rake-dist-macos", "crates/rake-build", "crates/rake-resources", "crates/rake-dist-windows", "crates/rake-dist-core", "crates/rake-dist", "crates/rake", "crates/rake-diagnostics", "crates/rake-http", "crates/rake-ui", "crates/rake-protocol", "crates/rake-client", "crates/rake-macros", "crates/rake-cli", "examples/tray", "examples/gpu-surface", "examples/gpu-surface/renderer", "examples/kit", "examples/desktop", "examples/distribution", "examples/foundation", "examples/files", "examples/routing", "examples/notes", "examples/view", "examples/preview", "examples/file-icons", "examples/agent", "tests/renamed-facade", "tests/web-platform"]
members = ["crates/rake-shortcuts", "vendor/blaze-live", "crates/rake-autostart", "crates/rake-view-syntax", "crates/rake-tray", "crates/rake-native", "crates/rake-dist-linux", "crates/rake-updates", "crates/rake-dist-macos", "crates/rake-build", "crates/rake-resources", "crates/rake-dist-windows", "crates/rake-dist-core", "crates/rake-dist", "crates/rake", "crates/rake-diagnostics", "crates/rake-http", "crates/rake-ui", "crates/rake-protocol", "crates/rake-client", "crates/rake-macros", "crates/rake-cli", "examples/tray", "examples/gpu-surface", "examples/gpu-surface/renderer", "examples/kit", "examples/desktop", "examples/distribution", "examples/foundation", "examples/files", "examples/routing", "examples/notes", "examples/view", "examples/preview", "examples/file-icons", "examples/agent", "tests/renamed-facade", "tests/web-platform"]
exclude = ["crates/rake-cli/lints/rake-gpui", "examples/inspector", "vendor/gpui-base", "vendor/gpui-pre-web"]
resolver = "3"

Expand Down
31 changes: 31 additions & 0 deletions crates/rake-shortcuts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[package]
name = "rake-shortcuts"
version = "0.1.0-alpha.4"
edition.workspace = true
rust-version.workspace = true
license.workspace = true
repository.workspace = true
description = "Owned native global shortcut registrations for Rake"

[lints]
workspace = true

[dependencies]
rake-tray = { version = "=0.1.0-alpha.4", path = "../rake-tray" }
futures.workspace = true
thiserror.workspace = true

[target.'cfg(target_os = "macos")'.dependencies]
objc2 = "0.6.5"

[target.'cfg(target_os = "windows")'.dependencies]
windows-sys = { version = "0.61", features = ["Win32_UI_WindowsAndMessaging", "Win32_UI_Input_KeyboardAndMouse", "Win32_System_LibraryLoader", "Win32_Graphics_Gdi"] }

[target.'cfg(target_os = "linux")'.dependencies]
ashpd = { version = "0.13.13", default-features = false, features = ["async-io", "global_shortcuts"] }
x11rb = "0.13.2"
async-io = "2.6"
blocking = "1.7"

[[example]]
name = "shortcut-probe"
37 changes: 37 additions & 0 deletions crates/rake-shortcuts/examples/shortcut-probe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! Short-lived real registration/conflict/cleanup probe; never simulates key presses.
use rake_shortcuts::{Error, Shortcut};

fn main() -> Result<(), Box<dyn std::error::Error>> {
futures::executor::block_on(async {
let key = "Ctrl+Alt+Shift+9";
let mut first = Shortcut::register(key).await?;

assert!(matches!(
Shortcut::register(key).await,
Err(Error::Conflict)
));
first.unregister().await?;
first.unregister().await?;
let second = Shortcut::register(key).await?;

drop(second);
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);

let mut third = loop {
match Shortcut::register(key).await {
Ok(shortcut) => break shortcut,
Err(Error::Conflict) if std::time::Instant::now() < deadline => {
std::thread::yield_now();
}
Err(error) => return Err(error),
}
};

third.unregister().await?;

Ok::<_, Error>(())
})?;
println!("global shortcut probe passed: register, conflict, unregister, drop, re-register");

Ok(())
}
156 changes: 156 additions & 0 deletions crates/rake-shortcuts/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
//! Owned global shortcuts. Native events are queued; callers choose their UI dispatcher.
//! macOS and Windows registration, event pumping and destruction belong on the UI thread.

use futures::channel::mpsc;
use rake_tray::Accelerator;
use std::marker::PhantomData;
use std::rc::Rc;

#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "windows")]
mod windows;

#[cfg(target_os = "linux")]
use linux as native;
#[cfg(target_os = "macos")]
use macos as native;
#[cfg(target_os = "windows")]
use windows as native;

/// Registration failed; no alternative shortcut is silently chosen.
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// No backend is available in this host/session.
#[error("global shortcuts are unsupported in this host/session")]
Unsupported,
/// Parsing or the native key mapping rejected the accelerator.
#[error("invalid global shortcut: {0}")]
InvalidInput(String),
/// Another registration already owns this shortcut.
#[error("global shortcut is already registered")]
Conflict,
/// Authorization was refused.
#[error("global shortcut permission denied")]
PermissionDenied,
/// An OS failure, including its diagnostic.
#[error("global shortcut failed: {0}")]
Native(String),
}

/// A thread-bound registration. Drop releases its native resources and stops events.
/// Use `unregister` to observe cleanup errors and await portal session closure.
pub struct Shortcut {
#[cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))]
native: Option<native::Registration>,
events: mpsc::Receiver<()>,
_thread: PhantomData<Rc<()>>,
}

impl Shortcut {
/// Registers a tray-parser accelerator. Poll on the native UI thread.
/// On Wayland the portal may prompt and choose a user-approved binding.
///
/// # Errors
/// Reports invalid keys, conflicts, denied permission and unavailable backends.
pub async fn register(value: &str) -> Result<Self, Error> {
let accelerator =
Accelerator::parse(value).map_err(|error| Error::InvalidInput(error.to_string()))?;

if accelerator.key.chars().count() != 1 {
return Err(Error::InvalidInput(
"key normalization produced multiple characters".into(),
));
}

if !(accelerator.control || accelerator.alt || accelerator.super_key) {
return Err(Error::InvalidInput(
"use Ctrl, Alt or Super with a key".into(),
));
}

let (sender, events) = mpsc::channel::<()>(1);

#[cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))]
{
let native = native::register(accelerator, sender).await?;

Ok(Self {
native: Some(native),
events,
_thread: PhantomData,
})
}

#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
{
let _ = (accelerator, sender, events);

Err(Error::Unsupported)
}
}

/// Takes the activation inbox for a separate UI dispatch task.
/// Subsequent calls return a closed inbox.
pub fn take_events(&mut self) -> mpsc::Receiver<()> {
std::mem::replace(&mut self.events, mpsc::channel(1).1)
}

/// Waits for an activation; a small bounded inbox coalesces repeated presses.
pub async fn next(&mut self) -> Option<()> {
use futures::StreamExt;

self.events.next().await
}

/// Releases the registration. Repeated calls are harmless.
///
/// # Errors
/// Reports native cleanup failures; Drop still attempts resource release.
pub async fn unregister(&mut self) -> Result<(), Error> {
self.events.close();

#[cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))]
if let Some(mut native) = self.native.take() {
native.unregister().await?;
}

Ok(())
}
}

/// Detects backend availability without registering keys or prompting.
pub async fn available() -> bool {
#[cfg(target_os = "linux")]
return linux::available().await;

#[cfg(not(target_os = "linux"))]
cfg!(any(target_os = "macos", target_os = "windows"))
}

#[cfg(test)]
mod tests {
//! Covers: parser validation before native registration.
//! Real: accelerator parser. Doubles: none. Requires: no graphical session.
use super::*;

#[test]
fn invalid_shortcuts_never_register() {
for value in [
"",
"Ctrl+Ctrl+k",
"Hyper+k",
"Ctrl+Enter",
"Ctrl+İ",
"k",
"Shift+k",
] {
assert!(matches!(
futures::executor::block_on(Shortcut::register(value)),
Err(Error::InvalidInput(_))
));
}
}
}
Loading
Loading