2019-07-05 18:37:25 +02:00
|
|
|
#![allow(non_snake_case)]
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2019-08-26 22:05:42 -04:00
|
|
|
mod runner;
|
|
|
|
|
|
2022-06-08 11:50:26 -07:00
|
|
|
use once_cell::sync::Lazy;
|
2019-02-05 10:30:33 -05:00
|
|
|
use parking_lot::Mutex;
|
2019-06-21 11:33:15 -04:00
|
|
|
use std::{
|
2020-12-20 12:54:42 +01:00
|
|
|
cell::Cell,
|
2020-07-04 15:46:41 -04:00
|
|
|
collections::VecDeque,
|
2022-03-30 10:30:45 +02:00
|
|
|
ffi::c_void,
|
2019-06-21 11:33:15 -04:00
|
|
|
marker::PhantomData,
|
|
|
|
|
mem, panic, ptr,
|
|
|
|
|
rc::Rc,
|
|
|
|
|
sync::{
|
|
|
|
|
mpsc::{self, Receiver, Sender},
|
|
|
|
|
Arc,
|
|
|
|
|
},
|
2020-05-04 15:14:13 -04:00
|
|
|
thread,
|
2019-06-21 11:33:15 -04:00
|
|
|
time::{Duration, Instant},
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
use windows_sys::Win32::{
|
|
|
|
|
Devices::HumanInterfaceDevice::MOUSE_MOVE_RELATIVE,
|
|
|
|
|
Foundation::{BOOL, HANDLE, HWND, LPARAM, LRESULT, POINT, RECT, WAIT_TIMEOUT, WPARAM},
|
|
|
|
|
Graphics::Gdi::{
|
|
|
|
|
ClientToScreen, GetMonitorInfoW, GetUpdateRect, MonitorFromRect, MonitorFromWindow,
|
|
|
|
|
RedrawWindow, ScreenToClient, ValidateRect, MONITORINFO, MONITOR_DEFAULTTONULL,
|
|
|
|
|
RDW_INTERNALPAINT, SC_SCREENSAVE,
|
2019-06-21 11:33:15 -04:00
|
|
|
},
|
2022-03-07 22:58:12 +01:00
|
|
|
Media::{timeBeginPeriod, timeEndPeriod, timeGetDevCaps, TIMECAPS, TIMERR_NOERROR},
|
2022-05-29 17:12:46 +02:00
|
|
|
System::{Ole::RevokeDragDrop, Threading::GetCurrentThreadId, WindowsProgramming::INFINITE},
|
2022-03-07 22:58:12 +01:00
|
|
|
UI::{
|
|
|
|
|
Controls::{HOVER_DEFAULT, WM_MOUSELEAVE},
|
|
|
|
|
Input::{
|
2022-05-07 05:29:25 +03:00
|
|
|
Ime::{GCS_COMPSTR, GCS_RESULTSTR, ISC_SHOWUICOMPOSITIONWINDOW},
|
2022-03-07 22:58:12 +01:00
|
|
|
KeyboardAndMouse::{
|
|
|
|
|
MapVirtualKeyA, ReleaseCapture, SetCapture, TrackMouseEvent, TME_LEAVE,
|
|
|
|
|
TRACKMOUSEEVENT, VK_F4,
|
|
|
|
|
},
|
|
|
|
|
Pointer::{
|
|
|
|
|
POINTER_FLAG_DOWN, POINTER_FLAG_UP, POINTER_FLAG_UPDATE, POINTER_INFO,
|
|
|
|
|
POINTER_PEN_INFO, POINTER_TOUCH_INFO,
|
|
|
|
|
},
|
|
|
|
|
Touch::{
|
|
|
|
|
CloseTouchInputHandle, GetTouchInputInfo, TOUCHEVENTF_DOWN, TOUCHEVENTF_MOVE,
|
|
|
|
|
TOUCHEVENTF_UP, TOUCHINPUT,
|
|
|
|
|
},
|
|
|
|
|
RIM_TYPEKEYBOARD, RIM_TYPEMOUSE,
|
|
|
|
|
},
|
|
|
|
|
WindowsAndMessaging::{
|
|
|
|
|
CreateWindowExW, DefWindowProcW, DestroyWindow, DispatchMessageW, GetClientRect,
|
|
|
|
|
GetCursorPos, GetMessageW, GetWindowLongW, LoadCursorW, MsgWaitForMultipleObjectsEx,
|
|
|
|
|
PeekMessageW, PostMessageW, PostThreadMessageW, RegisterClassExW,
|
|
|
|
|
RegisterWindowMessageA, SetCursor, SetWindowPos, TranslateMessage, CREATESTRUCTW,
|
|
|
|
|
GIDC_ARRIVAL, GIDC_REMOVAL, GWL_EXSTYLE, GWL_STYLE, GWL_USERDATA, HTCAPTION, HTCLIENT,
|
|
|
|
|
MAPVK_VK_TO_VSC, MINMAXINFO, MSG, MWMO_INPUTAVAILABLE, PM_NOREMOVE, PM_QS_PAINT,
|
|
|
|
|
PM_REMOVE, PT_PEN, PT_TOUCH, QS_ALLEVENTS, RI_KEY_E0, RI_KEY_E1, RI_MOUSE_WHEEL,
|
2022-04-30 13:21:08 +02:00
|
|
|
SC_MINIMIZE, SC_RESTORE, SIZE_MAXIMIZED, SWP_NOACTIVATE, SWP_NOMOVE, SWP_NOSIZE,
|
|
|
|
|
SWP_NOZORDER, WHEEL_DELTA, WINDOWPOS, WM_CAPTURECHANGED, WM_CHAR, WM_CLOSE, WM_CREATE,
|
|
|
|
|
WM_DESTROY, WM_DPICHANGED, WM_DROPFILES, WM_ENTERSIZEMOVE, WM_EXITSIZEMOVE,
|
2022-05-07 05:29:25 +03:00
|
|
|
WM_GETMINMAXINFO, WM_IME_COMPOSITION, WM_IME_ENDCOMPOSITION, WM_IME_SETCONTEXT,
|
|
|
|
|
WM_IME_STARTCOMPOSITION, WM_INPUT, WM_INPUT_DEVICE_CHANGE, WM_KEYDOWN, WM_KEYUP,
|
|
|
|
|
WM_KILLFOCUS, WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MBUTTONDOWN, WM_MBUTTONUP,
|
|
|
|
|
WM_MOUSEHWHEEL, WM_MOUSEMOVE, WM_MOUSEWHEEL, WM_NCCREATE, WM_NCDESTROY,
|
|
|
|
|
WM_NCLBUTTONDOWN, WM_PAINT, WM_POINTERDOWN, WM_POINTERUP, WM_POINTERUPDATE,
|
|
|
|
|
WM_RBUTTONDOWN, WM_RBUTTONUP, WM_SETCURSOR, WM_SETFOCUS, WM_SETTINGCHANGE, WM_SIZE,
|
|
|
|
|
WM_SYSCHAR, WM_SYSCOMMAND, WM_SYSKEYDOWN, WM_SYSKEYUP, WM_TOUCH, WM_WINDOWPOSCHANGED,
|
|
|
|
|
WM_WINDOWPOSCHANGING, WM_XBUTTONDOWN, WM_XBUTTONUP, WNDCLASSEXW, WS_EX_LAYERED,
|
|
|
|
|
WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW, WS_EX_TRANSPARENT, WS_OVERLAPPED, WS_POPUP,
|
|
|
|
|
WS_VISIBLE,
|
2022-03-07 22:58:12 +01:00
|
|
|
},
|
2019-06-21 11:33:15 -04:00
|
|
|
},
|
2019-02-05 10:30:33 -05:00
|
|
|
};
|
2019-06-21 11:33:15 -04:00
|
|
|
|
|
|
|
|
use crate::{
|
2019-06-19 16:49:43 -04:00
|
|
|
dpi::{PhysicalPosition, PhysicalSize},
|
2022-05-07 05:29:25 +03:00
|
|
|
event::{DeviceEvent, Event, Force, Ime, KeyboardInput, Touch, TouchPhase, WindowEvent},
|
2019-06-21 11:33:15 -04:00
|
|
|
event_loop::{ControlFlow, EventLoopClosed, EventLoopWindowTarget as RootELW},
|
2020-09-07 20:20:47 +03:00
|
|
|
monitor::MonitorHandle as RootMonitorHandle,
|
2019-06-21 11:33:15 -04:00
|
|
|
platform_impl::platform::{
|
2020-11-30 19:04:26 +01:00
|
|
|
dark_mode::try_theme,
|
2021-11-17 18:33:44 +01:00
|
|
|
dpi::{become_dpi_aware, dpi_to_scale_factor},
|
2019-06-21 11:33:15 -04:00
|
|
|
drop_handler::FileDropHandler,
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
event::{self, handle_extended_keys, process_key_params, vkey_to_winit_vkey},
|
2022-05-07 05:29:25 +03:00
|
|
|
ime::ImeContext,
|
2020-07-04 15:46:41 -04:00
|
|
|
monitor::{self, MonitorHandle},
|
|
|
|
|
raw_input, util,
|
2021-07-16 12:40:48 +02:00
|
|
|
window::InitData,
|
2022-05-07 05:29:25 +03:00
|
|
|
window_state::{CursorFlags, ImeState, WindowFlags, WindowState},
|
2019-06-21 11:33:15 -04:00
|
|
|
wrap_device_id, WindowId, DEVICE_ID,
|
|
|
|
|
},
|
2019-10-05 16:23:30 -04:00
|
|
|
window::{Fullscreen, WindowId as RootWindowId},
|
2019-02-05 10:30:33 -05:00
|
|
|
};
|
2020-05-04 15:14:13 -04:00
|
|
|
use runner::{EventLoopRunner, EventLoopRunnerShared};
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2019-07-05 18:37:25 +02:00
|
|
|
type GetPointerFrameInfoHistory = unsafe extern "system" fn(
|
2022-03-07 22:58:12 +01:00
|
|
|
pointerId: u32,
|
|
|
|
|
entriesCount: *mut u32,
|
|
|
|
|
pointerCount: *mut u32,
|
|
|
|
|
pointerInfo: *mut POINTER_INFO,
|
2019-07-05 18:37:25 +02:00
|
|
|
) -> BOOL;
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
type SkipPointerFrameMessages = unsafe extern "system" fn(pointerId: u32) -> BOOL;
|
2019-07-26 09:12:06 +02:00
|
|
|
type GetPointerDeviceRects = unsafe extern "system" fn(
|
|
|
|
|
device: HANDLE,
|
|
|
|
|
pointerDeviceRect: *mut RECT,
|
|
|
|
|
displayRect: *mut RECT,
|
|
|
|
|
) -> BOOL;
|
2019-07-05 18:37:25 +02:00
|
|
|
|
2019-09-09 20:15:49 +02:00
|
|
|
type GetPointerTouchInfo =
|
2022-03-07 22:58:12 +01:00
|
|
|
unsafe extern "system" fn(pointerId: u32, touchInfo: *mut POINTER_TOUCH_INFO) -> BOOL;
|
2019-09-09 20:15:49 +02:00
|
|
|
|
|
|
|
|
type GetPointerPenInfo =
|
2022-03-07 22:58:12 +01:00
|
|
|
unsafe extern "system" fn(pointId: u32, penInfo: *mut POINTER_PEN_INFO) -> BOOL;
|
2019-09-09 20:15:49 +02:00
|
|
|
|
2022-06-08 11:50:26 -07:00
|
|
|
static GET_POINTER_FRAME_INFO_HISTORY: Lazy<Option<GetPointerFrameInfoHistory>> =
|
|
|
|
|
Lazy::new(|| get_function!("user32.dll", GetPointerFrameInfoHistory));
|
|
|
|
|
static SKIP_POINTER_FRAME_MESSAGES: Lazy<Option<SkipPointerFrameMessages>> =
|
|
|
|
|
Lazy::new(|| get_function!("user32.dll", SkipPointerFrameMessages));
|
|
|
|
|
static GET_POINTER_DEVICE_RECTS: Lazy<Option<GetPointerDeviceRects>> =
|
|
|
|
|
Lazy::new(|| get_function!("user32.dll", GetPointerDeviceRects));
|
|
|
|
|
static GET_POINTER_TOUCH_INFO: Lazy<Option<GetPointerTouchInfo>> =
|
|
|
|
|
Lazy::new(|| get_function!("user32.dll", GetPointerTouchInfo));
|
|
|
|
|
static GET_POINTER_PEN_INFO: Lazy<Option<GetPointerPenInfo>> =
|
|
|
|
|
Lazy::new(|| get_function!("user32.dll", GetPointerPenInfo));
|
|
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
pub(crate) struct WindowData<T: 'static> {
|
2019-02-05 10:30:33 -05:00
|
|
|
pub window_state: Arc<Mutex<WindowState>>,
|
|
|
|
|
pub event_loop_runner: EventLoopRunnerShared<T>,
|
2021-08-30 19:40:02 +02:00
|
|
|
pub _file_drop_handler: Option<FileDropHandler>,
|
2021-07-16 12:40:48 +02:00
|
|
|
pub userdata_removed: Cell<bool>,
|
2020-12-20 12:54:42 +01:00
|
|
|
pub recurse_depth: Cell<u32>,
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
impl<T> WindowData<T> {
|
2020-01-06 15:28:58 -05:00
|
|
|
unsafe fn send_event(&self, event: Event<'_, T>) {
|
2019-02-05 10:30:33 -05:00
|
|
|
self.event_loop_runner.send_event(event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
struct ThreadMsgTargetData<T: 'static> {
|
2019-02-05 10:30:33 -05:00
|
|
|
event_loop_runner: EventLoopRunnerShared<T>,
|
|
|
|
|
user_event_receiver: Receiver<T>,
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
impl<T> ThreadMsgTargetData<T> {
|
2020-01-06 15:28:58 -05:00
|
|
|
unsafe fn send_event(&self, event: Event<'_, T>) {
|
2019-02-05 10:30:33 -05:00
|
|
|
self.event_loop_runner.send_event(event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct EventLoop<T: 'static> {
|
|
|
|
|
thread_msg_sender: Sender<T>,
|
|
|
|
|
window_target: RootELW<T>,
|
2022-03-30 10:30:45 +02:00
|
|
|
msg_hook: Option<Box<dyn FnMut(*const c_void) -> bool + 'static>>,
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
|
2022-02-16 22:09:03 +01:00
|
|
|
pub(crate) struct PlatformSpecificEventLoopAttributes {
|
|
|
|
|
pub(crate) any_thread: bool,
|
|
|
|
|
pub(crate) dpi_aware: bool,
|
2022-03-30 10:30:45 +02:00
|
|
|
pub(crate) msg_hook: Option<Box<dyn FnMut(*const c_void) -> bool + 'static>>,
|
2022-02-16 22:09:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for PlatformSpecificEventLoopAttributes {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
any_thread: false,
|
|
|
|
|
dpi_aware: true,
|
2022-03-30 10:30:45 +02:00
|
|
|
msg_hook: None,
|
2022-02-16 22:09:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-19 16:49:43 -04:00
|
|
|
pub struct EventLoopWindowTarget<T: 'static> {
|
2022-03-07 22:58:12 +01:00
|
|
|
thread_id: u32,
|
2019-02-05 10:30:33 -05:00
|
|
|
thread_msg_target: HWND,
|
|
|
|
|
pub(crate) runner_shared: EventLoopRunnerShared<T>,
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 22:09:03 +01:00
|
|
|
impl<T: 'static> EventLoop<T> {
|
2022-03-30 10:30:45 +02:00
|
|
|
pub(crate) fn new(attributes: &mut PlatformSpecificEventLoopAttributes) -> Self {
|
2022-03-07 22:58:12 +01:00
|
|
|
let thread_id = unsafe { GetCurrentThreadId() };
|
2022-02-16 22:09:03 +01:00
|
|
|
|
|
|
|
|
if !attributes.any_thread && thread_id != main_thread_id() {
|
|
|
|
|
panic!(
|
2019-10-18 11:51:06 -04:00
|
|
|
"Initializing the event loop outside of the main thread is a significant \
|
2022-02-16 22:09:03 +01:00
|
|
|
cross-platform compatibility hazard. If you absolutely need to create an \
|
|
|
|
|
EventLoop on a different thread, you can use the \
|
|
|
|
|
`EventLoopBuilderExtWindows::any_thread` function."
|
|
|
|
|
);
|
2019-10-18 11:51:06 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-02-16 22:09:03 +01:00
|
|
|
if attributes.dpi_aware {
|
|
|
|
|
become_dpi_aware();
|
|
|
|
|
}
|
2020-05-04 15:14:13 -04:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
let thread_msg_target = create_event_target_window::<T>();
|
2020-05-04 15:14:13 -04:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
thread::spawn(move || wait_thread(thread_id, thread_msg_target));
|
2020-05-04 15:14:13 -04:00
|
|
|
let wait_thread_id = get_wait_thread_id();
|
|
|
|
|
|
|
|
|
|
let runner_shared = Rc::new(EventLoopRunner::new(thread_msg_target, wait_thread_id));
|
|
|
|
|
|
|
|
|
|
let thread_msg_sender =
|
2021-07-16 12:40:48 +02:00
|
|
|
insert_event_target_window_data::<T>(thread_msg_target, runner_shared.clone());
|
2019-12-30 14:11:11 -05:00
|
|
|
raw_input::register_all_mice_and_keyboards_for_raw_input(thread_msg_target);
|
2019-02-05 10:30:33 -05:00
|
|
|
|
|
|
|
|
EventLoop {
|
|
|
|
|
thread_msg_sender,
|
|
|
|
|
window_target: RootELW {
|
|
|
|
|
p: EventLoopWindowTarget {
|
|
|
|
|
thread_id,
|
|
|
|
|
thread_msg_target,
|
|
|
|
|
runner_shared,
|
|
|
|
|
},
|
|
|
|
|
_marker: PhantomData,
|
|
|
|
|
},
|
2022-03-30 10:30:45 +02:00
|
|
|
msg_hook: attributes.msg_hook.take(),
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-18 11:51:06 -04:00
|
|
|
pub fn window_target(&self) -> &RootELW<T> {
|
|
|
|
|
&self.window_target
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
pub fn run<F>(mut self, event_handler: F) -> !
|
2019-06-21 11:33:15 -04:00
|
|
|
where
|
2019-06-19 16:49:43 -04:00
|
|
|
F: 'static + FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
|
2019-02-05 10:30:33 -05:00
|
|
|
{
|
2022-01-11 01:23:20 +01:00
|
|
|
let exit_code = self.run_return(event_handler);
|
|
|
|
|
::std::process::exit(exit_code);
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
|
2022-01-11 01:23:20 +01:00
|
|
|
pub fn run_return<F>(&mut self, mut event_handler: F) -> i32
|
2019-06-21 11:33:15 -04:00
|
|
|
where
|
2019-06-19 16:49:43 -04:00
|
|
|
F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
|
2019-02-05 10:30:33 -05:00
|
|
|
{
|
|
|
|
|
let event_loop_windows_ref = &self.window_target;
|
|
|
|
|
|
2019-08-26 22:05:42 -04:00
|
|
|
unsafe {
|
|
|
|
|
self.window_target
|
|
|
|
|
.p
|
|
|
|
|
.runner_shared
|
2020-05-04 15:14:13 -04:00
|
|
|
.set_event_handler(move |event, control_flow| {
|
2019-08-26 22:05:42 -04:00
|
|
|
event_handler(event, event_loop_windows_ref, control_flow)
|
2020-05-04 15:14:13 -04:00
|
|
|
});
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
|
2019-08-26 22:05:42 -04:00
|
|
|
let runner = &self.window_target.p.runner_shared;
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-01-11 01:23:20 +01:00
|
|
|
let exit_code = unsafe {
|
2019-06-29 00:07:36 +02:00
|
|
|
let mut msg = mem::zeroed();
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2020-05-04 15:14:13 -04:00
|
|
|
runner.poll();
|
2019-02-05 10:30:33 -05:00
|
|
|
'main: loop {
|
2022-03-07 22:58:12 +01:00
|
|
|
if GetMessageW(&mut msg, 0, 0, 0) == false.into() {
|
2022-01-11 01:23:20 +01:00
|
|
|
break 'main 0;
|
2020-01-06 15:28:58 -05:00
|
|
|
}
|
2022-03-30 10:30:45 +02:00
|
|
|
|
|
|
|
|
let handled = if let Some(callback) = self.msg_hook.as_deref_mut() {
|
|
|
|
|
callback(&mut msg as *mut _ as *mut _)
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
};
|
|
|
|
|
if !handled {
|
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
|
DispatchMessageW(&msg);
|
|
|
|
|
}
|
2020-01-06 15:28:58 -05:00
|
|
|
|
2020-05-04 15:14:13 -04:00
|
|
|
if let Err(payload) = runner.take_panic_error() {
|
|
|
|
|
runner.reset_runner();
|
|
|
|
|
panic::resume_unwind(payload);
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
2020-01-06 15:28:58 -05:00
|
|
|
|
2022-01-11 01:23:20 +01:00
|
|
|
if let ControlFlow::ExitWithCode(code) = runner.control_flow() {
|
|
|
|
|
if !runner.handling_events() {
|
|
|
|
|
break 'main code;
|
|
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
}
|
2022-01-11 01:23:20 +01:00
|
|
|
};
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2020-05-04 15:14:13 -04:00
|
|
|
unsafe {
|
2020-07-02 16:53:47 -04:00
|
|
|
runner.loop_destroyed();
|
2020-05-04 15:14:13 -04:00
|
|
|
}
|
2022-01-11 01:23:20 +01:00
|
|
|
|
2020-05-04 15:14:13 -04:00
|
|
|
runner.reset_runner();
|
2022-01-11 01:23:20 +01:00
|
|
|
exit_code
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn create_proxy(&self) -> EventLoopProxy<T> {
|
|
|
|
|
EventLoopProxy {
|
|
|
|
|
target_window: self.window_target.p.thread_msg_target,
|
|
|
|
|
event_send: self.thread_msg_sender.clone(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> EventLoopWindowTarget<T> {
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub(crate) fn create_thread_executor(&self) -> EventLoopThreadExecutor {
|
|
|
|
|
EventLoopThreadExecutor {
|
|
|
|
|
thread_id: self.thread_id,
|
|
|
|
|
target_window: self.thread_msg_target,
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-04 15:46:41 -04:00
|
|
|
|
|
|
|
|
// TODO: Investigate opportunities for caching
|
|
|
|
|
pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
|
|
|
|
|
monitor::available_monitors()
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 20:20:47 +03:00
|
|
|
pub fn primary_monitor(&self) -> Option<RootMonitorHandle> {
|
|
|
|
|
let monitor = monitor::primary_monitor();
|
|
|
|
|
Some(RootMonitorHandle { inner: monitor })
|
2020-07-04 15:46:41 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
|
2021-06-05 10:46:44 +00:00
|
|
|
/// Returns the id of the main thread.
|
|
|
|
|
///
|
|
|
|
|
/// Windows has no real API to check if the current executing thread is the "main thread", unlike
|
|
|
|
|
/// macOS.
|
|
|
|
|
///
|
|
|
|
|
/// Windows will let us look up the current thread's id, but there's no API that lets us check what
|
|
|
|
|
/// the id of the main thread is. We would somehow need to get the main thread's id before a
|
|
|
|
|
/// developer could spin off any other threads inside of the main entrypoint in order to emulate the
|
|
|
|
|
/// capabilities of other platforms.
|
|
|
|
|
///
|
|
|
|
|
/// We can get the id of the main thread by using CRT initialization. CRT initialization can be used
|
|
|
|
|
/// to setup global state within a program. The OS will call a list of function pointers which
|
|
|
|
|
/// assign values to a static variable. To have get a hold of the main thread id, we need to place
|
|
|
|
|
/// our function pointer inside of the `.CRT$XCU` section so it is called before the main
|
|
|
|
|
/// entrypoint.
|
|
|
|
|
///
|
|
|
|
|
/// Full details of CRT initialization can be found here:
|
2022-06-17 14:19:09 +02:00
|
|
|
/// <https://docs.microsoft.com/en-us/cpp/c-runtime-library/crt-initialization?view=msvc-160>
|
2022-03-07 22:58:12 +01:00
|
|
|
fn main_thread_id() -> u32 {
|
|
|
|
|
static mut MAIN_THREAD_ID: u32 = 0;
|
2021-06-05 10:46:44 +00:00
|
|
|
|
|
|
|
|
/// Function pointer used in CRT initialization section to set the above static field's value.
|
|
|
|
|
|
|
|
|
|
// Mark as used so this is not removable.
|
2019-10-18 11:51:06 -04:00
|
|
|
#[used]
|
|
|
|
|
#[allow(non_upper_case_globals)]
|
2021-06-05 10:46:44 +00:00
|
|
|
// Place the function pointer inside of CRT initialization section so it is loaded before
|
|
|
|
|
// main entrypoint.
|
|
|
|
|
//
|
|
|
|
|
// See: https://doc.rust-lang.org/stable/reference/abi.html#the-link_section-attribute
|
2019-10-18 11:51:06 -04:00
|
|
|
#[link_section = ".CRT$XCU"]
|
|
|
|
|
static INIT_MAIN_THREAD_ID: unsafe fn() = {
|
|
|
|
|
unsafe fn initer() {
|
2022-03-07 22:58:12 +01:00
|
|
|
MAIN_THREAD_ID = GetCurrentThreadId();
|
2019-10-18 11:51:06 -04:00
|
|
|
}
|
|
|
|
|
initer
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
unsafe { MAIN_THREAD_ID }
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
fn get_wait_thread_id() -> u32 {
|
2020-05-04 15:14:13 -04:00
|
|
|
unsafe {
|
|
|
|
|
let mut msg = mem::zeroed();
|
2022-03-07 22:58:12 +01:00
|
|
|
let result = GetMessageW(
|
2020-05-04 15:14:13 -04:00
|
|
|
&mut msg,
|
2022-03-07 22:58:12 +01:00
|
|
|
-1,
|
2020-05-04 15:14:13 -04:00
|
|
|
*SEND_WAIT_THREAD_ID_MSG_ID,
|
|
|
|
|
*SEND_WAIT_THREAD_ID_MSG_ID,
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
msg.message, *SEND_WAIT_THREAD_ID_MSG_ID,
|
|
|
|
|
"this shouldn't be possible. please open an issue with Winit. error code: {}",
|
|
|
|
|
result
|
|
|
|
|
);
|
2022-03-07 22:58:12 +01:00
|
|
|
msg.lParam as u32
|
2020-05-04 15:14:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 11:50:26 -07:00
|
|
|
static WAIT_PERIOD_MIN: Lazy<Option<u32>> = Lazy::new(|| unsafe {
|
|
|
|
|
let mut caps = TIMECAPS {
|
|
|
|
|
wPeriodMin: 0,
|
|
|
|
|
wPeriodMax: 0,
|
2021-11-02 21:51:39 +01:00
|
|
|
};
|
2022-06-08 11:50:26 -07:00
|
|
|
if timeGetDevCaps(&mut caps, mem::size_of::<TIMECAPS>() as u32) == TIMERR_NOERROR {
|
|
|
|
|
Some(caps.wPeriodMin)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-11-02 21:51:39 +01:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
fn wait_thread(parent_thread_id: u32, msg_window_id: HWND) {
|
2020-05-04 15:14:13 -04:00
|
|
|
unsafe {
|
2022-03-07 22:58:12 +01:00
|
|
|
let mut msg: MSG;
|
2020-05-04 15:14:13 -04:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
let cur_thread_id = GetCurrentThreadId();
|
|
|
|
|
PostThreadMessageW(
|
2020-05-04 15:14:13 -04:00
|
|
|
parent_thread_id,
|
|
|
|
|
*SEND_WAIT_THREAD_ID_MSG_ID,
|
2019-02-05 10:30:33 -05:00
|
|
|
0,
|
2020-05-04 15:14:13 -04:00
|
|
|
cur_thread_id as LPARAM,
|
2019-02-05 10:30:33 -05:00
|
|
|
);
|
|
|
|
|
|
2020-05-04 15:14:13 -04:00
|
|
|
let mut wait_until_opt = None;
|
|
|
|
|
'main: loop {
|
|
|
|
|
// Zeroing out the message ensures that the `WaitUntilInstantBox` doesn't get
|
|
|
|
|
// double-freed if `MsgWaitForMultipleObjectsEx` returns early and there aren't
|
|
|
|
|
// additional messages to process.
|
|
|
|
|
msg = mem::zeroed();
|
|
|
|
|
|
|
|
|
|
if wait_until_opt.is_some() {
|
2022-03-07 22:58:12 +01:00
|
|
|
if PeekMessageW(&mut msg, 0, 0, 0, PM_REMOVE) != false.into() {
|
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
|
DispatchMessageW(&msg);
|
2020-05-04 15:14:13 -04:00
|
|
|
}
|
2022-03-07 22:58:12 +01:00
|
|
|
} else if GetMessageW(&mut msg, 0, 0, 0) == false.into() {
|
|
|
|
|
break 'main;
|
2020-05-04 15:14:13 -04:00
|
|
|
} else {
|
2022-03-07 22:58:12 +01:00
|
|
|
TranslateMessage(&msg);
|
|
|
|
|
DispatchMessageW(&msg);
|
2020-05-04 15:14:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if msg.message == *WAIT_UNTIL_MSG_ID {
|
|
|
|
|
wait_until_opt = Some(*WaitUntilInstantBox::from_raw(msg.lParam as *mut _));
|
|
|
|
|
} else if msg.message == *CANCEL_WAIT_UNTIL_MSG_ID {
|
|
|
|
|
wait_until_opt = None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(wait_until) = wait_until_opt {
|
|
|
|
|
let now = Instant::now();
|
|
|
|
|
if now < wait_until {
|
2021-11-02 21:51:39 +01:00
|
|
|
// Windows' scheduler has a default accuracy of several ms. This isn't good enough for
|
|
|
|
|
// `WaitUntil`, so we request the Windows scheduler to use a higher accuracy if possible.
|
|
|
|
|
// If we couldn't query the timer capabilities, then we use the default resolution.
|
|
|
|
|
if let Some(period) = *WAIT_PERIOD_MIN {
|
2022-03-07 22:58:12 +01:00
|
|
|
timeBeginPeriod(period);
|
2021-11-02 21:51:39 +01:00
|
|
|
}
|
|
|
|
|
// `MsgWaitForMultipleObjects` is bound by the granularity of the scheduler period.
|
|
|
|
|
// Because of this, we try to reduce the requested time just enough to undershoot `wait_until`
|
|
|
|
|
// by the smallest amount possible, and then we busy loop for the remaining time inside the
|
|
|
|
|
// NewEvents message handler.
|
2022-03-07 22:58:12 +01:00
|
|
|
let resume_reason = MsgWaitForMultipleObjectsEx(
|
2020-05-04 15:14:13 -04:00
|
|
|
0,
|
|
|
|
|
ptr::null(),
|
2021-11-02 21:51:39 +01:00
|
|
|
dur2timeout(wait_until - now).saturating_sub(WAIT_PERIOD_MIN.unwrap_or(1)),
|
2022-03-07 22:58:12 +01:00
|
|
|
QS_ALLEVENTS,
|
|
|
|
|
MWMO_INPUTAVAILABLE,
|
2020-05-04 15:14:13 -04:00
|
|
|
);
|
2021-11-02 21:51:39 +01:00
|
|
|
if let Some(period) = *WAIT_PERIOD_MIN {
|
2022-03-07 22:58:12 +01:00
|
|
|
timeEndPeriod(period);
|
2021-11-02 21:51:39 +01:00
|
|
|
}
|
2022-03-07 22:58:12 +01:00
|
|
|
if resume_reason == WAIT_TIMEOUT {
|
|
|
|
|
PostMessageW(msg_window_id, *PROCESS_NEW_EVENTS_MSG_ID, 0, 0);
|
2020-05-04 15:14:13 -04:00
|
|
|
wait_until_opt = None;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-03-07 22:58:12 +01:00
|
|
|
PostMessageW(msg_window_id, *PROCESS_NEW_EVENTS_MSG_ID, 0, 0);
|
2020-05-04 15:14:13 -04:00
|
|
|
wait_until_opt = None;
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-19 01:27:47 +01:00
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
// Implementation taken from https://github.com/rust-lang/rust/blob/db5476571d9b27c862b95c1e64764b0ac8980e23/src/libstd/sys/windows/mod.rs
|
2022-03-07 22:58:12 +01:00
|
|
|
fn dur2timeout(dur: Duration) -> u32 {
|
2019-02-05 10:30:33 -05:00
|
|
|
// Note that a duration is a (u64, u32) (seconds, nanoseconds) pair, and the
|
|
|
|
|
// timeouts in windows APIs are typically u32 milliseconds. To translate, we
|
|
|
|
|
// have two pieces to take care of:
|
|
|
|
|
//
|
|
|
|
|
// * Nanosecond precision is rounded up
|
|
|
|
|
// * Greater than u32::MAX milliseconds (50 days) is rounded up to INFINITE
|
|
|
|
|
// (never time out).
|
2019-06-21 11:33:15 -04:00
|
|
|
dur.as_secs()
|
|
|
|
|
.checked_mul(1000)
|
|
|
|
|
.and_then(|ms| ms.checked_add((dur.subsec_nanos() as u64) / 1_000_000))
|
|
|
|
|
.and_then(|ms| {
|
|
|
|
|
ms.checked_add(if dur.subsec_nanos() % 1_000_000 > 0 {
|
|
|
|
|
1
|
|
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.map(|ms| {
|
2022-03-07 22:58:12 +01:00
|
|
|
if ms > u32::MAX as u64 {
|
|
|
|
|
INFINITE
|
2019-06-21 11:33:15 -04:00
|
|
|
} else {
|
2022-03-07 22:58:12 +01:00
|
|
|
ms as u32
|
2019-06-21 11:33:15 -04:00
|
|
|
}
|
|
|
|
|
})
|
2022-03-07 22:58:12 +01:00
|
|
|
.unwrap_or(INFINITE)
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Drop for EventLoop<T> {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
unsafe {
|
2022-03-07 22:58:12 +01:00
|
|
|
DestroyWindow(self.window_target.p.thread_msg_target);
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) struct EventLoopThreadExecutor {
|
2022-03-07 22:58:12 +01:00
|
|
|
thread_id: u32,
|
2019-02-05 10:30:33 -05:00
|
|
|
target_window: HWND,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe impl Send for EventLoopThreadExecutor {}
|
|
|
|
|
unsafe impl Sync for EventLoopThreadExecutor {}
|
|
|
|
|
|
|
|
|
|
impl EventLoopThreadExecutor {
|
|
|
|
|
/// Check to see if we're in the parent event loop's thread.
|
|
|
|
|
pub(super) fn in_event_loop_thread(&self) -> bool {
|
2022-03-07 22:58:12 +01:00
|
|
|
let cur_thread_id = unsafe { GetCurrentThreadId() };
|
2019-02-05 10:30:33 -05:00
|
|
|
self.thread_id == cur_thread_id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Executes a function in the event loop thread. If we're already in the event loop thread,
|
|
|
|
|
/// we just call the function directly.
|
|
|
|
|
///
|
|
|
|
|
/// The `Inserted` can be used to inject a `WindowState` for the callback to use. The state is
|
|
|
|
|
/// removed automatically if the callback receives a `WM_CLOSE` message for the window.
|
|
|
|
|
///
|
|
|
|
|
/// Note that if you are using this to change some property of a window and updating
|
|
|
|
|
/// `WindowState` then you should call this within the lock of `WindowState`. Otherwise the
|
|
|
|
|
/// events may be sent to the other thread in different order to the one in which you set
|
|
|
|
|
/// `WindowState`, leaving them out of sync.
|
|
|
|
|
///
|
|
|
|
|
/// Note that we use a FnMut instead of a FnOnce because we're too lazy to create an equivalent
|
|
|
|
|
/// to the unstable FnBox.
|
|
|
|
|
pub(super) fn execute_in_thread<F>(&self, mut function: F)
|
2019-06-21 11:33:15 -04:00
|
|
|
where
|
|
|
|
|
F: FnMut() + Send + 'static,
|
2019-02-05 10:30:33 -05:00
|
|
|
{
|
|
|
|
|
unsafe {
|
|
|
|
|
if self.in_event_loop_thread() {
|
|
|
|
|
function();
|
|
|
|
|
} else {
|
|
|
|
|
// We double-box because the first box is a fat pointer.
|
2022-03-07 22:58:12 +01:00
|
|
|
let boxed2: ThreadExecFn = Box::new(Box::new(function));
|
2019-02-05 10:30:33 -05:00
|
|
|
|
|
|
|
|
let raw = Box::into_raw(boxed2);
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
let res = PostMessageW(self.target_window, *EXEC_MSG_ID, raw as usize, 0);
|
|
|
|
|
assert!(
|
|
|
|
|
res != false.into(),
|
|
|
|
|
"PostMessage failed; is the messages queue full?"
|
2019-02-05 10:30:33 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-18 02:27:00 +08:00
|
|
|
type ThreadExecFn = Box<Box<dyn FnMut()>>;
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2019-02-21 10:51:43 +01:00
|
|
|
pub struct EventLoopProxy<T: 'static> {
|
2019-02-05 10:30:33 -05:00
|
|
|
target_window: HWND,
|
|
|
|
|
event_send: Sender<T>,
|
|
|
|
|
}
|
2019-02-21 10:51:43 +01:00
|
|
|
unsafe impl<T: Send + 'static> Send for EventLoopProxy<T> {}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2019-08-06 05:51:42 +09:00
|
|
|
impl<T: 'static> Clone for EventLoopProxy<T> {
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
target_window: self.target_window,
|
|
|
|
|
event_send: self.event_send.clone(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 10:51:43 +01:00
|
|
|
impl<T: 'static> EventLoopProxy<T> {
|
2019-12-07 18:22:03 +01:00
|
|
|
pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> {
|
2019-02-05 10:30:33 -05:00
|
|
|
unsafe {
|
2022-03-07 22:58:12 +01:00
|
|
|
if PostMessageW(self.target_window, *USER_EVENT_MSG_ID, 0, 0) != false.into() {
|
2019-02-05 10:30:33 -05:00
|
|
|
self.event_send.send(event).ok();
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
2019-12-07 18:22:03 +01:00
|
|
|
Err(EventLoopClosed(event))
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 15:14:13 -04:00
|
|
|
type WaitUntilInstantBox = Box<Instant>;
|
|
|
|
|
|
2022-06-08 11:50:26 -07:00
|
|
|
// Message sent by the `EventLoopProxy` when we want to wake up the thread.
|
|
|
|
|
// WPARAM and LPARAM are unused.
|
|
|
|
|
static USER_EVENT_MSG_ID: Lazy<u32> =
|
|
|
|
|
Lazy::new(|| unsafe { RegisterWindowMessageA("Winit::WakeupMsg\0".as_ptr()) });
|
|
|
|
|
// Message sent when we want to execute a closure in the thread.
|
|
|
|
|
// WPARAM contains a Box<Box<dyn FnMut()>> that must be retrieved with `Box::from_raw`,
|
|
|
|
|
// and LPARAM is unused.
|
|
|
|
|
static EXEC_MSG_ID: Lazy<u32> =
|
|
|
|
|
Lazy::new(|| unsafe { RegisterWindowMessageA("Winit::ExecMsg\0".as_ptr()) });
|
|
|
|
|
static PROCESS_NEW_EVENTS_MSG_ID: Lazy<u32> =
|
|
|
|
|
Lazy::new(|| unsafe { RegisterWindowMessageA("Winit::ProcessNewEvents\0".as_ptr()) });
|
|
|
|
|
/// lparam is the wait thread's message id.
|
|
|
|
|
static SEND_WAIT_THREAD_ID_MSG_ID: Lazy<u32> =
|
|
|
|
|
Lazy::new(|| unsafe { RegisterWindowMessageA("Winit::SendWaitThreadId\0".as_ptr()) });
|
|
|
|
|
/// lparam points to a `Box<Instant>` signifying the time `PROCESS_NEW_EVENTS_MSG_ID` should
|
|
|
|
|
/// be sent.
|
|
|
|
|
static WAIT_UNTIL_MSG_ID: Lazy<u32> =
|
|
|
|
|
Lazy::new(|| unsafe { RegisterWindowMessageA("Winit::WaitUntil\0".as_ptr()) });
|
|
|
|
|
static CANCEL_WAIT_UNTIL_MSG_ID: Lazy<u32> =
|
|
|
|
|
Lazy::new(|| unsafe { RegisterWindowMessageA("Winit::CancelWaitUntil\0".as_ptr()) });
|
|
|
|
|
// Message sent by a `Window` when it wants to be destroyed by the main thread.
|
|
|
|
|
// WPARAM and LPARAM are unused.
|
|
|
|
|
pub static DESTROY_MSG_ID: Lazy<u32> =
|
|
|
|
|
Lazy::new(|| unsafe { RegisterWindowMessageA("Winit::DestroyMsg\0".as_ptr()) });
|
|
|
|
|
// WPARAM is a bool specifying the `WindowFlags::MARKER_RETAIN_STATE_ON_SIZE` flag. See the
|
|
|
|
|
// documentation in the `window_state` module for more information.
|
|
|
|
|
pub static SET_RETAIN_STATE_ON_SIZE_MSG_ID: Lazy<u32> =
|
|
|
|
|
Lazy::new(|| unsafe { RegisterWindowMessageA("Winit::SetRetainMaximized\0".as_ptr()) });
|
|
|
|
|
static THREAD_EVENT_TARGET_WINDOW_CLASS: Lazy<Vec<u16>> =
|
|
|
|
|
Lazy::new(|| util::encode_wide("Winit Thread Event Target"));
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
fn create_event_target_window<T: 'static>() -> HWND {
|
|
|
|
|
unsafe {
|
2022-03-07 22:58:12 +01:00
|
|
|
let class = WNDCLASSEXW {
|
|
|
|
|
cbSize: mem::size_of::<WNDCLASSEXW>() as u32,
|
2019-02-05 10:30:33 -05:00
|
|
|
style: 0,
|
2021-07-16 12:40:48 +02:00
|
|
|
lpfnWndProc: Some(thread_event_target_callback::<T>),
|
2019-02-05 10:30:33 -05:00
|
|
|
cbClsExtra: 0,
|
|
|
|
|
cbWndExtra: 0,
|
2022-05-29 17:12:46 +02:00
|
|
|
hInstance: util::get_instance_handle(),
|
2022-03-07 22:58:12 +01:00
|
|
|
hIcon: 0,
|
|
|
|
|
hCursor: 0, // must be null in order for cursor state to work properly
|
|
|
|
|
hbrBackground: 0,
|
2019-02-05 10:30:33 -05:00
|
|
|
lpszMenuName: ptr::null(),
|
2021-07-16 12:40:48 +02:00
|
|
|
lpszClassName: THREAD_EVENT_TARGET_WINDOW_CLASS.as_ptr(),
|
2022-03-07 22:58:12 +01:00
|
|
|
hIconSm: 0,
|
2019-02-05 10:30:33 -05:00
|
|
|
};
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
RegisterClassExW(&class);
|
2021-07-16 12:40:48 +02:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
|
|
|
|
unsafe {
|
2022-03-07 22:58:12 +01:00
|
|
|
let window = CreateWindowExW(
|
|
|
|
|
WS_EX_NOACTIVATE
|
|
|
|
|
| WS_EX_TRANSPARENT
|
|
|
|
|
| WS_EX_LAYERED
|
2021-08-11 14:02:40 -04:00
|
|
|
// WS_EX_TOOLWINDOW prevents this window from ever showing up in the taskbar, which
|
|
|
|
|
// we want to avoid. If you remove this style, this window won't show up in the
|
|
|
|
|
// taskbar *initially*, but it can show up at some later point. This can sometimes
|
|
|
|
|
// happen on its own after several hours have passed, although this has proven
|
|
|
|
|
// difficult to reproduce. Alternatively, it can be manually triggered by killing
|
|
|
|
|
// `explorer.exe` and then starting the process back up.
|
|
|
|
|
// It is unclear why the bug is triggered by waiting for several hours.
|
2022-03-07 22:58:12 +01:00
|
|
|
| WS_EX_TOOLWINDOW,
|
2019-02-05 10:30:33 -05:00
|
|
|
THREAD_EVENT_TARGET_WINDOW_CLASS.as_ptr(),
|
2022-03-07 22:58:12 +01:00
|
|
|
ptr::null(),
|
|
|
|
|
WS_OVERLAPPED,
|
2019-02-05 10:30:33 -05:00
|
|
|
0,
|
2019-06-21 11:33:15 -04:00
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
2022-03-07 22:58:12 +01:00
|
|
|
0,
|
2022-05-29 17:12:46 +02:00
|
|
|
util::get_instance_handle(),
|
2022-03-07 22:58:12 +01:00
|
|
|
ptr::null(),
|
2019-02-05 10:30:33 -05:00
|
|
|
);
|
2022-03-07 22:58:12 +01:00
|
|
|
|
|
|
|
|
super::set_window_long(
|
2019-02-05 10:30:33 -05:00
|
|
|
window,
|
2022-03-07 22:58:12 +01:00
|
|
|
GWL_STYLE,
|
2019-02-05 10:30:33 -05:00
|
|
|
// The window technically has to be visible to receive WM_PAINT messages (which are used
|
|
|
|
|
// for delivering events during resizes), but it isn't displayed to the user because of
|
|
|
|
|
// the LAYERED style.
|
2022-03-07 22:58:12 +01:00
|
|
|
(WS_VISIBLE | WS_POPUP) as isize,
|
2019-02-05 10:30:33 -05:00
|
|
|
);
|
2020-05-04 15:14:13 -04:00
|
|
|
window
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
fn insert_event_target_window_data<T>(
|
|
|
|
|
thread_msg_target: HWND,
|
2020-05-04 15:14:13 -04:00
|
|
|
event_loop_runner: EventLoopRunnerShared<T>,
|
|
|
|
|
) -> Sender<T> {
|
2021-07-16 12:40:48 +02:00
|
|
|
let (tx, rx) = mpsc::channel();
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
let userdata = ThreadMsgTargetData {
|
|
|
|
|
event_loop_runner,
|
|
|
|
|
user_event_receiver: rx,
|
|
|
|
|
};
|
|
|
|
|
let input_ptr = Box::into_raw(Box::new(userdata));
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
unsafe { super::set_window_long(thread_msg_target, GWL_USERDATA, input_ptr as isize) };
|
2021-07-16 12:40:48 +02:00
|
|
|
|
|
|
|
|
tx
|
2020-12-20 12:54:42 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
/// Capture mouse input, allowing `window` to receive mouse events when the cursor is outside of
|
|
|
|
|
/// the window.
|
|
|
|
|
unsafe fn capture_mouse(window: HWND, window_state: &mut WindowState) {
|
2020-12-15 03:31:13 -03:00
|
|
|
window_state.mouse.capture_count += 1;
|
2022-03-07 22:58:12 +01:00
|
|
|
SetCapture(window);
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Release mouse input, stopping windows on this thread from receiving mouse input when the cursor
|
|
|
|
|
/// is outside the window.
|
2020-12-15 03:31:13 -03:00
|
|
|
unsafe fn release_mouse(mut window_state: parking_lot::MutexGuard<'_, WindowState>) {
|
|
|
|
|
window_state.mouse.capture_count = window_state.mouse.capture_count.saturating_sub(1);
|
|
|
|
|
if window_state.mouse.capture_count == 0 {
|
|
|
|
|
// ReleaseCapture() causes a WM_CAPTURECHANGED where we lock the window_state.
|
|
|
|
|
drop(window_state);
|
2022-03-07 22:58:12 +01:00
|
|
|
ReleaseCapture();
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 20:15:49 +02:00
|
|
|
fn normalize_pointer_pressure(pressure: u32) -> Option<Force> {
|
|
|
|
|
match pressure {
|
|
|
|
|
1..=1024 => Some(Force::Normalized(pressure as f64 / 1024.0)),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 15:14:13 -04:00
|
|
|
/// Flush redraw events for Winit's windows.
|
|
|
|
|
///
|
|
|
|
|
/// Winit's API guarantees that all redraw events will be clustered together and dispatched all at
|
|
|
|
|
/// once, but the standard Windows message loop doesn't always exhibit that behavior. If multiple
|
|
|
|
|
/// windows have had redraws scheduled, but an input event is pushed to the message queue between
|
|
|
|
|
/// the `WM_PAINT` call for the first window and the `WM_PAINT` call for the second window, Windows
|
|
|
|
|
/// will dispatch the input event immediately instead of flushing all the redraw events. This
|
|
|
|
|
/// function explicitly pulls all of Winit's redraw events out of the event queue so that they
|
|
|
|
|
/// always all get processed in one fell swoop.
|
|
|
|
|
///
|
|
|
|
|
/// Returns `true` if this invocation flushed all the redraw events. If this function is re-entrant,
|
|
|
|
|
/// it won't flush the redraw events and will return `false`.
|
|
|
|
|
#[must_use]
|
|
|
|
|
unsafe fn flush_paint_messages<T: 'static>(
|
|
|
|
|
except: Option<HWND>,
|
|
|
|
|
runner: &EventLoopRunner<T>,
|
|
|
|
|
) -> bool {
|
|
|
|
|
if !runner.redrawing() {
|
|
|
|
|
runner.main_events_cleared();
|
|
|
|
|
let mut msg = mem::zeroed();
|
|
|
|
|
runner.owned_windows(|redraw_window| {
|
|
|
|
|
if Some(redraw_window) == except {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
if PeekMessageW(
|
2020-05-04 15:14:13 -04:00
|
|
|
&mut msg,
|
|
|
|
|
redraw_window,
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_PAINT,
|
|
|
|
|
WM_PAINT,
|
|
|
|
|
PM_REMOVE | PM_QS_PAINT,
|
|
|
|
|
) == false.into()
|
|
|
|
|
{
|
2020-05-04 15:14:13 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
TranslateMessage(&msg);
|
|
|
|
|
DispatchMessageW(&msg);
|
2020-05-04 15:14:13 -04:00
|
|
|
});
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe fn process_control_flow<T: 'static>(runner: &EventLoopRunner<T>) {
|
|
|
|
|
match runner.control_flow() {
|
|
|
|
|
ControlFlow::Poll => {
|
2022-03-07 22:58:12 +01:00
|
|
|
PostMessageW(runner.thread_msg_target(), *PROCESS_NEW_EVENTS_MSG_ID, 0, 0);
|
2020-05-04 15:14:13 -04:00
|
|
|
}
|
|
|
|
|
ControlFlow::Wait => (),
|
|
|
|
|
ControlFlow::WaitUntil(until) => {
|
2022-03-07 22:58:12 +01:00
|
|
|
PostThreadMessageW(
|
2020-05-04 15:14:13 -04:00
|
|
|
runner.wait_thread_id(),
|
|
|
|
|
*WAIT_UNTIL_MSG_ID,
|
|
|
|
|
0,
|
2022-03-07 22:58:12 +01:00
|
|
|
Box::into_raw(WaitUntilInstantBox::new(until)) as isize,
|
2020-05-04 15:14:13 -04:00
|
|
|
);
|
|
|
|
|
}
|
2022-01-11 01:23:20 +01:00
|
|
|
ControlFlow::ExitWithCode(_) => (),
|
2020-05-04 15:14:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
/// Emit a `ModifiersChanged` event whenever modifiers have changed.
|
2021-07-16 12:40:48 +02:00
|
|
|
fn update_modifiers<T>(window: HWND, userdata: &WindowData<T>) {
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
use crate::event::WindowEvent::ModifiersChanged;
|
|
|
|
|
|
|
|
|
|
let modifiers = event::get_key_mods();
|
2021-07-16 12:40:48 +02:00
|
|
|
let mut window_state = userdata.window_state.lock();
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
if window_state.modifiers_state != modifiers {
|
|
|
|
|
window_state.modifiers_state = modifiers;
|
|
|
|
|
|
2020-03-08 01:22:53 -05:00
|
|
|
// Drop lock
|
|
|
|
|
drop(window_state);
|
|
|
|
|
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
unsafe {
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: ModifiersChanged(modifiers),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
/// 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.
|
|
|
|
|
//
|
|
|
|
|
// This is the callback that is called by `DispatchMessage` in the events loop.
|
|
|
|
|
//
|
|
|
|
|
// Returning 0 tells the Win32 API that the message has been processed.
|
|
|
|
|
// FIXME: detect WM_DWMCOMPOSITIONCHANGED and call DwmEnableBlurBehindWindow if necessary
|
2021-07-16 12:40:48 +02:00
|
|
|
pub(super) unsafe extern "system" fn public_window_callback<T: 'static>(
|
2019-02-05 10:30:33 -05:00
|
|
|
window: HWND,
|
2022-03-07 22:58:12 +01:00
|
|
|
msg: u32,
|
2019-02-05 10:30:33 -05:00
|
|
|
wparam: WPARAM,
|
|
|
|
|
lparam: LPARAM,
|
|
|
|
|
) -> LRESULT {
|
2022-03-07 22:58:12 +01:00
|
|
|
let userdata = super::get_window_long(window, GWL_USERDATA);
|
|
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
let userdata_ptr = match (userdata, msg) {
|
2022-03-07 22:58:12 +01:00
|
|
|
(0, WM_NCCREATE) => {
|
|
|
|
|
let createstruct = &mut *(lparam as *mut CREATESTRUCTW);
|
|
|
|
|
let initdata = &mut *(createstruct.lpCreateParams as *mut InitData<'_, T>);
|
2021-07-16 12:40:48 +02:00
|
|
|
|
2021-11-17 18:33:44 +01:00
|
|
|
let result = match initdata.on_nccreate(window) {
|
|
|
|
|
Some(userdata) => {
|
2022-03-07 22:58:12 +01:00
|
|
|
super::set_window_long(window, GWL_USERDATA, userdata as _);
|
|
|
|
|
DefWindowProcW(window, msg, wparam, lparam)
|
2021-11-17 18:33:44 +01:00
|
|
|
}
|
|
|
|
|
None => -1, // failed to create the window
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return result;
|
2021-07-16 12:40:48 +02:00
|
|
|
}
|
|
|
|
|
// Getting here should quite frankly be impossible,
|
|
|
|
|
// but we'll make window creation fail here just in case.
|
2022-03-07 22:58:12 +01:00
|
|
|
(0, WM_CREATE) => return -1,
|
|
|
|
|
(_, WM_CREATE) => {
|
|
|
|
|
let createstruct = &mut *(lparam as *mut CREATESTRUCTW);
|
|
|
|
|
let initdata = createstruct.lpCreateParams;
|
2021-11-17 18:33:44 +01:00
|
|
|
let initdata = &mut *(initdata as *mut InitData<'_, T>);
|
|
|
|
|
|
|
|
|
|
initdata.on_create();
|
2022-03-07 22:58:12 +01:00
|
|
|
return DefWindowProcW(window, msg, wparam, lparam);
|
2021-11-17 18:33:44 +01:00
|
|
|
}
|
2022-03-07 22:58:12 +01:00
|
|
|
(0, _) => return DefWindowProcW(window, msg, wparam, lparam),
|
2021-07-16 12:40:48 +02:00
|
|
|
_ => userdata as *mut WindowData<T>,
|
|
|
|
|
};
|
2020-12-20 12:54:42 +01:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
let (result, userdata_removed, recurse_depth) = {
|
|
|
|
|
let userdata = &*(userdata_ptr);
|
2020-12-20 12:54:42 +01:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.recurse_depth.set(userdata.recurse_depth.get() + 1);
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
let result = public_window_callback_inner(window, msg, wparam, lparam, userdata);
|
2021-07-16 12:40:48 +02:00
|
|
|
|
|
|
|
|
let userdata_removed = userdata.userdata_removed.get();
|
|
|
|
|
let recurse_depth = userdata.recurse_depth.get() - 1;
|
|
|
|
|
userdata.recurse_depth.set(recurse_depth);
|
|
|
|
|
|
|
|
|
|
(result, userdata_removed, recurse_depth)
|
2020-12-20 12:54:42 +01:00
|
|
|
};
|
|
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
if userdata_removed && recurse_depth == 0 {
|
|
|
|
|
Box::from_raw(userdata_ptr);
|
2020-12-20 12:54:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe fn public_window_callback_inner<T: 'static>(
|
|
|
|
|
window: HWND,
|
2022-03-07 22:58:12 +01:00
|
|
|
msg: u32,
|
2020-12-20 12:54:42 +01:00
|
|
|
wparam: WPARAM,
|
|
|
|
|
lparam: LPARAM,
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata: &WindowData<T>,
|
2020-12-20 12:54:42 +01:00
|
|
|
) -> LRESULT {
|
2022-03-07 22:58:12 +01:00
|
|
|
RedrawWindow(
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.event_loop_runner.thread_msg_target(),
|
2020-05-04 15:14:13 -04:00
|
|
|
ptr::null(),
|
2022-03-07 22:58:12 +01:00
|
|
|
0,
|
|
|
|
|
RDW_INTERNALPAINT,
|
2020-05-04 15:14:13 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// I decided to bind the closure to `callback` and pass it to catch_unwind rather than passing
|
|
|
|
|
// the closure to catch_unwind directly so that the match body indendation wouldn't change and
|
|
|
|
|
// the git blame and history would be preserved.
|
|
|
|
|
let callback = || match msg {
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_ENTERSIZEMOVE => {
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata
|
2020-05-04 15:14:13 -04:00
|
|
|
.window_state
|
|
|
|
|
.lock()
|
|
|
|
|
.set_window_flags_in_place(|f| f.insert(WindowFlags::MARKER_IN_SIZE_MOVE));
|
2019-02-05 10:30:33 -05:00
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2020-05-04 15:14:13 -04:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_EXITSIZEMOVE => {
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata
|
2020-05-04 15:14:13 -04:00
|
|
|
.window_state
|
|
|
|
|
.lock()
|
|
|
|
|
.set_window_flags_in_place(|f| f.remove(WindowFlags::MARKER_IN_SIZE_MOVE));
|
2019-02-05 10:30:33 -05:00
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2020-05-04 15:14:13 -04:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_NCLBUTTONDOWN => {
|
|
|
|
|
if wparam == HTCAPTION as _ {
|
|
|
|
|
PostMessageW(window, WM_MOUSEMOVE, 0, lparam);
|
2019-04-14 11:48:31 -04:00
|
|
|
}
|
2022-03-07 22:58:12 +01:00
|
|
|
DefWindowProcW(window, msg, wparam, lparam)
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-04-14 11:48:31 -04:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_CLOSE => {
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::event::WindowEvent::CloseRequested;
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: CloseRequested,
|
|
|
|
|
});
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_DESTROY => {
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::event::WindowEvent::Destroyed;
|
2022-03-07 22:58:12 +01:00
|
|
|
RevokeDragDrop(window);
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: Destroyed,
|
|
|
|
|
});
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.event_loop_runner.remove_window(window);
|
2020-10-24 00:04:18 +07:00
|
|
|
0
|
|
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_NCDESTROY => {
|
|
|
|
|
super::set_window_long(window, GWL_USERDATA, 0);
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.userdata_removed.set(true);
|
2019-02-05 10:30:33 -05:00
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_PAINT => {
|
2021-07-16 12:40:48 +02:00
|
|
|
if userdata.event_loop_runner.should_buffer() {
|
2020-05-04 15:14:13 -04:00
|
|
|
// this branch can happen in response to `UpdateWindow`, if win32 decides to
|
|
|
|
|
// redraw the window outside the normal flow of the event loop.
|
2022-03-07 22:58:12 +01:00
|
|
|
RedrawWindow(window, ptr::null(), 0, RDW_INTERNALPAINT);
|
2020-05-04 15:14:13 -04:00
|
|
|
} else {
|
|
|
|
|
let managing_redraw =
|
2021-07-16 12:40:48 +02:00
|
|
|
flush_paint_messages(Some(window), &userdata.event_loop_runner);
|
|
|
|
|
userdata.send_event(Event::RedrawRequested(RootWindowId(WindowId(window))));
|
2020-05-04 15:14:13 -04:00
|
|
|
if managing_redraw {
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.event_loop_runner.redraw_events_cleared();
|
|
|
|
|
process_control_flow(&userdata.event_loop_runner);
|
2020-05-04 15:14:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
DefWindowProcW(window, msg, wparam, lparam)
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_WINDOWPOSCHANGING => {
|
2021-07-16 12:40:48 +02:00
|
|
|
let mut window_state = userdata.window_state.lock();
|
2019-10-05 16:23:30 -04:00
|
|
|
if let Some(ref mut fullscreen) = window_state.fullscreen {
|
2022-03-07 22:58:12 +01:00
|
|
|
let window_pos = &mut *(lparam as *mut WINDOWPOS);
|
2019-10-05 16:23:30 -04:00
|
|
|
let new_rect = RECT {
|
|
|
|
|
left: window_pos.x,
|
|
|
|
|
top: window_pos.y,
|
|
|
|
|
right: window_pos.x + window_pos.cx,
|
|
|
|
|
bottom: window_pos.y + window_pos.cy,
|
|
|
|
|
};
|
2022-04-30 13:21:08 +02:00
|
|
|
|
|
|
|
|
const NOMOVE_OR_NOSIZE: u32 = SWP_NOMOVE | SWP_NOSIZE;
|
|
|
|
|
|
|
|
|
|
let new_rect = if window_pos.flags & NOMOVE_OR_NOSIZE != 0 {
|
|
|
|
|
let cur_rect = util::get_window_rect(window)
|
|
|
|
|
.expect("Unexpected GetWindowRect failure; please report this error to https://github.com/rust-windowing/winit");
|
|
|
|
|
|
|
|
|
|
match window_pos.flags & NOMOVE_OR_NOSIZE {
|
|
|
|
|
NOMOVE_OR_NOSIZE => None,
|
|
|
|
|
|
|
|
|
|
SWP_NOMOVE => Some(RECT {
|
|
|
|
|
left: cur_rect.left,
|
|
|
|
|
top: cur_rect.top,
|
|
|
|
|
right: cur_rect.left + window_pos.cx,
|
|
|
|
|
bottom: cur_rect.top + window_pos.cy,
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
SWP_NOSIZE => Some(RECT {
|
|
|
|
|
left: window_pos.x,
|
|
|
|
|
top: window_pos.y,
|
|
|
|
|
right: window_pos.x - cur_rect.left + cur_rect.right,
|
|
|
|
|
bottom: window_pos.y - cur_rect.top + cur_rect.bottom,
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Some(new_rect)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Some(new_rect) = new_rect {
|
|
|
|
|
let new_monitor = MonitorFromRect(&new_rect, MONITOR_DEFAULTTONULL);
|
|
|
|
|
match fullscreen {
|
|
|
|
|
Fullscreen::Borderless(ref mut fullscreen_monitor) => {
|
|
|
|
|
if new_monitor != 0
|
|
|
|
|
&& fullscreen_monitor
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map(|monitor| new_monitor != monitor.inner.hmonitor())
|
|
|
|
|
.unwrap_or(true)
|
|
|
|
|
{
|
|
|
|
|
if let Ok(new_monitor_info) = monitor::get_monitor_info(new_monitor)
|
|
|
|
|
{
|
|
|
|
|
let new_monitor_rect = new_monitor_info.monitorInfo.rcMonitor;
|
|
|
|
|
window_pos.x = new_monitor_rect.left;
|
|
|
|
|
window_pos.y = new_monitor_rect.top;
|
|
|
|
|
window_pos.cx = new_monitor_rect.right - new_monitor_rect.left;
|
|
|
|
|
window_pos.cy = new_monitor_rect.bottom - new_monitor_rect.top;
|
|
|
|
|
}
|
|
|
|
|
*fullscreen_monitor = Some(crate::monitor::MonitorHandle {
|
|
|
|
|
inner: MonitorHandle::new(new_monitor),
|
|
|
|
|
});
|
2019-10-05 16:23:30 -04:00
|
|
|
}
|
|
|
|
|
}
|
2022-04-30 13:21:08 +02:00
|
|
|
Fullscreen::Exclusive(ref video_mode) => {
|
|
|
|
|
let old_monitor = video_mode.video_mode.monitor.hmonitor();
|
|
|
|
|
if let Ok(old_monitor_info) = monitor::get_monitor_info(old_monitor) {
|
|
|
|
|
let old_monitor_rect = old_monitor_info.monitorInfo.rcMonitor;
|
|
|
|
|
window_pos.x = old_monitor_rect.left;
|
|
|
|
|
window_pos.y = old_monitor_rect.top;
|
|
|
|
|
window_pos.cx = old_monitor_rect.right - old_monitor_rect.left;
|
|
|
|
|
window_pos.cy = old_monitor_rect.bottom - old_monitor_rect.top;
|
|
|
|
|
}
|
2019-10-05 16:23:30 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
// WM_MOVE supplies client area positions, so we send Moved here instead.
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_WINDOWPOSCHANGED => {
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::event::WindowEvent::Moved;
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
let windowpos = lparam as *const WINDOWPOS;
|
|
|
|
|
if (*windowpos).flags & SWP_NOMOVE != SWP_NOMOVE {
|
2019-06-19 16:49:43 -04:00
|
|
|
let physical_position =
|
2020-03-08 00:21:04 -05:00
|
|
|
PhysicalPosition::new((*windowpos).x as i32, (*windowpos).y as i32);
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
2019-06-19 16:49:43 -04:00
|
|
|
event: Moved(physical_position),
|
2019-02-05 10:30:33 -05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This is necessary for us to still get sent WM_SIZE.
|
2022-03-07 22:58:12 +01:00
|
|
|
DefWindowProcW(window, msg, wparam, lparam)
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_SIZE => {
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::event::WindowEvent::Resized;
|
2022-03-07 22:58:12 +01:00
|
|
|
let w = super::loword(lparam as u32) as u32;
|
|
|
|
|
let h = super::hiword(lparam as u32) as u32;
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2019-06-19 16:49:43 -04:00
|
|
|
let physical_size = PhysicalSize::new(w, h);
|
2019-02-05 10:30:33 -05:00
|
|
|
let event = Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(WindowId(window)),
|
2019-06-19 16:49:43 -04:00
|
|
|
event: Resized(physical_size),
|
2019-02-05 10:30:33 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
{
|
2021-07-16 12:40:48 +02:00
|
|
|
let mut w = userdata.window_state.lock();
|
2019-02-05 10:30:33 -05:00
|
|
|
// See WindowFlags::MARKER_RETAIN_STATE_ON_SIZE docs for info on why this `if` check exists.
|
2019-06-21 11:33:15 -04:00
|
|
|
if !w
|
|
|
|
|
.window_flags()
|
|
|
|
|
.contains(WindowFlags::MARKER_RETAIN_STATE_ON_SIZE)
|
|
|
|
|
{
|
2022-03-07 22:58:12 +01:00
|
|
|
let maximized = wparam == SIZE_MAXIMIZED as usize;
|
2019-02-05 10:30:33 -05:00
|
|
|
w.set_window_flags_in_place(|f| f.set(WindowFlags::MAXIMIZED, maximized));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(event);
|
2019-02-05 10:30:33 -05:00
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_CHAR | WM_SYSCHAR => {
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::event::WindowEvent::ReceivedCharacter;
|
2019-09-16 14:26:56 -04:00
|
|
|
use std::char;
|
2021-08-30 19:40:02 +02:00
|
|
|
let is_high_surrogate = (0xD800..=0xDBFF).contains(&wparam);
|
|
|
|
|
let is_low_surrogate = (0xDC00..=0xDFFF).contains(&wparam);
|
2019-09-15 22:09:08 -04:00
|
|
|
|
2019-09-16 14:26:56 -04:00
|
|
|
if is_high_surrogate {
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.window_state.lock().high_surrogate = Some(wparam as u16);
|
2019-09-16 14:26:56 -04:00
|
|
|
} else if is_low_surrogate {
|
2021-07-16 12:40:48 +02:00
|
|
|
let high_surrogate = userdata.window_state.lock().high_surrogate.take();
|
2019-09-16 14:26:56 -04:00
|
|
|
|
|
|
|
|
if let Some(high_surrogate) = high_surrogate {
|
2019-09-15 22:09:08 -04:00
|
|
|
let pair = [high_surrogate, wparam as u16];
|
2019-09-16 14:26:56 -04:00
|
|
|
if let Some(Ok(chr)) = char::decode_utf16(pair.iter().copied()).next() {
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-09-15 22:09:08 -04:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: ReceivedCharacter(chr),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.window_state.lock().high_surrogate = None;
|
2019-09-16 14:26:56 -04:00
|
|
|
|
|
|
|
|
if let Some(chr) = char::from_u32(wparam as u32) {
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-09-16 14:26:56 -04:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: ReceivedCharacter(chr),
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-09-15 22:09:08 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-05-07 05:29:25 +03:00
|
|
|
WM_IME_STARTCOMPOSITION => {
|
|
|
|
|
let ime_allowed = userdata.window_state.lock().ime_allowed;
|
|
|
|
|
if ime_allowed {
|
|
|
|
|
userdata.window_state.lock().ime_state = ImeState::Enabled;
|
|
|
|
|
|
|
|
|
|
userdata.send_event(Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: WindowEvent::Ime(Ime::Enabled),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DefWindowProcW(window, msg, wparam, lparam)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WM_IME_COMPOSITION => {
|
|
|
|
|
let ime_allowed_and_composing = {
|
|
|
|
|
let w = userdata.window_state.lock();
|
|
|
|
|
w.ime_allowed && w.ime_state != ImeState::Disabled
|
|
|
|
|
};
|
|
|
|
|
// Windows Hangul IME sends WM_IME_COMPOSITION after WM_IME_ENDCOMPOSITION, so
|
|
|
|
|
// check whether composing.
|
|
|
|
|
if ime_allowed_and_composing {
|
|
|
|
|
let ime_context = ImeContext::current(window);
|
|
|
|
|
|
|
|
|
|
if lparam == 0 {
|
|
|
|
|
userdata.send_event(Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: WindowEvent::Ime(Ime::Preedit(String::new(), None)),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Google Japanese Input and ATOK have both flags, so
|
|
|
|
|
// first, receive composing result if exist.
|
|
|
|
|
if (lparam as u32 & GCS_RESULTSTR) != 0 {
|
|
|
|
|
if let Some(text) = ime_context.get_composed_text() {
|
|
|
|
|
userdata.window_state.lock().ime_state = ImeState::Enabled;
|
|
|
|
|
|
|
|
|
|
userdata.send_event(Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: WindowEvent::Ime(Ime::Commit(text)),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Next, receive preedit range for next composing if exist.
|
|
|
|
|
if (lparam as u32 & GCS_COMPSTR) != 0 {
|
|
|
|
|
if let Some((text, first, last)) = ime_context.get_composing_text_and_cursor() {
|
|
|
|
|
userdata.window_state.lock().ime_state = ImeState::Preedit;
|
|
|
|
|
let cursor_range = first.map(|f| (f, last.unwrap_or(f)));
|
|
|
|
|
|
|
|
|
|
userdata.send_event(Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: WindowEvent::Ime(Ime::Preedit(text, cursor_range)),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Not calling DefWindowProc to hide composing text drawn by IME.
|
|
|
|
|
0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WM_IME_ENDCOMPOSITION => {
|
|
|
|
|
let ime_allowed_or_composing = {
|
|
|
|
|
let w = userdata.window_state.lock();
|
|
|
|
|
w.ime_allowed || w.ime_state != ImeState::Disabled
|
|
|
|
|
};
|
|
|
|
|
if ime_allowed_or_composing {
|
|
|
|
|
if userdata.window_state.lock().ime_state == ImeState::Preedit {
|
|
|
|
|
// Windows Hangul IME sends WM_IME_COMPOSITION after WM_IME_ENDCOMPOSITION, so
|
|
|
|
|
// trying receiving composing result and commit if exists.
|
|
|
|
|
let ime_context = ImeContext::current(window);
|
|
|
|
|
if let Some(text) = ime_context.get_composed_text() {
|
|
|
|
|
userdata.send_event(Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: WindowEvent::Ime(Ime::Commit(text)),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userdata.window_state.lock().ime_state = ImeState::Disabled;
|
|
|
|
|
|
|
|
|
|
userdata.send_event(Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: WindowEvent::Ime(Ime::Disabled),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DefWindowProcW(window, msg, wparam, lparam)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WM_IME_SETCONTEXT => {
|
|
|
|
|
// Hide composing text drawn by IME.
|
|
|
|
|
let wparam = wparam & (!ISC_SHOWUICOMPOSITIONWINDOW as usize);
|
|
|
|
|
|
|
|
|
|
DefWindowProcW(window, msg, wparam, lparam)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 01:04:11 -05:00
|
|
|
// this is necessary for us to maintain minimize/restore state
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_SYSCOMMAND => {
|
|
|
|
|
if wparam == SC_RESTORE as usize {
|
2021-07-16 12:40:48 +02:00
|
|
|
let mut w = userdata.window_state.lock();
|
2019-12-22 01:04:11 -05:00
|
|
|
w.set_window_flags_in_place(|f| f.set(WindowFlags::MINIMIZED, false));
|
|
|
|
|
}
|
2022-03-07 22:58:12 +01:00
|
|
|
if wparam == SC_MINIMIZE as usize {
|
2021-07-16 12:40:48 +02:00
|
|
|
let mut w = userdata.window_state.lock();
|
2019-12-22 01:04:11 -05:00
|
|
|
w.set_window_flags_in_place(|f| f.set(WindowFlags::MINIMIZED, true));
|
|
|
|
|
}
|
|
|
|
|
// Send `WindowEvent::Minimized` here if we decide to implement one
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
if wparam == SC_SCREENSAVE as usize {
|
2021-07-16 12:40:48 +02:00
|
|
|
let window_state = userdata.window_state.lock();
|
2019-07-29 11:18:23 +02:00
|
|
|
if window_state.fullscreen.is_some() {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
DefWindowProcW(window, msg, wparam, lparam)
|
2019-07-29 11:18:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_MOUSEMOVE => {
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::event::WindowEvent::{CursorEntered, CursorMoved};
|
2019-02-05 10:30:33 -05:00
|
|
|
let mouse_was_outside_window = {
|
2021-07-16 12:40:48 +02:00
|
|
|
let mut w = userdata.window_state.lock();
|
2019-02-05 10:30:33 -05:00
|
|
|
|
|
|
|
|
let was_outside_window = !w.mouse.cursor_flags().contains(CursorFlags::IN_WINDOW);
|
2019-06-21 11:33:15 -04:00
|
|
|
w.mouse
|
|
|
|
|
.set_cursor_flags(window, |f| f.set(CursorFlags::IN_WINDOW, true))
|
|
|
|
|
.ok();
|
2019-02-05 10:30:33 -05:00
|
|
|
was_outside_window
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if mouse_was_outside_window {
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
2019-06-21 11:33:15 -04:00
|
|
|
event: CursorEntered {
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
},
|
2019-02-05 10:30:33 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Calling TrackMouseEvent in order to receive mouse leave events.
|
2022-03-07 22:58:12 +01:00
|
|
|
TrackMouseEvent(&mut TRACKMOUSEEVENT {
|
|
|
|
|
cbSize: mem::size_of::<TRACKMOUSEEVENT>() as u32,
|
|
|
|
|
dwFlags: TME_LEAVE,
|
2019-02-05 10:30:33 -05:00
|
|
|
hwndTrack: window,
|
2022-03-07 22:58:12 +01:00
|
|
|
dwHoverTime: HOVER_DEFAULT,
|
2019-02-05 10:30:33 -05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
let x = super::get_x_lparam(lparam as u32) as f64;
|
|
|
|
|
let y = super::get_y_lparam(lparam as u32) as f64;
|
2019-06-19 16:49:43 -04:00
|
|
|
let position = PhysicalPosition::new(x, y);
|
2020-03-06 22:15:49 +01:00
|
|
|
let cursor_moved;
|
|
|
|
|
{
|
|
|
|
|
// handle spurious WM_MOUSEMOVE messages
|
|
|
|
|
// see https://devblogs.microsoft.com/oldnewthing/20031001-00/?p=42343
|
|
|
|
|
// and http://debugandconquer.blogspot.com/2015/08/the-cause-of-spurious-mouse-move.html
|
2021-07-16 12:40:48 +02:00
|
|
|
let mut w = userdata.window_state.lock();
|
2020-03-06 22:15:49 +01:00
|
|
|
cursor_moved = w.mouse.last_position != Some(position);
|
|
|
|
|
w.mouse.last_position = Some(position);
|
|
|
|
|
}
|
|
|
|
|
if cursor_moved {
|
2021-07-16 12:40:48 +02:00
|
|
|
update_modifiers(window, userdata);
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2020-03-06 22:15:49 +01:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: CursorMoved {
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
position,
|
|
|
|
|
modifiers: event::get_key_mods(),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_MOUSELEAVE => {
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::event::WindowEvent::CursorLeft;
|
2019-02-05 10:30:33 -05:00
|
|
|
{
|
2021-07-16 12:40:48 +02:00
|
|
|
let mut w = userdata.window_state.lock();
|
2019-06-21 11:33:15 -04:00
|
|
|
w.mouse
|
|
|
|
|
.set_cursor_flags(window, |f| f.set(CursorFlags::IN_WINDOW, false))
|
|
|
|
|
.ok();
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
2019-06-21 11:33:15 -04:00
|
|
|
event: CursorLeft {
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
},
|
2019-02-05 10:30:33 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_MOUSEWHEEL => {
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::event::MouseScrollDelta::LineDelta;
|
2019-02-05 10:30:33 -05:00
|
|
|
|
|
|
|
|
let value = (wparam >> 16) as i16;
|
|
|
|
|
let value = value as i32;
|
2022-03-07 22:58:12 +01:00
|
|
|
let value = value as f32 / WHEEL_DELTA as f32;
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
update_modifiers(window, userdata);
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
2019-06-21 11:33:15 -04:00
|
|
|
event: WindowEvent::MouseWheel {
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
delta: LineDelta(0.0, value),
|
|
|
|
|
phase: TouchPhase::Moved,
|
|
|
|
|
modifiers: event::get_key_mods(),
|
|
|
|
|
},
|
2019-02-05 10:30:33 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_MOUSEHWHEEL => {
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::event::MouseScrollDelta::LineDelta;
|
2019-02-22 15:31:16 +01:00
|
|
|
|
|
|
|
|
let value = (wparam >> 16) as i16;
|
|
|
|
|
let value = value as i32;
|
2022-03-13 14:22:02 +01:00
|
|
|
let value = -value as f32 / WHEEL_DELTA as f32; // NOTE: inverted! See https://github.com/rust-windowing/winit/pull/2105/
|
2019-02-22 15:31:16 +01:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
update_modifiers(window, userdata);
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-22 15:31:16 +01:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
2019-06-21 11:33:15 -04:00
|
|
|
event: WindowEvent::MouseWheel {
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
delta: LineDelta(value, 0.0),
|
|
|
|
|
phase: TouchPhase::Moved,
|
|
|
|
|
modifiers: event::get_key_mods(),
|
|
|
|
|
},
|
2019-02-22 15:31:16 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-22 15:31:16 +01:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_KEYDOWN | WM_SYSKEYDOWN => {
|
2019-06-21 11:33:15 -04:00
|
|
|
use crate::event::{ElementState::Pressed, VirtualKeyCode};
|
2022-03-07 22:58:12 +01:00
|
|
|
if msg == WM_SYSKEYDOWN && wparam == VK_F4 as usize {
|
|
|
|
|
DefWindowProcW(window, msg, wparam, lparam)
|
2019-02-05 10:30:33 -05:00
|
|
|
} else {
|
|
|
|
|
if let Some((scancode, vkey)) = process_key_params(wparam, lparam) {
|
2021-07-16 12:40:48 +02:00
|
|
|
update_modifiers(window, userdata);
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
|
2020-01-09 22:29:31 -07:00
|
|
|
#[allow(deprecated)]
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: WindowEvent::KeyboardInput {
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
input: KeyboardInput {
|
|
|
|
|
state: Pressed,
|
2019-06-21 11:33:15 -04:00
|
|
|
scancode,
|
2019-02-05 10:30:33 -05:00
|
|
|
virtual_keycode: vkey,
|
|
|
|
|
modifiers: event::get_key_mods(),
|
|
|
|
|
},
|
2019-12-07 15:51:37 -07:00
|
|
|
is_synthetic: false,
|
2019-02-05 10:30:33 -05:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
// Windows doesn't emit a delete character by default, but in order to make it
|
|
|
|
|
// consistent with the other platforms we'll emit a delete character here.
|
|
|
|
|
if vkey == Some(VirtualKeyCode::Delete) {
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: WindowEvent::ReceivedCharacter('\u{7F}'),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
0
|
|
|
|
|
}
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_KEYUP | WM_SYSKEYUP => {
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::event::ElementState::Released;
|
2019-02-05 10:30:33 -05:00
|
|
|
if let Some((scancode, vkey)) = process_key_params(wparam, lparam) {
|
2021-07-16 12:40:48 +02:00
|
|
|
update_modifiers(window, userdata);
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
|
2020-01-09 22:29:31 -07:00
|
|
|
#[allow(deprecated)]
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: WindowEvent::KeyboardInput {
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
input: KeyboardInput {
|
|
|
|
|
state: Released,
|
2019-06-21 11:33:15 -04:00
|
|
|
scancode,
|
2019-02-05 10:30:33 -05:00
|
|
|
virtual_keycode: vkey,
|
|
|
|
|
modifiers: event::get_key_mods(),
|
|
|
|
|
},
|
2019-12-07 15:51:37 -07:00
|
|
|
is_synthetic: false,
|
2019-02-05 10:30:33 -05:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_LBUTTONDOWN => {
|
2019-06-21 11:33:15 -04:00
|
|
|
use crate::event::{ElementState::Pressed, MouseButton::Left, WindowEvent::MouseInput};
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
capture_mouse(window, &mut *userdata.window_state.lock());
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
update_modifiers(window, userdata);
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
2019-06-21 11:33:15 -04:00
|
|
|
event: MouseInput {
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
state: Pressed,
|
|
|
|
|
button: Left,
|
|
|
|
|
modifiers: event::get_key_mods(),
|
|
|
|
|
},
|
2019-02-05 10:30:33 -05:00
|
|
|
});
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_LBUTTONUP => {
|
2019-06-21 11:33:15 -04:00
|
|
|
use crate::event::{
|
|
|
|
|
ElementState::Released, MouseButton::Left, WindowEvent::MouseInput,
|
|
|
|
|
};
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
release_mouse(userdata.window_state.lock());
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
update_modifiers(window, userdata);
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
2019-06-21 11:33:15 -04:00
|
|
|
event: MouseInput {
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
state: Released,
|
|
|
|
|
button: Left,
|
|
|
|
|
modifiers: event::get_key_mods(),
|
|
|
|
|
},
|
2019-02-05 10:30:33 -05:00
|
|
|
});
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_RBUTTONDOWN => {
|
2019-06-21 11:33:15 -04:00
|
|
|
use crate::event::{
|
|
|
|
|
ElementState::Pressed, MouseButton::Right, WindowEvent::MouseInput,
|
|
|
|
|
};
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
capture_mouse(window, &mut *userdata.window_state.lock());
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
update_modifiers(window, userdata);
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
2019-06-21 11:33:15 -04:00
|
|
|
event: MouseInput {
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
state: Pressed,
|
|
|
|
|
button: Right,
|
|
|
|
|
modifiers: event::get_key_mods(),
|
|
|
|
|
},
|
2019-02-05 10:30:33 -05:00
|
|
|
});
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_RBUTTONUP => {
|
2019-06-21 11:33:15 -04:00
|
|
|
use crate::event::{
|
|
|
|
|
ElementState::Released, MouseButton::Right, WindowEvent::MouseInput,
|
|
|
|
|
};
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
release_mouse(userdata.window_state.lock());
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
update_modifiers(window, userdata);
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
2019-06-21 11:33:15 -04:00
|
|
|
event: MouseInput {
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
state: Released,
|
|
|
|
|
button: Right,
|
|
|
|
|
modifiers: event::get_key_mods(),
|
|
|
|
|
},
|
2019-02-05 10:30:33 -05:00
|
|
|
});
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_MBUTTONDOWN => {
|
2019-06-21 11:33:15 -04:00
|
|
|
use crate::event::{
|
|
|
|
|
ElementState::Pressed, MouseButton::Middle, WindowEvent::MouseInput,
|
|
|
|
|
};
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
capture_mouse(window, &mut *userdata.window_state.lock());
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
update_modifiers(window, userdata);
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
2019-06-21 11:33:15 -04:00
|
|
|
event: MouseInput {
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
state: Pressed,
|
|
|
|
|
button: Middle,
|
|
|
|
|
modifiers: event::get_key_mods(),
|
|
|
|
|
},
|
2019-02-05 10:30:33 -05:00
|
|
|
});
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_MBUTTONUP => {
|
2019-06-21 11:33:15 -04:00
|
|
|
use crate::event::{
|
|
|
|
|
ElementState::Released, MouseButton::Middle, WindowEvent::MouseInput,
|
|
|
|
|
};
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
release_mouse(userdata.window_state.lock());
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
update_modifiers(window, userdata);
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
2019-06-21 11:33:15 -04:00
|
|
|
event: MouseInput {
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
state: Released,
|
|
|
|
|
button: Middle,
|
|
|
|
|
modifiers: event::get_key_mods(),
|
|
|
|
|
},
|
2019-02-05 10:30:33 -05:00
|
|
|
});
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_XBUTTONDOWN => {
|
2019-06-21 11:33:15 -04:00
|
|
|
use crate::event::{
|
|
|
|
|
ElementState::Pressed, MouseButton::Other, WindowEvent::MouseInput,
|
|
|
|
|
};
|
2022-03-07 22:58:12 +01:00
|
|
|
let xbutton = super::get_xbutton_wparam(wparam as u32);
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
capture_mouse(window, &mut *userdata.window_state.lock());
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
update_modifiers(window, userdata);
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
2019-06-21 11:33:15 -04:00
|
|
|
event: MouseInput {
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
state: Pressed,
|
2020-12-09 23:11:25 +03:00
|
|
|
button: Other(xbutton),
|
2019-06-21 11:33:15 -04:00
|
|
|
modifiers: event::get_key_mods(),
|
|
|
|
|
},
|
2019-02-05 10:30:33 -05:00
|
|
|
});
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_XBUTTONUP => {
|
2019-06-21 11:33:15 -04:00
|
|
|
use crate::event::{
|
|
|
|
|
ElementState::Released, MouseButton::Other, WindowEvent::MouseInput,
|
|
|
|
|
};
|
2022-03-07 22:58:12 +01:00
|
|
|
let xbutton = super::get_xbutton_wparam(wparam as u32);
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
release_mouse(userdata.window_state.lock());
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
update_modifiers(window, userdata);
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
2019-06-21 11:33:15 -04:00
|
|
|
event: MouseInput {
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
state: Released,
|
2020-12-09 23:11:25 +03:00
|
|
|
button: Other(xbutton),
|
2019-06-21 11:33:15 -04:00
|
|
|
modifiers: event::get_key_mods(),
|
|
|
|
|
},
|
2019-02-05 10:30:33 -05:00
|
|
|
});
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_CAPTURECHANGED => {
|
2021-01-19 17:41:02 +01:00
|
|
|
// lparam here is a handle to the window which is gaining mouse capture.
|
|
|
|
|
// If it is the same as our window, then we're essentially retaining the capture. This
|
|
|
|
|
// can happen if `SetCapture` is called on our window when it already has the mouse
|
|
|
|
|
// capture.
|
2022-03-07 22:58:12 +01:00
|
|
|
if lparam != window {
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.window_state.lock().mouse.capture_count = 0;
|
2021-01-19 17:41:02 +01:00
|
|
|
}
|
2020-12-15 03:31:13 -03:00
|
|
|
0
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_TOUCH => {
|
|
|
|
|
let pcount = super::loword(wparam as u32) as usize;
|
2019-06-21 11:33:15 -04:00
|
|
|
let mut inputs = Vec::with_capacity(pcount);
|
2022-03-07 22:58:12 +01:00
|
|
|
let htouch = lparam;
|
|
|
|
|
if GetTouchInputInfo(
|
2019-02-05 10:30:33 -05:00
|
|
|
htouch,
|
2022-03-07 22:58:12 +01:00
|
|
|
pcount as u32,
|
2019-02-05 10:30:33 -05:00
|
|
|
inputs.as_mut_ptr(),
|
2022-03-07 22:58:12 +01:00
|
|
|
mem::size_of::<TOUCHINPUT>() as i32,
|
2019-06-21 11:33:15 -04:00
|
|
|
) > 0
|
|
|
|
|
{
|
2022-03-07 22:58:12 +01:00
|
|
|
inputs.set_len(pcount);
|
2019-02-05 10:30:33 -05:00
|
|
|
for input in &inputs {
|
2019-07-17 18:25:35 +02:00
|
|
|
let mut location = POINT {
|
|
|
|
|
x: input.x / 100,
|
|
|
|
|
y: input.y / 100,
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
if ScreenToClient(window, &mut location) == false.into() {
|
2019-07-17 18:25:35 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let x = location.x as f64 + (input.x % 100) as f64 / 100f64;
|
|
|
|
|
let y = location.y as f64 + (input.y % 100) as f64 / 100f64;
|
2019-06-19 16:49:43 -04:00
|
|
|
let location = PhysicalPosition::new(x, y);
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: WindowEvent::Touch(Touch {
|
2022-03-07 22:58:12 +01:00
|
|
|
phase: if util::has_flag(input.dwFlags, TOUCHEVENTF_DOWN) {
|
2019-02-05 10:30:33 -05:00
|
|
|
TouchPhase::Started
|
2022-03-07 22:58:12 +01:00
|
|
|
} else if util::has_flag(input.dwFlags, TOUCHEVENTF_UP) {
|
2019-02-05 10:30:33 -05:00
|
|
|
TouchPhase::Ended
|
2022-03-07 22:58:12 +01:00
|
|
|
} else if util::has_flag(input.dwFlags, TOUCHEVENTF_MOVE) {
|
2019-02-05 10:30:33 -05:00
|
|
|
TouchPhase::Moved
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
},
|
|
|
|
|
location,
|
2019-09-09 20:15:49 +02:00
|
|
|
force: None, // WM_TOUCH doesn't support pressure information
|
2019-02-05 10:30:33 -05:00
|
|
|
id: input.dwID as u64,
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-07 22:58:12 +01:00
|
|
|
CloseTouchInputHandle(htouch);
|
2019-02-05 10:30:33 -05:00
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_POINTERDOWN | WM_POINTERUPDATE | WM_POINTERUP => {
|
2019-07-26 09:12:06 +02:00
|
|
|
if let (
|
|
|
|
|
Some(GetPointerFrameInfoHistory),
|
|
|
|
|
Some(SkipPointerFrameMessages),
|
|
|
|
|
Some(GetPointerDeviceRects),
|
|
|
|
|
) = (
|
2019-07-05 18:37:25 +02:00
|
|
|
*GET_POINTER_FRAME_INFO_HISTORY,
|
|
|
|
|
*SKIP_POINTER_FRAME_MESSAGES,
|
2019-07-26 09:12:06 +02:00
|
|
|
*GET_POINTER_DEVICE_RECTS,
|
2019-07-05 18:37:25 +02:00
|
|
|
) {
|
2022-03-07 22:58:12 +01:00
|
|
|
let pointer_id = super::loword(wparam as u32) as u32;
|
|
|
|
|
let mut entries_count = 0u32;
|
|
|
|
|
let mut pointers_count = 0u32;
|
2019-07-05 18:37:25 +02:00
|
|
|
if GetPointerFrameInfoHistory(
|
|
|
|
|
pointer_id,
|
2022-03-07 22:58:12 +01:00
|
|
|
&mut entries_count,
|
|
|
|
|
&mut pointers_count,
|
|
|
|
|
ptr::null_mut(),
|
|
|
|
|
) == false.into()
|
2019-07-05 18:37:25 +02:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-06-27 11:34:38 +02:00
|
|
|
|
2019-07-05 18:37:25 +02:00
|
|
|
let pointer_info_count = (entries_count * pointers_count) as usize;
|
|
|
|
|
let mut pointer_infos = Vec::with_capacity(pointer_info_count);
|
|
|
|
|
if GetPointerFrameInfoHistory(
|
|
|
|
|
pointer_id,
|
2022-03-07 22:58:12 +01:00
|
|
|
&mut entries_count,
|
|
|
|
|
&mut pointers_count,
|
2019-07-05 18:37:25 +02:00
|
|
|
pointer_infos.as_mut_ptr(),
|
2022-03-07 22:58:12 +01:00
|
|
|
) == false.into()
|
2019-07-05 18:37:25 +02:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2022-03-07 22:58:12 +01:00
|
|
|
pointer_infos.set_len(pointer_info_count);
|
2019-06-27 11:34:38 +02:00
|
|
|
|
2019-07-05 18:37:25 +02:00
|
|
|
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getpointerframeinfohistory
|
|
|
|
|
// The information retrieved appears in reverse chronological order, with the most recent entry in the first
|
|
|
|
|
// row of the returned array
|
|
|
|
|
for pointer_info in pointer_infos.iter().rev() {
|
2019-09-09 20:15:49 +02:00
|
|
|
let mut device_rect = mem::MaybeUninit::uninit();
|
|
|
|
|
let mut display_rect = mem::MaybeUninit::uninit();
|
2019-07-26 09:12:06 +02:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
if GetPointerDeviceRects(
|
2019-07-26 09:12:06 +02:00
|
|
|
pointer_info.sourceDevice,
|
2019-09-09 20:15:49 +02:00
|
|
|
device_rect.as_mut_ptr(),
|
|
|
|
|
display_rect.as_mut_ptr(),
|
2022-03-07 22:58:12 +01:00
|
|
|
) == false.into()
|
2019-07-26 09:12:06 +02:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 20:15:49 +02:00
|
|
|
let device_rect = device_rect.assume_init();
|
|
|
|
|
let display_rect = display_rect.assume_init();
|
|
|
|
|
|
2019-07-26 09:12:06 +02:00
|
|
|
// For the most precise himetric to pixel conversion we calculate the ratio between the resolution
|
|
|
|
|
// of the display device (pixel) and the touch device (himetric).
|
|
|
|
|
let himetric_to_pixel_ratio_x = (display_rect.right - display_rect.left) as f64
|
|
|
|
|
/ (device_rect.right - device_rect.left) as f64;
|
|
|
|
|
let himetric_to_pixel_ratio_y = (display_rect.bottom - display_rect.top) as f64
|
|
|
|
|
/ (device_rect.bottom - device_rect.top) as f64;
|
|
|
|
|
|
|
|
|
|
// ptHimetricLocation's origin is 0,0 even on multi-monitor setups.
|
|
|
|
|
// On multi-monitor setups we need to translate the himetric location to the rect of the
|
|
|
|
|
// display device it's attached to.
|
|
|
|
|
let x = display_rect.left as f64
|
|
|
|
|
+ pointer_info.ptHimetricLocation.x as f64 * himetric_to_pixel_ratio_x;
|
|
|
|
|
let y = display_rect.top as f64
|
|
|
|
|
+ pointer_info.ptHimetricLocation.y as f64 * himetric_to_pixel_ratio_y;
|
|
|
|
|
|
2019-07-17 18:25:35 +02:00
|
|
|
let mut location = POINT {
|
2019-07-26 09:12:06 +02:00
|
|
|
x: x.floor() as i32,
|
|
|
|
|
y: y.floor() as i32,
|
2019-07-17 18:25:35 +02:00
|
|
|
};
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
if ScreenToClient(window, &mut location) == false.into() {
|
2019-07-17 18:25:35 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 20:15:49 +02:00
|
|
|
let force = match pointer_info.pointerType {
|
2022-03-07 22:58:12 +01:00
|
|
|
PT_TOUCH => {
|
2019-09-09 20:15:49 +02:00
|
|
|
let mut touch_info = mem::MaybeUninit::uninit();
|
|
|
|
|
GET_POINTER_TOUCH_INFO.and_then(|GetPointerTouchInfo| {
|
|
|
|
|
match GetPointerTouchInfo(
|
|
|
|
|
pointer_info.pointerId,
|
|
|
|
|
touch_info.as_mut_ptr(),
|
|
|
|
|
) {
|
|
|
|
|
0 => None,
|
|
|
|
|
_ => normalize_pointer_pressure(
|
|
|
|
|
touch_info.assume_init().pressure,
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2022-03-07 22:58:12 +01:00
|
|
|
PT_PEN => {
|
2019-09-09 20:15:49 +02:00
|
|
|
let mut pen_info = mem::MaybeUninit::uninit();
|
|
|
|
|
GET_POINTER_PEN_INFO.and_then(|GetPointerPenInfo| {
|
|
|
|
|
match GetPointerPenInfo(
|
|
|
|
|
pointer_info.pointerId,
|
|
|
|
|
pen_info.as_mut_ptr(),
|
|
|
|
|
) {
|
|
|
|
|
0 => None,
|
|
|
|
|
_ => {
|
|
|
|
|
normalize_pointer_pressure(pen_info.assume_init().pressure)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
_ => None,
|
|
|
|
|
};
|
|
|
|
|
|
2019-07-26 09:12:06 +02:00
|
|
|
let x = location.x as f64 + x.fract();
|
|
|
|
|
let y = location.y as f64 + y.fract();
|
2019-06-19 16:49:43 -04:00
|
|
|
let location = PhysicalPosition::new(x, y);
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-07-05 18:37:25 +02:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: WindowEvent::Touch(Touch {
|
2022-03-07 22:58:12 +01:00
|
|
|
phase: if util::has_flag(pointer_info.pointerFlags, POINTER_FLAG_DOWN) {
|
2019-07-05 18:37:25 +02:00
|
|
|
TouchPhase::Started
|
2022-03-07 22:58:12 +01:00
|
|
|
} else if util::has_flag(pointer_info.pointerFlags, POINTER_FLAG_UP) {
|
2019-07-05 18:37:25 +02:00
|
|
|
TouchPhase::Ended
|
2022-03-07 22:58:12 +01:00
|
|
|
} else if util::has_flag(pointer_info.pointerFlags, POINTER_FLAG_UPDATE)
|
2019-07-05 18:37:25 +02:00
|
|
|
{
|
|
|
|
|
TouchPhase::Moved
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
},
|
|
|
|
|
location,
|
2019-09-09 20:15:49 +02:00
|
|
|
force,
|
2019-07-05 18:37:25 +02:00
|
|
|
id: pointer_info.pointerId as u64,
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-06-27 11:34:38 +02:00
|
|
|
|
2019-07-05 18:37:25 +02:00
|
|
|
SkipPointerFrameMessages(pointer_id);
|
|
|
|
|
}
|
2019-06-27 11:34:38 +02:00
|
|
|
0
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_SETFOCUS => {
|
2019-12-27 16:26:23 -05:00
|
|
|
use crate::event::{ElementState::Released, WindowEvent::Focused};
|
|
|
|
|
for windows_keycode in event::get_pressed_keys() {
|
2022-03-07 22:58:12 +01:00
|
|
|
let scancode = MapVirtualKeyA(windows_keycode as u32, MAPVK_VK_TO_VSC);
|
2019-12-27 16:26:23 -05:00
|
|
|
let virtual_keycode = event::vkey_to_winit_vkey(windows_keycode);
|
|
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
update_modifiers(window, userdata);
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
|
2020-01-09 22:29:31 -07:00
|
|
|
#[allow(deprecated)]
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-12-27 16:26:23 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: WindowEvent::KeyboardInput {
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
input: KeyboardInput {
|
|
|
|
|
scancode,
|
|
|
|
|
virtual_keycode,
|
|
|
|
|
state: Released,
|
|
|
|
|
modifiers: event::get_key_mods(),
|
|
|
|
|
},
|
|
|
|
|
is_synthetic: true,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: Focused(true),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_KILLFOCUS => {
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
use crate::event::{
|
|
|
|
|
ElementState::Released,
|
|
|
|
|
ModifiersState,
|
|
|
|
|
WindowEvent::{Focused, ModifiersChanged},
|
|
|
|
|
};
|
2019-12-27 16:26:23 -05:00
|
|
|
for windows_keycode in event::get_pressed_keys() {
|
2022-03-07 22:58:12 +01:00
|
|
|
let scancode = MapVirtualKeyA(windows_keycode as u32, MAPVK_VK_TO_VSC);
|
2019-12-27 16:26:23 -05:00
|
|
|
let virtual_keycode = event::vkey_to_winit_vkey(windows_keycode);
|
|
|
|
|
|
2020-01-09 22:29:31 -07:00
|
|
|
#[allow(deprecated)]
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-12-27 16:26:23 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: WindowEvent::KeyboardInput {
|
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
|
input: KeyboardInput {
|
|
|
|
|
scancode,
|
|
|
|
|
virtual_keycode,
|
|
|
|
|
state: Released,
|
|
|
|
|
modifiers: event::get_key_mods(),
|
|
|
|
|
},
|
|
|
|
|
is_synthetic: true,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.window_state.lock().modifiers_state = ModifiersState::empty();
|
|
|
|
|
userdata.send_event(Event::WindowEvent {
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: ModifiersChanged(ModifiersState::empty()),
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-02-05 10:30:33 -05:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: Focused(false),
|
|
|
|
|
});
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_SETCURSOR => {
|
2019-02-05 10:30:33 -05:00
|
|
|
let set_cursor_to = {
|
2021-07-16 12:40:48 +02:00
|
|
|
let window_state = userdata.window_state.lock();
|
2021-01-05 17:39:13 +01:00
|
|
|
// The return value for the preceding `WM_NCHITTEST` message is conveniently
|
|
|
|
|
// provided through the low-order word of lParam. We use that here since
|
|
|
|
|
// `WM_MOUSEMOVE` seems to come after `WM_SETCURSOR` for a given cursor movement.
|
2022-03-07 22:58:12 +01:00
|
|
|
let in_client_area = super::loword(lparam as u32) as u32 == HTCLIENT;
|
2021-01-05 17:39:13 +01:00
|
|
|
if in_client_area {
|
2019-02-05 10:30:33 -05:00
|
|
|
Some(window_state.mouse.cursor)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match set_cursor_to {
|
|
|
|
|
Some(cursor) => {
|
2022-03-07 22:58:12 +01:00
|
|
|
let cursor = LoadCursorW(0, cursor.to_windows_cursor());
|
|
|
|
|
SetCursor(cursor);
|
2019-02-05 10:30:33 -05:00
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2022-03-07 22:58:12 +01:00
|
|
|
None => DefWindowProcW(window, msg, wparam, lparam),
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_DROPFILES => {
|
2019-02-05 10:30:33 -05:00
|
|
|
// See `FileDropHandler` for implementation.
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_GETMINMAXINFO => {
|
|
|
|
|
let mmi = lparam as *mut MINMAXINFO;
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
let window_state = userdata.window_state.lock();
|
2019-02-05 10:30:33 -05:00
|
|
|
|
|
|
|
|
if window_state.min_size.is_some() || window_state.max_size.is_some() {
|
|
|
|
|
if let Some(min_size) = window_state.min_size {
|
2020-02-13 20:41:41 +01:00
|
|
|
let min_size = min_size.to_physical(window_state.scale_factor);
|
2020-01-04 01:29:40 -05:00
|
|
|
let (width, height): (u32, u32) = util::adjust_size(window, min_size).into();
|
2019-06-21 11:33:15 -04:00
|
|
|
(*mmi).ptMinTrackSize = POINT {
|
|
|
|
|
x: width as i32,
|
|
|
|
|
y: height as i32,
|
|
|
|
|
};
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
if let Some(max_size) = window_state.max_size {
|
2020-02-13 20:41:41 +01:00
|
|
|
let max_size = max_size.to_physical(window_state.scale_factor);
|
2020-01-04 01:29:40 -05:00
|
|
|
let (width, height): (u32, u32) = util::adjust_size(window, max_size).into();
|
2019-06-21 11:33:15 -04:00
|
|
|
(*mmi).ptMaxTrackSize = POINT {
|
|
|
|
|
x: width as i32,
|
|
|
|
|
y: height as i32,
|
|
|
|
|
};
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
|
|
|
|
// Only sent on Windows 8.1 or newer. On Windows 7 and older user has to log out to change
|
|
|
|
|
// DPI, therefore all applications are closed while DPI is changing.
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_DPICHANGED => {
|
2020-01-05 16:57:32 -05:00
|
|
|
use crate::event::WindowEvent::ScaleFactorChanged;
|
2019-02-05 10:30:33 -05:00
|
|
|
|
|
|
|
|
// This message actually provides two DPI values - x and y. However MSDN says that
|
|
|
|
|
// "you only need to use either the X-axis or the Y-axis value when scaling your
|
|
|
|
|
// application since they are the same".
|
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/dn312083(v=vs.85).aspx
|
2022-03-07 22:58:12 +01:00
|
|
|
let new_dpi_x = super::loword(wparam as u32) as u32;
|
2020-02-13 20:41:41 +01:00
|
|
|
let new_scale_factor = dpi_to_scale_factor(new_dpi_x);
|
|
|
|
|
let old_scale_factor: f64;
|
2019-02-05 10:30:33 -05:00
|
|
|
|
|
|
|
|
let allow_resize = {
|
2021-07-16 12:40:48 +02:00
|
|
|
let mut window_state = userdata.window_state.lock();
|
2020-02-13 20:41:41 +01:00
|
|
|
old_scale_factor = window_state.scale_factor;
|
|
|
|
|
window_state.scale_factor = new_scale_factor;
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2020-02-13 20:41:41 +01:00
|
|
|
if new_scale_factor == old_scale_factor {
|
2020-01-04 01:28:34 -05:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window_state.fullscreen.is_none()
|
|
|
|
|
&& !window_state.window_flags().contains(WindowFlags::MAXIMIZED)
|
2019-02-05 10:30:33 -05:00
|
|
|
};
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
let style = GetWindowLongW(window, GWL_STYLE) as u32;
|
|
|
|
|
let style_ex = GetWindowLongW(window, GWL_EXSTYLE) as u32;
|
2019-06-19 16:49:43 -04:00
|
|
|
|
|
|
|
|
// New size as suggested by Windows.
|
2020-01-04 01:28:34 -05:00
|
|
|
let suggested_rect = *(lparam as *const RECT);
|
2019-06-19 16:49:43 -04:00
|
|
|
|
|
|
|
|
// The window rect provided is the window's outer size, not it's inner size. However,
|
|
|
|
|
// win32 doesn't provide an `UnadjustWindowRectEx` function to get the client rect from
|
|
|
|
|
// the outer rect, so we instead adjust the window rect to get the decoration margins
|
|
|
|
|
// and remove them from the outer size.
|
2020-01-04 01:28:34 -05:00
|
|
|
let margin_left: i32;
|
|
|
|
|
let margin_top: i32;
|
|
|
|
|
// let margin_right: i32;
|
|
|
|
|
// let margin_bottom: i32;
|
2019-06-19 16:49:43 -04:00
|
|
|
{
|
2020-01-21 14:43:36 -05:00
|
|
|
let adjusted_rect =
|
|
|
|
|
util::adjust_window_rect_with_styles(window, style, style_ex, suggested_rect)
|
|
|
|
|
.unwrap_or(suggested_rect);
|
2020-01-04 01:28:34 -05:00
|
|
|
margin_left = suggested_rect.left - adjusted_rect.left;
|
|
|
|
|
margin_top = suggested_rect.top - adjusted_rect.top;
|
|
|
|
|
// margin_right = adjusted_rect.right - suggested_rect.right;
|
|
|
|
|
// margin_bottom = adjusted_rect.bottom - suggested_rect.bottom;
|
2019-06-19 16:49:43 -04:00
|
|
|
}
|
|
|
|
|
|
2020-01-04 01:28:34 -05:00
|
|
|
let old_physical_inner_rect = {
|
|
|
|
|
let mut old_physical_inner_rect = mem::zeroed();
|
2022-03-07 22:58:12 +01:00
|
|
|
GetClientRect(window, &mut old_physical_inner_rect);
|
2020-01-04 01:28:34 -05:00
|
|
|
let mut origin = mem::zeroed();
|
2022-03-07 22:58:12 +01:00
|
|
|
ClientToScreen(window, &mut origin);
|
2020-01-04 01:29:40 -05:00
|
|
|
|
2020-01-04 01:28:34 -05:00
|
|
|
old_physical_inner_rect.left += origin.x;
|
|
|
|
|
old_physical_inner_rect.right += origin.x;
|
|
|
|
|
old_physical_inner_rect.top += origin.y;
|
|
|
|
|
old_physical_inner_rect.bottom += origin.y;
|
2020-01-04 01:29:40 -05:00
|
|
|
|
2020-01-04 01:28:34 -05:00
|
|
|
old_physical_inner_rect
|
2020-01-04 01:29:40 -05:00
|
|
|
};
|
2020-01-04 01:28:34 -05:00
|
|
|
let old_physical_inner_size = PhysicalSize::new(
|
|
|
|
|
(old_physical_inner_rect.right - old_physical_inner_rect.left) as u32,
|
|
|
|
|
(old_physical_inner_rect.bottom - old_physical_inner_rect.top) as u32,
|
|
|
|
|
);
|
|
|
|
|
|
2019-06-19 16:49:43 -04:00
|
|
|
// `allow_resize` prevents us from re-applying DPI adjustment to the restored size after
|
|
|
|
|
// exiting fullscreen (the restored size is already DPI adjusted).
|
2020-01-03 00:28:41 -05:00
|
|
|
let mut new_physical_inner_size = match allow_resize {
|
|
|
|
|
// We calculate our own size because the default suggested rect doesn't do a great job
|
|
|
|
|
// of preserving the window's logical size.
|
|
|
|
|
true => old_physical_inner_size
|
2020-02-13 20:41:41 +01:00
|
|
|
.to_logical::<f64>(old_scale_factor)
|
|
|
|
|
.to_physical::<u32>(new_scale_factor),
|
2020-01-03 00:28:41 -05:00
|
|
|
false => old_physical_inner_size,
|
|
|
|
|
};
|
2019-06-19 16:49:43 -04:00
|
|
|
|
2022-06-10 13:43:33 +03:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2019-06-19 16:49:43 -04:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
2020-01-05 16:57:32 -05:00
|
|
|
event: ScaleFactorChanged {
|
2020-02-13 20:41:41 +01:00
|
|
|
scale_factor: new_scale_factor,
|
2020-01-03 00:28:41 -05:00
|
|
|
new_inner_size: &mut new_physical_inner_size,
|
2019-06-19 16:49:43 -04:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2020-05-04 15:14:13 -04:00
|
|
|
let dragging_window: bool;
|
|
|
|
|
|
|
|
|
|
{
|
2021-07-16 12:40:48 +02:00
|
|
|
let window_state = userdata.window_state.lock();
|
2020-05-04 15:14:13 -04:00
|
|
|
dragging_window = window_state
|
|
|
|
|
.window_flags()
|
|
|
|
|
.contains(WindowFlags::MARKER_IN_SIZE_MOVE);
|
|
|
|
|
// Unset maximized if we're changing the window's size.
|
|
|
|
|
if new_physical_inner_size != old_physical_inner_size {
|
|
|
|
|
WindowState::set_window_flags(window_state, window, |f| {
|
|
|
|
|
f.set(WindowFlags::MAXIMIZED, false)
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-01-04 01:28:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let new_outer_rect: RECT;
|
|
|
|
|
{
|
|
|
|
|
let suggested_ul = (
|
|
|
|
|
suggested_rect.left + margin_left,
|
|
|
|
|
suggested_rect.top + margin_top,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let mut conservative_rect = RECT {
|
|
|
|
|
left: suggested_ul.0,
|
|
|
|
|
top: suggested_ul.1,
|
2022-03-07 22:58:12 +01:00
|
|
|
right: suggested_ul.0 + new_physical_inner_size.width as i32,
|
|
|
|
|
bottom: suggested_ul.1 + new_physical_inner_size.height as i32,
|
2020-01-04 01:28:34 -05:00
|
|
|
};
|
|
|
|
|
|
2020-01-21 14:43:36 -05:00
|
|
|
conservative_rect = util::adjust_window_rect_with_styles(
|
|
|
|
|
window,
|
2020-01-04 01:28:34 -05:00
|
|
|
style,
|
|
|
|
|
style_ex,
|
2020-01-21 14:43:36 -05:00
|
|
|
conservative_rect,
|
|
|
|
|
)
|
|
|
|
|
.unwrap_or(conservative_rect);
|
2020-01-04 01:28:34 -05:00
|
|
|
|
2020-05-04 15:14:13 -04:00
|
|
|
// If we're dragging the window, offset the window so that the cursor's
|
2020-01-04 01:28:34 -05:00
|
|
|
// relative horizontal position in the title bar is preserved.
|
|
|
|
|
if dragging_window {
|
|
|
|
|
let bias = {
|
|
|
|
|
let cursor_pos = {
|
|
|
|
|
let mut pos = mem::zeroed();
|
2022-03-07 22:58:12 +01:00
|
|
|
GetCursorPos(&mut pos);
|
2020-01-04 01:28:34 -05:00
|
|
|
pos
|
|
|
|
|
};
|
|
|
|
|
let suggested_cursor_horizontal_ratio = (cursor_pos.x - suggested_rect.left)
|
|
|
|
|
as f64
|
|
|
|
|
/ (suggested_rect.right - suggested_rect.left) as f64;
|
|
|
|
|
|
|
|
|
|
(cursor_pos.x
|
|
|
|
|
- (suggested_cursor_horizontal_ratio
|
|
|
|
|
* (conservative_rect.right - conservative_rect.left) as f64)
|
2022-03-07 22:58:12 +01:00
|
|
|
as i32)
|
2020-01-04 01:28:34 -05:00
|
|
|
- conservative_rect.left
|
|
|
|
|
};
|
|
|
|
|
conservative_rect.left += bias;
|
|
|
|
|
conservative_rect.right += bias;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check to see if the new window rect is on the monitor with the new DPI factor.
|
|
|
|
|
// If it isn't, offset the window so that it is.
|
2022-03-07 22:58:12 +01:00
|
|
|
let new_dpi_monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONULL);
|
|
|
|
|
let conservative_rect_monitor =
|
|
|
|
|
MonitorFromRect(&conservative_rect, MONITOR_DEFAULTTONULL);
|
2020-01-04 01:28:34 -05:00
|
|
|
new_outer_rect = if conservative_rect_monitor == new_dpi_monitor {
|
|
|
|
|
conservative_rect
|
|
|
|
|
} else {
|
|
|
|
|
let get_monitor_rect = |monitor| {
|
2022-03-07 22:58:12 +01:00
|
|
|
let mut monitor_info = MONITORINFO {
|
|
|
|
|
cbSize: mem::size_of::<MONITORINFO>() as _,
|
2020-01-04 01:28:34 -05:00
|
|
|
..mem::zeroed()
|
|
|
|
|
};
|
2022-03-07 22:58:12 +01:00
|
|
|
GetMonitorInfoW(monitor, &mut monitor_info);
|
2020-01-04 01:28:34 -05:00
|
|
|
monitor_info.rcMonitor
|
|
|
|
|
};
|
|
|
|
|
let wrong_monitor = conservative_rect_monitor;
|
|
|
|
|
let wrong_monitor_rect = get_monitor_rect(wrong_monitor);
|
|
|
|
|
let new_monitor_rect = get_monitor_rect(new_dpi_monitor);
|
|
|
|
|
|
|
|
|
|
// The direction to nudge the window in to get the window onto the monitor with
|
|
|
|
|
// the new DPI factor. We calculate this by seeing which monitor edges are
|
|
|
|
|
// shared and nudging away from the wrong monitor based on those.
|
|
|
|
|
let delta_nudge_to_dpi_monitor = (
|
|
|
|
|
if wrong_monitor_rect.left == new_monitor_rect.right {
|
|
|
|
|
-1
|
|
|
|
|
} else if wrong_monitor_rect.right == new_monitor_rect.left {
|
|
|
|
|
1
|
|
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
},
|
|
|
|
|
if wrong_monitor_rect.bottom == new_monitor_rect.top {
|
|
|
|
|
1
|
|
|
|
|
} else if wrong_monitor_rect.top == new_monitor_rect.bottom {
|
|
|
|
|
-1
|
|
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let abort_after_iterations = new_monitor_rect.right - new_monitor_rect.left
|
|
|
|
|
+ new_monitor_rect.bottom
|
|
|
|
|
- new_monitor_rect.top;
|
|
|
|
|
for _ in 0..abort_after_iterations {
|
|
|
|
|
conservative_rect.left += delta_nudge_to_dpi_monitor.0;
|
|
|
|
|
conservative_rect.right += delta_nudge_to_dpi_monitor.0;
|
|
|
|
|
conservative_rect.top += delta_nudge_to_dpi_monitor.1;
|
|
|
|
|
conservative_rect.bottom += delta_nudge_to_dpi_monitor.1;
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
if MonitorFromRect(&conservative_rect, MONITOR_DEFAULTTONULL)
|
|
|
|
|
== new_dpi_monitor
|
|
|
|
|
{
|
2020-01-04 01:28:34 -05:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conservative_rect
|
|
|
|
|
};
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
SetWindowPos(
|
2020-01-04 01:28:34 -05:00
|
|
|
window,
|
2022-03-07 22:58:12 +01:00
|
|
|
0,
|
2020-01-04 01:28:34 -05:00
|
|
|
new_outer_rect.left,
|
|
|
|
|
new_outer_rect.top,
|
|
|
|
|
new_outer_rect.right - new_outer_rect.left,
|
|
|
|
|
new_outer_rect.bottom - new_outer_rect.top,
|
2022-03-07 22:58:12 +01:00
|
|
|
SWP_NOZORDER | SWP_NOACTIVATE,
|
2020-01-04 01:28:34 -05:00
|
|
|
);
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_SETTINGCHANGE => {
|
2019-12-22 19:04:09 +00:00
|
|
|
use crate::event::WindowEvent::ThemeChanged;
|
|
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
let preferred_theme = userdata.window_state.lock().preferred_theme;
|
2019-12-22 19:04:09 +00:00
|
|
|
|
2020-11-30 19:04:26 +01:00
|
|
|
if preferred_theme == None {
|
|
|
|
|
let new_theme = try_theme(window, preferred_theme);
|
2021-07-16 12:40:48 +02:00
|
|
|
let mut window_state = userdata.window_state.lock();
|
2019-12-22 19:04:09 +00:00
|
|
|
|
2020-11-30 19:04:26 +01:00
|
|
|
if window_state.current_theme != new_theme {
|
|
|
|
|
window_state.current_theme = new_theme;
|
|
|
|
|
mem::drop(window_state);
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::WindowEvent {
|
2020-11-30 19:04:26 +01:00
|
|
|
window_id: RootWindowId(WindowId(window)),
|
|
|
|
|
event: ThemeChanged(new_theme),
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-12-22 19:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
DefWindowProcW(window, msg, wparam, lparam)
|
2019-12-22 19:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
_ => {
|
|
|
|
|
if msg == *DESTROY_MSG_ID {
|
2022-03-07 22:58:12 +01:00
|
|
|
DestroyWindow(window);
|
2019-02-05 10:30:33 -05:00
|
|
|
0
|
|
|
|
|
} else if msg == *SET_RETAIN_STATE_ON_SIZE_MSG_ID {
|
2021-07-16 12:40:48 +02:00
|
|
|
let mut window_state = userdata.window_state.lock();
|
2019-06-21 11:33:15 -04:00
|
|
|
window_state.set_window_flags_in_place(|f| {
|
|
|
|
|
f.set(WindowFlags::MARKER_RETAIN_STATE_ON_SIZE, wparam != 0)
|
|
|
|
|
});
|
2019-02-05 10:30:33 -05:00
|
|
|
0
|
|
|
|
|
} else {
|
2022-03-07 22:58:12 +01:00
|
|
|
DefWindowProcW(window, msg, wparam, lparam)
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2020-05-04 15:14:13 -04:00
|
|
|
};
|
|
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata
|
2020-05-04 15:14:13 -04:00
|
|
|
.event_loop_runner
|
|
|
|
|
.catch_unwind(callback)
|
|
|
|
|
.unwrap_or(-1)
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
|
2019-06-19 16:49:43 -04:00
|
|
|
unsafe extern "system" fn thread_event_target_callback<T: 'static>(
|
2019-02-05 10:30:33 -05:00
|
|
|
window: HWND,
|
2022-03-07 22:58:12 +01:00
|
|
|
msg: u32,
|
2019-02-05 10:30:33 -05:00
|
|
|
wparam: WPARAM,
|
|
|
|
|
lparam: LPARAM,
|
|
|
|
|
) -> LRESULT {
|
2022-03-07 22:58:12 +01:00
|
|
|
let userdata_ptr = super::get_window_long(window, GWL_USERDATA) as *mut ThreadMsgTargetData<T>;
|
2021-07-16 12:40:48 +02:00
|
|
|
if userdata_ptr.is_null() {
|
|
|
|
|
// `userdata_ptr` will always be null for the first `WM_GETMINMAXINFO`, as well as `WM_NCCREATE` and
|
|
|
|
|
// `WM_CREATE`.
|
2022-03-07 22:58:12 +01:00
|
|
|
return DefWindowProcW(window, msg, wparam, lparam);
|
2021-07-16 12:40:48 +02:00
|
|
|
}
|
|
|
|
|
let userdata = Box::from_raw(userdata_ptr);
|
2020-05-04 15:14:13 -04:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
if msg != WM_PAINT {
|
|
|
|
|
RedrawWindow(window, ptr::null(), 0, RDW_INTERNALPAINT);
|
2020-05-04 15:14:13 -04:00
|
|
|
}
|
|
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
let mut userdata_removed = false;
|
2020-12-20 12:54:42 +01:00
|
|
|
|
2020-05-04 15:14:13 -04:00
|
|
|
// I decided to bind the closure to `callback` and pass it to catch_unwind rather than passing
|
|
|
|
|
// the closure to catch_unwind directly so that the match body indendation wouldn't change and
|
|
|
|
|
// the git blame and history would be preserved.
|
|
|
|
|
let callback = || match msg {
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_NCDESTROY => {
|
|
|
|
|
super::set_window_long(window, GWL_USERDATA, 0);
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata_removed = true;
|
2019-02-05 10:30:33 -05:00
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
// Because WM_PAINT comes after all other messages, we use it during modal loops to detect
|
|
|
|
|
// when the event queue has been emptied. See `process_event` for more details.
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_PAINT => {
|
|
|
|
|
ValidateRect(window, ptr::null());
|
2020-05-04 15:14:13 -04:00
|
|
|
// If the WM_PAINT handler in `public_window_callback` has already flushed the redraw
|
|
|
|
|
// events, `handling_events` will return false and we won't emit a second
|
|
|
|
|
// `RedrawEventsCleared` event.
|
2021-07-16 12:40:48 +02:00
|
|
|
if userdata.event_loop_runner.handling_events() {
|
|
|
|
|
if userdata.event_loop_runner.should_buffer() {
|
2020-07-02 16:53:47 -04:00
|
|
|
// This branch can be triggered when a nested win32 event loop is triggered
|
|
|
|
|
// inside of the `event_handler` callback.
|
2022-03-07 22:58:12 +01:00
|
|
|
RedrawWindow(window, ptr::null(), 0, RDW_INTERNALPAINT);
|
2020-07-02 16:53:47 -04:00
|
|
|
} else {
|
|
|
|
|
// This WM_PAINT handler will never be re-entrant because `flush_paint_messages`
|
|
|
|
|
// doesn't call WM_PAINT for the thread event target (i.e. this window).
|
2021-07-16 12:40:48 +02:00
|
|
|
assert!(flush_paint_messages(None, &userdata.event_loop_runner));
|
|
|
|
|
userdata.event_loop_runner.redraw_events_cleared();
|
|
|
|
|
process_control_flow(&userdata.event_loop_runner);
|
2020-07-02 16:53:47 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
2020-05-04 15:14:13 -04:00
|
|
|
|
2020-11-24 23:05:29 +01:00
|
|
|
// Default WM_PAINT behaviour. This makes sure modals and popups are shown immediatly when opening them.
|
2022-03-07 22:58:12 +01:00
|
|
|
DefWindowProcW(window, msg, wparam, lparam)
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-12-30 14:11:11 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_INPUT_DEVICE_CHANGE => {
|
|
|
|
|
let event = match wparam as u32 {
|
|
|
|
|
GIDC_ARRIVAL => DeviceEvent::Added,
|
|
|
|
|
GIDC_REMOVAL => DeviceEvent::Removed,
|
2019-12-30 14:11:11 -05:00
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::DeviceEvent {
|
2022-03-07 22:58:12 +01:00
|
|
|
device_id: wrap_device_id(lparam as u32),
|
2019-12-30 14:11:11 -05:00
|
|
|
event,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
WM_INPUT => {
|
2019-12-30 14:11:11 -05:00
|
|
|
use crate::event::{
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
DeviceEvent::{Button, Key, Motion, MouseMotion, MouseWheel},
|
2019-12-30 14:11:11 -05:00
|
|
|
ElementState::{Pressed, Released},
|
|
|
|
|
MouseScrollDelta::LineDelta,
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
if let Some(data) = raw_input::get_raw_input_data(lparam) {
|
|
|
|
|
let device_id = wrap_device_id(data.header.hDevice as u32);
|
2019-12-30 14:11:11 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
if data.header.dwType == RIM_TYPEMOUSE {
|
|
|
|
|
let mouse = data.data.mouse;
|
2019-12-30 14:11:11 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
if util::has_flag(mouse.usFlags as u32, MOUSE_MOVE_RELATIVE) {
|
2019-12-30 14:11:11 -05:00
|
|
|
let x = mouse.lLastX as f64;
|
|
|
|
|
let y = mouse.lLastY as f64;
|
|
|
|
|
|
|
|
|
|
if x != 0.0 {
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::DeviceEvent {
|
2019-12-30 14:11:11 -05:00
|
|
|
device_id,
|
|
|
|
|
event: Motion { axis: 0, value: x },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if y != 0.0 {
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::DeviceEvent {
|
2019-12-30 14:11:11 -05:00
|
|
|
device_id,
|
|
|
|
|
event: Motion { axis: 1, value: y },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if x != 0.0 || y != 0.0 {
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::DeviceEvent {
|
2019-12-30 14:11:11 -05:00
|
|
|
device_id,
|
|
|
|
|
event: MouseMotion { delta: (x, y) },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
let mouse_button_flags = mouse.Anonymous.Anonymous.usButtonFlags;
|
|
|
|
|
|
|
|
|
|
if util::has_flag(mouse_button_flags as u32, RI_MOUSE_WHEEL) {
|
|
|
|
|
let delta = mouse_button_flags as i16 as f32 / WHEEL_DELTA as f32;
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::DeviceEvent {
|
2019-12-30 14:11:11 -05:00
|
|
|
device_id,
|
|
|
|
|
event: MouseWheel {
|
2021-03-11 22:08:29 +01:00
|
|
|
delta: LineDelta(0.0, delta),
|
2019-12-30 14:11:11 -05:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
let button_state =
|
|
|
|
|
raw_input::get_raw_mouse_button_state(mouse_button_flags as u32);
|
2019-12-30 14:11:11 -05:00
|
|
|
// Left, middle, and right, respectively.
|
|
|
|
|
for (index, state) in button_state.iter().enumerate() {
|
|
|
|
|
if let Some(state) = *state {
|
|
|
|
|
// This gives us consistency with X11, since there doesn't
|
|
|
|
|
// seem to be anything else reasonable to do for a mouse
|
|
|
|
|
// button ID.
|
2022-03-07 22:58:12 +01:00
|
|
|
let button = (index + 1) as u32;
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::DeviceEvent {
|
2019-12-30 14:11:11 -05:00
|
|
|
device_id,
|
|
|
|
|
event: Button { button, state },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-07 22:58:12 +01:00
|
|
|
} else if data.header.dwType == RIM_TYPEKEYBOARD {
|
|
|
|
|
let keyboard = data.data.keyboard;
|
2019-12-30 14:11:11 -05:00
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
let pressed =
|
|
|
|
|
keyboard.Message == WM_KEYDOWN || keyboard.Message == WM_SYSKEYDOWN;
|
|
|
|
|
let released = keyboard.Message == WM_KEYUP || keyboard.Message == WM_SYSKEYUP;
|
2019-12-30 14:11:11 -05:00
|
|
|
|
|
|
|
|
if pressed || released {
|
|
|
|
|
let state = if pressed { Pressed } else { Released };
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
let scancode = keyboard.MakeCode;
|
|
|
|
|
let extended = util::has_flag(keyboard.Flags, RI_KEY_E0 as u16)
|
|
|
|
|
| util::has_flag(keyboard.Flags, RI_KEY_E1 as u16);
|
2019-12-30 14:11:11 -05:00
|
|
|
|
|
|
|
|
if let Some((vkey, scancode)) =
|
2022-03-07 22:58:12 +01:00
|
|
|
handle_extended_keys(keyboard.VKey, scancode as u32, extended)
|
2019-12-30 14:11:11 -05:00
|
|
|
{
|
|
|
|
|
let virtual_keycode = vkey_to_winit_vkey(vkey);
|
|
|
|
|
|
2020-01-09 22:29:31 -07:00
|
|
|
#[allow(deprecated)]
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.send_event(Event::DeviceEvent {
|
2019-12-30 14:11:11 -05:00
|
|
|
device_id,
|
|
|
|
|
event: Key(KeyboardInput {
|
|
|
|
|
scancode,
|
|
|
|
|
state,
|
|
|
|
|
virtual_keycode,
|
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>
2020-03-06 15:43:55 -07:00
|
|
|
modifiers: event::get_key_mods(),
|
2019-12-30 14:11:11 -05:00
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
DefWindowProcW(window, msg, wparam, lparam)
|
2019-12-30 14:11:11 -05:00
|
|
|
}
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
_ if msg == *USER_EVENT_MSG_ID => {
|
2021-07-16 12:40:48 +02:00
|
|
|
if let Ok(event) = userdata.user_event_receiver.recv() {
|
|
|
|
|
userdata.send_event(Event::UserEvent(event));
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-02-05 10:30:33 -05:00
|
|
|
_ if msg == *EXEC_MSG_ID => {
|
|
|
|
|
let mut function: ThreadExecFn = Box::from_raw(wparam as usize as *mut _);
|
|
|
|
|
function();
|
|
|
|
|
0
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2020-05-04 15:14:13 -04:00
|
|
|
_ if msg == *PROCESS_NEW_EVENTS_MSG_ID => {
|
2022-03-07 22:58:12 +01:00
|
|
|
PostThreadMessageW(
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.event_loop_runner.wait_thread_id(),
|
2020-05-04 15:14:13 -04:00
|
|
|
*CANCEL_WAIT_UNTIL_MSG_ID,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// if the control_flow is WaitUntil, make sure the given moment has actually passed
|
|
|
|
|
// before emitting NewEvents
|
2021-07-16 12:40:48 +02:00
|
|
|
if let ControlFlow::WaitUntil(wait_until) = userdata.event_loop_runner.control_flow() {
|
2020-05-04 15:14:13 -04:00
|
|
|
let mut msg = mem::zeroed();
|
|
|
|
|
while Instant::now() < wait_until {
|
2022-03-07 22:58:12 +01:00
|
|
|
if PeekMessageW(&mut msg, 0, 0, 0, PM_NOREMOVE) != false.into() {
|
2020-05-04 15:14:13 -04:00
|
|
|
// This works around a "feature" in PeekMessageW. If the message PeekMessageW
|
|
|
|
|
// gets is a WM_PAINT message that had RDW_INTERNALPAINT set (i.e. doesn't
|
|
|
|
|
// have an update region), PeekMessageW will remove that window from the
|
|
|
|
|
// redraw queue even though we told it not to remove messages from the
|
|
|
|
|
// queue. We fix it by re-dispatching an internal paint message to that
|
|
|
|
|
// window.
|
2022-03-07 22:58:12 +01:00
|
|
|
if msg.message == WM_PAINT {
|
2020-05-04 15:14:13 -04:00
|
|
|
let mut rect = mem::zeroed();
|
2022-03-07 22:58:12 +01:00
|
|
|
if GetUpdateRect(msg.hwnd, &mut rect, false.into()) == false.into() {
|
|
|
|
|
RedrawWindow(msg.hwnd, ptr::null(), 0, RDW_INTERNALPAINT);
|
2020-05-04 15:14:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-16 12:40:48 +02:00
|
|
|
userdata.event_loop_runner.poll();
|
2020-05-04 15:14:13 -04:00
|
|
|
0
|
|
|
|
|
}
|
2022-03-07 22:58:12 +01:00
|
|
|
_ => DefWindowProcW(window, msg, wparam, lparam),
|
2020-05-04 15:14:13 -04:00
|
|
|
};
|
|
|
|
|
|
2021-07-16 12:40:48 +02:00
|
|
|
let result = userdata
|
2020-12-20 12:54:42 +01:00
|
|
|
.event_loop_runner
|
|
|
|
|
.catch_unwind(callback)
|
|
|
|
|
.unwrap_or(-1);
|
2021-07-16 12:40:48 +02:00
|
|
|
if userdata_removed {
|
|
|
|
|
mem::drop(userdata);
|
2020-12-20 12:54:42 +01:00
|
|
|
} else {
|
2021-07-16 12:40:48 +02:00
|
|
|
Box::into_raw(userdata);
|
2020-12-20 12:54:42 +01:00
|
|
|
}
|
|
|
|
|
result
|
2019-02-05 10:30:33 -05:00
|
|
|
}
|