This commit is contained in:
Antonio Scandurra 2024-01-09 17:34:57 +01:00
parent 84b05d6c05
commit 3bb29acd26
2 changed files with 30 additions and 31 deletions

View File

@ -70,18 +70,35 @@ impl DispatchTree {
self.keystroke_matchers.clear();
}
pub fn push_node(&mut self, context: Option<KeyContext>) {
pub fn push_node(
&mut self,
context: Option<KeyContext>,
focus_id: Option<FocusId>,
view_id: Option<EntityId>,
) {
let parent = self.node_stack.last().copied();
let node_id = DispatchNodeId(self.nodes.len());
self.nodes.push(DispatchNode {
parent,
focus_id,
view_id,
..Default::default()
});
self.node_stack.push(node_id);
if let Some(context) = context {
self.active_node().context = Some(context.clone());
self.context_stack.push(context);
}
if let Some(focus_id) = focus_id {
self.focusable_node_ids.insert(focus_id, node_id);
}
if let Some(view_id) = view_id {
self.view_stack.push(view_id);
self.view_node_ids.insert(view_id, node_id);
}
}
pub fn pop_node(&mut self) {
@ -96,14 +113,11 @@ impl DispatchTree {
}
fn move_node(&mut self, source_node: &mut DispatchNode) {
self.push_node(source_node.context.take());
if let Some(focus_id) = source_node.focus_id {
self.make_focusable(focus_id);
}
if let Some(view_id) = source_node.view_id {
self.associate_view(view_id);
}
self.push_node(
source_node.context.take(),
source_node.focus_id,
source_node.view_id,
);
let target_node = self.active_node();
target_node.key_listeners = mem::take(&mut source_node.key_listeners);
target_node.action_listeners = mem::take(&mut source_node.action_listeners);
@ -195,19 +209,6 @@ impl DispatchTree {
});
}
pub fn make_focusable(&mut self, focus_id: FocusId) {
let node_id = self.active_node_id();
self.active_node().focus_id = Some(focus_id);
self.focusable_node_ids.insert(focus_id, node_id);
}
pub fn associate_view(&mut self, view_id: EntityId) {
let node_id = self.active_node_id();
self.active_node().view_id = Some(view_id);
self.view_node_ids.insert(view_id, node_id);
self.view_stack.push(view_id);
}
pub fn focus_contains(&self, parent: FocusId, child: FocusId) -> bool {
if parent == child {
return true;

View File

@ -1866,13 +1866,12 @@ impl<'a> WindowContext<'a> {
f: impl FnOnce(Option<FocusHandle>, &mut Self) -> R,
) -> R {
let window = &mut self.window;
window.next_frame.dispatch_tree.push_node(context.clone());
if let Some(focus_handle) = focus_handle.as_ref() {
window
.next_frame
.dispatch_tree
.make_focusable(focus_handle.id);
}
let focus_id = focus_handle.as_ref().map(|handle| handle.id);
window
.next_frame
.dispatch_tree
.push_node(context.clone(), focus_id, None);
let result = f(focus_handle, self);
self.window.next_frame.dispatch_tree.pop_node();
@ -2165,11 +2164,10 @@ pub trait BorrowWindow: BorrowMut<Window> + BorrowMut<AppContext> {
}
fn with_view_id<R>(&mut self, view_id: EntityId, f: impl FnOnce(&mut Self) -> R) -> R {
self.window_mut().next_frame.dispatch_tree.push_node(None);
self.window_mut()
.next_frame
.dispatch_tree
.associate_view(view_id);
.push_node(None, None, Some(view_id));
let result = f(self);
self.window_mut().next_frame.dispatch_tree.pop_node();
result