Skip to content

Commit 4aef63d

Browse files
committed
Wait works
1 parent 72ed426 commit 4aef63d

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

examples/multiwindow.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ extern crate winit;
22

33
use std::collections::HashMap;
44
use winit::window::Window;
5-
use winit::event::{Event, WindowEvent, ElementState, KeyboardInput};
5+
use winit::event::{Event, WindowEvent, ElementState, KeyboardInput, VirtualKeyCode};
66
use winit::event_loop::{EventLoop, ControlFlow};
77

88
fn main() {
@@ -21,19 +21,23 @@ fn main() {
2121
match event {
2222
WindowEvent::CloseRequested => {
2323
println!("Window {:?} has received the signal to close", window_id);
24-
2524
// This drops the window, causing it to close.
2625
windows.remove(&window_id);
27-
26+
},
27+
WindowEvent::Destroyed => {
2828
if windows.is_empty() {
2929
*control_flow = ControlFlow::Exit;
3030
}
3131
},
32-
WindowEvent::KeyboardInput { input: KeyboardInput { state: ElementState::Pressed, .. }, .. } => {
33-
let window = Window::new(&event_loop).unwrap();
34-
windows.insert(window.id(), window);
32+
WindowEvent::KeyboardInput { input: KeyboardInput { state: ElementState::Pressed, virtual_keycode, .. }, .. } => {
33+
if Some(VirtualKeyCode::Escape) == virtual_keycode {
34+
windows.remove(&window_id);
35+
} else {
36+
let window = Window::new(&event_loop).unwrap();
37+
windows.insert(window.id(), window);
38+
}
3539
},
36-
_ => ()
40+
_ => (),
3741
}
3842
}
3943
_ => (),

src/platform_impl/macos/app_state.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
22
self, collections::VecDeque, fmt::{self, Debug, Formatter},
3-
hint::unreachable_unchecked, mem, sync::{Mutex, MutexGuard},
3+
hint::unreachable_unchecked, mem, sync::{Mutex, MutexGuard}, time::Instant,
44
};
55

66
use cocoa::{appkit::NSApp, base::nil};
@@ -70,6 +70,7 @@ where
7070
struct Handler {
7171
control_flow: Mutex<ControlFlow>,
7272
control_flow_prev: Mutex<ControlFlow>,
73+
start_time: Mutex<Option<Instant>>,
7374
callback: Mutex<Option<Box<dyn EventHandler>>>,
7475
pending_events: Mutex<VecDeque<Event<Never>>>,
7576
waker: Mutex<EventLoopWaker>,
@@ -99,6 +100,14 @@ impl Handler {
99100
(old, new)
100101
}
101102

103+
fn get_start_time(&self) -> Option<Instant> {
104+
*self.start_time.lock().unwrap()
105+
}
106+
107+
fn update_start_time(&self) {
108+
*self.start_time.lock().unwrap() = Some(Instant::now());
109+
}
110+
102111
fn take_events(&self) -> VecDeque<Event<Never>> {
103112
mem::replace(&mut *self.events(), Default::default())
104113
}
@@ -138,9 +147,10 @@ impl AppState {
138147
}
139148

140149
pub fn wakeup() {
150+
let start = HANDLER.get_start_time().unwrap();
141151
let cause = match HANDLER.get_control_flow_and_update_prev() {
142152
ControlFlow::Poll => StartCause::Poll,
143-
/*ControlFlow::Wait => StartCause::WaitCancelled {
153+
ControlFlow::Wait => StartCause::WaitCancelled {
144154
start,
145155
requested_resume: None,
146156
},
@@ -156,9 +166,8 @@ impl AppState {
156166
requested_resume: Some(requested_resume),
157167
}
158168
}
159-
},*/
169+
},
160170
ControlFlow::Exit => StartCause::Poll,//panic!("unexpected `ControlFlow::Exit`"),
161-
_ => unimplemented!(),
162171
};
163172
HANDLER.handle_nonuser_event(Event::NewEvents(cause));
164173
}
@@ -176,6 +185,7 @@ impl AppState {
176185
for event in HANDLER.take_events() {
177186
HANDLER.handle_nonuser_event(event);
178187
}
188+
HANDLER.update_start_time();
179189
match HANDLER.get_old_and_new_control_flow() {
180190
(ControlFlow::Poll, ControlFlow::Poll) => (),
181191
(ControlFlow::Wait, ControlFlow::Wait) => (),

0 commit comments

Comments
 (0)