From 09dea3a2bfdb71170c0245beee790a9a7dc99e3e Mon Sep 17 00:00:00 2001 From: Alex Charlton Date: Thu, 21 May 2026 18:28:22 -0700 Subject: [PATCH] Win: defer SetFocus tasks Otherwise, a panic will occur when calling Window.focus while handling events. --- src/win/window.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/win/window.rs b/src/win/window.rs index f71394c3..f86b1c27 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -586,6 +586,9 @@ impl WindowState { ) }; } + WindowTask::Focus => unsafe { + SetFocus(self.hwnd); + }, } } } @@ -597,6 +600,8 @@ pub(super) enum WindowTask { /// Resize the window to the given size. The size is in logical pixels. DPI scaling is applied /// automatically. Resize(Size), + /// Request keyboard focus for the window. + Focus, } pub struct Window<'a> { @@ -828,9 +833,9 @@ impl Window<'_> { } pub fn focus(&mut self) { - unsafe { - SetFocus(self.state.hwnd); - } + // To avoid reentrant event handler calls we'll defer the actual focus request until after + // the event has been handled + self.state.deferred_tasks.borrow_mut().push_back(WindowTask::Focus); } pub fn resize(&mut self, size: Size) {