56 lines
3.5 KiB
Rust
56 lines
3.5 KiB
Rust
//! OS lifecycle notifications contain captured identities, never an implicit current tab.
|
|
use crate::app::connections::SessionKey;
|
|
use std::sync::{Arc,Mutex,OnceLock,Weak};
|
|
|
|
#[derive(Clone,Copy,Debug,PartialEq,Eq)]
|
|
pub enum Permission {Microphone,Camera,Notifications}
|
|
#[derive(Clone,Copy,Debug,Default)]
|
|
pub struct Insets {pub top:f32,pub right:f32,pub bottom:f32,pub left:f32,pub keyboard:f32}
|
|
#[derive(Clone,Debug)]
|
|
pub enum NotificationAction {Show,Mute,Deafen,Disconnect,Conversation{client_id:Option<u16>,uid:Option<String>,channel_id:Option<u64>}}
|
|
#[derive(Clone,Debug)]
|
|
pub enum PlatformEvent {
|
|
Visibility{ui_generation:u64,visible:bool},
|
|
UiDestroyed{ui_generation:u64},
|
|
Insets{ui_generation:u64,value:Insets},
|
|
Accessibility{ui_generation:u64,text_scale:f32,reduce_motion:bool,high_contrast:bool,screen_reader:bool},
|
|
PictureInPicture{key:SessionKey,stream_id:String,active:bool,playing:bool},
|
|
DesktopTheme{ui_generation:u64,dark:bool},
|
|
/// Geometry is expressed in logical pixels, not raw monitor pixels.
|
|
DesktopWindow{ui_generation:u64,x:i32,y:i32,width:u32,height:u32,maximized:bool},
|
|
PhysicalKey{ui_generation:u64,code:String,mask:u8,down:bool,repeat:bool,text_input:bool},
|
|
Focus{ui_generation:u64,focused:bool},
|
|
AudioInterruption{began:bool,should_resume:bool},
|
|
AudioRouteChanged,
|
|
NetworkChanged,
|
|
ScreenLocked(bool),
|
|
PermissionChanged{permission:Permission,granted:bool},
|
|
CallLeaseEnded{key:SessionKey},
|
|
/// OS callbacks are tied to the exact Android lease and authorization revision.
|
|
#[cfg(target_os="android")]
|
|
AndroidCallAudio{key:SessionKey,token:u64,revision:u64,restart_output:bool},
|
|
Notification{key:SessionKey,action:NotificationAction},
|
|
MemoryPressure,
|
|
Quit,
|
|
}
|
|
pub trait PlatformEventSink:Send+Sync+'static {fn on_event(&self,event:PlatformEvent);}
|
|
impl<F:Fn(PlatformEvent)+Send+Sync+'static> PlatformEventSink for F {fn on_event(&self,event:PlatformEvent){self(event)}}
|
|
struct Registered {generation:u64,sink:Weak<dyn PlatformEventSink>}
|
|
#[derive(Default)]
|
|
struct Registry {next:u64,current:Option<Registered>}
|
|
fn registry()->&'static Mutex<Registry>{static REGISTRY:OnceLock<Mutex<Registry>>=OnceLock::new();REGISTRY.get_or_init(||Mutex::new(Registry::default()))}
|
|
/// Retained by the process runtime. OS callbacks do not keep a terminated runtime alive.
|
|
pub struct Subscription {generation:u64,_sink:Arc<dyn PlatformEventSink>}
|
|
pub fn subscribe(sink:Arc<dyn PlatformEventSink>)->crate::services::Result<Subscription>{
|
|
let mut registry=registry().lock().expect("platform callback registry");
|
|
if registry.current.as_ref().and_then(|entry|entry.sink.upgrade()).is_some(){return Err(crate::services::ServiceError::new("platform_owner_exists","The process already has an OS lifecycle owner"));}
|
|
registry.next=registry.next.checked_add(1).ok_or_else(||crate::services::ServiceError::new("platform_generation_exhausted","OS lifecycle generation exhausted"))?;
|
|
let generation=registry.next;registry.current=Some(Registered{generation,sink:Arc::downgrade(&sink)});
|
|
Ok(Subscription{generation,_sink:sink})
|
|
}
|
|
pub fn emit(event:PlatformEvent)->bool{
|
|
let sink=registry().lock().expect("platform callback registry").current.as_ref().and_then(|entry|entry.sink.upgrade());
|
|
if let Some(sink)=sink{sink.on_event(event);true}else{false}
|
|
}
|
|
impl Drop for Subscription{fn drop(&mut self){let mut registry=registry().lock().expect("platform callback registry");if registry.current.as_ref().is_some_and(|entry|entry.generation==self.generation){registry.current=None;}}}
|