Add tab switcher (#7987)

The Tab Switcher implementation (#7653):
- `ctrl-tab` opens the Tab Switcher and moves selection to the
previously selcted tab. It also cycles selection forward.
- `ctrl-shift-tab` opens the Tab Switcher and moves selection to the
last tab in the list. It also cycles selection backward.
- Tab is selected and the Tab Switcher is closed on the shortcut
modifier key (`ctrl` by default) release.
- List items are in reverse activation history order.
- The list reacts to the item changes in background (new tab, tab
closed, tab title changed etc.)

Intentionally not in scope of this PR:
- File icons
- Close buttons

I will come back to these features. I think they need to be implemented
in separate PRs, and be synchronized with changes in how tabs are
rendered, to reuse the code as it's done in the current implementation.
The Tab Switcher looks usable even without them.

Known Issues:

Tab Switcher doesn't react to mouse click on a list item. It's not a tab
switcher specific problem, it looks like ctrl-clicks are not handled the
same way in Zed as cmd-clicks. For instance, menu items can be activated
with cmd-click, but don't react to ctrl-click. Since the Tab Switcher's
default keybinding is `ctrl-tab`, the user can only click an item with
`ctrl` pushed down, thus preventing `on_click()` from firing.

fixes #7653, #7321

Release Notes:

- Added Tab Switcher which is accessible via `ctrl-tab` and
`ctrl-shift-tab` (#7653) (#7321)

Related issues:

- Unblocks #7356, I hope 😄

How it looks and works (it's only `ctrl-tab`'s and `ctrl-shift-tab`'s,
no `enter`'s or mouse clicks):


https://github.com/zed-industries/zed/assets/2101250/4ad4ec6a-5314-481b-8b35-7ac85e43eb92

---------

Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
Co-authored-by: Mikayla Maki <mikayla@zed.dev>
This commit is contained in:
Andrew Lygin 2024-03-27 21:15:08 +03:00 committed by GitHub
parent 9c22009e7b
commit 894b39a918
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 715 additions and 56 deletions

View file

@ -28,8 +28,13 @@ impl Head {
Self::Editor(editor)
}
pub fn empty(cx: &mut WindowContext) -> Self {
Self::Empty(cx.new_view(|cx| EmptyHead::new(cx)))
pub fn empty<V: 'static>(
blur_handler: impl FnMut(&mut V, &mut ViewContext<'_, V>) + 'static,
cx: &mut ViewContext<V>,
) -> Self {
let head = cx.new_view(|cx| EmptyHead::new(cx));
cx.on_blur(&head.focus_handle(cx), blur_handler).detach();
Self::Empty(head)
}
}

View file

@ -2,8 +2,8 @@ use anyhow::Result;
use editor::{scroll::Autoscroll, Editor};
use gpui::{
div, list, prelude::*, uniform_list, AnyElement, AppContext, ClickEvent, DismissEvent,
EventEmitter, FocusHandle, FocusableView, Length, ListState, Render, Task,
UniformListScrollHandle, View, ViewContext, WindowContext,
EventEmitter, FocusHandle, FocusableView, Length, ListState, MouseButton, MouseUpEvent, Render,
Task, UniformListScrollHandle, View, ViewContext, WindowContext,
};
use head::Head;
use std::{sync::Arc, time::Duration};
@ -116,7 +116,7 @@ impl<D: PickerDelegate> Picker<D> {
/// A picker, which displays its matches using `gpui::uniform_list`, all matches should have the same height.
/// If `PickerDelegate::render_match` can return items with different heights, use `Picker::list`.
pub fn nonsearchable_uniform_list(delegate: D, cx: &mut ViewContext<Self>) -> Self {
let head = Head::empty(cx);
let head = Head::empty(Self::on_empty_head_blur, cx);
Self::new(delegate, ContainerKind::UniformList, head, cx)
}
@ -313,6 +313,13 @@ impl<D: PickerDelegate> Picker<D> {
}
}
fn on_empty_head_blur(&mut self, cx: &mut ViewContext<Self>) {
let Head::Empty(_) = &self.head else {
panic!("unexpected call");
};
self.cancel(&menu::Cancel, cx);
}
pub fn refresh(&mut self, cx: &mut ViewContext<Self>) {
let query = self.query(cx);
self.update_matches(query, cx);
@ -394,6 +401,16 @@ impl<D: PickerDelegate> Picker<D> {
.on_click(cx.listener(move |this, event: &ClickEvent, cx| {
this.handle_click(ix, event.down.modifiers.command, cx)
}))
// As of this writing, GPUI intercepts `ctrl-[mouse-event]`s on macOS
// and produces right mouse button events. This matches platforms norms
// but means that UIs which depend on holding ctrl down (such as the tab
// switcher) can't be clicked on. Hence, this handler.
.on_mouse_up(
MouseButton::Right,
cx.listener(move |this, event: &MouseUpEvent, cx| {
this.handle_click(ix, event.modifiers.command, cx)
}),
)
.children(
self.delegate
.render_match(ix, ix == self.delegate.selected_index(), cx),