mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2024-12-25 06:41:31 +11:00
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:
parent
7daf27f389
commit
c62296dc2b
|
@ -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;
|
||||||
|
|
|
@ -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,
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue