Skip to content

Commit fa46825

Browse files
committedNov 15, 2018
Replace &EventLoop in callback with &EventLoopWindowTarget
1 parent 9f36a7a commit fa46825

File tree

6 files changed

+55
-30
lines changed

6 files changed

+55
-30
lines changed
 

‎src/event_loop.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! [send_event]: ./struct.EventLoopProxy.html#method.send_event
1212
use std::{fmt, error};
1313
use std::time::Instant;
14+
use std::ops::Deref;
1415

1516
use platform_impl;
1617
use event::Event;
@@ -34,6 +35,11 @@ pub struct EventLoop<T: 'static> {
3435
pub(crate) _marker: ::std::marker::PhantomData<*mut ()> // Not Send nor Sync
3536
}
3637

38+
pub struct EventLoopWindowTarget<T: 'static> {
39+
pub(crate) p: platform_impl::EventLoopWindowTarget<T>,
40+
pub(crate) _marker: ::std::marker::PhantomData<*mut ()> // Not Send nor Sync
41+
}
42+
3743
impl<T> fmt::Debug for EventLoop<T> {
3844
fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
3945
fmtr.pad("EventLoop { .. }")
@@ -123,7 +129,7 @@ impl<T> EventLoop<T> {
123129
/// [`ControlFlow`]: ./enum.ControlFlow.html
124130
#[inline]
125131
pub fn run<F>(self, event_handler: F) -> !
126-
where F: 'static + FnMut(Event<T>, &EventLoop<T>, &mut ControlFlow)
132+
where F: 'static + FnMut(Event<T>, &EventLoopWindowTarget<T>, &mut ControlFlow)
127133
{
128134
self.event_loop.run(event_handler)
129135
}
@@ -137,6 +143,13 @@ impl<T> EventLoop<T> {
137143
}
138144
}
139145

146+
impl<T> Deref for EventLoop<T> {
147+
type Target = EventLoopWindowTarget<T>;
148+
fn deref(&self) -> &EventLoopWindowTarget<T> {
149+
self.event_loop.window_target()
150+
}
151+
}
152+
140153
/// Used to send custom events to `EventLoop`.
141154
#[derive(Clone)]
142155
pub struct EventLoopProxy<T> {

‎src/platform/desktop.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
))]
66

77
use event::Event;
8-
use event_loop::{EventLoop, ControlFlow};
8+
use event_loop::{EventLoop, EventLoopWindowTarget, ControlFlow};
99

1010
/// Additional methods on `EventLoop` that are specific to desktop platforms.
1111
pub trait EventLoopExtDesktop {
@@ -17,14 +17,14 @@ pub trait EventLoopExtDesktop {
1717
/// Unlikes `run`, this function accepts non-`'static` (i.e. non-`move`) closures and returns
1818
/// control flow to the caller when `control_flow` is set to `ControlFlow::Exit`.
1919
fn run_return<F>(&mut self, event_handler: F)
20-
where F: FnMut(Event<Self::UserEvent>, &EventLoop<Self::UserEvent>, &mut ControlFlow);
20+
where F: FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget<Self::UserEvent>, &mut ControlFlow);
2121
}
2222

2323
impl<T> EventLoopExtDesktop for EventLoop<T> {
2424
type UserEvent = T;
2525

2626
fn run_return<F>(&mut self, event_handler: F)
27-
where F: FnMut(Event<T>, &EventLoop<T>, &mut ControlFlow)
27+
where F: FnMut(Event<T>, &EventLoopWindowTarget<T>, &mut ControlFlow)
2828
{
2929
self.event_loop.run_return(event_handler)
3030
}

‎src/platform_impl/windows/event_loop.rs

+30-18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use std::time::{Duration, Instant};
2222
use std::rc::Rc;
2323
use std::cell::RefCell;
2424
use std::collections::VecDeque;
25+
use std::marker::PhantomData;
2526
use parking_lot::Mutex;
2627

2728
use winapi::ctypes::c_int;
@@ -43,7 +44,7 @@ use winapi::um::winnt::{LONG, LPCSTR, SHORT};
4344

4445
use window::WindowId as RootWindowId;
4546
use monitor::MonitorHandle;
46-
use event_loop::{ControlFlow, EventLoop as RootEventLoop, EventLoopClosed};
47+
use event_loop::{ControlFlow, EventLoopWindowTarget as RootELW, EventLoopClosed};
4748
use dpi::{LogicalPosition, LogicalSize, PhysicalSize};
4849
use event::{DeviceEvent, Touch, TouchPhase, StartCause, KeyboardInput, Event, WindowEvent};
4950
use platform_impl::platform::{event, Cursor, WindowId, DEVICE_ID, wrap_device_id, util};
@@ -138,11 +139,15 @@ impl<T> ThreadMsgTargetSubclassInput<T> {
138139
}
139140
}
140141

141-
pub struct EventLoop<T> {
142+
pub struct EventLoop<T: 'static> {
142143
// Id of the background thread from the Win32 API.
143-
thread_id: DWORD,
144144
thread_msg_target: HWND,
145145
thread_msg_sender: Sender<T>,
146+
window_target: RootELW<T>
147+
}
148+
149+
pub struct EventLoopWindowTarget<T> {
150+
thread_id: DWORD,
146151
trigger_newevents_on_redraw: Arc<AtomicBool>,
147152
pub(crate) runner_shared: EventLoopRunnerShared<T>,
148153
}
@@ -152,6 +157,10 @@ impl<T: 'static> EventLoop<T> {
152157
Self::with_dpi_awareness(true)
153158
}
154159

160+
pub fn window_target(&self) -> &RootELW<T> {
161+
&self.window_target
162+
}
163+
155164
pub fn with_dpi_awareness(dpi_aware: bool) -> EventLoop<T> {
156165
become_dpi_aware(dpi_aware);
157166

@@ -163,37 +172,40 @@ impl<T: 'static> EventLoop<T> {
163172
let (thread_msg_target, thread_msg_sender) = thread_event_target_window(runner_shared.clone());
164173

165174
EventLoop {
166-
thread_id,
167175
thread_msg_target, thread_msg_sender,
168-
trigger_newevents_on_redraw: Arc::new(AtomicBool::new(true)),
169-
runner_shared
176+
window_target: RootELW {
177+
p: EventLoopWindowTarget {
178+
thread_id,
179+
trigger_newevents_on_redraw: Arc::new(AtomicBool::new(true)),
180+
runner_shared
181+
},
182+
_marker: PhantomData
183+
}
170184
}
171185
}
172186

173187
pub fn run<F>(mut self, event_handler: F) -> !
174-
where F: 'static + FnMut(Event<T>, &RootEventLoop<T>, &mut ControlFlow)
188+
where F: 'static + FnMut(Event<T>, &RootELW<T>, &mut ControlFlow)
175189
{
176190
self.run_return(event_handler);
177191
::std::process::exit(0);
178192
}
179193

180194
pub fn run_return<F>(&mut self, mut event_handler: F)
181-
where F: FnMut(Event<T>, &RootEventLoop<T>, &mut ControlFlow)
195+
where F: FnMut(Event<T>, &RootELW<T>, &mut ControlFlow)
182196
{
183197
unsafe{ winuser::IsGUIThread(1); }
184198

185-
assert_eq!(mem::size_of::<RootEventLoop<T>>(), mem::size_of::<EventLoop<T>>());
186-
let self_ptr = self as *const EventLoop<T>;
199+
let event_loop_windows_ref = &self.window_target;
187200

188201
let mut runner = unsafe{ EventLoopRunner::new(
189202
self,
190203
move |event, control_flow| {
191-
let event_loop_ref = &*(self_ptr as *const RootEventLoop<T>);
192-
event_handler(event, event_loop_ref, control_flow)
204+
event_handler(event, event_loop_windows_ref, control_flow)
193205
}
194206
) };
195207
{
196-
let runner_shared = self.runner_shared.clone();
208+
let runner_shared = self.window_target.p.runner_shared.clone();
197209
let mut runner_ref = runner_shared.runner.borrow_mut();
198210
loop {
199211
let event = runner_shared.buffer.borrow_mut().pop_front();
@@ -206,7 +218,7 @@ impl<T: 'static> EventLoop<T> {
206218
}
207219

208220
macro_rules! runner {
209-
() => {{ self.runner_shared.runner.borrow_mut().as_mut().unwrap() }};
221+
() => {{ self.window_target.p.runner_shared.runner.borrow_mut().as_mut().unwrap() }};
210222
}
211223

212224
unsafe {
@@ -244,7 +256,7 @@ impl<T: 'static> EventLoop<T> {
244256
}
245257

246258
runner!().call_event_handler(Event::LoopDestroyed);
247-
*self.runner_shared.runner.borrow_mut() = None;
259+
*self.window_target.p.runner_shared.runner.borrow_mut() = None;
248260
}
249261

250262
pub fn create_proxy(&self) -> EventLoopProxy<T> {
@@ -253,7 +265,9 @@ impl<T: 'static> EventLoop<T> {
253265
event_send: self.thread_msg_sender.clone()
254266
}
255267
}
268+
}
256269

270+
impl<T> EventLoopWindowTarget<T> {
257271
#[inline(always)]
258272
pub(crate) fn create_thread_executor(&self) -> EventLoopThreadExecutor {
259273
EventLoopThreadExecutor {
@@ -309,7 +323,7 @@ impl<T> EventLoopRunner<T> {
309323
where F: FnMut(Event<T>, &mut ControlFlow)
310324
{
311325
EventLoopRunner {
312-
trigger_newevents_on_redraw: event_loop.trigger_newevents_on_redraw.clone(),
326+
trigger_newevents_on_redraw: event_loop.window_target.p.trigger_newevents_on_redraw.clone(),
313327
control_flow: ControlFlow::default(),
314328
runner_state: RunnerState::New,
315329
in_modal_loop: false,
@@ -545,8 +559,6 @@ impl<T> Drop for EventLoop<T> {
545559
fn drop(&mut self) {
546560
unsafe {
547561
winuser::DestroyWindow(self.thread_msg_target);
548-
// Posting `WM_QUIT` will cause `GetMessage` to stop.
549-
winuser::PostThreadMessageA(self.thread_id, winuser::WM_QUIT, 0, 0);
550562
}
551563
}
552564
}

‎src/platform_impl/windows/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use winapi;
44
use winapi::shared::windef::HWND;
55

6-
pub use self::event_loop::{EventLoop, EventLoopProxy};
6+
pub use self::event_loop::{EventLoop, EventLoopWindowTarget, EventLoopProxy};
77
pub use self::monitor::MonitorHandle;
88
pub use self::window::Window;
99

‎src/platform_impl/windows/window.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use dpi::{LogicalPosition, LogicalSize, PhysicalSize};
2323
use monitor::MonitorHandle as RootMonitorHandle;
2424
use platform_impl::platform::{Cursor, PlatformSpecificWindowBuilderAttributes, WindowId};
2525
use platform_impl::platform::dpi::{dpi_to_scale_factor, get_hwnd_dpi};
26-
use platform_impl::platform::event_loop::{self, EventLoop, DESTROY_MSG_ID, INITIAL_DPI_MSG_ID, REQUEST_REDRAW_NO_NEWEVENTS_MSG_ID};
26+
use platform_impl::platform::event_loop::{self, EventLoopWindowTarget, DESTROY_MSG_ID, INITIAL_DPI_MSG_ID, REQUEST_REDRAW_NO_NEWEVENTS_MSG_ID};
2727
use platform_impl::platform::event_loop::WindowState;
2828
use platform_impl::platform::icon::{self, IconType, WinIcon};
2929
use platform_impl::platform::monitor;
@@ -69,7 +69,7 @@ unsafe fn unjust_window_rect(prc: &mut RECT, style: DWORD, ex_style: DWORD) -> B
6969

7070
impl Window {
7171
pub fn new<T: 'static>(
72-
event_loop: &EventLoop<T>,
72+
event_loop: &EventLoopWindowTarget<T>,
7373
w_attr: WindowAttributes,
7474
pl_attr: PlatformSpecificWindowBuilderAttributes,
7575
) -> Result<Window, CreationError> {
@@ -857,7 +857,7 @@ pub unsafe fn adjust_size(
857857
unsafe fn init<T: 'static>(
858858
mut attributes: WindowAttributes,
859859
mut pl_attribs: PlatformSpecificWindowBuilderAttributes,
860-
event_loop: &event_loop::EventLoop<T>,
860+
event_loop: &EventLoopWindowTarget<T>,
861861
) -> Result<Window, CreationError> {
862862
let title = OsStr::new(&attributes.title)
863863
.encode_wide()

‎src/window.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use std::{fmt, error};
33

44
use platform_impl;
5-
use event_loop::EventLoop;
5+
use event_loop::EventLoopWindowTarget;
66
use monitor::{AvailableMonitorsIter, MonitorHandle};
77
use dpi::{LogicalPosition, LogicalSize};
88

@@ -283,7 +283,7 @@ impl WindowBuilder {
283283
/// Error should be very rare and only occur in case of permission denied, incompatible system,
284284
/// out of memory, etc.
285285
#[inline]
286-
pub fn build<T: 'static>(mut self, event_loop: &EventLoop<T>) -> Result<Window, CreationError> {
286+
pub fn build<T: 'static>(mut self, window_target: &EventLoopWindowTarget<T>) -> Result<Window, CreationError> {
287287
self.window.dimensions = Some(self.window.dimensions.unwrap_or_else(|| {
288288
if let Some(ref monitor) = self.window.fullscreen {
289289
// resizing the window to the dimensions of the monitor when fullscreen
@@ -296,7 +296,7 @@ impl WindowBuilder {
296296

297297
// building
298298
platform_impl::Window::new(
299-
&event_loop.event_loop,
299+
&window_target.p,
300300
self.window,
301301
self.platform_specific,
302302
).map(|window| Window { window })
@@ -311,7 +311,7 @@ impl Window {
311311
/// Error should be very rare and only occur in case of permission denied, incompatible system,
312312
/// out of memory, etc.
313313
#[inline]
314-
pub fn new<T: 'static>(event_loop: &EventLoop<T>) -> Result<Window, CreationError> {
314+
pub fn new<T: 'static>(event_loop: &EventLoopWindowTarget<T>) -> Result<Window, CreationError> {
315315
let builder = WindowBuilder::new();
316316
builder.build(event_loop)
317317
}

0 commit comments

Comments
 (0)