From 917559da35dd4d626205da705b071c800c9c2553 Mon Sep 17 00:00:00 2001 From: Yann Defretin Date: Mon, 16 Nov 2026 14:20:13 +0000 Subject: [PATCH] sidebar: Add keyboard navigation to the thread switcher (#66951) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary The thread switcher only cycles: `ctrl-tab` steps forward, `ctrl-shift-tab` steps back, and releasing the modifier confirms. Its view only handles `menu::Confirm`, `menu::Cancel` and `agents_sidebar::ToggleThreadSwitcher`, so the global `menu::SelectNext` / `menu::SelectPrevious` bindings – arrow keys, `ctrl-n` / `ctrl-p` – and `menu::SelectFirst` / `menu::SelectLast` do nothing while it is open. With threads from several workspaces in the list, reaching the nth entry costs n−1 presses of the trigger. This PR makes the switcher handle those four actions, like the other modal lists in Zed: - Next and previous wrap around and preview the selected thread without confirming it, the same way the cycle bindings do. - `ctrl-up` / `ctrl-down` are bound in the `ThreadSwitcher` context, mirroring the tab switcher, so the selection can move while the modifier that opened the switcher is still held. On macOS, `ctrl-up` / `ctrl-down` are taken by Mission Control and App Exposé by default, so they may never reach Zed – `ctrl-n` / `ctrl-p` still work with `ctrl` held. The thread switcher paragraph in `docs/src/ai/parallel-agents.md` mentions the new keys. Proposed in discussion #66923. https://github.com/user-attachments/assets/b5138dab-3e16-4e2a-a99a-a5b7708a66bc ## Testing - `test_thread_switcher_menu_navigation_wraps_without_confirming` covers wrapping in both directions, `SelectFirst` / `SelectLast`, and checks that stepping previews a thread without confirming it or recording an access – only `menu::Confirm` does. - `cargo test -p sidebar` and `./script/clippy -p sidebar` pass locally. - Tested manually on macOS: open the switcher with `ctrl-tab`, keep `ctrl` held and step with `ctrl-n` / `ctrl-p`, release to confirm. ## 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: - Added keyboard navigation to the thread switcher: arrow keys, `ctrl-n` / `ctrl-p`, Home / End, and `ctrl-up` / `ctrl-down` while the switcher's modifier is held. --- assets/keymaps/default-linux.json | 2 + assets/keymaps/default-macos.json | 2 + assets/keymaps/default-windows.json | 2 + crates/sidebar/src/sidebar.rs | 4 +- crates/sidebar/src/sidebar_tests.rs | 120 +++++++++++++++++++++++++- crates/sidebar/src/thread_switcher.rs | 44 ++++++++-- docs/src/ai/parallel-agents.md | 2 +- 7 files changed, 164 insertions(+), 12 deletions(-) diff --git a/assets/keymaps/default-linux.json b/assets/keymaps/default-linux.json index 39d273dbced..234072e62e5 100644 --- a/assets/keymaps/default-linux.json +++ b/assets/keymaps/default-linux.json @@ -772,6 +772,8 @@ "bindings": { "ctrl-tab": "agents_sidebar::ToggleThreadSwitcher", "ctrl-shift-tab": ["agents_sidebar::ToggleThreadSwitcher", { "select_last": true }], + "ctrl-up": "menu::SelectPrevious", + "ctrl-down": "menu::SelectNext", }, }, { diff --git a/assets/keymaps/default-macos.json b/assets/keymaps/default-macos.json index 3d40d674b77..9cfba310030 100644 --- a/assets/keymaps/default-macos.json +++ b/assets/keymaps/default-macos.json @@ -827,6 +827,8 @@ "bindings": { "ctrl-tab": "agents_sidebar::ToggleThreadSwitcher", "ctrl-shift-tab": ["agents_sidebar::ToggleThreadSwitcher", { "select_last": true }], + "ctrl-up": "menu::SelectPrevious", + "ctrl-down": "menu::SelectNext", }, }, { diff --git a/assets/keymaps/default-windows.json b/assets/keymaps/default-windows.json index cc1eb872267..d628a9af312 100644 --- a/assets/keymaps/default-windows.json +++ b/assets/keymaps/default-windows.json @@ -780,6 +780,8 @@ "bindings": { "ctrl-tab": "agents_sidebar::ToggleThreadSwitcher", "ctrl-shift-tab": ["agents_sidebar::ToggleThreadSwitcher", { "select_last": true }], + "ctrl-up": "menu::SelectPrevious", + "ctrl-down": "menu::SelectNext", }, }, { diff --git a/crates/sidebar/src/sidebar.rs b/crates/sidebar/src/sidebar.rs index ac31eb563b8..da0dc357306 100644 --- a/crates/sidebar/src/sidebar.rs +++ b/crates/sidebar/src/sidebar.rs @@ -6079,9 +6079,9 @@ impl Sidebar { if let Some(thread_switcher) = &self.thread_switcher { thread_switcher.update(cx, |switcher, cx| { if select_last { - switcher.select_last(cx); + switcher.select_previous(&SelectPrevious, window, cx); } else { - switcher.cycle_selection(cx); + switcher.select_next(&SelectNext, window, cx); } }); return; diff --git a/crates/sidebar/src/sidebar_tests.rs b/crates/sidebar/src/sidebar_tests.rs index cf1cb8348fb..371dd517527 100644 --- a/crates/sidebar/src/sidebar_tests.rs +++ b/crates/sidebar/src/sidebar_tests.rs @@ -10588,12 +10588,12 @@ async fn test_thread_switcher_ordering(cx: &mut TestAppContext) { let switcher = sidebar.thread_switcher.as_ref().unwrap(); assert_eq!(switcher.read(cx).selected_index(), 1); }); - sidebar.update_in(cx, |sidebar, _window, cx| { + sidebar.update_in(cx, |sidebar, window, cx| { sidebar .thread_switcher .as_ref() .unwrap() - .update(cx, |s, cx| s.cycle_selection(cx)); + .update(cx, |s, cx| s.select_next(&menu::SelectNext, window, cx)); }); cx.run_until_parked(); assert_eq!(switcher_selected_id(&sidebar, cx), thread_id_c); @@ -10673,9 +10673,11 @@ async fn test_thread_switcher_ordering(cx: &mut TestAppContext) { vec![thread_id_a, thread_id_c, thread_id_b,], ); - sidebar.update_in(cx, |sidebar, _window, cx| { + sidebar.update_in(cx, |sidebar, window, cx| { let switcher = sidebar.thread_switcher.as_ref().unwrap(); - switcher.update(cx, |switcher, cx| switcher.cycle_selection(cx)); + switcher.update(cx, |switcher, cx| { + switcher.select_next(&menu::SelectNext, window, cx) + }); }); cx.run_until_parked(); @@ -10780,6 +10782,116 @@ async fn test_thread_switcher_ordering(cx: &mut TestAppContext) { cx.run_until_parked(); } +#[gpui::test] +async fn test_thread_switcher_menu_navigation_wraps_without_confirming(cx: &mut TestAppContext) { + let project = init_test_project_with_agent_panel("/my-project", cx).await; + let (multi_workspace, cx) = + cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx)); + let (sidebar, panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx); + + // Opened oldest first, so the switcher lists them as A, B, C. + let mut session_ids = Vec::new(); + for (day, title) in [(1, "Thread C"), (2, "Thread B"), (3, "Thread A")] { + let connection = StubAgentConnection::new(); + connection.set_next_prompt_updates(vec![acp_v1::SessionUpdate::AgentMessageChunk( + acp_v1::ContentChunk::new("Done".into()), + )]); + open_thread_with_connection(&panel, connection, cx); + send_message(&panel, cx); + let session_id = active_session_id(&panel, cx); + let timestamp = chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, day, 0, 0, 0).unwrap(); + save_thread_metadata( + session_id.clone(), + Some(title.into()), + timestamp, + Some(timestamp), + None, + &project, + cx, + ); + session_ids.push(session_id); + } + let [session_id_c, session_id_b, session_id_a] = + session_ids.try_into().expect("three threads were opened"); + + let selected_session_id = |cx: &mut gpui::VisualTestContext| -> acp::SessionId { + sidebar.read_with(cx, |sidebar, cx| { + let switcher = sidebar + .thread_switcher + .as_ref() + .expect("switcher should be open"); + switcher + .read(cx) + .selected_entry() + .and_then(|entry| match entry { + ThreadSwitcherEntry::Thread(entry) => entry.metadata.session_id.clone(), + ThreadSwitcherEntry::Terminal(_) => None, + }) + .expect("a thread should be selected") + }) + }; + let dispatch_to_switcher = |action: &dyn gpui::Action, cx: &mut gpui::VisualTestContext| { + sidebar.update_in(cx, |sidebar, window, cx| { + let switcher = sidebar + .thread_switcher + .as_ref() + .expect("switcher should be open"); + switcher + .focus_handle(cx) + .dispatch_action(action, window, cx); + }); + cx.run_until_parked(); + }; + + focus_sidebar(&sidebar, cx); + sidebar.update_in(cx, |sidebar, window, cx| { + sidebar.on_toggle_thread_switcher(&ToggleThreadSwitcher::default(), window, cx); + }); + cx.run_until_parked(); + assert_eq!(selected_session_id(cx), session_id_b); + + dispatch_to_switcher(&menu::SelectNext, cx); + assert_eq!(selected_session_id(cx), session_id_c); + sidebar.read_with(cx, |sidebar, _cx| { + assert!( + is_active_session(sidebar, &session_id_c), + "stepping should preview the newly selected thread" + ); + }); + + dispatch_to_switcher(&menu::SelectNext, cx); + assert_eq!(selected_session_id(cx), session_id_a); + + dispatch_to_switcher(&menu::SelectPrevious, cx); + assert_eq!(selected_session_id(cx), session_id_c); + + dispatch_to_switcher(&menu::SelectPrevious, cx); + assert_eq!(selected_session_id(cx), session_id_b); + + dispatch_to_switcher(&menu::SelectLast, cx); + assert_eq!(selected_session_id(cx), session_id_c); + + dispatch_to_switcher(&menu::SelectFirst, cx); + assert_eq!(selected_session_id(cx), session_id_a); + + sidebar.read_with(cx, |sidebar, _cx| { + assert!( + sidebar.thread_switcher.is_some(), + "navigating should not confirm or dismiss the switcher" + ); + assert!( + sidebar.thread_last_accessed.is_empty(), + "navigating should not record a thread access" + ); + }); + + dispatch_to_switcher(&menu::Confirm, cx); + sidebar.read_with(cx, |sidebar, _cx| { + assert!(sidebar.thread_switcher.is_none()); + assert!(is_active_session(sidebar, &session_id_a)); + }); +} + #[gpui::test] async fn test_archive_thread_keeps_metadata_but_hides_from_sidebar(cx: &mut TestAppContext) { let project = init_test_project("/my-project", cx).await; diff --git a/crates/sidebar/src/thread_switcher.rs b/crates/sidebar/src/thread_switcher.rs index 5ed4ced917c..a11e8128c9c 100644 --- a/crates/sidebar/src/thread_switcher.rs +++ b/crates/sidebar/src/thread_switcher.rs @@ -249,15 +249,26 @@ impl ThreadSwitcher { self.selected_index } - pub fn cycle_selection(&mut self, cx: &mut Context) { + pub fn select_next( + &mut self, + _: &menu::SelectNext, + _window: &mut gpui::Window, + cx: &mut Context, + ) { if self.entries.is_empty() { return; } self.selected_index = (self.selected_index + 1) % self.entries.len(); self.emit_preview(cx); + cx.notify(); } - pub fn select_last(&mut self, cx: &mut Context) { + pub fn select_previous( + &mut self, + _: &menu::SelectPrevious, + _window: &mut gpui::Window, + cx: &mut Context, + ) { if self.entries.is_empty() { return; } @@ -267,6 +278,25 @@ impl ThreadSwitcher { self.selected_index -= 1; } self.emit_preview(cx); + cx.notify(); + } + + fn select_first( + &mut self, + _: &menu::SelectFirst, + _window: &mut gpui::Window, + cx: &mut Context, + ) { + self.select_index(0, cx); + } + + fn select_last( + &mut self, + _: &menu::SelectLast, + _window: &mut gpui::Window, + cx: &mut Context, + ) { + self.select_index(self.entries.len().saturating_sub(1), cx); } fn emit_preview(&mut self, cx: &mut Context) { @@ -312,13 +342,13 @@ impl ThreadSwitcher { fn toggle( &mut self, action: &ToggleThreadSwitcher, - _window: &mut gpui::Window, + window: &mut gpui::Window, cx: &mut Context, ) { if action.select_last { - self.select_last(cx); + self.select_previous(&menu::SelectPrevious, window, cx); } else { - self.cycle_selection(cx); + self.select_next(&menu::SelectNext, window, cx); } } @@ -367,6 +397,10 @@ impl Render for ThreadSwitcher { .on_action(cx.listener(Self::confirm)) .on_action(cx.listener(Self::cancel)) .on_action(cx.listener(Self::toggle)) + .on_action(cx.listener(Self::select_next)) + .on_action(cx.listener(Self::select_previous)) + .on_action(cx.listener(Self::select_first)) + .on_action(cx.listener(Self::select_last)) .child( v_flex() .id("thread-switcher-list") diff --git a/docs/src/ai/parallel-agents.md b/docs/src/ai/parallel-agents.md index a23c1d4b86d..e8f1fbd7843 100644 --- a/docs/src/ai/parallel-agents.md +++ b/docs/src/ai/parallel-agents.md @@ -29,7 +29,7 @@ The “Threads Sidebar Auto Open” setting controls whether opening a folder in Click any thread in the sidebar to switch to it. The Agent Panel updates to show that thread's conversation. -For quick switching without opening the sidebar, use the thread switcher: press {#kb agents_sidebar::ToggleThreadSwitcher} to cycle forward through recent threads, or hold `Shift` while pressing that binding to go backward. This works from both the Agent Panel and the Threads Sidebar. +For quick switching without opening the sidebar, use the thread switcher: press {#kb agents_sidebar::ToggleThreadSwitcher} to cycle forward through recent threads, or hold `Shift` while pressing that binding to go backward. While the switcher is open, `Ctrl+Down` and `Ctrl+Up` also move the selection one thread at a time, as do the arrow keys when no modifier is held. This works from both the Agent Panel and the Threads Sidebar. ### Thread History {#threads-history}