Move ModifiersChanged variant to WindowEvent (#1381)
* Move `ModifiersChanged` variant to `WindowEvent` * macos: Fix flags_changed for ModifiersChanged variant move I haven't look too deep at what this does internally, but at least cargo-check is fully happy now. :) Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com> * macos: Fire a ModifiersChanged event on window_did_resign_key From debugging, I determined that macOS' emission of a flagsChanged around window switching is inconsistent. It is fair to assume, I think, that when the user switches windows, they do not expect their former modifiers state to remain effective; so I think it's best to clear that state by sending a ModifiersChanged(ModifiersState::empty()). Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com> * windows: Fix build I don't know enough about the code to implement the fix as it is done on this branch, but this commit at least fixes the build. Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com> * windows: Send ModifiersChanged(ModifiersState::empty) on KILLFOCUS Very similar to the changes made in [1], as focus is lost, send an event to the window indicating that the modifiers have been released. It's unclear to me (without a Windows device to test this on) whether this is necessary, but it certainly ensures that unfocused windows will have at least received this event, which is an improvement. [1]: f79f21641a31da3e4039d41be89047cdcc6028f7 Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com> * macos: Add a hook to update stale modifiers Sometimes, `ViewState` and `event` might have different values for their stored `modifiers` flags. These are internally stored as a bitmask in the latter and an enum in the former. We can check to see if they differ, and if they do, automatically dispatch an event to update consumers of modifier state as well as the stored `state.modifiers`. That's what the hook does. This hook is then called in the key_down, mouse_entered, mouse_exited, mouse_click, scroll_wheel, and pressure_change_with_event callbacks, which each will contain updated modifiers. Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com> * Only call event_mods once when determining whether to update state Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com> * flags_changed: Memoize window_id collection Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com> * window_did_resign_key: Remove synthetic ModifiersChanged event We no longer need to emit this event, since we are checking the state of our modifiers before emitting most other events. Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com> * mouse_motion: Add a call to update_potentially_stale_modifiers Now, cover all events (that I can think of, at least) where stale modifiers might affect how user programs behave. Effectively, every human-interface event (keypress, mouse click, keydown, etc.) will cause a ModifiersChanged event to be fired if something has changed. Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com> * key_up: Add a call to update_potentially_stale_modifiers We also want to make sure modifiers state is synchronized here, too. Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com> * mouse_motion: Remove update_potentially_stale_modifiers invocation Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com> * Retry CI * ViewState: Promote visibility of modifiers to the macos impl This is so that we can interact with the ViewState directly from the WindowDelegate. Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com> * window_delegate: Synthetically set modifiers state to empty on resignKey This logic is implemented similarly on other platforms, so we wish to regain parity here. Originally this behavior was implemented to always fire an event with ModifiersState::empty(), but that was not the best as it was not necessarily correct and could be a duplicate event. This solution is perhaps the most elegant possible to implement the desired behavior of sending a synthetic empty modifiers event when a window loses focus, trading some safety for interoperation between the NSWindowDelegate and the NSView (as the objc runtime must now be consulted in order to acquire access to the ViewState which is "owned" by the NSView). Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com> * Check for modifiers change in window events * Fix modifier changed on macOS Since the `mouse_entered` function was generating a mouse motion, which updates the modifier state, a modifiers changed event was incorrectly generated. The updating of the modifier state has also been changed to make sure it consistently happens before events that have a modifier state attached to it, without happening on any other event. This of course means that no `CursorMoved` event is generated anymore when the user enters the window without it being focused, however I'd say that is consistent with how winit should behave. * Fix unused variable warning * Move changelog entry into `Unreleased` section Co-authored-by: Freya Gentz <zegentzy@protonmail.com> Co-authored-by: Kristofer Rye <kristofer.rye@gmail.com> Co-authored-by: Christian Duerr <contact@christianduerr.com>
This commit is contained in:
parent
71bd6e73ca
commit
e707052f66
10 changed files with 229 additions and 122 deletions
|
|
@ -50,9 +50,7 @@ use crate::{
|
|||
dark_mode::try_dark_mode,
|
||||
dpi::{become_dpi_aware, dpi_to_scale_factor, enable_non_client_dpi_scaling},
|
||||
drop_handler::FileDropHandler,
|
||||
event::{
|
||||
self, handle_extended_keys, process_key_params, vkey_to_winit_vkey, ModifiersStateSide,
|
||||
},
|
||||
event::{self, handle_extended_keys, process_key_params, vkey_to_winit_vkey},
|
||||
monitor, raw_input, util,
|
||||
window_state::{CursorFlags, WindowFlags, WindowState},
|
||||
wrap_device_id, WindowId, DEVICE_ID,
|
||||
|
|
@ -108,7 +106,6 @@ impl<T> SubclassInput<T> {
|
|||
struct ThreadMsgTargetSubclassInput<T: 'static> {
|
||||
event_loop_runner: EventLoopRunnerShared<T>,
|
||||
user_event_receiver: Receiver<T>,
|
||||
modifiers_state: ModifiersStateSide,
|
||||
}
|
||||
|
||||
impl<T> ThreadMsgTargetSubclassInput<T> {
|
||||
|
|
@ -553,7 +550,6 @@ fn thread_event_target_window<T>(event_loop_runner: EventLoopRunnerShared<T>) ->
|
|||
let subclass_input = ThreadMsgTargetSubclassInput {
|
||||
event_loop_runner,
|
||||
user_event_receiver: rx,
|
||||
modifiers_state: ModifiersStateSide::default(),
|
||||
};
|
||||
let input_ptr = Box::into_raw(Box::new(subclass_input));
|
||||
let subclass_result = commctrl::SetWindowSubclass(
|
||||
|
|
@ -606,6 +602,24 @@ fn normalize_pointer_pressure(pressure: u32) -> Option<Force> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Emit a `ModifiersChanged` event whenever modifiers have changed.
|
||||
fn update_modifiers<T>(window: HWND, subclass_input: &SubclassInput<T>) {
|
||||
use crate::event::WindowEvent::ModifiersChanged;
|
||||
|
||||
let modifiers = event::get_key_mods();
|
||||
let mut window_state = subclass_input.window_state.lock();
|
||||
if window_state.modifiers_state != modifiers {
|
||||
window_state.modifiers_state = modifiers;
|
||||
|
||||
unsafe {
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
event: ModifiersChanged(modifiers),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Any window whose callback is configured to this function will have its events propagated
|
||||
/// through the events loop of the thread the window was created in.
|
||||
//
|
||||
|
|
@ -866,6 +880,8 @@ unsafe extern "system" fn public_window_callback<T: 'static>(
|
|||
w.mouse.last_position = Some(position);
|
||||
}
|
||||
if cursor_moved {
|
||||
update_modifiers(window, subclass_input);
|
||||
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
event: CursorMoved {
|
||||
|
|
@ -905,6 +921,8 @@ unsafe extern "system" fn public_window_callback<T: 'static>(
|
|||
let value = value as i32;
|
||||
let value = value as f32 / winuser::WHEEL_DELTA as f32;
|
||||
|
||||
update_modifiers(window, subclass_input);
|
||||
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
event: WindowEvent::MouseWheel {
|
||||
|
|
@ -925,6 +943,8 @@ unsafe extern "system" fn public_window_callback<T: 'static>(
|
|||
let value = value as i32;
|
||||
let value = value as f32 / winuser::WHEEL_DELTA as f32;
|
||||
|
||||
update_modifiers(window, subclass_input);
|
||||
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
event: WindowEvent::MouseWheel {
|
||||
|
|
@ -944,6 +964,8 @@ unsafe extern "system" fn public_window_callback<T: 'static>(
|
|||
commctrl::DefSubclassProc(window, msg, wparam, lparam)
|
||||
} else {
|
||||
if let Some((scancode, vkey)) = process_key_params(wparam, lparam) {
|
||||
update_modifiers(window, subclass_input);
|
||||
|
||||
#[allow(deprecated)]
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
|
|
@ -974,6 +996,8 @@ unsafe extern "system" fn public_window_callback<T: 'static>(
|
|||
winuser::WM_KEYUP | winuser::WM_SYSKEYUP => {
|
||||
use crate::event::ElementState::Released;
|
||||
if let Some((scancode, vkey)) = process_key_params(wparam, lparam) {
|
||||
update_modifiers(window, subclass_input);
|
||||
|
||||
#[allow(deprecated)]
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
|
|
@ -997,6 +1021,8 @@ unsafe extern "system" fn public_window_callback<T: 'static>(
|
|||
|
||||
capture_mouse(window, &mut *subclass_input.window_state.lock());
|
||||
|
||||
update_modifiers(window, subclass_input);
|
||||
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
event: MouseInput {
|
||||
|
|
@ -1016,6 +1042,8 @@ unsafe extern "system" fn public_window_callback<T: 'static>(
|
|||
|
||||
release_mouse(&mut *subclass_input.window_state.lock());
|
||||
|
||||
update_modifiers(window, subclass_input);
|
||||
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
event: MouseInput {
|
||||
|
|
@ -1035,6 +1063,8 @@ unsafe extern "system" fn public_window_callback<T: 'static>(
|
|||
|
||||
capture_mouse(window, &mut *subclass_input.window_state.lock());
|
||||
|
||||
update_modifiers(window, subclass_input);
|
||||
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
event: MouseInput {
|
||||
|
|
@ -1054,6 +1084,8 @@ unsafe extern "system" fn public_window_callback<T: 'static>(
|
|||
|
||||
release_mouse(&mut *subclass_input.window_state.lock());
|
||||
|
||||
update_modifiers(window, subclass_input);
|
||||
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
event: MouseInput {
|
||||
|
|
@ -1073,6 +1105,8 @@ unsafe extern "system" fn public_window_callback<T: 'static>(
|
|||
|
||||
capture_mouse(window, &mut *subclass_input.window_state.lock());
|
||||
|
||||
update_modifiers(window, subclass_input);
|
||||
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
event: MouseInput {
|
||||
|
|
@ -1092,6 +1126,8 @@ unsafe extern "system" fn public_window_callback<T: 'static>(
|
|||
|
||||
release_mouse(&mut *subclass_input.window_state.lock());
|
||||
|
||||
update_modifiers(window, subclass_input);
|
||||
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
event: MouseInput {
|
||||
|
|
@ -1112,6 +1148,8 @@ unsafe extern "system" fn public_window_callback<T: 'static>(
|
|||
|
||||
capture_mouse(window, &mut *subclass_input.window_state.lock());
|
||||
|
||||
update_modifiers(window, subclass_input);
|
||||
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
event: MouseInput {
|
||||
|
|
@ -1132,6 +1170,8 @@ unsafe extern "system" fn public_window_callback<T: 'static>(
|
|||
|
||||
release_mouse(&mut *subclass_input.window_state.lock());
|
||||
|
||||
update_modifiers(window, subclass_input);
|
||||
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
event: MouseInput {
|
||||
|
|
@ -1340,6 +1380,8 @@ unsafe extern "system" fn public_window_callback<T: 'static>(
|
|||
winuser::MapVirtualKeyA(windows_keycode as _, winuser::MAPVK_VK_TO_VSC);
|
||||
let virtual_keycode = event::vkey_to_winit_vkey(windows_keycode);
|
||||
|
||||
update_modifiers(window, subclass_input);
|
||||
|
||||
#[allow(deprecated)]
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
|
|
@ -1365,7 +1407,11 @@ unsafe extern "system" fn public_window_callback<T: 'static>(
|
|||
}
|
||||
|
||||
winuser::WM_KILLFOCUS => {
|
||||
use crate::event::{ElementState::Released, WindowEvent::Focused};
|
||||
use crate::event::{
|
||||
ElementState::Released,
|
||||
ModifiersState,
|
||||
WindowEvent::{Focused, ModifiersChanged},
|
||||
};
|
||||
for windows_keycode in event::get_pressed_keys() {
|
||||
let scancode =
|
||||
winuser::MapVirtualKeyA(windows_keycode as _, winuser::MAPVK_VK_TO_VSC);
|
||||
|
|
@ -1387,6 +1433,12 @@ unsafe extern "system" fn public_window_callback<T: 'static>(
|
|||
})
|
||||
}
|
||||
|
||||
subclass_input.window_state.lock().modifiers_state = ModifiersState::empty();
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
event: ModifiersChanged(ModifiersState::empty()),
|
||||
});
|
||||
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
event: Focused(false),
|
||||
|
|
@ -1795,10 +1847,9 @@ unsafe extern "system" fn thread_event_target_callback<T: 'static>(
|
|||
|
||||
winuser::WM_INPUT => {
|
||||
use crate::event::{
|
||||
DeviceEvent::{Button, Key, ModifiersChanged, Motion, MouseMotion, MouseWheel},
|
||||
DeviceEvent::{Button, Key, Motion, MouseMotion, MouseWheel},
|
||||
ElementState::{Pressed, Released},
|
||||
MouseScrollDelta::LineDelta,
|
||||
VirtualKeyCode,
|
||||
};
|
||||
|
||||
if let Some(data) = raw_input::get_raw_input_data(lparam as _) {
|
||||
|
|
@ -1877,48 +1928,6 @@ unsafe extern "system" fn thread_event_target_callback<T: 'static>(
|
|||
{
|
||||
let virtual_keycode = vkey_to_winit_vkey(vkey);
|
||||
|
||||
// If we ever change the DeviceEvent API to only emit events when a
|
||||
// window is focused, we'll need to emit synthetic `ModifiersChanged`
|
||||
// events when Winit windows lose focus so that these don't drift out
|
||||
// of sync with the actual modifier state.
|
||||
let old_modifiers_state =
|
||||
subclass_input.modifiers_state.filter_out_altgr().into();
|
||||
match virtual_keycode {
|
||||
Some(VirtualKeyCode::LShift) => subclass_input
|
||||
.modifiers_state
|
||||
.set(ModifiersStateSide::LSHIFT, pressed),
|
||||
Some(VirtualKeyCode::RShift) => subclass_input
|
||||
.modifiers_state
|
||||
.set(ModifiersStateSide::RSHIFT, pressed),
|
||||
Some(VirtualKeyCode::LControl) => subclass_input
|
||||
.modifiers_state
|
||||
.set(ModifiersStateSide::LCTRL, pressed),
|
||||
Some(VirtualKeyCode::RControl) => subclass_input
|
||||
.modifiers_state
|
||||
.set(ModifiersStateSide::RCTRL, pressed),
|
||||
Some(VirtualKeyCode::LAlt) => subclass_input
|
||||
.modifiers_state
|
||||
.set(ModifiersStateSide::LALT, pressed),
|
||||
Some(VirtualKeyCode::RAlt) => subclass_input
|
||||
.modifiers_state
|
||||
.set(ModifiersStateSide::RALT, pressed),
|
||||
Some(VirtualKeyCode::LWin) => subclass_input
|
||||
.modifiers_state
|
||||
.set(ModifiersStateSide::LLOGO, pressed),
|
||||
Some(VirtualKeyCode::RWin) => subclass_input
|
||||
.modifiers_state
|
||||
.set(ModifiersStateSide::RLOGO, pressed),
|
||||
_ => (),
|
||||
}
|
||||
let new_modifiers_state =
|
||||
subclass_input.modifiers_state.filter_out_altgr().into();
|
||||
if new_modifiers_state != old_modifiers_state {
|
||||
subclass_input.send_event(Event::DeviceEvent {
|
||||
device_id,
|
||||
event: ModifiersChanged(new_modifiers_state),
|
||||
});
|
||||
}
|
||||
|
||||
#[allow(deprecated)]
|
||||
subclass_input.send_event(Event::DeviceEvent {
|
||||
device_id,
|
||||
|
|
@ -1926,7 +1935,7 @@ unsafe extern "system" fn thread_event_target_callback<T: 'static>(
|
|||
scancode,
|
||||
state,
|
||||
virtual_keycode,
|
||||
modifiers: new_modifiers_state,
|
||||
modifiers: event::get_key_mods(),
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue