Skip to main content

euv_core/platform/info/
fn.rs

1use super::*;
2
3/// Sanitizes a single safe-area inset scalar, clamping NaN, infinite, or negative values to `0.0`.
4///
5/// # Arguments
6///
7/// - `f32` - Candidate inset value in logical pixels.
8///
9/// # Returns
10///
11/// - `f32` - Non-negative finite inset value.
12#[must_use]
13fn sanitize_inset_dp(value: f32) -> f32 {
14    if value.is_finite() && value > 0.0 {
15        value
16    } else {
17        0.0
18    }
19}
20
21/// Selects the highest-refresh `DisplayModeInfo` matching `current`'s physical resolution without imposing a 60Hz cap.
22///
23/// # Arguments
24///
25/// - `DisplayModeInfo` - Currently active hardware display mode.
26/// - `&[DisplayModeInfo]` - All display modes reported by the OS (`Display.getSupportedModes()`).
27///
28/// # Returns
29///
30/// - `DisplayModeInfo` - Preferred mode carrying the highest refresh rate at the native resolution.
31#[must_use]
32pub fn select_highest_refresh_mode(
33    current: DisplayModeInfo,
34    supported: &[DisplayModeInfo],
35) -> DisplayModeInfo {
36    let mut best: DisplayModeInfo = current;
37    let target_w: u32 = current.get_physical_width();
38    let target_h: u32 = current.get_physical_height();
39    let mut best_hz: f32 = sanitize_inset_dp(best.get_refresh_rate_hz());
40    for mode in supported.iter().copied() {
41        let same_size: bool =
42            mode.get_physical_width() == target_w && mode.get_physical_height() == target_h;
43        let hz: f32 = sanitize_inset_dp(mode.get_refresh_rate_hz());
44        if same_size && hz > best_hz {
45            best = mode;
46            best_hz = hz;
47        }
48    }
49    if best_hz < MIN_REFRESH_RATE_HZ {
50        for mode in supported.iter().copied() {
51            let hz: f32 = sanitize_inset_dp(mode.get_refresh_rate_hz());
52            if hz > best_hz {
53                best = mode;
54                best_hz = hz;
55            }
56        }
57    }
58    best
59}
60
61/// Computes high-refresh VSync pacing intervals (`60`/`90`/`120`/`144`/`240` Hz+) with zero 60Hz throttling.
62///
63/// # Arguments
64///
65/// - `f32` - Detected hardware display refresh rate in Hertz.
66///
67/// # Returns
68///
69/// - `FramePacingConfig` - Normalized adaptive refresh range and frame duration math.
70#[must_use]
71pub fn compute_frame_pacing(refresh_rate_hz: f32) -> FramePacingConfig {
72    let preferred_hz: f32 = if refresh_rate_hz.is_finite() && refresh_rate_hz >= MIN_REFRESH_RATE_HZ
73    {
74        refresh_rate_hz.clamp(MIN_REFRESH_RATE_HZ, MAX_REFRESH_RATE_HZ)
75    } else {
76        FALLBACK_REFRESH_RATE_HZ
77    };
78    let min_hz: f32 = if preferred_hz >= 120.0 {
79        PRO_MOTION_MIN_ACTIVE_HZ
80    } else {
81        preferred_hz.min(FALLBACK_REFRESH_RATE_HZ)
82    };
83    let frame_interval_secs: f32 = 1.0 / preferred_hz;
84    let frame_interval_nanos: u64 = (1_000_000_000.0 / f64::from(preferred_hz)).round() as u64;
85    FramePacingConfig::new(
86        min_hz,
87        preferred_hz,
88        preferred_hz,
89        frame_interval_secs,
90        frame_interval_nanos,
91    )
92}
93
94/// Resolves safe-area insets in logical dp, guaranteeing cold-start and Atomic Island / Dynamic Island clearance when not fullscreen.
95///
96/// # Arguments
97///
98/// - `SafeAreaInsets` - Live visible window insets reported by the OS.
99/// - `SafeAreaInsets` - Visibility-ignoring system bar and cutout insets (`getInsetsIgnoringVisibility`).
100/// - `f32` - Maximum top cutout / island bounding-rect bottom edge in logical dp.
101/// - `f32` - Cold-start fallback status-bar height in logical dp (`status_bar_height` dimen or `statusBarFrame`).
102/// - `bool` - Whether true edge-to-edge immersive fullscreen mode is currently active.
103///
104/// # Returns
105///
106/// - `SafeAreaInsets` - Sanitized safe-area insets (`0` on all edges in fullscreen; cleared top/cutout otherwise).
107#[must_use]
108pub fn resolve_mobile_safe_area(
109    visible: SafeAreaInsets,
110    ignoring_visibility: SafeAreaInsets,
111    cutout_bounding_top_dp: f32,
112    fallback_status_bar_dp: f32,
113    immersive_fullscreen: bool,
114) -> SafeAreaInsets {
115    if immersive_fullscreen {
116        return SafeAreaInsets::default();
117    }
118    let top: f32 = sanitize_inset_dp(visible.get_top())
119        .max(sanitize_inset_dp(ignoring_visibility.get_top()))
120        .max(sanitize_inset_dp(cutout_bounding_top_dp))
121        .max(sanitize_inset_dp(fallback_status_bar_dp));
122    let right: f32 = sanitize_inset_dp(visible.get_right())
123        .max(sanitize_inset_dp(ignoring_visibility.get_right()));
124    let bottom: f32 = sanitize_inset_dp(visible.get_bottom())
125        .max(sanitize_inset_dp(ignoring_visibility.get_bottom()));
126    let left: f32 = sanitize_inset_dp(visible.get_left())
127        .max(sanitize_inset_dp(ignoring_visibility.get_left()));
128    SafeAreaInsets::new(top, right, bottom, left)
129}
130
131/// Converts a non-negative physical pixel count to logical dp using display density `scale`.
132///
133/// # Arguments
134///
135/// - `i32` - Physical pixel measurement.
136/// - `f32` - Sanitized display density scale (`>= 0.75`).
137///
138/// # Returns
139///
140/// - `f32` - Logical dp value.
141#[must_use]
142fn px_to_dp(px: i32, scale: f32) -> f32 {
143    (px.max(0) as f32) / scale
144}
145
146/// Converts raw Android physical-pixel window insets, cutout rects, and status-bar fallback into logical `SafeAreaInsets`.
147///
148/// # Arguments
149///
150/// - `[i32; 4]` - Visible insets `[top, right, bottom, left]` in physical pixels.
151/// - `[i32; 4]` - Ignoring-visibility insets `[top, right, bottom, left]` in physical pixels.
152/// - `i32` - Maximum top display-cutout / Atomic Island bounding-rect bottom edge in physical pixels.
153/// - `i32` - Fallback Android `status_bar_height` system dimen in physical pixels.
154/// - `f32` - Active display density scale (`displayMetrics.density`).
155/// - `bool` - Whether immersive fullscreen mode is active.
156///
157/// # Returns
158///
159/// - `SafeAreaInsets` - Resolved logical-dp safe-area insets.
160#[must_use]
161pub fn resolve_safe_area_from_px(
162    visible_px: [i32; 4],
163    ignoring_visibility_px: [i32; 4],
164    cutout_max_bottom_px: i32,
165    fallback_status_bar_px: i32,
166    density: f32,
167    immersive_fullscreen: bool,
168) -> SafeAreaInsets {
169    let scale: f32 = if density.is_finite() && density >= 0.75 {
170        density
171    } else {
172        1.0
173    };
174    let visible: SafeAreaInsets = SafeAreaInsets::new(
175        px_to_dp(visible_px[0], scale),
176        px_to_dp(visible_px[1], scale),
177        px_to_dp(visible_px[2], scale),
178        px_to_dp(visible_px[3], scale),
179    );
180    let ignoring: SafeAreaInsets = SafeAreaInsets::new(
181        px_to_dp(ignoring_visibility_px[0], scale),
182        px_to_dp(ignoring_visibility_px[1], scale),
183        px_to_dp(ignoring_visibility_px[2], scale),
184        px_to_dp(ignoring_visibility_px[3], scale),
185    );
186    resolve_mobile_safe_area(
187        visible,
188        ignoring,
189        px_to_dp(cutout_max_bottom_px, scale),
190        px_to_dp(fallback_status_bar_px, scale),
191        immersive_fullscreen,
192    )
193}
194
195/// Parses a single browser CSS safe-area pixel length (e.g. `"0px"`, `"47px"`, `"59.5px"`) into logical pixels without ever fabricating a non-zero fallback.
196///
197/// # Arguments
198///
199/// - `&str` - Raw CSS computed property value string from `getComputedStyle`.
200///
201/// # Returns
202///
203/// - `f32` - Exact finite inset in logical CSS pixels (`0.0` when unset, `"0px"`, unexpanded `env(...)`, negative, or non-finite).
204#[must_use]
205pub fn parse_css_safe_area_px(raw: &str) -> f32 {
206    let trimmed: &str = raw.trim();
207    if trimmed.is_empty() {
208        return 0.0;
209    }
210    let lower: String = trimmed.to_ascii_lowercase();
211    if lower.starts_with(CSS_SAFE_AREA_FUNC_ENV)
212        || lower.starts_with(CSS_SAFE_AREA_FUNC_VAR)
213        || lower.starts_with(CSS_SAFE_AREA_FUNC_CALC)
214    {
215        return 0.0;
216    }
217    let numeric: &str = lower
218        .strip_suffix(CSS_SAFE_AREA_PX_SUFFIX)
219        .unwrap_or(lower.as_str())
220        .trim();
221    let parsed: f32 = numeric.parse::<f32>().unwrap_or(0.0);
222    if parsed.is_finite() && parsed > 0.0 && parsed <= CSS_SAFE_AREA_MAX_DP {
223        parsed
224    } else {
225        0.0
226    }
227}
228
229/// Parses four browser CSS `env(safe-area-inset-*)` computed strings into `SafeAreaInsets`, returning strictly `0.0` for any edge without a real inset.
230///
231/// # Arguments
232///
233/// - `&str` - Computed top safe-area inset CSS string.
234/// - `&str` - Computed right safe-area inset CSS string.
235/// - `&str` - Computed bottom safe-area inset CSS string.
236/// - `&str` - Computed left safe-area inset CSS string.
237///
238/// # Returns
239///
240/// - `SafeAreaInsets` - Sanitized browser safe-area insets.
241#[must_use]
242pub fn parse_web_safe_area_css(top: &str, right: &str, bottom: &str, left: &str) -> SafeAreaInsets {
243    SafeAreaInsets::new(
244        parse_css_safe_area_px(top),
245        parse_css_safe_area_px(right),
246        parse_css_safe_area_px(bottom),
247        parse_css_safe_area_px(left),
248    )
249}