Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop-gpui): restore focus after closing Settings dropdowns

vedantggwp04b6e829c061
diff --git a/apps/desktop-gpui/src/settings_window.rs b/apps/desktop-gpui/src/settings_window.rs
index 37e5b466d..e51a6b85a 100644
--- a/apps/desktop-gpui/src/settings_window.rs
+++ b/apps/desktop-gpui/src/settings_window.rs
@@ -211,6 +211,7 @@ pub(crate) enum MenuKind {
struct OpenMenu {
kind: MenuKind,
+ return_focus: Option<FocusHandle>,
/// Anchor, row count and keyboard highlight. `Menu.popup()` with no
/// position argument opens at the pointer, so the origin is the faithful
/// anchor; the highlight is what makes arrows and Enter work.
@@ -1356,11 +1357,15 @@ impl SettingsWindow {
// them first -- its bindings sit deeper in the dispatch path. Opening
// a menu therefore takes focus back, which is also what clicking a
// `<button>` does to a focused `<input>` in the webview.
+ let return_focus = ui::keyboard_nav::active(window, cx)
+ .then(|| window.focused(cx))
+ .flatten();
let focus = self.focus.clone();
window.focus(&focus, cx);
let items = self.menu_items(kind);
self.menu = Some(OpenMenu {
kind,
+ return_focus,
state: ui::MenuState::new(origin, &items),
});
cx.notify();
@@ -1383,7 +1388,7 @@ impl SettingsWindow {
true
}
ui::MenuKey::Dismiss => {
- self.menu = None;
+ self.close_menu(window, cx);
cx.notify();
true
}
@@ -1391,6 +1396,16 @@ impl SettingsWindow {
}
}
+ fn close_menu(&mut self, window: &mut Window, cx: &mut Context<Self>) {
+ if let Some(menu) = self.menu.take()
+ && ui::keyboard_nav::active(window, cx)
+ && self.focus.is_focused(window)
+ && let Some(focus) = menu.return_focus
+ {
+ window.focus(&focus, cx);
+ }
+ }
+
/// One row per option, check-marked when it is the value in force.
fn menu_items(&self, kind: MenuKind) -> Vec<ui::MenuItem> {
match kind {
@@ -1425,7 +1440,7 @@ impl SettingsWindow {
window: &mut Window,
cx: &mut Context<Self>,
) {
- self.menu = None;
+ self.close_menu(window, cx);
match kind {
MenuKind::Countdown => {
if let Some((value, _)) = COUNTDOWN_OPTIONS.get(index) {