Skip to content

Commit a0fef1a

Browse files
committed
Fully invert windows control flow so win32 calls into winit's callback
1 parent 2c607ff commit a0fef1a

File tree

6 files changed

+510
-204
lines changed

6 files changed

+510
-204
lines changed

examples/timer.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
extern crate winit;
22
use std::time::{Duration, Instant};
3+
use winit::{Event, WindowEvent, StartCause, ControlFlow};
34

45
fn main() {
56
let events_loop = winit::EventLoop::new();
@@ -13,20 +14,16 @@ fn main() {
1314
println!("{:?}", event);
1415

1516
match event {
16-
winit::Event::NewEvents(winit::StartCause::Init) =>
17-
*control_flow = winit::ControlFlow::WaitTimeout(Duration::new(1, 0)),
18-
winit::Event::NewEvents(winit::StartCause::TimeoutExpired{..}) => {
19-
*control_flow = winit::ControlFlow::WaitTimeout(Duration::new(1, 0));
17+
Event::NewEvents(StartCause::Init) =>
18+
*control_flow = ControlFlow::WaitUntil(Instant::now() + Duration::new(1, 0)),
19+
Event::NewEvents(StartCause::ResumeTimeReached{..}) => {
20+
*control_flow = ControlFlow::WaitUntil(Instant::now() + Duration::new(1, 0));
2021
println!("\nTimer\n");
2122
},
22-
winit::Event::NewEvents(winit::StartCause::WaitCancelled{start, requested_duration}) => {
23-
println!("{:?}", Instant::now() - start);
24-
*control_flow = winit::ControlFlow::WaitTimeout(requested_duration.unwrap().checked_sub(Instant::now() - start).unwrap_or(Duration::new(0, 0)));
25-
}
26-
winit::Event::WindowEvent {
27-
event: winit::WindowEvent::CloseRequested,
23+
Event::WindowEvent {
24+
event: WindowEvent::CloseRequested,
2825
..
29-
} => *control_flow = winit::ControlFlow::Exit,
26+
} => *control_flow = ControlFlow::Exit,
3027
_ => ()
3128
}
3229
});

src/events.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::time::{Duration, Instant};
1+
use std::time::Instant;
22
use std::path::PathBuf;
33

44
use {DeviceId, LogicalPosition, LogicalSize, WindowId};
@@ -48,19 +48,19 @@ impl<T> Event<T> {
4848

4949
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5050
pub enum StartCause {
51-
/// Sent if the time specified by `ControlFlow::WaitTimeout` has been elapsed. Contains the
52-
/// moment the timeout was requested and the requested duration of the timeout. The actual
53-
/// duration is guaranteed to be greater than or equal to the requested timeout.
54-
TimeoutExpired {
51+
/// Sent if the time specified by `ControlFlow::WaitUntil` has been reached. Contains the
52+
/// moment the timeout was requested and the requested resume time. The actual resume time is
53+
/// guaranteed to be equal to or after the requested resume time.
54+
ResumeTimeReached {
5555
start: Instant,
56-
requested_duration: Duration,
56+
requested_resume: Instant
5757
},
5858

5959
/// Sent if the OS has new events to send to the window, after a wait was requested. Contains
60-
/// the moment the wait was requested, and if a wait timout was requested, its duration.
60+
/// the moment the wait was requested and the resume time, if requested.
6161
WaitCancelled {
6262
start: Instant,
63-
requested_duration: Option<Duration>
63+
requested_resume: Option<Instant>
6464
},
6565

6666
/// Sent if the event loop is being resumed after the loop's control flow was set to

src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ extern crate percent_encoding;
9393
#[cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))]
9494
extern crate smithay_client_toolkit as sctk;
9595

96-
use std::time::Duration;
96+
use std::time::Instant;
9797
pub(crate) use dpi::*; // TODO: Actually change the imports throughout the codebase.
9898
pub use events::*;
9999
pub use window::{AvailableMonitorsIter, MonitorId};
@@ -186,8 +186,8 @@ pub enum ControlFlow {
186186
/// When the current loop iteration finishes, suspend the thread until another event arrives.
187187
Wait,
188188
/// When the current loop iteration finishes, suspend the thread until either another event
189-
/// arrives or the timeout expires.
190-
WaitTimeout(Duration),
189+
/// arrives or the given time is reached.
190+
WaitUntil(Instant),
191191
/// When the current loop iteration finishes, immediately begin a new iteration regardless of
192192
/// whether or not new events are available to process.
193193
Poll,

src/platform/windows/drop_handler.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct FileDropHandlerData {
2424
pub interface: IDropTarget,
2525
refcount: AtomicUsize,
2626
window: HWND,
27-
event_sender: Sender<Event<()>>
27+
// event_sender: Sender<Event<()>>
2828
}
2929

3030
pub struct FileDropHandler {
@@ -33,14 +33,14 @@ pub struct FileDropHandler {
3333

3434
#[allow(non_snake_case)]
3535
impl FileDropHandler {
36-
pub fn new(window: HWND, event_sender: Sender<Event<()>>) -> FileDropHandler {
36+
pub fn new(window: HWND/*, event_sender: Sender<Event<()>>*/) -> FileDropHandler {
3737
let data = Box::new(FileDropHandlerData {
3838
interface: IDropTarget {
3939
lpVtbl: &DROP_TARGET_VTBL as *const IDropTargetVtbl,
4040
},
4141
refcount: AtomicUsize::new(1),
4242
window,
43-
event_sender,
43+
// event_sender,
4444
});
4545
FileDropHandler {
4646
data: Box::into_raw(data),
@@ -186,7 +186,7 @@ impl FileDropHandler {
186186

187187
impl FileDropHandlerData {
188188
fn send_event(&self, event: Event<()>) {
189-
self.event_sender.send(event).ok();
189+
// self.event_sender.send(event).ok();
190190
}
191191
}
192192

0 commit comments

Comments
 (0)