Skip to content

Commit

Permalink
Wait works
Browse files Browse the repository at this point in the history
  • Loading branch information
francesca64 committed Dec 17, 2018
1 parent 72ed426 commit 4aef63d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
18 changes: 11 additions & 7 deletions examples/multiwindow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate winit;

use std::collections::HashMap;
use winit::window::Window;
use winit::event::{Event, WindowEvent, ElementState, KeyboardInput};
use winit::event::{Event, WindowEvent, ElementState, KeyboardInput, VirtualKeyCode};
use winit::event_loop::{EventLoop, ControlFlow};

fn main() {
Expand All @@ -21,19 +21,23 @@ fn main() {
match event {
WindowEvent::CloseRequested => {
println!("Window {:?} has received the signal to close", window_id);

// This drops the window, causing it to close.
windows.remove(&window_id);

},
WindowEvent::Destroyed => {
if windows.is_empty() {
*control_flow = ControlFlow::Exit;
}
},
WindowEvent::KeyboardInput { input: KeyboardInput { state: ElementState::Pressed, .. }, .. } => {
let window = Window::new(&event_loop).unwrap();
windows.insert(window.id(), window);
WindowEvent::KeyboardInput { input: KeyboardInput { state: ElementState::Pressed, virtual_keycode, .. }, .. } => {
if Some(VirtualKeyCode::Escape) == virtual_keycode {
windows.remove(&window_id);
} else {
let window = Window::new(&event_loop).unwrap();
windows.insert(window.id(), window);
}
},
_ => ()
_ => (),
}
}
_ => (),
Expand Down
18 changes: 14 additions & 4 deletions src/platform_impl/macos/app_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
self, collections::VecDeque, fmt::{self, Debug, Formatter},
hint::unreachable_unchecked, mem, sync::{Mutex, MutexGuard},
hint::unreachable_unchecked, mem, sync::{Mutex, MutexGuard}, time::Instant,
};

use cocoa::{appkit::NSApp, base::nil};
Expand Down Expand Up @@ -70,6 +70,7 @@ where
struct Handler {
control_flow: Mutex<ControlFlow>,
control_flow_prev: Mutex<ControlFlow>,
start_time: Mutex<Option<Instant>>,
callback: Mutex<Option<Box<dyn EventHandler>>>,
pending_events: Mutex<VecDeque<Event<Never>>>,
waker: Mutex<EventLoopWaker>,
Expand Down Expand Up @@ -99,6 +100,14 @@ impl Handler {
(old, new)
}

fn get_start_time(&self) -> Option<Instant> {
*self.start_time.lock().unwrap()
}

fn update_start_time(&self) {
*self.start_time.lock().unwrap() = Some(Instant::now());
}

fn take_events(&self) -> VecDeque<Event<Never>> {
mem::replace(&mut *self.events(), Default::default())
}
Expand Down Expand Up @@ -138,9 +147,10 @@ impl AppState {
}

pub fn wakeup() {
let start = HANDLER.get_start_time().unwrap();
let cause = match HANDLER.get_control_flow_and_update_prev() {
ControlFlow::Poll => StartCause::Poll,
/*ControlFlow::Wait => StartCause::WaitCancelled {
ControlFlow::Wait => StartCause::WaitCancelled {
start,
requested_resume: None,
},
Expand All @@ -156,9 +166,8 @@ impl AppState {
requested_resume: Some(requested_resume),
}
}
},*/
},
ControlFlow::Exit => StartCause::Poll,//panic!("unexpected `ControlFlow::Exit`"),
_ => unimplemented!(),
};
HANDLER.handle_nonuser_event(Event::NewEvents(cause));
}
Expand All @@ -176,6 +185,7 @@ impl AppState {
for event in HANDLER.take_events() {
HANDLER.handle_nonuser_event(event);
}
HANDLER.update_start_time();
match HANDLER.get_old_and_new_control_flow() {
(ControlFlow::Poll, ControlFlow::Poll) => (),
(ControlFlow::Wait, ControlFlow::Wait) => (),
Expand Down

0 comments on commit 4aef63d

Please sign in to comment.