Add capture phase for action dispatch
Just like the DOM, we now bubble events down the tree during a capture phase before bubbling them back up.
This commit is contained in:
parent
7d119dcd54
commit
71241b1fb8
1 changed files with 118 additions and 40 deletions
|
@ -722,6 +722,7 @@ pub struct MutableAppContext {
|
||||||
foreground_platform: Rc<dyn platform::ForegroundPlatform>,
|
foreground_platform: Rc<dyn platform::ForegroundPlatform>,
|
||||||
assets: Arc<AssetCache>,
|
assets: Arc<AssetCache>,
|
||||||
cx: AppContext,
|
cx: AppContext,
|
||||||
|
capture_actions: HashMap<TypeId, HashMap<TypeId, Vec<Box<ActionCallback>>>>,
|
||||||
actions: HashMap<TypeId, HashMap<TypeId, Vec<Box<ActionCallback>>>>,
|
actions: HashMap<TypeId, HashMap<TypeId, Vec<Box<ActionCallback>>>>,
|
||||||
global_actions: HashMap<TypeId, Box<GlobalActionCallback>>,
|
global_actions: HashMap<TypeId, Box<GlobalActionCallback>>,
|
||||||
keystroke_matcher: keymap::Matcher,
|
keystroke_matcher: keymap::Matcher,
|
||||||
|
@ -768,6 +769,7 @@ impl MutableAppContext {
|
||||||
font_cache,
|
font_cache,
|
||||||
platform,
|
platform,
|
||||||
},
|
},
|
||||||
|
capture_actions: HashMap::new(),
|
||||||
actions: HashMap::new(),
|
actions: HashMap::new(),
|
||||||
global_actions: HashMap::new(),
|
global_actions: HashMap::new(),
|
||||||
keystroke_matcher: keymap::Matcher::default(),
|
keystroke_matcher: keymap::Matcher::default(),
|
||||||
|
@ -857,7 +859,25 @@ impl MutableAppContext {
|
||||||
.map(|debug_elements| debug_elements(&self.cx))
|
.map(|debug_elements| debug_elements(&self.cx))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_action<A, V, F>(&mut self, mut handler: F)
|
pub fn add_action<A, V, F>(&mut self, handler: F)
|
||||||
|
where
|
||||||
|
A: Action,
|
||||||
|
V: View,
|
||||||
|
F: 'static + FnMut(&mut V, &A, &mut ViewContext<V>),
|
||||||
|
{
|
||||||
|
self.add_action_internal(handler, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn capture_action<A, V, F>(&mut self, handler: F)
|
||||||
|
where
|
||||||
|
A: Action,
|
||||||
|
V: View,
|
||||||
|
F: 'static + FnMut(&mut V, &A, &mut ViewContext<V>),
|
||||||
|
{
|
||||||
|
self.add_action_internal(handler, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_action_internal<A, V, F>(&mut self, mut handler: F, capture: bool)
|
||||||
where
|
where
|
||||||
A: Action,
|
A: Action,
|
||||||
V: View,
|
V: View,
|
||||||
|
@ -881,7 +901,13 @@ impl MutableAppContext {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
self.actions
|
let actions = if capture {
|
||||||
|
&mut self.capture_actions
|
||||||
|
} else {
|
||||||
|
&mut self.actions
|
||||||
|
};
|
||||||
|
|
||||||
|
actions
|
||||||
.entry(TypeId::of::<V>())
|
.entry(TypeId::of::<V>())
|
||||||
.or_default()
|
.or_default()
|
||||||
.entry(TypeId::of::<A>())
|
.entry(TypeId::of::<A>())
|
||||||
|
@ -1169,29 +1195,33 @@ impl MutableAppContext {
|
||||||
) -> bool {
|
) -> bool {
|
||||||
self.update(|this| {
|
self.update(|this| {
|
||||||
this.halt_action_dispatch = false;
|
this.halt_action_dispatch = false;
|
||||||
for view_id in path.iter().rev() {
|
for (capture_phase, view_id) in path
|
||||||
if let Some(mut view) = this.cx.views.remove(&(window_id, *view_id)) {
|
.iter()
|
||||||
|
.map(|view_id| (true, *view_id))
|
||||||
|
.chain(path.iter().rev().map(|view_id| (false, *view_id)))
|
||||||
|
{
|
||||||
|
if let Some(mut view) = this.cx.views.remove(&(window_id, view_id)) {
|
||||||
let type_id = view.as_any().type_id();
|
let type_id = view.as_any().type_id();
|
||||||
|
|
||||||
if let Some((name, mut handlers)) = this
|
if let Some((name, mut handlers)) = this
|
||||||
.actions
|
.actions_mut(capture_phase)
|
||||||
.get_mut(&type_id)
|
.get_mut(&type_id)
|
||||||
.and_then(|h| h.remove_entry(&action.id()))
|
.and_then(|h| h.remove_entry(&action.id()))
|
||||||
{
|
{
|
||||||
for handler in handlers.iter_mut().rev() {
|
for handler in handlers.iter_mut().rev() {
|
||||||
this.halt_action_dispatch = true;
|
this.halt_action_dispatch = true;
|
||||||
handler(view.as_mut(), action, this, window_id, *view_id);
|
handler(view.as_mut(), action, this, window_id, view_id);
|
||||||
if this.halt_action_dispatch {
|
if this.halt_action_dispatch {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.actions
|
this.actions_mut(capture_phase)
|
||||||
.get_mut(&type_id)
|
.get_mut(&type_id)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.insert(name, handlers);
|
.insert(name, handlers);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.cx.views.insert((window_id, *view_id), view);
|
this.cx.views.insert((window_id, view_id), view);
|
||||||
|
|
||||||
if this.halt_action_dispatch {
|
if this.halt_action_dispatch {
|
||||||
break;
|
break;
|
||||||
|
@ -1206,6 +1236,17 @@ impl MutableAppContext {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn actions_mut(
|
||||||
|
&mut self,
|
||||||
|
capture_phase: bool,
|
||||||
|
) -> &mut HashMap<TypeId, HashMap<TypeId, Vec<Box<ActionCallback>>>> {
|
||||||
|
if capture_phase {
|
||||||
|
&mut self.capture_actions
|
||||||
|
} else {
|
||||||
|
&mut self.actions
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn dispatch_global_action<A: Action>(&mut self, action: A) {
|
pub fn dispatch_global_action<A: Action>(&mut self, action: A) {
|
||||||
self.dispatch_global_action_any(&action);
|
self.dispatch_global_action_any(&action);
|
||||||
}
|
}
|
||||||
|
@ -4320,40 +4361,58 @@ mod tests {
|
||||||
|
|
||||||
let actions = Rc::new(RefCell::new(Vec::new()));
|
let actions = Rc::new(RefCell::new(Vec::new()));
|
||||||
|
|
||||||
let actions_clone = actions.clone();
|
{
|
||||||
cx.add_global_action(move |_: &Action, _: &mut MutableAppContext| {
|
let actions = actions.clone();
|
||||||
actions_clone.borrow_mut().push("global".to_string());
|
cx.add_global_action(move |_: &Action, _: &mut MutableAppContext| {
|
||||||
});
|
actions.borrow_mut().push("global".to_string());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let actions_clone = actions.clone();
|
{
|
||||||
cx.add_action(move |view: &mut ViewA, action: &Action, cx| {
|
let actions = actions.clone();
|
||||||
assert_eq!(action.0, "bar");
|
cx.add_action(move |view: &mut ViewA, action: &Action, cx| {
|
||||||
cx.propagate_action();
|
assert_eq!(action.0, "bar");
|
||||||
actions_clone.borrow_mut().push(format!("{} a", view.id));
|
cx.propagate_action();
|
||||||
});
|
actions.borrow_mut().push(format!("{} a", view.id));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let actions_clone = actions.clone();
|
{
|
||||||
cx.add_action(move |view: &mut ViewA, _: &Action, cx| {
|
let actions = actions.clone();
|
||||||
if view.id != 1 {
|
cx.add_action(move |view: &mut ViewA, _: &Action, cx| {
|
||||||
cx.add_view(|cx| {
|
if view.id != 1 {
|
||||||
cx.propagate_action(); // Still works on a nested ViewContext
|
cx.add_view(|cx| {
|
||||||
ViewB { id: 5 }
|
cx.propagate_action(); // Still works on a nested ViewContext
|
||||||
});
|
ViewB { id: 5 }
|
||||||
}
|
});
|
||||||
actions_clone.borrow_mut().push(format!("{} b", view.id));
|
}
|
||||||
});
|
actions.borrow_mut().push(format!("{} b", view.id));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let actions_clone = actions.clone();
|
{
|
||||||
cx.add_action(move |view: &mut ViewB, _: &Action, cx| {
|
let actions = actions.clone();
|
||||||
cx.propagate_action();
|
cx.add_action(move |view: &mut ViewB, _: &Action, cx| {
|
||||||
actions_clone.borrow_mut().push(format!("{} c", view.id));
|
cx.propagate_action();
|
||||||
});
|
actions.borrow_mut().push(format!("{} c", view.id));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let actions_clone = actions.clone();
|
{
|
||||||
cx.add_action(move |view: &mut ViewB, _: &Action, cx| {
|
let actions = actions.clone();
|
||||||
cx.propagate_action();
|
cx.add_action(move |view: &mut ViewB, _: &Action, cx| {
|
||||||
actions_clone.borrow_mut().push(format!("{} d", view.id));
|
cx.propagate_action();
|
||||||
});
|
actions.borrow_mut().push(format!("{} d", view.id));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let actions = actions.clone();
|
||||||
|
cx.capture_action(move |view: &mut ViewA, _: &Action, cx| {
|
||||||
|
cx.propagate_action();
|
||||||
|
actions.borrow_mut().push(format!("{} capture", view.id));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let (window_id, view_1) = cx.add_window(Default::default(), |_| ViewA { id: 1 });
|
let (window_id, view_1) = cx.add_window(Default::default(), |_| ViewA { id: 1 });
|
||||||
let view_2 = cx.add_view(window_id, |_| ViewB { id: 2 });
|
let view_2 = cx.add_view(window_id, |_| ViewB { id: 2 });
|
||||||
|
@ -4368,7 +4427,17 @@ mod tests {
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
*actions.borrow(),
|
*actions.borrow(),
|
||||||
vec!["4 d", "4 c", "3 b", "3 a", "2 d", "2 c", "1 b"]
|
vec![
|
||||||
|
"1 capture",
|
||||||
|
"3 capture",
|
||||||
|
"4 d",
|
||||||
|
"4 c",
|
||||||
|
"3 b",
|
||||||
|
"3 a",
|
||||||
|
"2 d",
|
||||||
|
"2 c",
|
||||||
|
"1 b"
|
||||||
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
// Remove view_1, which doesn't propagate the action
|
// Remove view_1, which doesn't propagate the action
|
||||||
|
@ -4381,7 +4450,16 @@ mod tests {
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
*actions.borrow(),
|
*actions.borrow(),
|
||||||
vec!["4 d", "4 c", "3 b", "3 a", "2 d", "2 c", "global"]
|
vec![
|
||||||
|
"3 capture",
|
||||||
|
"4 d",
|
||||||
|
"4 c",
|
||||||
|
"3 b",
|
||||||
|
"3 a",
|
||||||
|
"2 d",
|
||||||
|
"2 c",
|
||||||
|
"global"
|
||||||
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue