Skip to content
Merged
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
5 changes: 5 additions & 0 deletions apps/desktop-gpui/src/hotkeys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ pub fn pause(cx: &mut App) -> bool {
true
}

pub fn is_paused(cx: &App) -> bool {
cx.try_global::<Hotkeys>()
.is_some_and(|hotkeys| hotkeys.paused)
}

pub fn editing_blocked(cx: &App) -> bool {
#[cfg(target_os = "linux")]
if cx
Expand Down
54 changes: 54 additions & 0 deletions apps/desktop-gpui/src/main_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,8 @@ pub struct MainWindow {
input_enumeration_gate: devices::InputEnumerationGate,
/// `scheduleTargetListPrewarm` (`new-main/index.tsx:1897-1965`).
prewarm_task: Option<gpui::Task<()>>,
keyboard_nav: bool,
_keyboard_nav: gpui::Subscription,
}

/// `createLicenseQuery` (`utils/queries.ts:257-273`): pro from the auth
Expand Down Expand Up @@ -1059,6 +1061,8 @@ impl MainWindow {
})
.detach();

let keyboard_nav = ui::keyboard_nav::install(window, cx, Self::keyboard_nav_blocked);

Self {
theme,
// `rawOptions.mode` -- the recording mode is a persisted setting,
Expand Down Expand Up @@ -1113,9 +1117,19 @@ impl MainWindow {
input_poll_generation: 0,
input_enumeration_gate: devices::InputEnumerationGate::default(),
prewarm_task: None,
keyboard_nav: false,
_keyboard_nav: keyboard_nav,
}
}

fn keyboard_nav_blocked(&self, cx: &gpui::App) -> bool {
self.session.read(cx).phase != Phase::Idle
|| self.microphone_warning.is_some()
|| self.sign_in_prompt.is_some()
|| self.idle_error(cx).is_some()
|| app_windows::clean_capture_pending(cx)
}

/// Kick off device enumeration.
///
/// Deliberately *not* called from `new`. `new` runs inside `open_window`'s
Expand Down Expand Up @@ -2695,6 +2709,10 @@ impl MainWindow {
impl Render for MainWindow {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
self.sync_appearance(window, cx);
if self.keyboard_nav_blocked(cx) {
ui::keyboard_nav::leave(window, cx);
}
self.keyboard_nav = ui::keyboard_nav::active(window, cx);
let theme = self.theme;
let meter_active = self.panel.is_none()
&& self.microphone.is_some()
Expand Down Expand Up @@ -2762,6 +2780,7 @@ impl Render for MainWindow {
cx.stop_propagation();
}
}))
.child(ui::keyboard_nav::pointer_watcher())
}
}

Expand Down Expand Up @@ -3456,6 +3475,7 @@ impl MainWindow {
/// 8px from the window edges (`mx-2`).
fn render_header_actions(&self, cx: &mut Context<Self>) -> impl IntoElement {
let theme = self.theme;
let keyboard_nav = self.keyboard_nav;

// `IconButton::header`: a 20px hit box with no fill, `text-gray-11`
// going to `text-gray-12` on hover.
Expand All @@ -3474,6 +3494,9 @@ impl MainWindow {
.group(id)
.when(cfg!(target_os = "windows"), |this| this.occlude())
.tab_index(0)
.when(keyboard_nav, ui::keyboard_nav::ring)
.role(gpui::Role::Button)
.aria_label(label)
.flex()
.items_center()
.justify_center()
Expand Down Expand Up @@ -3901,6 +3924,9 @@ impl MainWindow {
.child(
div()
.id("panel-back")
.when(self.keyboard_nav, ui::keyboard_nav::stop)
.role(gpui::Role::Button)
.aria_label("Back")
.flex()
.flex_row()
.items_center()
Expand Down Expand Up @@ -5631,6 +5657,10 @@ impl MainWindow {

div()
.id(id)
.when(self.keyboard_nav, ui::keyboard_nav::stop)
.role(gpui::Role::Button)
.aria_label(label.clone())
.aria_selected(selected)
.flex()
.flex_col()
.w(px(self.target_card_width()))
Expand Down Expand Up @@ -5876,6 +5906,10 @@ impl MainWindow {

div()
.id(id)
.when(self.keyboard_nav, ui::keyboard_nav::stop)
.role(gpui::Role::Button)
.aria_label(label.clone())
.aria_selected(selected)
.relative()
.flex()
.flex_row()
Expand Down Expand Up @@ -6150,6 +6184,10 @@ impl MainWindow {
div()
.id(SharedString::from(id))
.tab_index(0)
.when(self.keyboard_nav, ui::keyboard_nav::ring)
.role(gpui::Role::Button)
.aria_label(mode.panel_title())
.aria_selected(selected)
.relative()
.on_hover(cx.listener(move |this, hovered, _, cx| {
this.update_mode_hover(mode, false, *hovered, cx);
Expand Down Expand Up @@ -6218,6 +6256,9 @@ impl MainWindow {
div()
.id("mode-info")
.tab_index(0)
.when(self.keyboard_nav, ui::keyboard_nav::ring)
.role(gpui::Role::Button)
.aria_label("Recording mode info")
.tooltip(move |_, cx| ui::Tooltip::new(&theme, "Recording mode info").view(cx))
.tooltip_show_delay(ui::TOOLTIP_SHOW_DELAY)
.absolute()
Expand Down Expand Up @@ -6303,6 +6344,12 @@ impl MainWindow {
.child(
div()
.id(dropdown_id.clone())
.when(self.keyboard_nav, ui::keyboard_nav::stop)
.role(gpui::Role::Button)
.aria_label(match target {
TargetType::Display => "Choose a display",
_ => "Choose a window",
})
.flex()
.w(px(28.))
.rounded_r(px(9.))
Expand Down Expand Up @@ -6394,6 +6441,10 @@ impl MainWindow {

let base = div()
.id(SharedString::from(target.label()))
.when(self.keyboard_nav, ui::keyboard_nav::stop)
.role(gpui::Role::Button)
.aria_label(target.label())
.aria_selected(selected)
.flex()
.flex_1()
.py(px(8.))
Expand Down Expand Up @@ -6567,6 +6618,9 @@ impl MainWindow {

div()
.id(SharedString::from(id))
.when(self.keyboard_nav, ui::keyboard_nav::stop)
.role(gpui::Role::Button)
.aria_label(label.clone())
.relative()
.flex()
.flex_row()
Expand Down
108 changes: 100 additions & 8 deletions apps/desktop-gpui/src/menus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ use gpui::{

use crate::{
app_windows::{self, AppWindows},
hotkeys,
main_window::MainWindow,
platform,
session::{Phase, RecordingSession},
settings_window::Page,
store,
ui::text_input,
};
Expand Down Expand Up @@ -66,6 +68,8 @@ actions!(
Zoom,
/// `PredefinedMenuItem::fullscreen` -- ⌃⌘F.
ToggleFullscreen,
/// Settings… -- ⌘, on macOS, Ctrl+, on Windows and Linux.
OpenSettings,
]
);

Expand All @@ -77,14 +81,7 @@ pub fn init(cx: &mut App) {
// commands. Deliberately *not* including cmd-z/x/c/v/a -- those stay the
// text field's own `TextInput`-scoped bindings (and, for the editor, its
// `on_key_down` project-undo). A global binding here would shadow both.
cx.bind_keys([
KeyBinding::new("cmd-q", Quit, None),
KeyBinding::new("cmd-h", HideSelf, None),
KeyBinding::new("alt-cmd-h", HideOthers, None),
KeyBinding::new("cmd-w", CloseWindow, None),
KeyBinding::new("cmd-m", Minimize, None),
KeyBinding::new("ctrl-cmd-f", ToggleFullscreen, None),
]);
cx.bind_keys(bindings());

cx.on_action(|_: &About, cx: &mut App| {
// Ordering a window front re-enters gpui's window callbacks, so it runs
Expand All @@ -99,6 +96,29 @@ pub fn init(cx: &mut App) {
cx.on_action(|_: &CloseWindow, cx: &mut App| close_key_window(cx));
cx.on_action(|_: &Minimize, cx: &mut App| with_key_native(cx, platform::minimize_native));
cx.on_action(|_: &Zoom, cx: &mut App| with_key_native(cx, platform::zoom_native));
cx.on_action(|_: &OpenSettings, cx: &mut App| {
// While a recording runs, onboarding is up or the Shortcuts page is
// recording a combo, the chord belongs to whatever has focus.
let idle = RecordingSession::global(cx).read(cx).phase == Phase::Idle;
let onboarding_open = cx
.try_global::<AppWindows>()
.is_some_and(|windows| windows.onboarding.is_some());
if !settings_shortcut_allowed(idle, onboarding_open, hotkeys::is_paused(cx)) {
cx.propagate();
return;
}
// Deferred: the keystroke arrives inside the key window's dispatch, and
// `open_settings` updates the settings window, which may be that one.
cx.defer(|cx: &mut App| {
let page = cx
.try_global::<AppWindows>()
.and_then(|windows| windows.settings)
.and_then(|settings| settings.read(cx).ok().map(|settings| settings.page))
.unwrap_or(Page::General);
app_windows::open_settings(page, cx);
cx.activate(true);
});
});
cx.on_action(|_: &ToggleFullscreen, cx: &mut App| {
#[cfg(target_os = "windows")]
if let Some(handle) = cx.active_window() {
Expand All @@ -113,6 +133,22 @@ pub fn init(cx: &mut App) {
cx.set_menus(build_menus());
}

pub(crate) fn bindings() -> Vec<KeyBinding> {
vec![
KeyBinding::new("cmd-q", Quit, None),
KeyBinding::new("cmd-h", HideSelf, None),
KeyBinding::new("alt-cmd-h", HideOthers, None),
KeyBinding::new("cmd-w", CloseWindow, None),
KeyBinding::new("cmd-m", Minimize, None),
KeyBinding::new("ctrl-cmd-f", ToggleFullscreen, None),
KeyBinding::new("secondary-,", OpenSettings, None),
]
}

fn settings_shortcut_allowed(idle: bool, onboarding_open: bool, hotkeys_paused: bool) -> bool {
idle && !onboarding_open && !hotkeys_paused
}

/// The menu bar model.
///
/// One-to-one with `build_macos_app_menu`, including the empty Help submenu
Expand All @@ -125,6 +161,8 @@ pub fn build_menus() -> Vec<Menu> {
items: vec![
MenuItem::action(format!("About {APP_NAME}"), About),
MenuItem::separator(),
MenuItem::action("Settings…", OpenSettings),
MenuItem::separator(),
MenuItem::os_submenu("Services", SystemMenuType::Services),
MenuItem::separator(),
MenuItem::action(format!("Hide {APP_NAME}"), HideSelf),
Expand Down Expand Up @@ -880,6 +918,58 @@ mod tests {
assert!(gate.pending.is_none());
}

#[test]
fn settings_shortcut_yields_to_recording_onboarding_and_the_recorder() {
assert!(settings_shortcut_allowed(true, false, false));
assert!(!settings_shortcut_allowed(false, false, false));
assert!(!settings_shortcut_allowed(true, true, false));
assert!(!settings_shortcut_allowed(true, false, true));
}

fn keystrokes(binding: &KeyBinding) -> Vec<gpui::Keystroke> {
binding
.keystrokes()
.iter()
.map(|keystroke| keystroke.inner().clone())
.collect()
}

#[test]
fn global_keymap_is_unchanged_apart_from_settings() {
let settings = if cfg!(target_os = "macos") {
"cmd-,"
} else {
"ctrl-,"
};
let expected: &[(&str, &dyn gpui::Action)] = &[
("cmd-q", &Quit),
("cmd-h", &HideSelf),
("alt-cmd-h", &HideOthers),
("cmd-w", &CloseWindow),
("cmd-m", &Minimize),
("ctrl-cmd-f", &ToggleFullscreen),
(settings, &OpenSettings),
];
let bindings = bindings();
assert_eq!(bindings.len(), expected.len());
for (binding, (source, action)) in bindings.iter().zip(expected) {
assert_eq!(
keystrokes(binding),
vec![gpui::Keystroke::parse(source).unwrap()],
"{source}"
);
assert!(binding.action().partial_eq(*action), "{source}");
assert!(binding.predicate().is_none(), "{source}");
}
let mut seen = std::collections::HashSet::new();
for binding in &bindings {
assert!(
seen.insert(keystrokes(binding)),
"{binding:?} is bound twice"
);
}
}

/// The menu bar, transcribed from `build_macos_app_menu`. Separators are
/// spelled `"-"` so the ordering is checked too.
fn describe(menu: &Menu) -> (String, Vec<String>) {
Expand Down Expand Up @@ -907,6 +997,8 @@ mod tests {
vec![
"About Cap".to_string(),
"-".into(),
"Settings…".into(),
"-".into(),
"Services".into(),
"-".into(),
"Hide Cap".into(),
Expand Down
17 changes: 14 additions & 3 deletions apps/desktop-gpui/src/mode_select_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

use gpui::{
Context, FocusHandle, FontWeight, Hsla, InteractiveElement, IntoElement, ParentElement, Pixels,
Point, Render, SharedString, StatefulInteractiveElement, Styled, Window, div,
prelude::FluentBuilder, px, svg,
Point, Render, Role, SharedString, StatefulInteractiveElement, Styled, Subscription, Window,
div, prelude::FluentBuilder, px, svg,
};

use crate::{main_window::Mode, theme::Theme};
use crate::{main_window::Mode, theme::Theme, ui::keyboard_nav};

/// `.inner_size(580.0, 340.0)` / `.min_inner_size(580.0, 340.0)` /
/// `.resizable(false)` on the `ShowCapWindow::ModeSelect` arm in `windows.rs`,
Expand Down Expand Up @@ -45,6 +45,8 @@ pub struct ModeSelectWindow {
/// cannot drift.
mode: Mode,
focus: FocusHandle,
keyboard_nav: bool,
_keyboard_nav: Subscription,
}

impl ModeSelectWindow {
Expand All @@ -67,6 +69,8 @@ impl ModeSelectWindow {
theme: Theme::for_window(window, cx, false),
mode,
focus: cx.focus_handle(),
keyboard_nav: false,
_keyboard_nav: keyboard_nav::install(window, cx, |_, _| false),
}
}

Expand Down Expand Up @@ -104,6 +108,11 @@ impl ModeSelectWindow {

div()
.id(SharedString::from(mode.panel_title()))
.when(self.keyboard_nav, keyboard_nav::stop)
.role(Role::Button)
.aria_label(mode.panel_title())
.aria_description(mode.card_description())
.aria_selected(selected)
.relative()
.flex()
.flex_col()
Expand Down Expand Up @@ -236,10 +245,12 @@ impl ModeSelectWindow {
impl Render for ModeSelectWindow {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
self.sync_appearance(window, cx);
self.keyboard_nav = keyboard_nav::active(window, cx);
let theme = self.theme;

let shell = div()
.track_focus(&self.focus)
.child(keyboard_nav::pointer_watcher())
// `flex flex-col relative justify-center items-center min-h-screen
// bg-gray-1`
.size_full()
Expand Down
Loading
Loading