Move the Suspended event outside of WindowEvent (#284)

* Move Suspended event outside of WindowEvent

* Adjust the iOS code
This commit is contained in:
tomaka 2017-09-15 16:24:09 +02:00 committed by GitHub
parent 1d0b5bcfbd
commit 52a7b07c79
3 changed files with 65 additions and 56 deletions

View file

@ -12,6 +12,11 @@ pub enum Event {
event: DeviceEvent, event: DeviceEvent,
}, },
Awakened, Awakened,
/// The application has been suspended or resumed.
///
/// The parameter is true if app was suspended, and false if it has been resumed.
Suspended(bool),
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -78,13 +83,8 @@ pub enum WindowEvent {
/// The window needs to be redrawn. /// The window needs to be redrawn.
Refresh, Refresh,
/// App has been suspended or resumed.
///
/// The parameter is true if app was suspended, and false if it has been resumed.
Suspended(bool),
/// Touch event has been received /// Touch event has been received
Touch(Touch) Touch(Touch),
} }
/// Represents raw hardware events that are not associated with any particular window. /// Represents raw hardware events that are not associated with any particular window.

View file

@ -48,7 +48,9 @@ impl EventsLoop {
{ {
let event = match self.event_rx.try_recv() { let event = match self.event_rx.try_recv() {
Ok(android_glue::Event::EventMotion(motion)) => { Ok(android_glue::Event::EventMotion(motion)) => {
Some(WindowEvent::Touch(Touch { Some(Event::WindowEvent {
window_id: RootWindowId(WindowId),
event: WindowEvent::Touch(Touch {
phase: match motion.action { phase: match motion.action {
android_glue::MotionAction::Down => TouchPhase::Started, android_glue::MotionAction::Down => TouchPhase::Started,
android_glue::MotionAction::Move => TouchPhase::Moved, android_glue::MotionAction::Move => TouchPhase::Moved,
@ -58,15 +60,16 @@ impl EventsLoop {
location: (motion.x as f64, motion.y as f64), location: (motion.x as f64, motion.y as f64),
id: motion.pointer_id as u64, id: motion.pointer_id as u64,
device_id: DEVICE_ID, device_id: DEVICE_ID,
})) }),
})
}, },
Ok(android_glue::Event::InitWindow) => { Ok(android_glue::Event::InitWindow) => {
// The activity went to foreground. // The activity went to foreground.
Some(WindowEvent::Suspended(false)) Some(Event::Suspended(false))
}, },
Ok(android_glue::Event::TermWindow) => { Ok(android_glue::Event::TermWindow) => {
// The activity went to background. // The activity went to background.
Some(WindowEvent::Suspended(true)) Some(Event::Suspended(true))
}, },
Ok(android_glue::Event::WindowResized) | Ok(android_glue::Event::WindowResized) |
Ok(android_glue::Event::ConfigChanged) => { Ok(android_glue::Event::ConfigChanged) => {
@ -77,12 +80,18 @@ impl EventsLoop {
} else { } else {
let w = unsafe { ffi::ANativeWindow_getWidth(native_window as *const _) } as u32; let w = unsafe { ffi::ANativeWindow_getWidth(native_window as *const _) } as u32;
let h = unsafe { ffi::ANativeWindow_getHeight(native_window as *const _) } as u32; let h = unsafe { ffi::ANativeWindow_getHeight(native_window as *const _) } as u32;
Some(WindowEvent::Resized(w, h)) Some(Event::WindowEvent {
window_id: RootWindowId(WindowId),
event: WindowEvent::Resized(w, h),
})
} }
}, },
Ok(android_glue::Event::WindowRedrawNeeded) => { Ok(android_glue::Event::WindowRedrawNeeded) => {
// The activity needs to be redrawn. // The activity needs to be redrawn.
Some(WindowEvent::Refresh) Some(Event::WindowEvent {
window_id: RootWindowId(WindowId),
event: WindowEvent::Refresh,
})
} }
_ => { _ => {
None None
@ -90,10 +99,7 @@ impl EventsLoop {
}; };
if let Some(event) = event { if let Some(event) = event {
callback(Event::WindowEvent { callback(event);
window_id: RootWindowId(WindowId),
event: event
});
} }
} }

View file

@ -108,7 +108,7 @@ pub struct WindowProxy;
#[derive(Debug)] #[derive(Debug)]
struct DelegateState { struct DelegateState {
events_queue: VecDeque<WindowEvent>, events_queue: VecDeque<Event>,
window: id, window: id,
controller: id, controller: id,
size: (u32,u32), size: (u32,u32),
@ -196,20 +196,14 @@ impl EventsLoop {
let state = &mut *self.delegate_state; let state = &mut *self.delegate_state;
if let Some(event) = state.events_queue.pop_front() { if let Some(event) = state.events_queue.pop_front() {
callback(Event::WindowEvent { callback(event);
window_id: RootEventId(WindowId),
event: event,
});
return; return;
} }
// jump hack, so we won't quit on willTerminate event before processing it // jump hack, so we won't quit on willTerminate event before processing it
if setjmp(mem::transmute(&mut jmpbuf)) != 0 { if setjmp(mem::transmute(&mut jmpbuf)) != 0 {
if let Some(event) = state.events_queue.pop_front() { if let Some(event) = state.events_queue.pop_front() {
callback(Event::WindowEvent { callback(event);
window_id: RootEventId(WindowId),
event: event,
});
return; return;
} }
} }
@ -219,10 +213,7 @@ impl EventsLoop {
while CFRunLoopRunInMode(kCFRunLoopDefaultMode, seconds, 1) == kCFRunLoopRunHandledSource {} while CFRunLoopRunInMode(kCFRunLoopDefaultMode, seconds, 1) == kCFRunLoopRunHandledSource {}
if let Some(event) = state.events_queue.pop_front() { if let Some(event) = state.events_queue.pop_front() {
callback(Event::WindowEvent { callback(event)
window_id: RootEventId(WindowId),
event: event,
})
} }
} }
} }
@ -404,7 +395,10 @@ fn create_delegate_class() {
unsafe { unsafe {
let state: *mut c_void = *this.get_ivar("glutinState"); let state: *mut c_void = *this.get_ivar("glutinState");
let state = &mut *(state as *mut DelegateState); let state = &mut *(state as *mut DelegateState);
state.events_queue.push_back(WindowEvent::Focused(true)); state.events_queue.push_back(Event::WindowEvent {
window_id: RootEventId(WindowId),
event: WindowEvent::Focused(true),
});
} }
} }
@ -412,7 +406,10 @@ fn create_delegate_class() {
unsafe { unsafe {
let state: *mut c_void = *this.get_ivar("glutinState"); let state: *mut c_void = *this.get_ivar("glutinState");
let state = &mut *(state as *mut DelegateState); let state = &mut *(state as *mut DelegateState);
state.events_queue.push_back(WindowEvent::Focused(false)); state.events_queue.push_back(Event::WindowEvent {
window_id: RootEventId(WindowId),
event: WindowEvent::Focused(false),
});
} }
} }
@ -420,7 +417,7 @@ fn create_delegate_class() {
unsafe { unsafe {
let state: *mut c_void = *this.get_ivar("glutinState"); let state: *mut c_void = *this.get_ivar("glutinState");
let state = &mut *(state as *mut DelegateState); let state = &mut *(state as *mut DelegateState);
state.events_queue.push_back(WindowEvent::Suspended(false)); state.events_queue.push_back(Event::Suspended(false));
} }
} }
@ -428,7 +425,7 @@ fn create_delegate_class() {
unsafe { unsafe {
let state: *mut c_void = *this.get_ivar("glutinState"); let state: *mut c_void = *this.get_ivar("glutinState");
let state = &mut *(state as *mut DelegateState); let state = &mut *(state as *mut DelegateState);
state.events_queue.push_back(WindowEvent::Suspended(true)); state.events_queue.push_back(Event::Suspended(true));
} }
} }
@ -438,7 +435,10 @@ fn create_delegate_class() {
let state = &mut *(state as *mut DelegateState); let state = &mut *(state as *mut DelegateState);
// push event to the front to garantee that we'll process it // push event to the front to garantee that we'll process it
// immidiatly after jump // immidiatly after jump
state.events_queue.push_front(WindowEvent::Closed); state.events_queue.push_front(Event::WindowEvent {
window_id: RootEventId(WindowId),
event: WindowEvent::Closed,
});
longjmp(mem::transmute(&mut jmpbuf),1); longjmp(mem::transmute(&mut jmpbuf),1);
} }
} }
@ -459,7 +459,9 @@ fn create_delegate_class() {
let touch_id = touch as u64; let touch_id = touch as u64;
let phase: i32 = msg_send![touch, phase]; let phase: i32 = msg_send![touch, phase];
state.events_queue.push_back(WindowEvent::Touch(Touch { state.events_queue.push_back(Event::WindowEvent {
window_id: RootEventId(WindowId),
event: WindowEvent::Touch(Touch {
device_id: DEVICE_ID, device_id: DEVICE_ID,
id: touch_id, id: touch_id,
location: (location.x as f64, location.y as f64), location: (location.x as f64, location.y as f64),
@ -471,7 +473,8 @@ fn create_delegate_class() {
4 => TouchPhase::Cancelled, 4 => TouchPhase::Cancelled,
_ => panic!("unexpected touch phase: {:?}", phase) _ => panic!("unexpected touch phase: {:?}", phase)
} }
})); }),
});
} }
} }
} }