language_tools: Allow horizontal scrolling on key context view (#66528)

## Summary

Update `dev: open key context view` in order to add a horizontal
scrollbar to the view, as longer predicates could end up hidden if there
wasn't enough width to display them in the view.

Closes #66044 

## Testing

Tested manually, as shown in the recordings below ▼

<details>
  <summary>Before</summary>


https://github.com/user-attachments/assets/85c43b53-e6d7-4b2f-8499-4800a7ac5c33
</details>

<details>
  <summary>After</summary>


https://github.com/user-attachments/assets/b07b3247-9e72-49c8-8768-5964cc57b21f
</details>

## 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:

- Improved `dev: open key context view` in order to allow scrolling
through the results list
This commit is contained in:
Dino
2026-11-09 11:49:00 +00:00
committed by GitHub
parent 779e9fc437
commit 62acd36cec
+42 -20
View File
@@ -1,10 +1,11 @@
use gpui::{
Action, App, AppContext as _, Entity, EventEmitter, FocusHandle, Focusable,
KeyBindingContextPredicate, KeyContext, Keystroke, MouseButton, Render, Subscription, Task,
KeyBindingContextPredicate, KeyContext, Keystroke, MouseButton, Render, ScrollHandle,
Subscription, Task,
};
use itertools::Itertools;
use serde_json::json;
use ui::{Button, ButtonStyle};
use ui::{Button, ButtonStyle, ScrollAxes, Scrollbars, WithScrollbar};
use ui::{
ButtonCommon, Clickable, Context, FluentBuilder, InteractiveElement, Label, LabelCommon,
LabelSize, ParentElement, SharedString, StatefulInteractiveElement, Styled, Window, div,
@@ -34,6 +35,7 @@ struct KeyContextView {
last_possibilities: Vec<(SharedString, SharedString, Option<bool>)>,
context_stack: Vec<KeyContext>,
focus_handle: FocusHandle,
scroll_handle: ScrollHandle,
_subscriptions: [Subscription; 2],
}
@@ -101,6 +103,7 @@ impl KeyContextView {
});
Self {
scroll_handle: ScrollHandle::new(),
context_stack: Vec::new(),
pending_keystrokes: None,
last_keystrokes: None,
@@ -168,10 +171,10 @@ impl Item for KeyContextView {
}
impl Render for KeyContextView {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl ui::IntoElement {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl ui::IntoElement {
use itertools::Itertools;
let key_equivalents = cx.keyboard_mapper().get_key_equivalents();
let key_equivalents = cx.keyboard_mapper().get_key_equivalents().cloned();
v_flex()
.id("key-context-view")
.overflow_scroll()
@@ -261,22 +264,41 @@ impl Render for KeyContextView {
})
.when_some(self.last_keystrokes.as_ref(), |el, keystrokes| {
el.child(Label::new(format!("Typed: {}", keystrokes)).ml_4())
.children(
self.last_possibilities
.iter()
.map(|(name, predicate, state)| {
let (text, color) = match state {
Some(true) => ("(match)", ui::Color::Success),
Some(false) => ("(low precedence)", ui::Color::Hint),
None => ("(no match)", ui::Color::Error),
};
h_flex()
.gap_2()
.ml_8()
.child(div().min_w(px(200.)).child(Label::new(name.clone())))
.child(Label::new(predicate.clone()))
.child(Label::new(text).color(color))
}),
.child(
div()
.w_full()
.pr_4()
.child(
v_flex()
.id("key-context-possibilities")
.track_scroll(&self.scroll_handle)
.pb_4()
.pl_8()
.overflow_x_scroll()
.items_start()
.children(
self.last_possibilities
.iter()
.map(|(name, predicate, state)| {
let (text, color) = match state {
Some(true) => ("(match)", ui::Color::Success),
Some(false) => ("(low precedence)", ui::Color::Hint),
None => ("(no match)", ui::Color::Error),
};
h_flex()
.gap_2()
.child(div().min_w(px(200.)).flex_shrink_0().child(Label::new(name.clone()).single_line()))
.child(Label::new(predicate.clone()).single_line().flex_shrink_0())
.child(Label::new(text).color(color).single_line().flex_shrink_0())
}),
)
)
.custom_scrollbars(
Scrollbars::new(ScrollAxes::Horizontal)
.tracked_scroll_handle(&self.scroll_handle),
window,
cx,
)
)
})
.when_some(key_equivalents, |el, key_equivalents| {