Update run_forever to hijack thread

This commit is contained in:
Osspial
2018-07-13 01:39:53 -04:00
parent 2e83bac99c
commit 9feada206f
14 changed files with 100 additions and 64 deletions

View File

@@ -62,7 +62,7 @@
//! # use winit::EventLoop;
//! # let mut events_loop = EventLoop::new();
//!
//! events_loop.run_forever(|event| {
//! events_loop.run_forever(move |event, _: &EventLoop| {
//! match event {
//! Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
//! println!("The close button was pressed; stopping");
@@ -139,7 +139,7 @@ pub mod os;
/// let mut events_loop = EventLoop::new();
/// let window = Window::new(&events_loop).unwrap();
///
/// events_loop.run_forever(|event| {
/// events_loop.run_forever(move |event, _: &EventLoop| {
/// match event {
/// Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
/// ControlFlow::Break
@@ -199,6 +199,18 @@ impl std::fmt::Debug for EventLoop {
}
}
pub trait EventHandler {
fn handle_event(&mut self, event: Event, event_loop: &EventLoop) -> ControlFlow;
}
impl<F> EventHandler for F
where F: FnMut(Event, &EventLoop) -> ControlFlow
{
fn handle_event(&mut self, event: Event, event_loop: &EventLoop) -> ControlFlow {
self(event, event_loop)
}
}
/// Returned by the user callback given to the `EventLoop::run_forever` method.
///
/// Indicates whether the `run_forever` method should continue or complete.
@@ -240,19 +252,13 @@ impl EventLoop {
MonitorId { inner: self.events_loop.get_primary_monitor() }
}
/// Calls `callback` every time an event is received. If no event is available, sleeps the
/// current thread and waits for an event. If the callback returns `ControlFlow::Break` then
/// `run_forever` will immediately return.
/// Hijacks the calling thread and initializes the `winit` event loop. Can take a
/// `FnMut(Event, &EventLoop) -> ControlFlow` or a custom `EventHandler` type.
///
/// # Danger!
///
/// The callback is run after *every* event, so if its execution time is non-trivial the event queue may not empty
/// at a sufficient rate. Rendering in the callback with vsync enabled **will** cause significant lag.
/// Any values not passed to this function will *not* be dropped.
#[inline]
pub fn run_forever<F>(&mut self, callback: F)
where F: FnMut(Event) -> ControlFlow
{
self.events_loop.run_forever(callback)
pub fn run_forever(self, event_handler: impl 'static + EventHandler) -> ! {
self.events_loop.run_forever(event_handler)
}
/// Creates an `EventLoopProxy` that can be used to wake up the `EventLoop` from another

View File

@@ -5,6 +5,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::{mem, ptr};
use std::rc::Rc;
use std::cell::RefCell;
use std::collections::VecDeque;
use winapi::ctypes::c_void;
use winapi::shared::guiddef::REFIID;
@@ -25,7 +26,7 @@ pub struct FileDropHandlerData {
pub interface: IDropTarget,
refcount: AtomicUsize,
window: HWND,
event_queue: Rc<RefCell<Vec<Event>>>,
event_queue: Rc<RefCell<VecDeque<Event>>>,
}
pub struct FileDropHandler {
@@ -34,7 +35,7 @@ pub struct FileDropHandler {
#[allow(non_snake_case)]
impl FileDropHandler {
pub fn new(window: HWND, event_queue: Rc<RefCell<Vec<Event>>>) -> FileDropHandler {
pub fn new(window: HWND, event_queue: Rc<RefCell<VecDeque<Event>>>) -> FileDropHandler {
let data = Box::new(FileDropHandlerData {
interface: IDropTarget {
lpVtbl: &DROP_TARGET_VTBL as *const IDropTargetVtbl,
@@ -187,7 +188,7 @@ impl FileDropHandler {
impl FileDropHandlerData {
fn send_event(&self, event: Event) {
self.event_queue.borrow_mut().push(event);
self.event_queue.borrow_mut().push_back(event);
}
}

View File

@@ -17,9 +17,8 @@ use winapi::shared::basetsd::UINT_PTR;
use std::rc::Rc;
use std::{mem, ptr};
use std::cell::RefCell;
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
use std::sync::{Arc, Mutex};
use std::collections::VecDeque;
use winapi::ctypes::c_int;
use winapi::shared::minwindef::{
@@ -35,14 +34,13 @@ use winapi::shared::minwindef::{
};
use winapi::shared::windef::{HWND, POINT, RECT};
use winapi::shared::windowsx;
use winapi::shared::winerror::S_OK;
use winapi::um::{winuser, processthreadsapi, ole2, shellapi, commctrl};
use winapi::um::oleidl::LPDROPTARGET;
use winapi::um::{winuser, processthreadsapi, ole2, commctrl};
use winapi::um::winnt::{LONG, LPCSTR, SHORT};
use {
ControlFlow,
Event,
EventHandler,
EventLoopClosed,
KeyboardInput,
LogicalPosition,
@@ -110,13 +108,13 @@ pub struct WindowState {
pub(crate) struct SubclassInput {
pub window_state: Arc<Mutex<WindowState>>,
pub event_queue: Rc<RefCell<Vec<Event>>>,
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(event);
self.event_queue.borrow_mut().push_back(event);
}
}
@@ -136,7 +134,7 @@ impl WindowState {
pub struct EventLoop {
// Id of the background thread from the Win32 API.
thread_id: DWORD,
pub(crate) event_queue: Rc<RefCell<Vec<Event>>>
pub(crate) event_queue: Rc<RefCell<VecDeque<Event>>>
}
impl EventLoop {
@@ -151,13 +149,15 @@ impl EventLoop {
EventLoop {
thread_id,
event_queue: Rc::new(RefCell::new(Vec::new()))
event_queue: Rc::new(RefCell::new(VecDeque::new()))
}
}
pub fn run_forever<F>(&mut self, mut callback: F)
where F: FnMut(Event) -> ControlFlow
{
pub fn run_forever(self, mut event_handler: impl 'static + EventHandler) -> ! {
let event_loop = ::EventLoop {
events_loop: self,
_marker: ::std::marker::PhantomData
};
unsafe {
// Calling `PostThreadMessageA` on a thread that does not have an events queue yet
// will fail. In order to avoid this situation, we call `IsGuiThread` to initialize
@@ -166,17 +166,17 @@ impl EventLoop {
let mut msg = mem::uninitialized();
loop {
'main: loop {
if winuser::GetMessageW(&mut msg, ptr::null_mut(), 0, 0) == 0 {
// Only happens if the message is `WM_QUIT`.
debug_assert_eq!(msg.message, winuser::WM_QUIT);
return;
break 'main;
}
match msg.message {
x if x == *WAKEUP_MSG_ID => {
if ControlFlow::Break == callback(Event::Awakened) {
return;
if ControlFlow::Break == event_handler.handle_event(Event::Awakened, &event_loop) {
break 'main;
}
},
x if x == *EXEC_MSG_ID => {
@@ -184,20 +184,29 @@ impl EventLoop {
function()
}
_ => {
// Calls `callback` below.
// Calls `event_handler` below.
winuser::TranslateMessage(&msg);
winuser::DispatchMessageW(&msg);
}
}
let mut event_queue = self.event_queue.borrow_mut();
for event in event_queue.drain(..) {
if ControlFlow::Break == callback(event) {
return;
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
};
if ControlFlow::Break == event_handler.handle_event(event, &event_loop) {
break 'main;
}
}
}
}
drop(event_handler);
::std::process::exit(0);
}
pub fn create_proxy(&self) -> EventLoopProxy {