Add set_suspend_callback method for Android (#406)

Makes it possible for glutin to register a callback when a suspend event happens on Android
This commit is contained in:
Imanol Fernandez 2018-02-15 14:09:14 +01:00 committed by Pierre Krieger
parent 7daf27f389
commit c62296dc2b
2 changed files with 26 additions and 0 deletions

View file

@ -1,9 +1,22 @@
#![cfg(any(target_os = "android"))] #![cfg(any(target_os = "android"))]
use std::os::raw::c_void; use std::os::raw::c_void;
use EventsLoop;
use Window; use Window;
use WindowBuilder; use WindowBuilder;
/// Additional methods on `EventsLoop` that are specific to Android.
pub trait EventsLoopExt {
/// Makes it possible for glutin to register a callback when a suspend event happens on Android
fn set_suspend_callback(&self, cb: Option<Box<Fn(bool) -> ()>>);
}
impl EventsLoopExt for EventsLoop {
fn set_suspend_callback(&self, cb: Option<Box<Fn(bool) -> ()>>) {
self.events_loop.set_suspend_callback(cb);
}
}
/// Additional methods on `Window` that are specific to Android. /// Additional methods on `Window` that are specific to Android.
pub trait WindowExt { pub trait WindowExt {
fn get_native_window(&self) -> *const c_void; fn get_native_window(&self) -> *const c_void;

View file

@ -12,12 +12,14 @@ use events::{Touch, TouchPhase};
use window::MonitorId as RootMonitorId; use window::MonitorId as RootMonitorId;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::cell::RefCell;
use CursorState; use CursorState;
use WindowAttributes; use WindowAttributes;
pub struct EventsLoop { pub struct EventsLoop {
event_rx: Receiver<android_glue::Event>, event_rx: Receiver<android_glue::Event>,
suspend_callback: RefCell<Option<Box<Fn(bool) -> ()>>>
} }
#[derive(Clone)] #[derive(Clone)]
@ -29,6 +31,7 @@ impl EventsLoop {
android_glue::add_sender(tx); android_glue::add_sender(tx);
EventsLoop { EventsLoop {
event_rx: rx, event_rx: rx,
suspend_callback: RefCell::new(None),
} }
} }
@ -69,10 +72,16 @@ impl EventsLoop {
}, },
android_glue::Event::InitWindow => { android_glue::Event::InitWindow => {
// The activity went to foreground. // The activity went to foreground.
if let Some(cb) = self.suspend_callback.borrow().as_ref() {
(*cb)(false);
}
Some(Event::Suspended(false)) Some(Event::Suspended(false))
}, },
android_glue::Event::TermWindow => { android_glue::Event::TermWindow => {
// The activity went to background. // The activity went to background.
if let Some(cb) = self.suspend_callback.borrow().as_ref() {
(*cb)(true);
}
Some(Event::Suspended(true)) Some(Event::Suspended(true))
}, },
android_glue::Event::WindowResized | android_glue::Event::WindowResized |
@ -108,6 +117,10 @@ impl EventsLoop {
}; };
} }
pub fn set_suspend_callback(&self, cb: Option<Box<Fn(bool) -> ()>>) {
*self.suspend_callback.borrow_mut() = cb;
}
pub fn run_forever<F>(&mut self, mut callback: F) pub fn run_forever<F>(&mut self, mut callback: F)
where F: FnMut(::Event) -> ::ControlFlow, where F: FnMut(::Event) -> ::ControlFlow,
{ {