mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-19 06:59:22 +01:00
which_key: Don't dismiss open modals when showing pending bindings (#66630)
## Summary With which-key enabled, pausing in the middle of a multi-key binding closes whatever modal is open. I hit this with vim mode in the expanded commit editor: pressing `g` and waiting for the which-key popup closed the editor. The same thing happens with `cmd-k` in pickers like Go to Line, so their `cmd-k` chords can't be completed. The cause is that the modal layer only holds one modal at a time, and which-key opens as a modal. Before opening, it only checked whether a which-key popup was already open, so any other modal got replaced. I changed that check to skip the popup while any modal is open. The open modal stays, and the chord still completes inside it. The trade-off is that the popup isn't shown inside modals, but pending keys are still shown in the status bar (by the pending keystrokes indicator, or by the vim mode indicator in vim and Helix mode). Closes #66068 Closes #66246 ## Testing - Added `test_which_key_does_not_replace_open_modal`, which opens a modal, starts a chord, pauses, then completes it, and checks that the modal is still open and the chord's action ran. It fails without the fix. - Added `test_which_key_shows_for_pending_chord` to make sure the popup still shows when no modal is open. - Tested manually on macOS with vim mode and which-key enabled. Steps to reproduce: 1. Set `"vim_mode": true` and `"which_key": { "enabled": true, "delay_ms": 1000 }` 2. Open the Git panel (`ctrl-shift-g`), click the commit message box, and press `shift-escape` 3. Press `Escape`, then `g`, and wait a second 4. Before: the commit editor closes. After: it stays open. ## Self-review - [x] I've reviewed my diff for quality, security, reliability, and performance. - [x] UI changes follow the [checklist](https://zed.dev/docs/development/ui-checklist). - [x] Tests cover the new or changed behavior. Release Notes: - Fixed which-key closing the commit editor and pickers when a multi-key binding was pending --------- Co-authored-by: Divyanshu Singh <177622442+divyanshus2404@users.noreply.github.com> Co-authored-by: Smit Barmase <heysmitbarmase@gmail.com>
This commit is contained in:
Generated
+1
@@ -20734,6 +20734,7 @@ dependencies = [
|
||||
"collections",
|
||||
"command_palette",
|
||||
"gpui",
|
||||
"project",
|
||||
"settings",
|
||||
"theme",
|
||||
"theme_settings",
|
||||
|
||||
@@ -26,4 +26,6 @@ zed_actions.workspace = true
|
||||
[dev-dependencies]
|
||||
collections.workspace = true
|
||||
gpui = { workspace = true, features = ["test-support"] }
|
||||
project = { workspace = true, features = ["test-support"] }
|
||||
theme.workspace = true
|
||||
workspace = { workspace = true, features = ["test-support"] }
|
||||
|
||||
@@ -128,7 +128,9 @@ pub fn init(cx: &mut App) {
|
||||
.await;
|
||||
workspace_handle
|
||||
.update_in(cx, |workspace, window, cx| {
|
||||
if workspace.active_modal::<WhichKeyModal>(cx).is_some() {
|
||||
// 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) {
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -185,7 +187,12 @@ mod tests {
|
||||
use std::cell::Cell;
|
||||
|
||||
use collections::HashMap;
|
||||
use gpui::PlatformKeyboardMapper;
|
||||
use gpui::{
|
||||
BorrowAppContext, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable,
|
||||
InteractiveElement, IntoElement, KeyBinding, PlatformKeyboardMapper, Render,
|
||||
TestAppContext, VisualTestContext, actions, div,
|
||||
};
|
||||
use workspace::{AppState, ModalView, MultiWorkspace};
|
||||
|
||||
use super::*;
|
||||
|
||||
@@ -266,4 +273,105 @@ mod tests {
|
||||
|
||||
assert_eq!(binding_label(&action), "lazygit");
|
||||
}
|
||||
|
||||
actions!(which_key_test, [TestChordAction]);
|
||||
|
||||
struct TestModal {
|
||||
focus_handle: FocusHandle,
|
||||
chord_action_count: usize,
|
||||
}
|
||||
|
||||
impl TestModal {
|
||||
fn new(_: &mut Window, cx: &mut Context<Self>) -> Self {
|
||||
Self {
|
||||
focus_handle: cx.focus_handle(),
|
||||
chord_action_count: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl EventEmitter<DismissEvent> for TestModal {}
|
||||
|
||||
impl Focusable for TestModal {
|
||||
fn focus_handle(&self, _cx: &App) -> FocusHandle {
|
||||
self.focus_handle.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl ModalView for TestModal {}
|
||||
|
||||
impl Render for TestModal {
|
||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
div().track_focus(&self.focus_handle).on_action(cx.listener(
|
||||
|this, _: &TestChordAction, _, _| {
|
||||
this.chord_action_count += 1;
|
||||
},
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
async fn init_workspace_test(
|
||||
cx: &mut TestAppContext,
|
||||
) -> (Entity<Workspace>, &mut VisualTestContext) {
|
||||
let app_state = cx.update(|cx| {
|
||||
let app_state = AppState::test(cx);
|
||||
theme_settings::init(theme::LoadThemes::JustBase, cx);
|
||||
workspace::init(app_state.clone(), cx);
|
||||
init(cx);
|
||||
cx.update_global::<settings::SettingsStore, _>(|store, cx| {
|
||||
store
|
||||
.set_user_settings(r#"{ "which_key": { "enabled": true, "delay_ms": 0 } }"#, cx)
|
||||
.expect("valid test settings");
|
||||
});
|
||||
cx.bind_keys([KeyBinding::new("ctrl-k ctrl-x", TestChordAction, None)]);
|
||||
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)
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_which_key_shows_for_pending_chord(cx: &mut TestAppContext) {
|
||||
let (workspace, cx) = init_workspace_test(cx).await;
|
||||
|
||||
cx.simulate_keystrokes("ctrl-k");
|
||||
cx.run_until_parked();
|
||||
|
||||
workspace.read_with(cx, |workspace, cx| {
|
||||
assert!(workspace.active_modal::<WhichKeyModal>(cx).is_some());
|
||||
});
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_which_key_does_not_replace_open_modal(cx: &mut TestAppContext) {
|
||||
let (workspace, cx) = init_workspace_test(cx).await;
|
||||
workspace.update_in(cx, |workspace, window, cx| {
|
||||
workspace.toggle_modal(window, cx, TestModal::new);
|
||||
});
|
||||
cx.run_until_parked();
|
||||
|
||||
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"
|
||||
);
|
||||
});
|
||||
|
||||
cx.simulate_keystrokes("ctrl-x");
|
||||
cx.run_until_parked();
|
||||
|
||||
workspace.read_with(cx, |workspace, cx| {
|
||||
let modal = workspace
|
||||
.active_modal::<TestModal>(cx)
|
||||
.expect("modal should still be open");
|
||||
assert_eq!(modal.read(cx).chord_action_count, 1);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user