use super::*;
fn invoke_system_callbacks(callbacks: Vec<SystemEventCallback>, event: &SystemEvent) {
for callback in callbacks.into_iter() {
if let Ok(mut func) = callback.try_borrow_mut() {
func(event);
}
}
}
pub(crate) fn dispatch_system_event_listeners(event: &SystemEvent) {
let callbacks: Vec<SystemEventCallback> = SYSTEM_EVENT_HUB
.try_with(|cell: &RefCell<SystemEventHub>| {
cell.try_borrow()
.map(|hub: Ref<'_, SystemEventHub>| hub.snapshot_active())
.unwrap_or_default()
})
.unwrap_or_default();
invoke_system_callbacks(callbacks, event);
}
pub(crate) fn dispatch_system_event_to_scopes(scopes: &[ScopeId], event: &SystemEvent) {
let callbacks: Vec<SystemEventCallback> = SYSTEM_EVENT_HUB
.try_with(|cell: &RefCell<SystemEventHub>| {
cell.try_borrow()
.map(|hub: Ref<'_, SystemEventHub>| hub.snapshot_for_scopes(scopes))
.unwrap_or_default()
})
.unwrap_or_default();
invoke_system_callbacks(callbacks, event);
}
pub(crate) fn reset_system_event_hub() {
let _: Result<(), _> = SYSTEM_EVENT_HUB.try_with(|cell: &RefCell<SystemEventHub>| {
if let Ok(mut hub) = cell.try_borrow_mut() {
hub.clear();
}
});
}
pub fn on_system_event<F>(handler: F) -> SystemEventSubscription
where
F: FnMut(&SystemEvent) + 'static,
{
let owner_scope: Option<ScopeId> =
with_runtime(|rt: &mut ReactiveRuntime| rt.current_scope_id(), None);
let callback: SystemEventCallback = Rc::new(RefCell::new(handler));
let id: u64 = SYSTEM_EVENT_HUB
.try_with(|cell: &RefCell<SystemEventHub>| {
cell.try_borrow_mut()
.map(|mut hub: RefMut<'_, SystemEventHub>| hub.register(owner_scope, callback))
.unwrap_or(0)
})
.unwrap_or(0);
let sub: SystemEventSubscription = SystemEventSubscription::new(id);
if owner_scope.is_some() && id != 0 {
on_cleanup(move || {
sub.unsubscribe();
});
}
sub
}
pub fn on_appear<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut() + 'static,
{
on_system_event(move |event: &SystemEvent| {
if matches!(event, SystemEvent::Appear) {
handler();
}
})
}
pub fn on_disappear<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut() + 'static,
{
on_system_event(move |event: &SystemEvent| {
if matches!(event, SystemEvent::Disappear) {
handler();
}
})
}
pub fn on_foreground<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut() + 'static,
{
on_system_event(move |event: &SystemEvent| {
if matches!(event, SystemEvent::Foreground) {
handler();
}
})
}
pub fn on_background<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut() + 'static,
{
on_system_event(move |event: &SystemEvent| {
if matches!(event, SystemEvent::Background) {
handler();
}
})
}
pub fn on_inactive<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut() + 'static,
{
on_system_event(move |event: &SystemEvent| {
if matches!(event, SystemEvent::Inactive) {
handler();
}
})
}
pub fn on_terminate<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut() + 'static,
{
on_system_event(move |event: &SystemEvent| {
if matches!(event, SystemEvent::Terminate) {
handler();
}
})
}
pub fn on_focus_change<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut(bool) + 'static,
{
on_system_event(move |event: &SystemEvent| {
if let SystemEvent::FocusChanged(focused) = event {
handler(*focused);
}
})
}
pub fn on_resize<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut(f32, f32, f32) + 'static,
{
on_system_event(move |event: &SystemEvent| {
if let SystemEvent::Resize {
width,
height,
scale_factor,
} = event
{
handler(*width, *height, *scale_factor);
}
})
}
pub fn on_safe_area_change<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut(SafeAreaInsets) + 'static,
{
on_system_event(move |event: &SystemEvent| {
if let SystemEvent::SafeAreaChanged(insets) = event {
handler(*insets);
}
})
}
pub fn on_orientation_change<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut(ScreenOrientation) + 'static,
{
on_system_event(move |event: &SystemEvent| {
if let SystemEvent::OrientationChanged(orient) = event {
handler(*orient);
}
})
}
pub fn on_theme_change<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut(SystemTheme) + 'static,
{
on_system_event(move |event: &SystemEvent| {
if let SystemEvent::ThemeChanged(theme) = event {
handler(*theme);
}
})
}
pub fn on_locale_change<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut(&str) + 'static,
{
on_system_event(move |event: &SystemEvent| {
if let SystemEvent::LocaleChanged(locale) = event {
handler(locale.as_str());
}
})
}
pub fn on_keyboard_change<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut(bool, f32) + 'static,
{
on_system_event(move |event: &SystemEvent| {
if let SystemEvent::KeyboardVisibilityChanged { visible, height } = event {
handler(*visible, *height);
}
})
}
pub fn on_memory_warning<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut(MemoryPressure) + 'static,
{
on_system_event(move |event: &SystemEvent| {
if let SystemEvent::MemoryWarning(pressure) = event {
handler(*pressure);
}
})
}
pub fn on_low_power_change<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut(bool) + 'static,
{
on_system_event(move |event: &SystemEvent| {
if let SystemEvent::LowPowerModeChanged(enabled) = event {
handler(*enabled);
}
})
}
pub fn on_network_change<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut(NetworkStatus) + 'static,
{
on_system_event(move |event: &SystemEvent| {
if let SystemEvent::NetworkStatusChanged(status) = event {
handler(*status);
}
})
}
pub fn on_system_back<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut() + 'static,
{
on_system_event(move |event: &SystemEvent| {
if matches!(event, SystemEvent::BackPressed) {
handler();
}
})
}
pub fn on_deep_link<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut(&str) + 'static,
{
on_system_event(move |event: &SystemEvent| {
if let SystemEvent::DeepLink(uri) = event {
handler(uri.as_str());
}
})
}
pub fn on_clipboard_change<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut() + 'static,
{
on_system_event(move |event: &SystemEvent| {
if matches!(event, SystemEvent::ClipboardChanged) {
handler();
}
})
}
#[must_use]
pub fn use_app_state() -> Signal<AppState> {
current_context().app_state_signal()
}
#[must_use]
pub fn use_system_theme() -> Signal<SystemTheme> {
current_context().get_theme_mode()
}
#[must_use]
pub fn use_network_status() -> Signal<NetworkStatus> {
current_context().get_network_status()
}
#[must_use]
pub fn use_keyboard_height() -> Signal<f32> {
current_context().get_keyboard_height()
}
#[must_use]
pub fn use_safe_area() -> Signal<SafeAreaInsets> {
current_context().get_safe_area()
}