mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-19 06:59:22 +01:00
sidebar: Add keyboard navigation to the thread switcher (#66951)
## 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.
This commit is contained in:
@@ -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",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -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",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -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",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -249,15 +249,26 @@ impl ThreadSwitcher {
|
||||
self.selected_index
|
||||
}
|
||||
|
||||
pub fn cycle_selection(&mut self, cx: &mut Context<Self>) {
|
||||
pub fn select_next(
|
||||
&mut self,
|
||||
_: &menu::SelectNext,
|
||||
_window: &mut gpui::Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
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<Self>) {
|
||||
pub fn select_previous(
|
||||
&mut self,
|
||||
_: &menu::SelectPrevious,
|
||||
_window: &mut gpui::Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
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>,
|
||||
) {
|
||||
self.select_index(0, cx);
|
||||
}
|
||||
|
||||
fn select_last(
|
||||
&mut self,
|
||||
_: &menu::SelectLast,
|
||||
_window: &mut gpui::Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
self.select_index(self.entries.len().saturating_sub(1), cx);
|
||||
}
|
||||
|
||||
fn emit_preview(&mut self, cx: &mut Context<Self>) {
|
||||
@@ -312,13 +342,13 @@ impl ThreadSwitcher {
|
||||
fn toggle(
|
||||
&mut self,
|
||||
action: &ToggleThreadSwitcher,
|
||||
_window: &mut gpui::Window,
|
||||
window: &mut gpui::Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
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")
|
||||
|
||||
@@ -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}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user