diff --git a/Cargo.lock b/Cargo.lock index 324ca2375fd..ca7205db7f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20734,6 +20734,7 @@ dependencies = [ "collections", "command_palette", "gpui", + "project", "settings", "theme", "theme_settings", diff --git a/crates/which_key/Cargo.toml b/crates/which_key/Cargo.toml index 18e2bf1c756..7c34edc3c4f 100644 --- a/crates/which_key/Cargo.toml +++ b/crates/which_key/Cargo.toml @@ -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"] } diff --git a/crates/which_key/src/which_key.rs b/crates/which_key/src/which_key.rs index 11605431870..5950e15c524 100644 --- a/crates/which_key/src/which_key.rs +++ b/crates/which_key/src/which_key.rs @@ -128,7 +128,9 @@ pub fn init(cx: &mut App) { .await; workspace_handle .update_in(cx, |workspace, window, cx| { - if workspace.active_modal::(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 { + focus_handle: cx.focus_handle(), + chord_action_count: 0, + } + } + } + + impl EventEmitter 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) -> 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, &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::(|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::(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::(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::(cx) + .expect("modal should still be open"); + assert_eq!(modal.read(cx).chord_action_count, 1); + }); + } }