Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
Osspial committed May 15, 2020
1 parent c04ace5 commit 52157c2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
29 changes: 22 additions & 7 deletions examples/win32_modal_dialog.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use winapi::um::winuser;
use winit::{
event::{Event, KeyboardInput, WindowEvent, VirtualKeyCode, ElementState},
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop},
platform::windows::{EventLoopWindowTargetExtWindows, WindowExtWindows},
window::WindowBuilder,
platform::windows::{WindowExtWindows, EventLoopWindowTargetExtWindows},
};
use winapi::um::winuser;

#[derive(Debug, Clone, Copy)]
enum ModalDialogEvent {
Expand All @@ -27,8 +27,23 @@ fn main() {
println!("{:?}", event);

match event {
Event::WindowEvent{ event: WindowEvent::CloseRequested, .. } |
Event::WindowEvent{ event: WindowEvent::KeyboardInput{ input: KeyboardInput{ virtual_keycode: Some(VirtualKeyCode::Escape), state: ElementState::Pressed, ..}, ..}, ..} => {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
}
| Event::WindowEvent {
event:
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(VirtualKeyCode::Escape),
state: ElementState::Pressed,
..
},
..
},
..
} => {
let hwnd = window.hwnd();
let proxy = proxy.clone();
window_target.schedule_modal_fn(move || unsafe {
Expand All @@ -38,7 +53,7 @@ fn main() {
hwnd as _,
"Are you sure you want close the window?\0".as_ptr() as *const _,
"Confirm Close\0".as_ptr() as *const _,
winuser::MB_ICONEXCLAMATION | winuser::MB_YESNO
winuser::MB_ICONEXCLAMATION | winuser::MB_YESNO,
);

println!("\n\t\tend modal loop\n");
Expand All @@ -47,7 +62,7 @@ fn main() {
proxy.send_event(ModalDialogEvent::CloseWindow).unwrap();
}
});
},
}
Event::UserEvent(ModalDialogEvent::CloseWindow) => {
*control_flow = ControlFlow::Exit;
}
Expand Down
20 changes: 16 additions & 4 deletions src/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use crate::{
event::{DeviceId, Event},
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
monitor::MonitorHandle,
platform_impl::{EventLoop as WindowsEventLoop, WinIcon, EventLoopEmbedded as WindowsEventLoopEmbedded},
platform_impl::{
EventLoop as WindowsEventLoop, EventLoopEmbedded as WindowsEventLoopEmbedded, WinIcon,
},
window::{BadIcon, Icon, Window, WindowBuilder},
};

Expand Down Expand Up @@ -59,7 +61,12 @@ pub trait EventLoopExtWindows {
/// TODO: REWRITE `exit_requested` and `resume_panic_if_necessary` as trait functions.
fn run_embedded<'a, F>(self, event_handler: F) -> EventLoopEmbedded<'a, Self::UserEvent>
where
F: 'a + FnMut(Event<'_, Self::UserEvent>, &EventLoopWindowTarget<Self::UserEvent>, &mut ControlFlow);
F: 'a
+ FnMut(
Event<'_, Self::UserEvent>,
&EventLoopWindowTarget<Self::UserEvent>,
&mut ControlFlow,
);
}

impl<T> EventLoopExtWindows for EventLoop<T> {
Expand Down Expand Up @@ -90,10 +97,15 @@ impl<T> EventLoopExtWindows for EventLoop<T> {

fn run_embedded<'a, F>(self, event_handler: F) -> EventLoopEmbedded<'a, Self::UserEvent>
where
F: 'a + FnMut(Event<'_, Self::UserEvent>, &EventLoopWindowTarget<Self::UserEvent>, &mut ControlFlow)
F: 'a
+ FnMut(
Event<'_, Self::UserEvent>,
&EventLoopWindowTarget<Self::UserEvent>,
&mut ControlFlow,
),
{
EventLoopEmbedded {
p: self.event_loop.run_embedded(event_handler)
p: self.event_loop.run_embedded(event_handler),
}
}
}
Expand Down
18 changes: 8 additions & 10 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,19 +217,14 @@ impl<T: 'static> EventLoop<T> {

pub fn run_embedded<'a, F>(mut self, event_handler: F) -> EventLoopEmbedded<'a, T>
where
F: 'a + FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow)
F: 'a + FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
{
unsafe {
self.embedded_runner(event_handler)
}
unsafe { self.embedded_runner(event_handler) }
}

unsafe fn embedded_runner<'a, F>(
&mut self,
mut event_handler: F
) -> EventLoopEmbedded<'a, T>
unsafe fn embedded_runner<'a, F>(&mut self, mut event_handler: F) -> EventLoopEmbedded<'a, T>
where
F: 'a + FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow)
F: 'a + FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
{
let window_target = self.window_target.clone();
let event_loop_windows_ptr = &*window_target as *const RootELW<T>;
Expand Down Expand Up @@ -276,7 +271,10 @@ impl<T> EventLoopEmbedded<'_, T> {
impl<T> Drop for EventLoopEmbedded<'_, T> {
fn drop(&mut self) {
unsafe {
self.window_target.p.runner_shared.call_event_handler(Event::LoopDestroyed);
self.window_target
.p
.runner_shared
.call_event_handler(Event::LoopDestroyed);
self.window_target.p.runner_shared.reset_runner();
}
}
Expand Down

0 comments on commit 52157c2

Please sign in to comment.