From 4aef63dfb78dfaf38c83cb0e88d4ea9d8d0578a6 Mon Sep 17 00:00:00 2001 From: Francesca Plebani Date: Mon, 17 Dec 2018 13:54:59 -0500 Subject: [PATCH] Wait works --- examples/multiwindow.rs | 18 +++++++++++------- src/platform_impl/macos/app_state.rs | 18 ++++++++++++++---- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/examples/multiwindow.rs b/examples/multiwindow.rs index cf2703c2dd..4ff889f471 100644 --- a/examples/multiwindow.rs +++ b/examples/multiwindow.rs @@ -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() { @@ -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); + } }, - _ => () + _ => (), } } _ => (), diff --git a/src/platform_impl/macos/app_state.rs b/src/platform_impl/macos/app_state.rs index 357f8d7e23..3834a0cca3 100644 --- a/src/platform_impl/macos/app_state.rs +++ b/src/platform_impl/macos/app_state.rs @@ -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}; @@ -70,6 +70,7 @@ where struct Handler { control_flow: Mutex, control_flow_prev: Mutex, + start_time: Mutex>, callback: Mutex>>, pending_events: Mutex>>, waker: Mutex, @@ -99,6 +100,14 @@ impl Handler { (old, new) } + fn get_start_time(&self) -> Option { + *self.start_time.lock().unwrap() + } + + fn update_start_time(&self) { + *self.start_time.lock().unwrap() = Some(Instant::now()); + } + fn take_events(&self) -> VecDeque> { mem::replace(&mut *self.events(), Default::default()) } @@ -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, }, @@ -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)); } @@ -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) => (),