Merge branch 'main' into share-project

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld
2021-12-20 11:42:59 -08:00
co-authored by Nathan Sobo
16 changed files with 423 additions and 306 deletions
+18 -19
View File
@@ -667,7 +667,7 @@ pub struct MutableAppContext {
assets: Arc<AssetCache>,
cx: AppContext,
actions: HashMap<TypeId, HashMap<TypeId, Vec<Box<ActionCallback>>>>,
global_actions: HashMap<TypeId, Vec<Box<GlobalActionCallback>>>,
global_actions: HashMap<TypeId, Box<GlobalActionCallback>>,
keystroke_matcher: keymap::Matcher,
next_entity_id: usize,
next_window_id: usize,
@@ -838,10 +838,13 @@ impl MutableAppContext {
handler(action, cx);
});
self.global_actions
.entry(TypeId::of::<A>())
.or_default()
.push(handler);
if self
.global_actions
.insert(TypeId::of::<A>(), handler)
.is_some()
{
panic!("registered multiple global handlers for the same action type");
}
}
pub fn window_ids(&self) -> impl Iterator<Item = usize> + '_ {
@@ -1125,7 +1128,7 @@ impl MutableAppContext {
}
if !halted_dispatch {
this.dispatch_global_action_any(action);
halted_dispatch = this.dispatch_global_action_any(action);
}
halted_dispatch
})
@@ -1135,13 +1138,14 @@ impl MutableAppContext {
self.dispatch_global_action_any(&action);
}
fn dispatch_global_action_any(&mut self, action: &dyn AnyAction) {
fn dispatch_global_action_any(&mut self, action: &dyn AnyAction) -> bool {
self.update(|this| {
if let Some((name, mut handlers)) = this.global_actions.remove_entry(&action.id()) {
for handler in handlers.iter_mut().rev() {
handler(action, this);
}
this.global_actions.insert(name, handlers);
if let Some((name, mut handler)) = this.global_actions.remove_entry(&action.id()) {
handler(action, this);
this.global_actions.insert(name, handler);
true
} else {
false
}
})
}
@@ -3988,12 +3992,7 @@ mod tests {
let actions_clone = actions.clone();
cx.add_global_action(move |_: &Action, _: &mut MutableAppContext| {
actions_clone.borrow_mut().push("global a".to_string());
});
let actions_clone = actions.clone();
cx.add_global_action(move |_: &Action, _: &mut MutableAppContext| {
actions_clone.borrow_mut().push("global b".to_string());
actions_clone.borrow_mut().push("global".to_string());
});
let actions_clone = actions.clone();
@@ -4049,7 +4048,7 @@ mod tests {
assert_eq!(
*actions.borrow(),
vec!["4 d", "4 c", "3 b", "3 a", "2 d", "2 c", "global b", "global a"]
vec!["4 d", "4 c", "3 b", "3 a", "2 d", "2 c", "global"]
);
}