frust_core/app/metrics.rs
1//! [`WindowMetrics`] and [`Orientation`]: shell-provided window-shape context; `RenderRoot` never
2//! stores or reads it.
3
4use kurbo::Size;
5
6use crate::insets::WindowInsets;
7
8/// The window's logical size, device-pixel scale, derived [`Orientation`] and [`WindowInsets`],
9/// as a plain `provide_context` value: `use_context` subscribes to nothing, so a changed value is
10/// visible only on the next rebuild. Re-provide it only on change, beside the standalone insets.
11#[derive(Clone, Copy, Debug, PartialEq)]
12pub struct WindowMetrics {
13 /// The window's logical (density-independent) size.
14 pub size: Size,
15 /// The device-pixel scale factor (logical → physical px multiplier).
16 pub scale: f64,
17 /// Derived from `size`; no platform callback carries an orientation.
18 pub orientation: Orientation,
19 pub insets: WindowInsets,
20}
21
22impl WindowMetrics {
23 /// Construct a [`WindowMetrics`], deriving [`orientation`](Self::orientation) from `size`.
24 pub fn new(size: Size, scale: f64, insets: WindowInsets) -> Self {
25 Self {
26 size,
27 scale,
28 orientation: Orientation::from_size(size),
29 insets,
30 }
31 }
32}
33
34/// A window's derived portrait/landscape orientation.
35#[derive(Clone, Copy, Debug, PartialEq, Eq)]
36pub enum Orientation {
37 /// `size.height >= size.width`, including the exact-square case.
38 Portrait,
39 /// `size.height < size.width`.
40 Landscape,
41}
42
43impl Orientation {
44 /// Portrait when `height >= width` (an exact square reads as portrait), landscape otherwise.
45 pub fn from_size(size: Size) -> Self {
46 if size.height >= size.width {
47 Orientation::Portrait
48 } else {
49 Orientation::Landscape
50 }
51 }
52}