Skip to main content

cosmic_applet_avestruz/
screen.rs

1// SPDX-License-Identifier: MIT
2
3//! The screen the popup opens on: how much of its height there is for the
4//! popup, and how many of its pixels a point is.
5//!
6//! The popup may be as tall as its screen has room, and a picture is kept
7//! at as many pixels as it takes on it (see [`crate::images`]). Neither
8//! is told to a program: both are what the compositor does with a surface
9//! of its own. So the window keeps one that it asks: the surface that is
10//! there from the start and shows nothing (see
11//! [`keep_what_is_copied`](crate::state::AppModel::keep_what_is_copied))
12//! is a point wide and reaches from the top of the screen to the bottom,
13//! beside whatever keeps room for itself at an edge, as the panel does.
14//! How tall the compositor made it is how much room there is, and the
15//! scale it is drawn at is the screen's.
16//!
17//! It is asked when the window starts, and again whenever the popup
18//! opens, since a screen is something that gets plugged in and out, and
19//! set to another scale. Until it has answered, the popup keeps to a
20//! height that fits any screen, and pictures to a size that is sharp on
21//! any.
22//!
23//! Heights are logical: a display 2160 pixels tall at 170% is 1270 points
24//! tall, and points are what a layout is in. Its scale is how many pixels
25//! that makes a point: 1.7.
26
27/// How tall a surface is, at the least, that the compositor has laid out
28/// from the top of a screen to the bottom: one that is less was not laid
29/// out yet, and says nothing of its screen.
30const LAID_OUT: f32 = 100.0;
31
32/// A screen, as far as the window has to know it.
33#[derive(Debug, Clone, Copy, PartialEq)]
34pub struct Screen {
35    /// How much of its height there is for the popup, in points: all of
36    /// it, but what keeps room for itself at its top or bottom.
37    pub height: f32,
38    /// How many of its pixels a point is.
39    pub scale: f32,
40}
41
42/// What a surface that reaches from the top of its screen to the bottom
43/// says of it: one that is `height` points tall, and drawn at `scale`.
44/// `None` if it was not laid out yet, or says nonsense.
45pub fn measured(height: f32, scale: f32) -> Option<Screen> {
46    let sound = height.is_finite() && height >= LAID_OUT && scale.is_finite() && scale > 0.0;
47    sound.then_some(Screen { height, scale })
48}
49
50#[cfg(test)]
51mod tests {
52    use super::{Screen, measured};
53
54    #[test]
55    fn a_surface_as_tall_as_its_screen_has_room_says_how_tall_that_is_and_at_what_scale() {
56        // 2160 pixels at 170% are 1270 points, less a panel of 40.
57        assert_eq!(measured(1230.0, 1.7), Some(Screen { height: 1230.0, scale: 1.7 }));
58        assert_eq!(measured(1080.0, 1.0), Some(Screen { height: 1080.0, scale: 1.0 }));
59    }
60
61    #[test]
62    fn a_surface_not_laid_out_yet_is_no_answer_rather_than_a_wrong_one() {
63        // As it is before the compositor has said how large it is.
64        assert_eq!(measured(1.0, 1.0), None);
65        assert_eq!(measured(0.0, 1.7), None);
66        // And nonsense.
67        assert_eq!(measured(1230.0, 0.0), None);
68        assert_eq!(measured(1230.0, f32::NAN), None);
69        assert_eq!(measured(f32::INFINITY, 1.0), None);
70    }
71}