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,
},
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)]
@ -78,13 +83,8 @@ pub enum WindowEvent {
/// The window needs to be redrawn.
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(Touch)
Touch(Touch),
}
/// Represents raw hardware events that are not associated with any particular window.

View file

@ -48,25 +48,28 @@ impl EventsLoop {
{
let event = match self.event_rx.try_recv() {
Ok(android_glue::Event::EventMotion(motion)) => {
Some(WindowEvent::Touch(Touch {
phase: match motion.action {
android_glue::MotionAction::Down => TouchPhase::Started,
android_glue::MotionAction::Move => TouchPhase::Moved,
android_glue::MotionAction::Up => TouchPhase::Ended,
android_glue::MotionAction::Cancel => TouchPhase::Cancelled,
},
location: (motion.x as f64, motion.y as f64),
id: motion.pointer_id as u64,
device_id: DEVICE_ID,
}))
Some(Event::WindowEvent {
window_id: RootWindowId(WindowId),
event: WindowEvent::Touch(Touch {
phase: match motion.action {
android_glue::MotionAction::Down => TouchPhase::Started,
android_glue::MotionAction::Move => TouchPhase::Moved,
android_glue::MotionAction::Up => TouchPhase::Ended,
android_glue::MotionAction::Cancel => TouchPhase::Cancelled,
},
location: (motion.x as f64, motion.y as f64),
id: motion.pointer_id as u64,
device_id: DEVICE_ID,
}),
})
},
Ok(android_glue::Event::InitWindow) => {
// The activity went to foreground.
Some(WindowEvent::Suspended(false))
Some(Event::Suspended(false))
},
Ok(android_glue::Event::TermWindow) => {
// The activity went to background.
Some(WindowEvent::Suspended(true))
Some(Event::Suspended(true))
},
Ok(android_glue::Event::WindowResized) |
Ok(android_glue::Event::ConfigChanged) => {
@ -77,12 +80,18 @@ impl EventsLoop {
} else {
let w = unsafe { ffi::ANativeWindow_getWidth(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) => {
// The activity needs to be redrawn.
Some(WindowEvent::Refresh)
Some(Event::WindowEvent {
window_id: RootWindowId(WindowId),
event: WindowEvent::Refresh,
})
}
_ => {
None
@ -90,10 +99,7 @@ impl EventsLoop {
};
if let Some(event) = event {
callback(Event::WindowEvent {
window_id: RootWindowId(WindowId),
event: event
});
callback(event);
}
}

View file

@ -108,7 +108,7 @@ pub struct WindowProxy;
#[derive(Debug)]
struct DelegateState {
events_queue: VecDeque<WindowEvent>,
events_queue: VecDeque<Event>,
window: id,
controller: id,
size: (u32,u32),
@ -196,20 +196,14 @@ impl EventsLoop {
let state = &mut *self.delegate_state;
if let Some(event) = state.events_queue.pop_front() {
callback(Event::WindowEvent {
window_id: RootEventId(WindowId),
event: event,
});
callback(event);
return;
}
// jump hack, so we won't quit on willTerminate event before processing it
if setjmp(mem::transmute(&mut jmpbuf)) != 0 {
if let Some(event) = state.events_queue.pop_front() {
callback(Event::WindowEvent {
window_id: RootEventId(WindowId),
event: event,
});
callback(event);
return;
}
}
@ -219,10 +213,7 @@ impl EventsLoop {
while CFRunLoopRunInMode(kCFRunLoopDefaultMode, seconds, 1) == kCFRunLoopRunHandledSource {}
if let Some(event) = state.events_queue.pop_front() {
callback(Event::WindowEvent {
window_id: RootEventId(WindowId),
event: event,
})
callback(event)
}
}
}
@ -404,7 +395,10 @@ fn create_delegate_class() {
unsafe {
let state: *mut c_void = *this.get_ivar("glutinState");
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 {
let state: *mut c_void = *this.get_ivar("glutinState");
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 {
let state: *mut c_void = *this.get_ivar("glutinState");
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 {
let state: *mut c_void = *this.get_ivar("glutinState");
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);
// push event to the front to garantee that we'll process it
// 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);
}
}
@ -459,19 +459,22 @@ fn create_delegate_class() {
let touch_id = touch as u64;
let phase: i32 = msg_send![touch, phase];
state.events_queue.push_back(WindowEvent::Touch(Touch {
device_id: DEVICE_ID,
id: touch_id,
location: (location.x as f64, location.y as f64),
phase: match phase {
0 => TouchPhase::Started,
1 => TouchPhase::Moved,
// 2 is UITouchPhaseStationary and is not expected here
3 => TouchPhase::Ended,
4 => TouchPhase::Cancelled,
_ => panic!("unexpected touch phase: {:?}", phase)
}
}));
state.events_queue.push_back(Event::WindowEvent {
window_id: RootEventId(WindowId),
event: WindowEvent::Touch(Touch {
device_id: DEVICE_ID,
id: touch_id,
location: (location.x as f64, location.y as f64),
phase: match phase {
0 => TouchPhase::Started,
1 => TouchPhase::Moved,
// 2 is UITouchPhaseStationary and is not expected here
3 => TouchPhase::Ended,
4 => TouchPhase::Cancelled,
_ => panic!("unexpected touch phase: {:?}", phase)
}
}),
});
}
}
}