Use correct context path for focused element in WindowContext::bindings_for_action (#18843)

Previously, we were reaching in and using the context_stack on the dispatch tree, which was incorrect.

/cc @as-cii 
/cc @ConradIrwin

Release Notes:

- N/A

---------

Co-authored-by: Michael Sloan <michael@zed.dev>
This commit is contained in:
Nathan Sobo 2024-11-05 16:42:50 -07:00 committed by GitHub
parent 7931342455
commit e47b305ca7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 34 additions and 34 deletions

View file

@ -3683,22 +3683,11 @@ impl<'a> WindowContext<'a> {
/// Returns all available actions for the focused element.
pub fn available_actions(&self) -> Vec<Box<dyn Action>> {
let node_id = self
.window
.focus
.and_then(|focus_id| {
self.window
.rendered_frame
.dispatch_tree
.focusable_node_id(focus_id)
})
.unwrap_or_else(|| self.window.rendered_frame.dispatch_tree.root_node_id());
let mut actions = self
.window
.rendered_frame
.dispatch_tree
.available_actions(node_id);
.available_actions(self.focused_node_id());
for action_type in self.global_action_listeners.keys() {
if let Err(ix) = actions.binary_search_by_key(action_type, |a| a.as_any().type_id()) {
let action = self.actions.build_action_type(action_type).ok();
@ -3712,13 +3701,9 @@ impl<'a> WindowContext<'a> {
/// Returns key bindings that invoke the given action on the currently focused element.
pub fn bindings_for_action(&self, action: &dyn Action) -> Vec<KeyBinding> {
self.window
.rendered_frame
.dispatch_tree
.bindings_for_action(
action,
&self.window.rendered_frame.dispatch_tree.context_stack,
)
let dispatch_tree = &self.window.rendered_frame.dispatch_tree;
dispatch_tree
.bindings_for_action(action, &dispatch_tree.context_path(self.focused_node_id()))
}
/// Returns key bindings that invoke the given action on the currently focused element.
@ -3734,15 +3719,23 @@ impl<'a> WindowContext<'a> {
) -> Vec<KeyBinding> {
let dispatch_tree = &self.window.rendered_frame.dispatch_tree;
let Some(node_id) = dispatch_tree.focusable_node_id(focus_handle.id) else {
return vec![];
};
let context_stack: Vec<_> = dispatch_tree
.dispatch_path(node_id)
.into_iter()
.filter_map(|node_id| dispatch_tree.node(node_id).context.clone())
.collect();
dispatch_tree.bindings_for_action(action, &context_stack)
if let Some(node_id) = dispatch_tree.focusable_node_id(focus_handle.id) {
dispatch_tree.bindings_for_action(action, &dispatch_tree.context_path(node_id))
} else {
vec![]
}
}
fn focused_node_id(&self) -> DispatchNodeId {
self.window
.focus
.and_then(|focus_id| {
self.window
.rendered_frame
.dispatch_tree
.focusable_node_id(focus_id)
})
.unwrap_or_else(|| self.window.rendered_frame.dispatch_tree.root_node_id())
}
/// Returns a generic event listener that invokes the given listener with the view and context associated with the given view handle.