Skip to content

Commit 6629f8a

Browse files
committed
Implement new redraw API
1 parent 32aad83 commit 6629f8a

File tree

5 files changed

+68
-141
lines changed

5 files changed

+68
-141
lines changed

examples/window.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ fn main() {
2020
event: WindowEvent::CloseRequested,
2121
window_id,
2222
} if window_id == window.id() => *control_flow = ControlFlow::Exit,
23-
_ => *control_flow = ControlFlow::Wait,
23+
Event::MainEventsCleared => {
24+
window.request_redraw();
25+
}
26+
_ => *control_flow = ControlFlow::Poll,
2427
}
2528
});
2629
}

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
//! match event {
5151
//! Event::MainEventsCleared => {
5252
//! // Application update code.
53-
//!
53+
//!
5454
//! // Queue a RedrawRequested event.
5555
//! window.request_redraw();
5656
//! },

src/platform_impl/windows/event_loop.rs

+1-49
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use std::{
2121
mem, panic, ptr,
2222
rc::Rc,
2323
sync::{
24-
atomic::{AtomicBool, Ordering},
2524
mpsc::{self, Receiver, Sender},
2625
Arc,
2726
},
@@ -109,7 +108,6 @@ pub struct EventLoop<T: 'static> {
109108

110109
pub struct EventLoopWindowTarget<T> {
111110
thread_id: DWORD,
112-
trigger_newevents_on_redraw: Arc<AtomicBool>,
113111
thread_msg_target: HWND,
114112
pub(crate) runner_shared: EventLoopRunnerShared<T>,
115113
}
@@ -136,7 +134,6 @@ impl<T: 'static> EventLoop<T> {
136134
window_target: RootELW {
137135
p: EventLoopWindowTarget {
138136
thread_id,
139-
trigger_newevents_on_redraw: Arc::new(AtomicBool::new(true)),
140137
thread_msg_target,
141138
runner_shared,
142139
},
@@ -235,7 +232,6 @@ impl<T> EventLoopWindowTarget<T> {
235232
pub(crate) fn create_thread_executor(&self) -> EventLoopThreadExecutor {
236233
EventLoopThreadExecutor {
237234
thread_id: self.thread_id,
238-
trigger_newevents_on_redraw: self.trigger_newevents_on_redraw.clone(),
239235
target_window: self.thread_msg_target,
240236
}
241237
}
@@ -306,7 +302,6 @@ impl<T> Drop for EventLoop<T> {
306302

307303
pub(crate) struct EventLoopThreadExecutor {
308304
thread_id: DWORD,
309-
trigger_newevents_on_redraw: Arc<AtomicBool>,
310305
target_window: HWND,
311306
}
312307

@@ -320,10 +315,6 @@ impl EventLoopThreadExecutor {
320315
self.thread_id == cur_thread_id
321316
}
322317

323-
pub(super) fn trigger_newevents_on_redraw(&self) -> bool {
324-
!self.in_event_loop_thread() || self.trigger_newevents_on_redraw.load(Ordering::Relaxed)
325-
}
326-
327318
/// Executes a function in the event loop thread. If we're already in the event loop thread,
328319
/// we just call the function directly.
329320
///
@@ -415,12 +406,6 @@ lazy_static! {
415406
winuser::RegisterWindowMessageA("Winit::InitialDpiMsg\0".as_ptr() as LPCSTR)
416407
}
417408
};
418-
// Message sent by a `Window` if it's requesting a redraw without sending a NewEvents.
419-
pub static ref REQUEST_REDRAW_NO_NEWEVENTS_MSG_ID: u32 = {
420-
unsafe {
421-
winuser::RegisterWindowMessageA("Winit::RequestRedrawNoNewevents\0".as_ptr() as LPCSTR)
422-
}
423-
};
424409
// WPARAM is a bool specifying the `WindowFlags::MARKER_RETAIN_STATE_ON_SIZE` flag. See the
425410
// documentation in the `window_state` module for more information.
426411
pub static ref SET_RETAIN_STATE_ON_SIZE_MSG_ID: u32 = unsafe {
@@ -598,41 +583,8 @@ unsafe extern "system" fn public_window_callback<T>(
598583
0
599584
}
600585

601-
_ if msg == *REQUEST_REDRAW_NO_NEWEVENTS_MSG_ID => {
602-
use crate::event::WindowEvent::RedrawRequested;
603-
subclass_input.window_state.lock().queued_out_of_band_redraw = false;
604-
605-
// This check makes sure that requesting a redraw during `EventsCleared`
606-
// handling dispatch `RedrawRequested` immediately after `EventsCleared`, without
607-
// spinning up a new event loop iteration. We do this because that's what the API
608-
// says to do.
609-
match subclass_input.event_loop_runner.handling_events() {
610-
true => {
611-
winuser::RedrawWindow(
612-
window,
613-
ptr::null(),
614-
ptr::null_mut(),
615-
winuser::RDW_INTERNALPAINT,
616-
);
617-
}
618-
false => {
619-
subclass_input
620-
.event_loop_runner
621-
.call_event_handler(Event::WindowEvent {
622-
window_id: RootWindowId(WindowId(window)),
623-
event: RedrawRequested,
624-
});
625-
}
626-
}
627-
628-
0
629-
}
630586
winuser::WM_PAINT => {
631-
use crate::event::WindowEvent::RedrawRequested;
632-
subclass_input.send_event(Event::WindowEvent {
633-
window_id: RootWindowId(WindowId(window)),
634-
event: RedrawRequested,
635-
});
587+
subclass_input.send_event(Event::RedrawRequested(RootWindowId(WindowId(window))));
636588
commctrl::DefSubclassProc(window, msg, wparam, lparam)
637589
}
638590

0 commit comments

Comments
 (0)