Fix context_stack race in KeyContextView (#29324)

cc @notpeter

Before this change we used our own copy of `cx.key_context()` when
matching.
This led to races where the context queried could be either before (or
after) the
context used in dispatching.

To avoid the race, gpui now passes out the context stack actually used
instead.

Release Notes:

- Fixed a bug where the Key Context View could show the incorrect
context,
  causing confusing results.
This commit is contained in:
Conrad Irwin 2025-04-23 23:34:39 -06:00 committed by GitHub
parent 9d10489607
commit c0f8e0f605
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 52 additions and 23 deletions

View file

@ -41,17 +41,17 @@ struct KeyContextView {
impl KeyContextView {
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let sub1 = cx.observe_keystrokes(|this, e, window, cx| {
let sub1 = cx.observe_keystrokes(|this, e, _, cx| {
let mut pending = this.pending_keystrokes.take().unwrap_or_default();
pending.push(e.keystroke.clone());
let mut possibilities = cx.all_bindings_for_input(&pending);
possibilities.reverse();
this.context_stack = window.context_stack();
this.last_keystrokes = Some(
json!(pending.iter().map(|p| p.unparse()).join(" "))
.to_string()
.into(),
);
this.context_stack = e.context_stack.clone();
this.last_possibilities = possibilities
.into_iter()
.map(|binding| {
@ -89,6 +89,7 @@ impl KeyContextView {
)
})
.collect();
cx.notify();
});
let sub2 = cx.observe_pending_input(window, |this, window, cx| {
this.pending_keystrokes = window
@ -237,7 +238,7 @@ impl Render for KeyContextView {
.mt_8(),
)
.children({
window.context_stack().into_iter().enumerate().map(|(i, context)| {
self.context_stack.iter().enumerate().map(|(i, context)| {
let primary = context.primary().map(|e| e.key.clone()).unwrap_or_default();
let secondary = context
.secondary()