From 70eecb7429c52cccc01c5f4526ab7b2e448f11d4 Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Wed, 15 Nov 2023 13:43:26 -0800 Subject: [PATCH] Focus is now blurred when disabled apply_pending_state now checks that the focused widget is still enabled. If not, it transitions to no focus. --- src/context.rs | 18 +++++++++++------- src/window.rs | 16 ++++++++++++---- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/context.rs b/src/context.rs index 1146fdd..6c81f7c 100644 --- a/src/context.rs +++ b/src/context.rs @@ -228,10 +228,14 @@ impl<'context, 'window> EventContext<'context, 'window> { let mut focus_changes = 0; while focus_changes < MAX_ITERS { - let focus = self + let focus = match self .pending_state .focus - .and_then(|w| self.current_node.tree.widget(w)); + .and_then(|w| self.current_node.tree.widget(w)) + { + Some(focus) => self.for_other(&focus).enabled().then_some(focus), + None => None, + }; if self.current_node.tree.focused_widget() == focus.as_ref().map(|w| w.node_id) { break; } @@ -377,11 +381,11 @@ impl<'context, 'window> EventContext<'context, 'window> { break; } - if child - .lock() - .as_widget() - .accept_focus(&mut self.for_other(&child)) - { + let mut child_context = self.for_other(&child); + let accept_focus = child_context.enabled() + && child.lock().as_widget().accept_focus(&mut child_context); + drop(child_context); + if accept_focus { return Some(child.id()); } else if let Some(next_focus) = self.widget().explicit_focus_target(advance) { return Some(next_focus.id()); diff --git a/src/window.rs b/src/window.rs index 29dcad0..e052d68 100644 --- a/src/window.rs +++ b/src/window.rs @@ -284,7 +284,7 @@ struct GooeyWindow { initial_frame: bool, occluded: Dynamic, focused: Dynamic, - keyboard_activated: Option, + keyboard_activated: Option, min_inner_size: Option>, max_inner_size: Option>, theme: Option>, @@ -312,7 +312,11 @@ where ) { if is_pressed { if let Some(default) = widget.and_then(|id| self.root.tree.widget_from_node(id)) { - if let Some(previously_active) = self.keyboard_activated.take() { + if let Some(previously_active) = self + .keyboard_activated + .take() + .and_then(|id| self.root.tree.widget(id)) + { EventContext::new( WidgetContext::new( previously_active, @@ -338,9 +342,13 @@ where kludgine, ) .activate(); - self.keyboard_activated = Some(default); + self.keyboard_activated = Some(default.id()); } - } else if let Some(keyboard_activated) = self.keyboard_activated.take() { + } else if let Some(keyboard_activated) = self + .keyboard_activated + .take() + .and_then(|id| self.root.tree.widget(id)) + { EventContext::new( WidgetContext::new( keyboard_activated,