From c62296dc2b286c61ee67ea6e9f6d0d87000f2dd5 Mon Sep 17 00:00:00 2001 From: Imanol Fernandez Date: Thu, 15 Feb 2018 14:09:14 +0100 Subject: [PATCH] Add set_suspend_callback method for Android (#406) Makes it possible for glutin to register a callback when a suspend event happens on Android --- src/os/android.rs | 13 +++++++++++++ src/platform/android/mod.rs | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/os/android.rs b/src/os/android.rs index acff4cb6..06661a32 100644 --- a/src/os/android.rs +++ b/src/os/android.rs @@ -1,9 +1,22 @@ #![cfg(any(target_os = "android"))] use std::os::raw::c_void; +use EventsLoop; use Window; 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 ()>>); +} + +impl EventsLoopExt for EventsLoop { + fn set_suspend_callback(&self, cb: Option ()>>) { + self.events_loop.set_suspend_callback(cb); + } +} + /// Additional methods on `Window` that are specific to Android. pub trait WindowExt { fn get_native_window(&self) -> *const c_void; diff --git a/src/platform/android/mod.rs b/src/platform/android/mod.rs index b559e11a..2735265e 100644 --- a/src/platform/android/mod.rs +++ b/src/platform/android/mod.rs @@ -12,12 +12,14 @@ use events::{Touch, TouchPhase}; use window::MonitorId as RootMonitorId; use std::collections::VecDeque; +use std::cell::RefCell; use CursorState; use WindowAttributes; pub struct EventsLoop { event_rx: Receiver, + suspend_callback: RefCell ()>>> } #[derive(Clone)] @@ -29,6 +31,7 @@ impl EventsLoop { android_glue::add_sender(tx); EventsLoop { event_rx: rx, + suspend_callback: RefCell::new(None), } } @@ -69,10 +72,16 @@ impl EventsLoop { }, android_glue::Event::InitWindow => { // The activity went to foreground. + if let Some(cb) = self.suspend_callback.borrow().as_ref() { + (*cb)(false); + } Some(Event::Suspended(false)) }, android_glue::Event::TermWindow => { // The activity went to background. + if let Some(cb) = self.suspend_callback.borrow().as_ref() { + (*cb)(true); + } Some(Event::Suspended(true)) }, android_glue::Event::WindowResized | @@ -108,6 +117,10 @@ impl EventsLoop { }; } + pub fn set_suspend_callback(&self, cb: Option ()>>) { + *self.suspend_callback.borrow_mut() = cb; + } + pub fn run_forever(&mut self, mut callback: F) where F: FnMut(::Event) -> ::ControlFlow, {