windows: Improve handling of the WM_SETTINGCHANGE (#11738)

This event is broadcast to all windows, so we can handle this message in
the `WindowsWindow` ranther than in `WindowsPlatform`.

Release Notes:

- N/A
This commit is contained in:
张小白 2024-05-15 01:57:46 +08:00 committed by GitHub
parent 491c04e176
commit d0dd8bf059
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 25 additions and 81 deletions

View file

@ -16,10 +16,8 @@ use windows::Win32::{
use crate::*; use crate::*;
pub(crate) const CURSOR_STYLE_CHANGED: u32 = WM_USER + 1; pub(crate) const CURSOR_STYLE_CHANGED: u32 = WM_USER + 1;
pub(crate) const MOUSE_WHEEL_SETTINGS_CHANGED: u32 = WM_USER + 2; pub(crate) const CLOSE_ONE_WINDOW: u32 = WM_USER + 2;
pub(crate) const MOUSE_WHEEL_SETTINGS_SCROLL_CHARS_CHANGED: isize = 1;
pub(crate) const MOUSE_WHEEL_SETTINGS_SCROLL_LINES_CHANGED: isize = 2;
pub(crate) const CLOSE_ONE_WINDOW: u32 = WM_USER + 3;
const SIZE_MOVE_LOOP_TIMER_ID: usize = 1; const SIZE_MOVE_LOOP_TIMER_ID: usize = 1;
pub(crate) fn handle_msg( pub(crate) fn handle_msg(
@ -82,8 +80,8 @@ pub(crate) fn handle_msg(
WM_IME_STARTCOMPOSITION => handle_ime_position(handle, state_ptr), WM_IME_STARTCOMPOSITION => handle_ime_position(handle, state_ptr),
WM_IME_COMPOSITION => handle_ime_composition(handle, lparam, state_ptr), WM_IME_COMPOSITION => handle_ime_composition(handle, lparam, state_ptr),
WM_SETCURSOR => handle_set_cursor(lparam, state_ptr), WM_SETCURSOR => handle_set_cursor(lparam, state_ptr),
WM_SETTINGCHANGE => handle_system_settings_changed(state_ptr),
CURSOR_STYLE_CHANGED => handle_cursor_changed(lparam, state_ptr), CURSOR_STYLE_CHANGED => handle_cursor_changed(lparam, state_ptr),
MOUSE_WHEEL_SETTINGS_CHANGED => handle_mouse_wheel_settings_msg(wparam, lparam, state_ptr),
_ => None, _ => None,
}; };
if let Some(n) = handled { if let Some(n) = handled {
@ -504,7 +502,7 @@ fn handle_mouse_wheel_msg(
let mut lock = state_ptr.state.borrow_mut(); let mut lock = state_ptr.state.borrow_mut();
if let Some(mut callback) = lock.callbacks.input.take() { if let Some(mut callback) = lock.callbacks.input.take() {
let scale_factor = lock.scale_factor; let scale_factor = lock.scale_factor;
let wheel_scroll_lines = lock.mouse_wheel_settings.wheel_scroll_lines; let wheel_scroll_lines = lock.system_settings.mouse_wheel_settings.wheel_scroll_lines;
drop(lock); drop(lock);
let wheel_distance = let wheel_distance =
(wparam.signed_hiword() as f32 / WHEEL_DELTA as f32) * wheel_scroll_lines as f32; (wparam.signed_hiword() as f32 / WHEEL_DELTA as f32) * wheel_scroll_lines as f32;
@ -544,7 +542,7 @@ fn handle_mouse_horizontal_wheel_msg(
let mut lock = state_ptr.state.borrow_mut(); let mut lock = state_ptr.state.borrow_mut();
if let Some(mut callback) = lock.callbacks.input.take() { if let Some(mut callback) = lock.callbacks.input.take() {
let scale_factor = lock.scale_factor; let scale_factor = lock.scale_factor;
let wheel_scroll_chars = lock.mouse_wheel_settings.wheel_scroll_chars; let wheel_scroll_chars = lock.system_settings.mouse_wheel_settings.wheel_scroll_chars;
drop(lock); drop(lock);
let wheel_distance = let wheel_distance =
(-wparam.signed_hiword() as f32 / WHEEL_DELTA as f32) * wheel_scroll_chars as f32; (-wparam.signed_hiword() as f32 / WHEEL_DELTA as f32) * wheel_scroll_chars as f32;
@ -1037,28 +1035,10 @@ fn handle_set_cursor(lparam: LPARAM, state_ptr: Rc<WindowsWindowStatePtr>) -> Op
Some(1) Some(1)
} }
fn handle_mouse_wheel_settings_msg( fn handle_system_settings_changed(state_ptr: Rc<WindowsWindowStatePtr>) -> Option<isize> {
wparam: WPARAM, let mut lock = state_ptr.state.borrow_mut();
lparam: LPARAM, // mouse wheel
state_ptr: Rc<WindowsWindowStatePtr>, lock.system_settings.mouse_wheel_settings.update();
) -> Option<isize> {
match lparam.0 {
1 => {
state_ptr
.state
.borrow_mut()
.mouse_wheel_settings
.wheel_scroll_chars = wparam.0 as u32
}
2 => {
state_ptr
.state
.borrow_mut()
.mouse_wheel_settings
.wheel_scroll_lines = wparam.0 as u32
}
_ => unreachable!(),
}
Some(0) Some(0)
} }

View file

@ -48,7 +48,6 @@ pub(crate) struct WindowsPlatform {
pub(crate) struct WindowsPlatformState { pub(crate) struct WindowsPlatformState {
callbacks: PlatformCallbacks, callbacks: PlatformCallbacks,
pub(crate) settings: WindowsPlatformSystemSettings,
// NOTE: standard cursor handles don't need to close. // NOTE: standard cursor handles don't need to close.
pub(crate) current_cursor: HCURSOR, pub(crate) current_cursor: HCURSOR,
} }
@ -66,12 +65,10 @@ struct PlatformCallbacks {
impl WindowsPlatformState { impl WindowsPlatformState {
fn new() -> Self { fn new() -> Self {
let callbacks = PlatformCallbacks::default(); let callbacks = PlatformCallbacks::default();
let settings = WindowsPlatformSystemSettings::new();
let current_cursor = load_cursor(CursorStyle::Arrow); let current_cursor = load_cursor(CursorStyle::Arrow);
Self { Self {
callbacks, callbacks,
settings,
current_cursor, current_cursor,
} }
} }
@ -149,28 +146,6 @@ impl WindowsPlatform {
lock.is_empty() lock.is_empty()
} }
fn update_system_settings(&self) {
let mut lock = self.state.borrow_mut();
// mouse wheel
{
let (scroll_chars, scroll_lines) = lock.settings.mouse_wheel_settings.update();
if let Some(scroll_chars) = scroll_chars {
self.post_message(
MOUSE_WHEEL_SETTINGS_CHANGED,
WPARAM(scroll_chars as usize),
LPARAM(MOUSE_WHEEL_SETTINGS_SCROLL_CHARS_CHANGED),
);
}
if let Some(scroll_lines) = scroll_lines {
self.post_message(
MOUSE_WHEEL_SETTINGS_CHANGED,
WPARAM(scroll_lines as usize),
LPARAM(MOUSE_WHEEL_SETTINGS_SCROLL_LINES_CHANGED),
);
}
}
}
} }
impl Platform for WindowsPlatform { impl Platform for WindowsPlatform {
@ -219,7 +194,6 @@ impl Platform for WindowsPlatform {
break 'a; break 'a;
} }
} }
WM_SETTINGCHANGE => self.update_system_settings(),
_ => { _ => {
// todo(windows) // todo(windows)
// crate `windows 0.56` reports true as Err // crate `windows 0.56` reports true as Err
@ -325,7 +299,6 @@ impl Platform for WindowsPlatform {
options, options,
self.icon, self.icon,
self.foreground_executor.clone(), self.foreground_executor.clone(),
lock.settings.mouse_wheel_settings,
lock.current_cursor, lock.current_cursor,
); );
drop(lock); drop(lock);
@ -640,8 +613,11 @@ impl Platform for WindowsPlatform {
fn set_cursor_style(&self, style: CursorStyle) { fn set_cursor_style(&self, style: CursorStyle) {
let hcursor = load_cursor(style); let hcursor = load_cursor(style);
self.post_message(CURSOR_STYLE_CHANGED, WPARAM(0), LPARAM(hcursor.0)); let mut lock = self.state.borrow_mut();
self.state.borrow_mut().current_cursor = hcursor; if lock.current_cursor.0 != hcursor.0 {
self.post_message(CURSOR_STYLE_CHANGED, WPARAM(0), LPARAM(hcursor.0));
lock.current_cursor = hcursor;
}
} }
// todo(windows) // todo(windows)

View file

@ -8,8 +8,8 @@ use windows::Win32::UI::WindowsAndMessaging::{
/// Windows settings pulled from SystemParametersInfo /// Windows settings pulled from SystemParametersInfo
/// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfow /// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfow
#[derive(Default, Debug)] #[derive(Default, Debug, Clone, Copy)]
pub(crate) struct WindowsPlatformSystemSettings { pub(crate) struct WindowsSystemSettings {
pub(crate) mouse_wheel_settings: MouseWheelSettings, pub(crate) mouse_wheel_settings: MouseWheelSettings,
} }
@ -21,7 +21,7 @@ pub(crate) struct MouseWheelSettings {
pub(crate) wheel_scroll_lines: u32, pub(crate) wheel_scroll_lines: u32,
} }
impl WindowsPlatformSystemSettings { impl WindowsSystemSettings {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
let mut settings = Self::default(); let mut settings = Self::default();
settings.init(); settings.init();
@ -34,14 +34,12 @@ impl WindowsPlatformSystemSettings {
} }
impl MouseWheelSettings { impl MouseWheelSettings {
pub(crate) fn update(&mut self) -> (Option<u32>, Option<u32>) { pub(crate) fn update(&mut self) {
( self.update_wheel_scroll_chars();
self.update_wheel_scroll_chars(), self.update_wheel_scroll_lines();
self.update_wheel_scroll_lines(),
)
} }
fn update_wheel_scroll_chars(&mut self) -> Option<u32> { fn update_wheel_scroll_chars(&mut self) {
let mut value = c_uint::default(); let mut value = c_uint::default();
let result = unsafe { let result = unsafe {
SystemParametersInfoW( SystemParametersInfoW(
@ -54,13 +52,10 @@ impl MouseWheelSettings {
if result.log_err() != None && self.wheel_scroll_chars != value { if result.log_err() != None && self.wheel_scroll_chars != value {
self.wheel_scroll_chars = value; self.wheel_scroll_chars = value;
Some(value)
} else {
None
} }
} }
fn update_wheel_scroll_lines(&mut self) -> Option<u32> { fn update_wheel_scroll_lines(&mut self) {
let mut value = c_uint::default(); let mut value = c_uint::default();
let result = unsafe { let result = unsafe {
SystemParametersInfoW( SystemParametersInfoW(
@ -73,9 +68,6 @@ impl MouseWheelSettings {
if result.log_err() != None && self.wheel_scroll_lines != value { if result.log_err() != None && self.wheel_scroll_lines != value {
self.wheel_scroll_lines = value; self.wheel_scroll_lines = value;
Some(value)
} else {
None
} }
} }
} }

View file

@ -43,7 +43,7 @@ pub struct WindowsWindowState {
pub renderer: BladeRenderer, pub renderer: BladeRenderer,
pub click_state: ClickState, pub click_state: ClickState,
pub mouse_wheel_settings: MouseWheelSettings, pub system_settings: WindowsSystemSettings,
pub current_cursor: HCURSOR, pub current_cursor: HCURSOR,
pub display: WindowsDisplay, pub display: WindowsDisplay,
@ -64,7 +64,6 @@ impl WindowsWindowState {
hwnd: HWND, hwnd: HWND,
transparent: bool, transparent: bool,
cs: &CREATESTRUCTW, cs: &CREATESTRUCTW,
mouse_wheel_settings: MouseWheelSettings,
current_cursor: HCURSOR, current_cursor: HCURSOR,
display: WindowsDisplay, display: WindowsDisplay,
) -> Self { ) -> Self {
@ -82,6 +81,7 @@ impl WindowsWindowState {
let callbacks = Callbacks::default(); let callbacks = Callbacks::default();
let input_handler = None; let input_handler = None;
let click_state = ClickState::new(); let click_state = ClickState::new();
let system_settings = WindowsSystemSettings::new();
let fullscreen = None; let fullscreen = None;
Self { Self {
@ -93,7 +93,7 @@ impl WindowsWindowState {
input_handler, input_handler,
renderer, renderer,
click_state, click_state,
mouse_wheel_settings, system_settings,
current_cursor, current_cursor,
display, display,
fullscreen, fullscreen,
@ -195,7 +195,6 @@ impl WindowsWindowStatePtr {
hwnd, hwnd,
context.transparent, context.transparent,
cs, cs,
context.mouse_wheel_settings,
context.current_cursor, context.current_cursor,
context.display, context.display,
)); ));
@ -229,7 +228,6 @@ struct WindowCreateContext {
display: WindowsDisplay, display: WindowsDisplay,
transparent: bool, transparent: bool,
executor: ForegroundExecutor, executor: ForegroundExecutor,
mouse_wheel_settings: MouseWheelSettings,
current_cursor: HCURSOR, current_cursor: HCURSOR,
} }
@ -239,7 +237,6 @@ impl WindowsWindow {
params: WindowParams, params: WindowParams,
icon: HICON, icon: HICON,
executor: ForegroundExecutor, executor: ForegroundExecutor,
mouse_wheel_settings: MouseWheelSettings,
current_cursor: HCURSOR, current_cursor: HCURSOR,
) -> Self { ) -> Self {
let classname = register_wnd_class(icon); let classname = register_wnd_class(icon);
@ -271,7 +268,6 @@ impl WindowsWindow {
display, display,
transparent: params.window_background != WindowBackgroundAppearance::Opaque, transparent: params.window_background != WindowBackgroundAppearance::Opaque,
executor, executor,
mouse_wheel_settings,
current_cursor, current_cursor,
}; };
let lpparam = Some(&context as *const _ as *const _); let lpparam = Some(&context as *const _ as *const _);