mirror of
https://github.com/rust-windowing/winit.git
synced 2026-06-27 15:13:13 -04:00
Add ability to send custom user events
This commit is contained in:
@@ -3,9 +3,7 @@ use std::os::windows::ffi::OsStringExt;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::{mem, ptr};
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::VecDeque;
|
||||
use crossbeam_channel::Sender;
|
||||
|
||||
use winapi::ctypes::c_void;
|
||||
use winapi::shared::guiddef::REFIID;
|
||||
@@ -26,7 +24,7 @@ pub struct FileDropHandlerData {
|
||||
pub interface: IDropTarget,
|
||||
refcount: AtomicUsize,
|
||||
window: HWND,
|
||||
event_queue: Rc<RefCell<VecDeque<Event>>>,
|
||||
event_sender: Sender<Event<()>>
|
||||
}
|
||||
|
||||
pub struct FileDropHandler {
|
||||
@@ -35,14 +33,14 @@ pub struct FileDropHandler {
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
impl FileDropHandler {
|
||||
pub fn new(window: HWND, event_queue: Rc<RefCell<VecDeque<Event>>>) -> FileDropHandler {
|
||||
pub fn new(window: HWND, event_sender: Sender<Event<()>>) -> FileDropHandler {
|
||||
let data = Box::new(FileDropHandlerData {
|
||||
interface: IDropTarget {
|
||||
lpVtbl: &DROP_TARGET_VTBL as *const IDropTargetVtbl,
|
||||
},
|
||||
refcount: AtomicUsize::new(1),
|
||||
window,
|
||||
event_queue,
|
||||
event_sender,
|
||||
});
|
||||
FileDropHandler {
|
||||
data: Box::into_raw(data),
|
||||
@@ -187,8 +185,8 @@ impl FileDropHandler {
|
||||
}
|
||||
|
||||
impl FileDropHandlerData {
|
||||
fn send_event(&self, event: Event) {
|
||||
self.event_queue.borrow_mut().push_back(event);
|
||||
fn send_event(&self, event: Event<()>) {
|
||||
self.event_sender.send(event).ok();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,13 +14,11 @@
|
||||
|
||||
use winapi::shared::basetsd::DWORD_PTR;
|
||||
use winapi::shared::basetsd::UINT_PTR;
|
||||
use std::rc::Rc;
|
||||
use std::{mem, ptr};
|
||||
use std::cell::RefCell;
|
||||
use std::sync::Arc;
|
||||
use std::collections::VecDeque;
|
||||
use std::time::{Duration, Instant};
|
||||
use parking_lot::Mutex;
|
||||
use crossbeam_channel::{self, Sender, Receiver};
|
||||
|
||||
use winapi::ctypes::c_int;
|
||||
use winapi::shared::minwindef::{
|
||||
@@ -107,18 +105,6 @@ pub struct WindowState {
|
||||
pub mouse_buttons_down: u32
|
||||
}
|
||||
|
||||
pub(crate) struct SubclassInput {
|
||||
pub window_state: Arc<Mutex<WindowState>>,
|
||||
pub event_queue: Rc<RefCell<VecDeque<Event>>>,
|
||||
pub file_drop_handler: FileDropHandler
|
||||
}
|
||||
|
||||
impl SubclassInput {
|
||||
fn send_event(&self, event: Event) {
|
||||
self.event_queue.borrow_mut().push_back(event);
|
||||
}
|
||||
}
|
||||
|
||||
impl WindowState {
|
||||
pub fn update_min_max(&mut self, old_dpi_factor: f64, new_dpi_factor: f64) {
|
||||
let scale_factor = new_dpi_factor / old_dpi_factor;
|
||||
@@ -132,30 +118,55 @@ impl WindowState {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct EventLoop {
|
||||
// Id of the background thread from the Win32 API.
|
||||
thread_id: DWORD,
|
||||
pub(crate) event_queue: Rc<RefCell<VecDeque<Event>>>
|
||||
pub(crate) struct SubclassInput {
|
||||
pub window_state: Arc<Mutex<WindowState>>,
|
||||
pub event_send: Sender<Event<()>>,
|
||||
pub file_drop_handler: FileDropHandler
|
||||
}
|
||||
|
||||
impl EventLoop {
|
||||
pub fn new() -> EventLoop {
|
||||
impl SubclassInput {
|
||||
fn send_event(&self, event: Event<()>) {
|
||||
self.event_send.send(event).ok();
|
||||
}
|
||||
}
|
||||
|
||||
pub struct EventLoop<T> {
|
||||
// Id of the background thread from the Win32 API.
|
||||
thread_id: DWORD,
|
||||
|
||||
pub(crate) event_send: Sender<Event<()>>,
|
||||
pub(crate) user_event_send: Sender<T>,
|
||||
|
||||
event_recv: Receiver<Event<()>>,
|
||||
user_event_recv: Receiver<T>
|
||||
}
|
||||
|
||||
impl<T> EventLoop<T> {
|
||||
pub fn new() -> EventLoop<T> {
|
||||
Self::with_dpi_awareness(true)
|
||||
}
|
||||
|
||||
pub fn with_dpi_awareness(dpi_aware: bool) -> EventLoop {
|
||||
pub fn with_dpi_awareness(dpi_aware: bool) -> EventLoop<T> {
|
||||
become_dpi_aware(dpi_aware);
|
||||
|
||||
let thread_id = unsafe { processthreadsapi::GetCurrentThreadId() };
|
||||
|
||||
let (event_send, event_recv) = crossbeam_channel::unbounded();
|
||||
let (user_event_send, user_event_recv) = crossbeam_channel::unbounded();
|
||||
|
||||
EventLoop {
|
||||
thread_id,
|
||||
event_queue: Rc::new(RefCell::new(VecDeque::new()))
|
||||
|
||||
event_send,
|
||||
event_recv,
|
||||
|
||||
user_event_send,
|
||||
user_event_recv,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run<F>(self, mut event_handler: F) -> !
|
||||
where F: 'static + FnMut(Event, &::EventLoop, &mut ControlFlow)
|
||||
where F: 'static + FnMut(Event<T>, &::EventLoop<T>, &mut ControlFlow)
|
||||
{
|
||||
let event_loop = ::EventLoop {
|
||||
events_loop: self,
|
||||
@@ -239,9 +250,8 @@ impl EventLoop {
|
||||
|
||||
while has_message {
|
||||
match msg.message {
|
||||
x if x == *WAKEUP_MSG_ID => {
|
||||
call_event_handler!(Event::Awakened);
|
||||
},
|
||||
// Handler is called in loop below.
|
||||
x if x == *WAKEUP_MSG_ID => (),
|
||||
x if x == *EXEC_MSG_ID => {
|
||||
let mut function: Box<Box<FnMut()>> = Box::from_raw(msg.wParam as usize as *mut _);
|
||||
function()
|
||||
@@ -254,14 +264,16 @@ impl EventLoop {
|
||||
}
|
||||
|
||||
loop {
|
||||
// For whatever reason doing this in a `whlie let` loop doesn't drop the `RefMut`,
|
||||
// so we have to do it like this.
|
||||
let event = match event_loop.events_loop.event_queue.borrow_mut().pop_front() {
|
||||
Some(event) => event,
|
||||
None => break
|
||||
let full_event: Option<Event<T>> = select! {
|
||||
recv(event_loop.events_loop.event_recv) -> event =>
|
||||
event.ok().map(|e| e.map_nonuser_event().expect("User event sent through nonuser channel")),
|
||||
recv(event_loop.events_loop.user_event_recv) -> user_event =>
|
||||
user_event.ok().map(|e| Event::UserEvent(e)),
|
||||
default => break
|
||||
};
|
||||
|
||||
call_event_handler!(event);
|
||||
if let Some(full_event) = full_event {
|
||||
call_event_handler!(full_event);
|
||||
}
|
||||
}
|
||||
|
||||
if 0 == winuser::PeekMessageW(&mut msg, ptr::null_mut(), 0, 0, 1) {
|
||||
@@ -281,9 +293,17 @@ impl EventLoop {
|
||||
::std::process::exit(0);
|
||||
}
|
||||
|
||||
pub fn create_proxy(&self) -> EventLoopProxy {
|
||||
pub fn create_proxy(&self) -> EventLoopProxy<T> {
|
||||
EventLoopProxy {
|
||||
thread_id: self.thread_id,
|
||||
event_send: self.user_event_send.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn create_thread_executor(&self) -> EventLoopThreadExecutor {
|
||||
EventLoopThreadExecutor {
|
||||
thread_id: self.thread_id
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -310,7 +330,7 @@ pub fn dur2timeout(dur: Duration) -> DWORD {
|
||||
}).unwrap_or(winbase::INFINITE)
|
||||
}
|
||||
|
||||
impl Drop for EventLoop {
|
||||
impl<T> Drop for EventLoop<T> {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
// Posting `WM_QUIT` will cause `GetMessage` to stop.
|
||||
@@ -319,29 +339,11 @@ impl Drop for EventLoop {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct EventLoopProxy {
|
||||
thread_id: DWORD,
|
||||
pub(crate) struct EventLoopThreadExecutor {
|
||||
thread_id: DWORD
|
||||
}
|
||||
|
||||
impl EventLoopProxy {
|
||||
pub fn wakeup(&self) -> Result<(), EventLoopClosed> {
|
||||
unsafe {
|
||||
if winuser::PostThreadMessageA(self.thread_id, *WAKEUP_MSG_ID, 0, 0) != 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
// https://msdn.microsoft.com/fr-fr/library/windows/desktop/ms644946(v=vs.85).aspx
|
||||
// > If the function fails, the return value is zero. To get extended error
|
||||
// > information, call GetLastError. GetLastError returns ERROR_INVALID_THREAD_ID
|
||||
// > if idThread is not a valid thread identifier, or if the thread specified by
|
||||
// > idThread does not have a message queue. GetLastError returns
|
||||
// > ERROR_NOT_ENOUGH_QUOTA when the message limit is hit.
|
||||
// TODO: handle ERROR_NOT_ENOUGH_QUOTA
|
||||
Err(EventLoopClosed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl EventLoopThreadExecutor {
|
||||
/// Check to see if we're in the parent event loop's thread.
|
||||
pub(super) fn in_event_loop_thread(&self) -> bool {
|
||||
let cur_thread_id = unsafe { processthreadsapi::GetCurrentThreadId() };
|
||||
@@ -384,6 +386,32 @@ impl EventLoopProxy {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct EventLoopProxy<T> {
|
||||
thread_id: DWORD,
|
||||
event_send: Sender<T>
|
||||
}
|
||||
|
||||
impl<T> EventLoopProxy<T> {
|
||||
pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed> {
|
||||
unsafe {
|
||||
if winuser::PostThreadMessageA(self.thread_id, *WAKEUP_MSG_ID, 0, 0) != 0 {
|
||||
self.event_send.send(event).ok();
|
||||
Ok(())
|
||||
} else {
|
||||
// https://msdn.microsoft.com/fr-fr/library/windows/desktop/ms644946(v=vs.85).aspx
|
||||
// > If the function fails, the return value is zero. To get extended error
|
||||
// > information, call GetLastError. GetLastError returns ERROR_INVALID_THREAD_ID
|
||||
// > if idThread is not a valid thread identifier, or if the thread specified by
|
||||
// > idThread does not have a message queue. GetLastError returns
|
||||
// > ERROR_NOT_ENOUGH_QUOTA when the message limit is hit.
|
||||
// TODO: handle ERROR_NOT_ENOUGH_QUOTA
|
||||
Err(EventLoopClosed)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
// Message sent by the `EventLoopProxy` when we want to wake up the thread.
|
||||
// WPARAM and LPARAM are unused.
|
||||
|
||||
@@ -71,19 +71,19 @@ pub fn get_primary_monitor() -> MonitorId {
|
||||
MonitorId::from_hmonitor(hmonitor)
|
||||
}
|
||||
|
||||
impl EventLoop {
|
||||
pub fn get_current_monitor(hwnd: HWND) -> MonitorId {
|
||||
let hmonitor = unsafe {
|
||||
winuser::MonitorFromWindow(hwnd, winuser::MONITOR_DEFAULTTONEAREST)
|
||||
};
|
||||
MonitorId::from_hmonitor(hmonitor)
|
||||
}
|
||||
|
||||
impl<T> EventLoop<T> {
|
||||
// TODO: Investigate opportunities for caching
|
||||
pub fn get_available_monitors(&self) -> VecDeque<MonitorId> {
|
||||
get_available_monitors()
|
||||
}
|
||||
|
||||
pub fn get_current_monitor(hwnd: HWND) -> MonitorId {
|
||||
let hmonitor = unsafe {
|
||||
winuser::MonitorFromWindow(hwnd, winuser::MONITOR_DEFAULTTONEAREST)
|
||||
};
|
||||
MonitorId::from_hmonitor(hmonitor)
|
||||
}
|
||||
|
||||
pub fn get_primary_monitor(&self) -> MonitorId {
|
||||
get_primary_monitor()
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ use platform::platform::{Cursor, PlatformSpecificWindowBuilderAttributes, Window
|
||||
use platform::platform::dpi::{dpi_to_scale_factor, get_hwnd_dpi};
|
||||
use platform::platform::events_loop::{self, DESTROY_MSG_ID, EventLoop, INITIAL_DPI_MSG_ID, WindowState};
|
||||
use platform::platform::icon::{self, IconType, WinIcon};
|
||||
use platform::platform::monitor::get_available_monitors;
|
||||
use platform::platform::monitor;
|
||||
use platform::platform::raw_input::register_all_mice_and_keyboards_for_raw_input;
|
||||
use platform::platform::drop_handler::FileDropHandler;
|
||||
use platform::platform::util;
|
||||
@@ -48,7 +48,7 @@ pub struct Window {
|
||||
window_state: Arc<Mutex<WindowState>>,
|
||||
|
||||
// The events loop proxy.
|
||||
events_loop_proxy: events_loop::EventLoopProxy,
|
||||
thread_executor: events_loop::EventLoopThreadExecutor,
|
||||
}
|
||||
|
||||
// https://blogs.msdn.microsoft.com/oldnewthing/20131017-00/?p=2903
|
||||
@@ -74,8 +74,8 @@ unsafe fn unjust_window_rect(prc: &mut RECT, style: DWORD, ex_style: DWORD) -> B
|
||||
}
|
||||
|
||||
impl Window {
|
||||
pub fn new(
|
||||
events_loop: &EventLoop,
|
||||
pub fn new<T>(
|
||||
events_loop: &EventLoop<T>,
|
||||
w_attr: WindowAttributes,
|
||||
pl_attr: PlatformSpecificWindowBuilderAttributes,
|
||||
) -> Result<Window, CreationError> {
|
||||
@@ -322,7 +322,7 @@ impl Window {
|
||||
_ => winuser::IDC_ARROW, // use arrow for the missing cases.
|
||||
});
|
||||
self.window_state.lock().cursor = cursor_id;
|
||||
self.events_loop_proxy.execute_in_thread(move || unsafe {
|
||||
self.thread_executor.execute_in_thread(move || unsafe {
|
||||
let cursor = winuser::LoadCursorW(
|
||||
ptr::null_mut(),
|
||||
cursor_id.0,
|
||||
@@ -384,7 +384,7 @@ impl Window {
|
||||
let window = self.window.clone();
|
||||
let window_state = Arc::clone(&self.window_state);
|
||||
let (tx, rx) = channel();
|
||||
self.events_loop_proxy.execute_in_thread(move || {
|
||||
self.thread_executor.execute_in_thread(move || {
|
||||
let result = unsafe { Self::grab_cursor_inner(&window, grab) };
|
||||
if result.is_ok() {
|
||||
window_state.lock().cursor_grabbed = grab;
|
||||
@@ -410,7 +410,7 @@ impl Window {
|
||||
if hide == window_state_lock.cursor_hidden { return; }
|
||||
let (tx, rx) = channel();
|
||||
let window_state = Arc::clone(&self.window_state);
|
||||
self.events_loop_proxy.execute_in_thread(move || {
|
||||
self.thread_executor.execute_in_thread(move || {
|
||||
unsafe { Self::hide_cursor_inner(hide) };
|
||||
window_state.lock().cursor_hidden = hide;
|
||||
let _ = tx.send(());
|
||||
@@ -458,7 +458,7 @@ impl Window {
|
||||
let window = self.window.clone();
|
||||
unsafe {
|
||||
// `ShowWindow` resizes the window, so it must be called from the main thread.
|
||||
self.events_loop_proxy.execute_in_thread(move || {
|
||||
self.thread_executor.execute_in_thread(move || {
|
||||
winuser::ShowWindow(
|
||||
window.0,
|
||||
if maximized {
|
||||
@@ -524,7 +524,7 @@ impl Window {
|
||||
drop(window_state_lock);
|
||||
// We're restoring the window to its size and position from before being fullscreened.
|
||||
// `ShowWindow` resizes the window, so it must be called from the main thread.
|
||||
self.events_loop_proxy.execute_in_thread(move || {
|
||||
self.thread_executor.execute_in_thread(move || {
|
||||
let _ = Self::grab_cursor_inner(&window, false);
|
||||
|
||||
if resizable {
|
||||
@@ -585,7 +585,7 @@ impl Window {
|
||||
window_state_lock.fullscreen = monitor;
|
||||
drop(window_state_lock);
|
||||
|
||||
self.events_loop_proxy.execute_in_thread(move || {
|
||||
self.thread_executor.execute_in_thread(move || {
|
||||
let _ = Self::grab_cursor_inner(&window, false);
|
||||
|
||||
winuser::SetWindowLongW(
|
||||
@@ -681,7 +681,7 @@ impl Window {
|
||||
|
||||
let window = self.window.clone();
|
||||
|
||||
self.events_loop_proxy.execute_in_thread(move || {
|
||||
self.thread_executor.execute_in_thread(move || {
|
||||
winuser::SetWindowLongW(window.0, winuser::GWL_STYLE, style);
|
||||
winuser::SetWindowLongW(window.0, winuser::GWL_EXSTYLE, ex_style);
|
||||
winuser::AdjustWindowRectEx(&mut rect, style as _, 0, ex_style as _);
|
||||
@@ -709,7 +709,7 @@ impl Window {
|
||||
let mut window_state = self.window_state.lock();
|
||||
if mem::replace(&mut window_state.always_on_top, always_on_top) != always_on_top {
|
||||
let window = self.window.clone();
|
||||
self.events_loop_proxy.execute_in_thread(move || {
|
||||
self.thread_executor.execute_in_thread(move || {
|
||||
let insert_after = if always_on_top {
|
||||
winuser::HWND_TOPMOST
|
||||
} else {
|
||||
@@ -734,7 +734,7 @@ impl Window {
|
||||
#[inline]
|
||||
pub fn get_current_monitor(&self) -> RootMonitorId {
|
||||
RootMonitorId {
|
||||
inner: EventLoop::get_current_monitor(self.window.0),
|
||||
inner: monitor::get_current_monitor(self.window.0),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -809,10 +809,10 @@ pub unsafe fn adjust_size(
|
||||
(rect.right - rect.left, rect.bottom - rect.top)
|
||||
}
|
||||
|
||||
unsafe fn init(
|
||||
unsafe fn init<T>(
|
||||
mut attributes: WindowAttributes,
|
||||
mut pl_attribs: PlatformSpecificWindowBuilderAttributes,
|
||||
event_loop: &events_loop::EventLoop,
|
||||
event_loop: &events_loop::EventLoop<T>,
|
||||
) -> Result<Window, CreationError> {
|
||||
let title = OsStr::new(&attributes.title)
|
||||
.encode_wide()
|
||||
@@ -848,7 +848,7 @@ unsafe fn init(
|
||||
let class_name = register_window_class(&window_icon, &taskbar_icon);
|
||||
|
||||
let guessed_dpi_factor = {
|
||||
let monitors = get_available_monitors();
|
||||
let monitors = monitor::get_available_monitors();
|
||||
let dpi_factor = if !monitors.is_empty() {
|
||||
let mut dpi_factor = Some(monitors[0].get_hidpi_factor());
|
||||
for monitor in &monitors {
|
||||
@@ -1055,7 +1055,7 @@ unsafe fn init(
|
||||
let win = Window {
|
||||
window: real_window,
|
||||
window_state,
|
||||
events_loop_proxy: event_loop.create_proxy(),
|
||||
thread_executor: event_loop.create_thread_executor(),
|
||||
};
|
||||
|
||||
win.set_maximized(attributes.maximized);
|
||||
@@ -1076,7 +1076,7 @@ unsafe fn init(
|
||||
panic!("OleInitialize failed! Result was: `RPC_E_CHANGED_MODE`");
|
||||
}
|
||||
|
||||
let file_drop_handler = FileDropHandler::new(win.window.0, event_loop.event_queue.clone());
|
||||
let file_drop_handler = FileDropHandler::new(win.window.0, event_loop.event_send.clone());
|
||||
let handler_interface_ptr = &mut (*file_drop_handler.data).interface as LPDROPTARGET;
|
||||
|
||||
assert_eq!(ole2::RegisterDragDrop(win.window.0, handler_interface_ptr), S_OK);
|
||||
@@ -1085,8 +1085,8 @@ unsafe fn init(
|
||||
|
||||
let subclass_input = events_loop::SubclassInput {
|
||||
window_state: win.window_state.clone(),
|
||||
event_queue: event_loop.event_queue.clone(),
|
||||
file_drop_handler
|
||||
event_send: event_loop.event_send.clone(),
|
||||
file_drop_handler,
|
||||
};
|
||||
|
||||
events_loop::subclass_window(win.window.0, subclass_input);
|
||||
|
||||
Reference in New Issue
Block a user