Skip to main content

rosace_core/
safe_area.rs

1//! Global safe-area inset provider: `use_safe_area()` and `set_safe_area()`.
2//!
3//! Some platforms reserve screen regions the app shouldn't draw into (iOS
4//! status bar / Dynamic Island / home indicator; Android status/nav bars).
5//! Rather than have every widget branch on platform, the platform layer
6//! measures the inset once and publishes it here; widgets (starting with
7//! `Scaffold`) read it as ordinary padding. Desktop/web never set it, so it
8//! defaults to zero and nothing changes for them.
9
10use rosace_state::GlobalAtom;
11use rosace_trace::event::AtomId;
12
13/// Reserved atom ID for the safe-area atom (must not collide with other
14/// reserved IDs — see `rosace_theme::provider::THEME_ATOM_ID` at 0xFFFF).
15const SAFE_AREA_ATOM_ID: AtomId = AtomId(0xFFFE);
16
17/// Inset amounts on each edge, in logical pixels.
18#[derive(Debug, Clone, Copy, Default, PartialEq)]
19pub struct SafeArea {
20    pub top: f32,
21    pub right: f32,
22    pub bottom: f32,
23    pub left: f32,
24}
25
26static CURRENT_SAFE_AREA: GlobalAtom<SafeArea> = GlobalAtom::new(SAFE_AREA_ATOM_ID, SafeArea::default);
27
28/// Returns the currently active safe-area insets (zero on platforms that
29/// don't have any — desktop, web).
30pub fn use_safe_area() -> SafeArea {
31    CURRENT_SAFE_AREA.get()
32}
33
34/// Replaces the active safe-area insets. Called by the platform layer on
35/// startup and on resize/rotation; app code should not normally call this.
36pub fn set_safe_area(insets: SafeArea) {
37    CURRENT_SAFE_AREA.set(insets);
38}