Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop-gpui): leave keyboard mode on any press and under modals, focus selects, keep the settings page

Richie McIlroyd4b33f8528c6
diff --git a/apps/desktop-gpui/src/main_window.rs b/apps/desktop-gpui/src/main_window.rs
index eb97a6dfc..bca844d3b 100644
--- a/apps/desktop-gpui/src/main_window.rs
+++ b/apps/desktop-gpui/src/main_window.rs
@@ -2709,6 +2709,9 @@ 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()
@@ -2777,7 +2780,7 @@ impl Render for MainWindow {
cx.stop_propagation();
}
}))
- .capture_any_mouse_down(|_, window, cx| ui::keyboard_nav::pointer_used(window, cx))
+ .child(ui::keyboard_nav::pointer_watcher())
}
}
diff --git a/apps/desktop-gpui/src/menus.rs b/apps/desktop-gpui/src/menus.rs
index 7cf9201db..dc165622d 100644
--- a/apps/desktop-gpui/src/menus.rs
+++ b/apps/desktop-gpui/src/menus.rs
@@ -110,7 +110,12 @@ pub fn init(cx: &mut App) {
// 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| {
- app_windows::open_settings(Page::General, cx);
+ 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);
});
});
diff --git a/apps/desktop-gpui/src/mode_select_window.rs b/apps/desktop-gpui/src/mode_select_window.rs
index 4f80b3352..77dc4fc84 100644
--- a/apps/desktop-gpui/src/mode_select_window.rs
+++ b/apps/desktop-gpui/src/mode_select_window.rs
@@ -250,7 +250,7 @@ impl Render for ModeSelectWindow {
let shell = div()
.track_focus(&self.focus)
- .capture_any_mouse_down(|_, window, cx| keyboard_nav::pointer_used(window, cx))
+ .child(keyboard_nav::pointer_watcher())
// `flex flex-col relative justify-center items-center min-h-screen
// bg-gray-1`
.size_full()
diff --git a/apps/desktop-gpui/src/settings_pages.rs b/apps/desktop-gpui/src/settings_pages.rs
index 7f00fe3d1..dde8b7f54 100644
--- a/apps/desktop-gpui/src/settings_pages.rs
+++ b/apps/desktop-gpui/src/settings_pages.rs
@@ -1333,6 +1333,10 @@ impl SettingsWindow {
self.pages.listening.is_some()
}
+ pub(crate) fn switch_back_running(&self) -> bool {
+ self.pages.switch_back.is_some()
+ }
+
/// Leave recording mode without touching the binding. Safe to call when
/// nothing is recording; every exit path (Escape, a click elsewhere,
/// another page, the window losing focus or closing) lands here.
diff --git a/apps/desktop-gpui/src/settings_window.rs b/apps/desktop-gpui/src/settings_window.rs
index 7d75af5b4..cf59287fe 100644
--- a/apps/desktop-gpui/src/settings_window.rs
+++ b/apps/desktop-gpui/src/settings_window.rs
@@ -752,7 +752,7 @@ impl SettingsWindow {
let pages = crate::settings_pages::PagesState::new(window, cx);
let page_scroll = ScrollHandle::new();
let keyboard_nav = ui::keyboard_nav::install(window, cx, |this: &Self, _| {
- this.menu.is_some() || this.shortcut_listening()
+ this.menu.is_some() || this.shortcut_listening() || this.switch_back_running()
});
Self {
@@ -1560,6 +1560,9 @@ fn covers_default_exclusion(entry: &WindowExclusion, default: &WindowExclusion)
impl Render for SettingsWindow {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
self.sync_appearance(window, cx);
+ if self.switch_back_running() {
+ ui::keyboard_nav::leave(window, cx);
+ }
self.keyboard_nav = ui::keyboard_nav::active(window, cx);
let theme = self.theme;
if self.page == Page::Quality
@@ -1575,7 +1578,7 @@ impl Render for SettingsWindow {
let shell = div()
.track_focus(&self.focus)
- .capture_any_mouse_down(|_, window, cx| ui::keyboard_nav::pointer_used(window, cx))
+ .child(ui::keyboard_nav::pointer_watcher())
// `(window-chrome).tsx` binds Cmd-W to `getCurrentWindow().close()`
// for every chrome window. Escape is not bound there and is not
// bound here.
diff --git a/apps/desktop-gpui/src/ui/keyboard_nav.rs b/apps/desktop-gpui/src/ui/keyboard_nav.rs
index 68e194961..48ec55292 100644
--- a/apps/desktop-gpui/src/ui/keyboard_nav.rs
+++ b/apps/desktop-gpui/src/ui/keyboard_nav.rs
@@ -14,8 +14,8 @@
use std::collections::HashSet;
use gpui::{
- App, BoxShadow, Context, Global, InteractiveElement, Keystroke, Styled, Subscription, Window,
- WindowId, point, px, rgb,
+ App, BoxShadow, Context, DispatchPhase, Global, InteractiveElement, IntoElement, Keystroke,
+ MouseDownEvent, Styled, Subscription, Window, WindowId, canvas, point, px, rgb,
};
use crate::{theme::Theme, ui::text_input::text_input_is_composing};
@@ -57,13 +57,31 @@ pub fn active(window: &Window, cx: &App) -> bool {
.is_some_and(|windows| windows.0.contains(&window.window_handle().window_id()))
}
-pub fn pointer_used(window: &mut Window, cx: &mut App) {
+pub fn leave(window: &mut Window, cx: &mut App) {
let id = window.window_handle().window_id();
if cx.has_global::<KeyboardWindows>() && cx.global_mut::<KeyboardWindows>().0.remove(&id) {
window.refresh();
}
}
+/// Leaves keyboard mode on any mouse press in the window. A window-level
+/// listener, because `.occlude()` hitboxes (menus, modals, the Windows header)
+/// hide the root from element mouse listeners.
+pub fn pointer_watcher() -> impl IntoElement {
+ canvas(
+ |_, _, _| (),
+ |_, (), window, _| {
+ window.on_mouse_event(|_: &MouseDownEvent, phase, window, cx| {
+ if phase == DispatchPhase::Capture {
+ leave(window, cx);
+ }
+ });
+ },
+ )
+ .absolute()
+ .inset_0()
+}
+
/// Route Tab in `window` to focus navigation. `blocked` lets the owner keep
/// Tab for itself, e.g. while a shortcut recorder or a modal is up.
pub fn install<V: 'static>(
@@ -110,7 +128,11 @@ fn navigate(direction: Direction, window: &mut Window, cx: &mut App) {
// before that frame draws, so the move waits one frame more.
window.refresh();
window.on_next_frame(move |window, _| {
- window.on_next_frame(move |window, cx| move_focus(direction, window, cx));
+ window.on_next_frame(move |window, cx| {
+ if active(window, cx) {
+ move_focus(direction, window, cx);
+ }
+ });
});
}
diff --git a/apps/desktop-gpui/src/ui/select.rs b/apps/desktop-gpui/src/ui/select.rs
index fd5bd3a43..3b0498493 100644
--- a/apps/desktop-gpui/src/ui/select.rs
+++ b/apps/desktop-gpui/src/ui/select.rs
@@ -148,9 +148,13 @@ impl RenderOnce for Select {
on_open,
} = self;
+ let interactive = !disabled && (on_click.is_some() || on_open.is_some());
div()
.id(id)
- .when(keyboard_nav::active(window, cx), keyboard_nav::ring)
+ .when(
+ interactive && keyboard_nav::active(window, cx),
+ keyboard_nav::stop,
+ )
.flex()
.flex_row()
.items_center()
diff --git a/apps/desktop-gpui/src/ui/toggle.rs b/apps/desktop-gpui/src/ui/toggle.rs
index ec5c136cd..8e483e03a 100644
--- a/apps/desktop-gpui/src/ui/toggle.rs
+++ b/apps/desktop-gpui/src/ui/toggle.rs
@@ -145,6 +145,12 @@ impl RenderOnce for Toggle {
keyboard_nav::active(window, cx) && !disabled,
keyboard_nav::stop,
)
+ .role(gpui::Role::Switch)
+ .aria_toggled(if checked {
+ gpui::Toggled::True
+ } else {
+ gpui::Toggled::False
+ })
.w(width)
.h(height)
.p(padding)