From c848c70beac61de8f0a7eb90144f974e344b6765 Mon Sep 17 00:00:00 2001 From: Thomas Churchman Date: Fri, 27 Sep 2024 14:05:51 +0200 Subject: [PATCH] masonry: rename has_focus to subtree_has_focus --- masonry/src/contexts.rs | 8 +++---- masonry/src/debug_logger.rs | 2 +- masonry/src/passes/update.rs | 38 ++++++++++++++++++------------ masonry/src/widget/widget_state.rs | 6 ++--- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/masonry/src/contexts.rs b/masonry/src/contexts.rs index 94494eb47..a877b6e74 100644 --- a/masonry/src/contexts.rs +++ b/masonry/src/contexts.rs @@ -290,7 +290,7 @@ impl_context_method!( /// The focus status of a widget. /// /// Returns `true` if this specific widget is focused. - /// To check if any descendants are focused use [`has_focus`]. + /// To check if any descendants are focused use [`subtree_has_focus`]. /// /// Focus means that the widget receives keyboard events. /// @@ -314,8 +314,8 @@ impl_context_method!( /// /// Returns `true` if either this specific widget or any one of its descendants is focused. /// To check if only this specific widget is focused use [`is_focused`](Self::is_focused). - pub fn has_focus(&self) -> bool { - self.widget_state.has_focus + pub fn subtree_has_focus(&self) -> bool { + self.widget_state.subtree_has_focus } /// Whether this specific widget is in the focus chain. @@ -742,7 +742,7 @@ impl EventCtx<'_> { /// See [`is_focused`](Self::is_focused) for more information about focus. pub fn resign_focus(&mut self) { trace!("resign_focus"); - if self.has_focus() { + if self.subtree_has_focus() { self.global_state.next_focused_widget = None; } else { warn!( diff --git a/masonry/src/debug_logger.rs b/masonry/src/debug_logger.rs index d0292aa38..bc9bb286c 100644 --- a/masonry/src/debug_logger.rs +++ b/masonry/src/debug_logger.rs @@ -202,7 +202,7 @@ impl DebugLogger { StateTree::new("is_explicitly_disabled", w_state.is_explicitly_disabled), StateTree::new("is_hot", w_state.is_hot), StateTree::new("needs_layout", w_state.needs_layout), - StateTree::new("has_focus", w_state.has_focus), + StateTree::new("subtree_has_focus", w_state.subtree_has_focus), StateTree::new("request_anim", w_state.request_anim), StateTree::new("children_changed", w_state.children_changed), StateTree::new("update_focus_chain", w_state.update_focus_chain), diff --git a/masonry/src/passes/update.rs b/masonry/src/passes/update.rs index a5cb3b6cf..c652a3db5 100644 --- a/masonry/src/passes/update.rs +++ b/masonry/src/passes/update.rs @@ -214,12 +214,12 @@ pub(crate) fn run_update_focus_pass(root: &mut RenderRoot, root_state: &mut Widg focused_set: &HashSet, ) { run_targeted_update_pass(root, Some(widget_id), |widget, ctx| { - let has_focus = focused_set.contains(&ctx.widget_id()); + let subtree_has_focus = focused_set.contains(&ctx.widget_id()); - if ctx.widget_state.has_focus != has_focus { - widget.on_status_change(ctx, &StatusChange::ChildFocusChanged(has_focus)); + if ctx.widget_state.subtree_has_focus != subtree_has_focus { + widget.on_status_change(ctx, &StatusChange::ChildFocusChanged(subtree_has_focus)); } - ctx.widget_state.has_focus = has_focus; + ctx.widget_state.subtree_has_focus = subtree_has_focus; }); } @@ -227,7 +227,11 @@ pub(crate) fn run_update_focus_pass(root: &mut RenderRoot, root_state: &mut Widg // TODO - Document the iteration order for update_focus pass. for widget_id in prev_focused_path.iter().copied() { if root.widget_arena.has(widget_id) - && root.widget_arena.get_state_mut(widget_id).item.has_focus + && root + .widget_arena + .get_state_mut(widget_id) + .item + .subtree_has_focus != focused_set.contains(&widget_id) { update_focused_status_of(root, widget_id, &focused_set); @@ -235,7 +239,11 @@ pub(crate) fn run_update_focus_pass(root: &mut RenderRoot, root_state: &mut Widg } for widget_id in next_focused_path.iter().copied() { if root.widget_arena.has(widget_id) - && root.widget_arena.get_state_mut(widget_id).item.has_focus + && root + .widget_arena + .get_state_mut(widget_id) + .item + .subtree_has_focus != focused_set.contains(&widget_id) { update_focused_status_of(root, widget_id, &focused_set); @@ -560,9 +568,9 @@ fn update_focus_chain_for_widget( return; } - // Replace has_focus to check if the value changed in the meantime - state.item.has_focus = global_state.focused_widget == Some(id); - let had_focus = state.item.has_focus; + // Replace subtree_has_focus to check if the value changed in the meantime + state.item.subtree_has_focus = global_state.focused_widget == Some(id); + let subtree_had_focus = state.item.subtree_has_focus; state.item.in_focus_chain = false; state.item.focus_chain.clear(); @@ -597,16 +605,16 @@ fn update_focus_chain_for_widget( parent_focus_chain.extend(&state.item.focus_chain); } - // had_focus is the old focus value. state.has_focus was replaced with parent_ctx.is_focused(). - // Therefore if had_focus is true but state.has_focus is false then the widget which is - // currently focused is not part of the functional tree anymore - // (Lifecycle::BuildFocusChain.should_propagate_to_hidden() is false!) and should + // subtree_had_focus is the old focus value. state.subtree_has_focus was replaced with + // parent_ctx.is_focused(). Therefore if subtree_had_focus is true but state.subtree_has_focus + // is false then the widget which is currently focused is not part of the functional tree + // anymore (Lifecycle::BuildFocusChain.should_propagate_to_hidden() is false!) and should // resign the focus. - if had_focus && !state.item.has_focus { + if subtree_had_focus && !state.item.subtree_has_focus { // Not sure about this logic, might remove global_state.next_focused_widget = None; } - state.item.has_focus = had_focus; + state.item.subtree_has_focus = subtree_had_focus; } pub(crate) fn run_update_focus_chain_pass(root: &mut RenderRoot) { diff --git a/masonry/src/widget/widget_state.rs b/masonry/src/widget/widget_state.rs index 7c837b9d1..c6256a7df 100644 --- a/masonry/src/widget/widget_state.rs +++ b/masonry/src/widget/widget_state.rs @@ -143,7 +143,7 @@ pub struct WidgetState { /// In the focused path, starting from window and ending at the focused widget. /// Descendants of the focused widget are not in the focused path. - pub(crate) has_focus: bool, + pub(crate) subtree_has_focus: bool, /// Whether this specific widget is in the focus chain. pub(crate) in_focus_chain: bool, @@ -194,7 +194,7 @@ impl WidgetState { needs_paint: true, request_accessibility: true, needs_accessibility: true, - has_focus: false, + subtree_has_focus: false, in_focus_chain: false, request_anim: true, needs_anim: true, @@ -260,7 +260,7 @@ impl WidgetState { self.needs_anim |= child_state.needs_anim; self.needs_accessibility |= child_state.needs_accessibility; self.needs_update_disabled |= child_state.needs_update_disabled; - self.has_focus |= child_state.has_focus; + self.subtree_has_focus |= child_state.subtree_has_focus; self.children_changed |= child_state.children_changed; self.update_focus_chain |= child_state.update_focus_chain; self.needs_update_stashed |= child_state.needs_update_stashed;