Make winit focus take activity into account on Windows (#2159)

winit's notion of "focus" is very simple; you're either focused or not.
However, Windows has both notions of focused window and active window
and paying attention only to WM_SETFOCUS/WM_KILLFOCUS can cause a window
to believe the user is interacting with it when they're not. (this
manifests when a user switches to another application between when a
winit application starts and it creates its first window)
This commit is contained in:
Steve Wooster 2022-07-15 01:27:27 -07:00 committed by GitHub
parent 9116b6c8cd
commit 1091a8ba1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 114 additions and 62 deletions

View file

@ -45,6 +45,10 @@ pub struct WindowState {
pub ime_state: ImeState,
pub ime_allowed: bool,
// Used by WM_NCACTIVATE, WM_SETFOCUS and WM_KILLFOCUS
pub is_active: bool,
pub is_focused: bool,
}
#[derive(Clone)]
@ -145,6 +149,9 @@ impl WindowState {
ime_state: ImeState::Disabled,
ime_allowed: false,
is_active: false,
is_focused: false,
}
}
@ -170,6 +177,24 @@ impl WindowState {
{
f(&mut self.window_flags);
}
pub fn has_active_focus(&self) -> bool {
self.is_active && self.is_focused
}
// Updates is_active and returns whether active-focus state has changed
pub fn set_active(&mut self, is_active: bool) -> bool {
let old = self.has_active_focus();
self.is_active = is_active;
old != self.has_active_focus()
}
// Updates is_focused and returns whether active-focus state has changed
pub fn set_focused(&mut self, is_focused: bool) -> bool {
let old = self.has_active_focus();
self.is_focused = is_focused;
old != self.has_active_focus()
}
}
impl MouseProperties {