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);
}
#[must_use]
pub(crate) fn sanitize_motion_scalar(value: f32, limit: f32, fallback: f32) -> f32 {
if value.is_finite() {
value.clamp(-limit, limit)
} else {
fallback
}
}
#[must_use]
pub(crate) fn rotate_device_vec_to_screen_up(vec: [f32; 3], screen_angle_deg: i32) -> [f32; 3] {
let norm_deg: i32 = screen_angle_deg.rem_euclid(360);
let snapped: i32 = ((norm_deg + 45) / 90) % 4;
let dx: f32 = vec[0];
let dy: f32 = vec[1];
let dz: f32 = vec[2];
match snapped {
1 => [-dy, dx, dz],
2 => [-dx, -dy, dz],
3 => [dy, -dx, dz],
_ => [dx, dy, dz],
}
}
pub(crate) fn record_latest_device_motion(sample: DeviceMotionSample) {
let _: Result<(), _> = LATEST_DEVICE_MOTION.try_with(|cell: &Cell<DeviceMotionSample>| {
cell.set(sample);
});
}
#[must_use]
pub fn latest_device_motion() -> DeviceMotionSample {
LATEST_DEVICE_MOTION
.try_with(|cell: &Cell<DeviceMotionSample>| cell.get())
.unwrap_or_default()
}
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();
}
});
let _: Result<(), _> = LATEST_DEVICE_MOTION.try_with(|cell: &Cell<DeviceMotionSample>| {
cell.set(DeviceMotionSample::upright());
});
}
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(ThemeMode) + '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<ThemeMode> {
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()
}
pub fn on_device_motion<F>(mut handler: F) -> SystemEventSubscription
where
F: FnMut(DeviceMotionSample) + 'static,
{
on_system_event(move |event: &SystemEvent| {
if let SystemEvent::DeviceMotion(sample) = event {
handler(*sample);
}
})
}
#[must_use]
pub fn use_safe_area() -> Signal<SafeAreaInsets> {
current_context().get_safe_area()
}
#[must_use]
pub fn use_device_motion() -> Signal<DeviceMotionSample> {
current_context().get_device_motion()
}
#[must_use]
pub fn request_web_motion_permission() -> bool {
#[cfg(target_arch = "wasm32")]
{
web_request_motion_permission_impl()
}
#[cfg(not(target_arch = "wasm32"))]
{
true
}
}