which_key: Show popup above open modals (#66838)

Follow-up to #66068

With which-key enabled, the popup didn't show while a modal was open,
like the commit editor or Go to Line. It was drawn as a modal, and the
modal layer holds only one.

It's now drawn in its own slot on `MultiWorkspace`, deferred above the
modal layer like the sidebar overlay. It doesn't take focus, so the
chord still completes in the open modal.

<img width="500"
src="https://github.com/user-attachments/assets/2089163d-e96b-4b5e-8d2d-0ca5d9826f63"
/>

Release Notes:

- Improved the which-key popup to also show inside open dialogs, like Go
to Line and the commit editor.
This commit is contained in:
Smit Barmase
2026-11-13 05:28:29 +00:00
committed by GitHub
parent 42fa909546
commit 48741fd17d
5 changed files with 96 additions and 69 deletions
+62 -35
View File
@@ -6,7 +6,8 @@ mod which_key_modal;
mod which_key_settings;
use gpui::{
Action, App, KeybindingKeystroke, Keystroke, PlatformKeyboardMapper, SharedString, Window,
Action, App, AppContext as _, DismissEvent, KeybindingKeystroke, Keystroke,
PlatformKeyboardMapper, SharedString, Window,
};
pub use pending_keystrokes_indicator::PendingKeystrokesIndicator;
use settings::Settings;
@@ -14,7 +15,7 @@ use std::{sync::LazyLock, time::Duration};
use util::ResultExt;
use which_key_modal::WhichKeyModal;
use which_key_settings::WhichKeySettings;
use workspace::Workspace;
use workspace::MultiWorkspace;
gpui::actions!(
which_key,
@@ -101,16 +102,14 @@ fn binding_label(action: &dyn Action) -> SharedString {
pub fn init(cx: &mut App) {
WhichKeySettings::register(cx);
cx.observe_new(|_: &mut Workspace, window, cx| {
cx.observe_new(|_: &mut MultiWorkspace, window, cx| {
let Some(window) = window else {
return;
};
let mut timer = None;
cx.observe_pending_input(window, move |workspace, window, cx| {
cx.observe_pending_input(window, move |multi_workspace, window, cx| {
if window.pending_input_keystrokes().is_none() {
if let Some(modal) = workspace.active_modal::<WhichKeyModal>(cx) {
modal.update(cx, |modal, cx| modal.dismiss(cx));
};
multi_workspace.set_pending_bindings_popup(None, cx);
timer.take();
return;
}
@@ -122,21 +121,23 @@ pub fn init(cx: &mut App) {
let delay_ms = which_key_settings.delay_ms;
timer.replace(cx.spawn_in(window, async move |workspace_handle, cx| {
timer.replace(cx.spawn_in(window, async move |multi_workspace, cx| {
cx.background_executor()
.timer(Duration::from_millis(delay_ms))
.await;
workspace_handle
.update_in(cx, |workspace, window, cx| {
// The modal layer holds one modal at a time, so showing which-key
// would dismiss an open modal such as the commit editor or a picker.
if workspace.has_active_modal(window, cx) {
multi_workspace
.update_in(cx, |multi_workspace, window, cx| {
if multi_workspace.pending_bindings_popup().is_some() {
return;
};
}
workspace.toggle_modal(window, cx, |window, cx| {
WhichKeyModal::new(workspace_handle.clone(), window, cx)
});
let workspace = multi_workspace.workspace().downgrade();
let popup = cx.new(|cx| WhichKeyModal::new(workspace, window, cx));
cx.subscribe(&popup, |multi_workspace, _, _: &DismissEvent, cx| {
multi_workspace.set_pending_bindings_popup(None, cx);
})
.detach();
multi_workspace.set_pending_bindings_popup(Some(popup.into()), cx);
})
.log_err();
}));
@@ -188,11 +189,11 @@ mod tests {
use collections::HashMap;
use gpui::{
BorrowAppContext, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable,
BorrowAppContext, Context, Entity, EventEmitter, FocusHandle, Focusable,
InteractiveElement, IntoElement, KeyBinding, PlatformKeyboardMapper, Render,
TestAppContext, VisualTestContext, actions, div,
};
use workspace::{AppState, ModalView, MultiWorkspace};
use workspace::{AppState, ModalView};
use super::*;
@@ -312,7 +313,7 @@ mod tests {
async fn init_workspace_test(
cx: &mut TestAppContext,
) -> (Entity<Workspace>, &mut VisualTestContext) {
) -> (Entity<MultiWorkspace>, &mut VisualTestContext) {
let app_state = cx.update(|cx| {
let app_state = AppState::test(cx);
theme_settings::init(theme::LoadThemes::JustBase, cx);
@@ -327,28 +328,31 @@ mod tests {
app_state
});
let project = project::Project::test(app_state.fs.clone(), [], cx).await;
let (multi_workspace, cx) =
cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
let workspace =
multi_workspace.read_with(cx, |multi_workspace, _| multi_workspace.workspace().clone());
(workspace, cx)
cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx))
}
#[gpui::test]
async fn test_which_key_shows_for_pending_chord(cx: &mut TestAppContext) {
let (workspace, cx) = init_workspace_test(cx).await;
let (multi_workspace, cx) = init_workspace_test(cx).await;
cx.simulate_keystrokes("ctrl-k");
cx.run_until_parked();
multi_workspace.read_with(cx, |multi_workspace, _| {
assert!(multi_workspace.pending_bindings_popup().is_some());
});
workspace.read_with(cx, |workspace, cx| {
assert!(workspace.active_modal::<WhichKeyModal>(cx).is_some());
cx.simulate_keystrokes("escape");
cx.run_until_parked();
multi_workspace.read_with(cx, |multi_workspace, _| {
assert!(multi_workspace.pending_bindings_popup().is_none());
});
}
#[gpui::test]
async fn test_which_key_does_not_replace_open_modal(cx: &mut TestAppContext) {
let (workspace, cx) = init_workspace_test(cx).await;
async fn test_which_key_shows_above_open_modal(cx: &mut TestAppContext) {
let (multi_workspace, cx) = init_workspace_test(cx).await;
let workspace =
multi_workspace.read_with(cx, |multi_workspace, _| multi_workspace.workspace().clone());
workspace.update_in(cx, |workspace, window, cx| {
workspace.toggle_modal(window, cx, TestModal::new);
});
@@ -357,11 +361,14 @@ mod tests {
cx.simulate_keystrokes("ctrl-k");
cx.run_until_parked();
workspace.read_with(cx, |workspace, cx| {
assert!(
workspace.active_modal::<TestModal>(cx).is_some(),
"which-key should not dismiss the open modal"
);
assert!(
workspace.read_with(cx, |workspace, cx| workspace
.active_modal::<TestModal>(cx)
.is_some()),
"which-key should not dismiss the open modal"
);
multi_workspace.read_with(cx, |multi_workspace, _| {
assert!(multi_workspace.pending_bindings_popup().is_some());
});
cx.simulate_keystrokes("ctrl-x");
@@ -373,5 +380,25 @@ mod tests {
.expect("modal should still be open");
assert_eq!(modal.read(cx).chord_action_count, 1);
});
multi_workspace.read_with(cx, |multi_workspace, _| {
assert!(multi_workspace.pending_bindings_popup().is_none());
});
}
#[gpui::test]
async fn test_keystroke_without_pending_chord_does_not_notify(cx: &mut TestAppContext) {
let (multi_workspace, cx) = init_workspace_test(cx).await;
let notify_count = std::rc::Rc::new(Cell::new(0));
let _subscription = cx.update(|_, cx| {
let notify_count = notify_count.clone();
cx.observe(&multi_workspace, move |_, _| {
notify_count.set(notify_count.get() + 1);
})
});
cx.simulate_keystrokes("a");
cx.run_until_parked();
assert_eq!(notify_count.get(), 0);
}
}
+1 -11
View File
@@ -8,7 +8,7 @@ use settings::Settings;
use std::rc::Rc;
use theme_settings::ThemeSettings;
use ui::{DynamicSpacing, prelude::*};
use workspace::{ModalView, Workspace};
use workspace::Workspace;
use crate::{
bindings_for_which_key, map_pending_keystrokes,
@@ -55,10 +55,6 @@ impl WhichKeyModal {
this
}
pub fn dismiss(&self, cx: &mut Context<Self>) {
cx.emit(DismissEvent)
}
fn update_pending_keys(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let Some(pending_keys) = window.pending_input_keystrokes() else {
cx.emit(DismissEvent);
@@ -130,9 +126,3 @@ impl Focusable for WhichKeyModal {
self.focus_handle.clone()
}
}
impl ModalView for WhichKeyModal {
fn render_bare(&self) -> bool {
true
}
}
+6 -19
View File
@@ -58,10 +58,6 @@ pub trait ModalView: ManagedView {
fn fade_out_background(&self) -> bool {
false
}
fn render_bare(&self) -> bool {
false
}
}
trait ModalViewHandle {
@@ -70,7 +66,6 @@ trait ModalViewHandle {
fn focus_handle(&self, cx: &App) -> FocusHandle;
fn subscribe_dismiss(&self, window: &mut Window, cx: &mut Context<ModalLayer>) -> Subscription;
fn fade_out_background(&self, cx: &mut App) -> bool;
fn render_bare(&self, cx: &mut App) -> bool;
}
impl<V: ModalView> ModalViewHandle for Entity<V> {
@@ -95,10 +90,6 @@ impl<V: ModalView> ModalViewHandle for Entity<V> {
fn fade_out_background(&self, cx: &mut App) -> bool {
self.read(cx).fade_out_background()
}
fn render_bare(&self, cx: &mut App) -> bool {
self.read(cx).render_bare()
}
}
pub struct ActiveModal {
@@ -114,10 +105,10 @@ pub struct ModalLayer {
// can reveal it again with its exact prior state. Left intact across
// non-reopenable modals (e.g. the command palette), which may trigger the reopen.
stashed_modal: Option<Box<dyn ModalViewHandle>>,
// Set when a reveal was requested while another modal was still active (e.g. the
// which-key popup that is dismissed only after the reopen action fires). The reveal
// then happens once that modal closes, so it doesn't matter whether the triggering
// modal is dismissed before or after the action dispatches.
// Set when a reveal was requested while another modal was still active (e.g. one
// that is dismissed only after the reopen action fires). The reveal then happens
// once that modal closes, so it doesn't matter whether the triggering modal is
// dismissed before or after the action dispatches.
reveal_stash_when_free: bool,
dismiss_on_focus_lost: bool,
}
@@ -209,8 +200,8 @@ impl ModalLayer {
return false;
}
if self.active_modal.is_some() {
// Another modal is still closing (e.g. the which-key popup that dismisses
// only after this action fires). Reveal once it is gone; see `hide_modal`.
// Another modal is still closing (e.g. one that dismisses only after this
// action fires). Reveal once it is gone; see `hide_modal`.
self.reveal_stash_when_free = true;
return false;
}
@@ -291,10 +282,6 @@ impl Render for ModalLayer {
return div().into_any_element();
};
if active_modal.modal.render_bare(cx) {
return active_modal.modal.view().into_any_element();
}
div()
.absolute()
.size_full()
+25 -1
View File
@@ -316,6 +316,7 @@ pub struct MultiWorkspace {
sidebar: Option<Box<dyn SidebarHandle>>,
sidebar_open: bool,
sidebar_overlay: Option<AnyView>,
pending_bindings_popup: Option<AnyView>,
pending_removal_tasks: Vec<Task<()>>,
_serialize_task: Option<Task<()>>,
_subscriptions: Vec<Subscription>,
@@ -377,6 +378,7 @@ impl MultiWorkspace {
sidebar: None,
sidebar_open: false,
sidebar_overlay: None,
pending_bindings_popup: None,
pending_removal_tasks: Vec::new(),
_serialize_task: None,
_subscriptions: vec![release_subscription, settings_subscription],
@@ -407,6 +409,22 @@ impl MultiWorkspace {
cx.notify();
}
/// Shows the which-key popup for the pending key sequence. It is drawn above the modal layer
/// instead of in it, so the chord can still complete in an open modal.
pub fn set_pending_bindings_popup(&mut self, popup: Option<AnyView>, cx: &mut Context<Self>) {
// Which-key clears the popup on every keystroke that leaves no pending input, and
// observers of this entity, like the agent panel's terminal notification, react to notify.
if popup.is_none() && self.pending_bindings_popup.is_none() {
return;
}
self.pending_bindings_popup = popup;
cx.notify();
}
pub fn pending_bindings_popup(&self) -> Option<&AnyView> {
self.pending_bindings_popup.as_ref()
}
pub fn sidebar_open(&self) -> bool {
self.sidebar_open
}
@@ -2273,7 +2291,13 @@ impl Render for MultiWorkspace {
),
))
.with_priority(2)
})),
}))
// Above the sidebar overlay, since a chord can be pending inside it too.
.children(
self.pending_bindings_popup
.clone()
.map(|popup| deferred(popup).with_priority(3)),
),
rounded_corners,
window,
cx,
+2 -3
View File
@@ -17539,9 +17539,8 @@ mod tests {
});
cx.executor().run_until_parked();
// A non-reopenable modal (e.g. the which-key popup) is already
// active when the reopen fires, and it dismisses *after* the action rather
// than before it (which-key dismisses when pending input clears).
// A non-reopenable modal is already active when the reopen fires, and it
// dismisses *after* the action rather than before it.
workspace.update_in(cx, |workspace, window, cx| {
workspace.toggle_modal(window, cx, TestModal::new);
});