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(); 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 parent = self.node_stack.last().copied();
let node_id = DispatchNodeId(self.nodes.len()); let node_id = DispatchNodeId(self.nodes.len());
self.nodes.push(DispatchNode { self.nodes.push(DispatchNode {
parent, parent,
focus_id,
view_id,
..Default::default() ..Default::default()
}); });
self.node_stack.push(node_id); self.node_stack.push(node_id);
if let Some(context) = context { if let Some(context) = context {
self.active_node().context = Some(context.clone()); self.active_node().context = Some(context.clone());
self.context_stack.push(context); 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) { pub fn pop_node(&mut self) {
@ -96,14 +113,11 @@ impl DispatchTree {
} }
fn move_node(&mut self, source_node: &mut DispatchNode) { fn move_node(&mut self, source_node: &mut DispatchNode) {
self.push_node(source_node.context.take()); self.push_node(
if let Some(focus_id) = source_node.focus_id { source_node.context.take(),
self.make_focusable(focus_id); source_node.focus_id,
} source_node.view_id,
if let Some(view_id) = source_node.view_id { );
self.associate_view(view_id);
}
let target_node = self.active_node(); let target_node = self.active_node();
target_node.key_listeners = mem::take(&mut source_node.key_listeners); target_node.key_listeners = mem::take(&mut source_node.key_listeners);
target_node.action_listeners = mem::take(&mut source_node.action_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 { pub fn focus_contains(&self, parent: FocusId, child: FocusId) -> bool {
if parent == child { if parent == child {
return true; return true;

View File

@ -1866,13 +1866,12 @@ impl<'a> WindowContext<'a> {
f: impl FnOnce(Option<FocusHandle>, &mut Self) -> R, f: impl FnOnce(Option<FocusHandle>, &mut Self) -> R,
) -> R { ) -> R {
let window = &mut self.window; let window = &mut self.window;
window.next_frame.dispatch_tree.push_node(context.clone()); let focus_id = focus_handle.as_ref().map(|handle| handle.id);
if let Some(focus_handle) = focus_handle.as_ref() { window
window .next_frame
.next_frame .dispatch_tree
.dispatch_tree .push_node(context.clone(), focus_id, None);
.make_focusable(focus_handle.id);
}
let result = f(focus_handle, self); let result = f(focus_handle, self);
self.window.next_frame.dispatch_tree.pop_node(); 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 { 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() self.window_mut()
.next_frame .next_frame
.dispatch_tree .dispatch_tree
.associate_view(view_id); .push_node(None, None, Some(view_id));
let result = f(self); let result = f(self);
self.window_mut().next_frame.dispatch_tree.pop_node(); self.window_mut().next_frame.dispatch_tree.pop_node();
result result