Skip to main content

mitsuami_core/
geometry.rs

1use std::fmt;
2
3use mitsuami_reactive::{IntoValue, Value};
4
5/// A size in logical units (points / DIPs), never physical pixels.
6#[derive(Clone, Copy, Debug, Default, PartialEq)]
7pub struct Size {
8    pub width: f32,
9    pub height: f32,
10}
11
12impl Size {
13    pub const ZERO: Size = Size { width: 0.0, height: 0.0 };
14
15    pub const fn new(width: f32, height: f32) -> Size {
16        Size { width, height }
17    }
18
19    pub fn is_empty(&self) -> bool {
20        self.width <= 0.0 || self.height <= 0.0
21    }
22}
23
24/// The content size a window opens at.
25#[derive(Clone, Copy, Debug, PartialEq)]
26pub enum WindowSize {
27    Fixed(Size),
28    /// This width, and the height of the content at its first layout, so
29    /// the window fits what each platform's controls add up to. After that
30    /// it's an ordinary window: it doesn't follow later content changes.
31    FitHeight(f32),
32    /// This width, and the height of the content as it changes: the window
33    /// grows and shrinks with it. The user resizes only its width (GTK has
34    /// no such window: there it isn't resizable). `Ui::set_window_size`
35    /// sets its width.
36    FollowHeight(f32),
37    /// Like `FollowHeight` until the user, or `Ui::set_window_size`,
38    /// changes its height: then it's an ordinary window.
39    FollowHeightUntilResized(f32),
40    /// These fractions (`0.0`–`1.0`) of the work area of the screen it
41    /// opens on (the screen less its bars and docks), as an app sizes its
42    /// first window to the screen it has.
43    Screen {
44        width: f32,
45        height: f32,
46    },
47}
48
49/// Where a window opens when no [`WindowFrame`] says.
50#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
51pub enum Placement {
52    /// Where the platform puts a new window. The default.
53    #[default]
54    System,
55    /// Centred on its screen's work area. Where the platform places
56    /// windows itself (Wayland, and GTK everywhere), it still does.
57    Centred,
58}
59
60impl IntoValue<Placement> for Placement {
61    fn into_value(self) -> Value<Placement> {
62        Value::Static(self)
63    }
64}
65
66/// A window's restored frame: where it is and how large while it's neither
67/// maximized nor in full screen, and what it goes back to when it stops
68/// being either, the frame an app keeps to open its window as it was left
69/// (as Windows' `GetWindowPlacement`, AppKit's saved frames and Qt's
70/// `saveGeometry` keep it).
71#[derive(Clone, Copy, Debug, PartialEq)]
72pub struct WindowFrame {
73    /// Its top-left corner in the platform's own desktop coordinates
74    /// (Windows' physical pixels, AppKit's points from the bottom left of
75    /// the main screen, X11's pixels): a value to hand back to the same
76    /// platform, not to compute with. `None` where a window can't know
77    /// where it is (Wayland, and GTK everywhere).
78    pub position: Option<Point>,
79    /// Its content size in points, what `Window::size` takes.
80    pub size: Size,
81}
82
83impl From<Size> for WindowSize {
84    fn from(size: Size) -> WindowSize {
85        WindowSize::Fixed(size)
86    }
87}
88
89impl IntoValue<WindowSize> for WindowSize {
90    fn into_value(self) -> Value<WindowSize> {
91        Value::Static(self)
92    }
93}
94
95impl IntoValue<WindowSize> for Size {
96    fn into_value(self) -> Value<WindowSize> {
97        Value::Static(WindowSize::Fixed(self))
98    }
99}
100
101#[derive(Clone, Copy, Debug, Default, PartialEq)]
102pub struct Point {
103    pub x: f32,
104    pub y: f32,
105}
106
107impl Point {
108    pub const ZERO: Point = Point { x: 0.0, y: 0.0 };
109
110    pub const fn new(x: f32, y: f32) -> Point {
111        Point { x, y }
112    }
113}
114
115/// Distances in from a rectangle's edges, in logical units.
116#[derive(Clone, Copy, Debug, Default, PartialEq)]
117pub struct Insets {
118    pub top: f32,
119    pub right: f32,
120    pub bottom: f32,
121    pub left: f32,
122}
123
124impl Insets {
125    pub const ZERO: Insets = Insets { top: 0.0, right: 0.0, bottom: 0.0, left: 0.0 };
126
127    pub const fn new(top: f32, right: f32, bottom: f32, left: f32) -> Insets {
128        Insets { top, right, bottom, left }
129    }
130}
131
132/// A rectangle in logical units.
133#[derive(Clone, Copy, Debug, Default, PartialEq)]
134pub struct Rect {
135    pub origin: Point,
136    pub size: Size,
137}
138
139impl Rect {
140    pub const ZERO: Rect = Rect { origin: Point::ZERO, size: Size::ZERO };
141
142    pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Rect {
143        Rect { origin: Point { x, y }, size: Size { width, height } }
144    }
145
146    pub fn x(&self) -> f32 {
147        self.origin.x
148    }
149
150    pub fn y(&self) -> f32 {
151        self.origin.y
152    }
153
154    pub fn width(&self) -> f32 {
155        self.size.width
156    }
157
158    pub fn height(&self) -> f32 {
159        self.size.height
160    }
161
162    pub fn max_x(&self) -> f32 {
163        self.origin.x + self.size.width
164    }
165
166    pub fn max_y(&self) -> f32 {
167        self.origin.y + self.size.height
168    }
169
170    pub fn offset(&self, by: Point) -> Rect {
171        Rect { origin: Point::new(self.origin.x + by.x, self.origin.y + by.y), size: self.size }
172    }
173
174    /// The rectangle inside these insets, empty if they overlap.
175    pub fn inset(&self, by: Insets) -> Rect {
176        Rect::new(
177            self.x() + by.left,
178            self.y() + by.top,
179            (self.width() - by.left - by.right).max(0.0),
180            (self.height() - by.top - by.bottom).max(0.0),
181        )
182    }
183
184    /// The smallest rectangle that holds both.
185    pub fn union(&self, other: &Rect) -> Rect {
186        let (x, y) = (self.x().min(other.x()), self.y().min(other.y()));
187        Rect::new(x, y, self.max_x().max(other.max_x()) - x, self.max_y().max(other.max_y()) - y)
188    }
189
190    /// The overlapping area, or `None` when the rectangles don't overlap.
191    pub fn intersection(&self, other: &Rect) -> Option<Rect> {
192        let x = self.x().max(other.x());
193        let y = self.y().max(other.y());
194        let max_x = self.max_x().min(other.max_x());
195        let max_y = self.max_y().min(other.max_y());
196        (max_x > x && max_y > y).then(|| Rect::new(x, y, max_x - x, max_y - y))
197    }
198}
199
200impl fmt::Display for Rect {
201    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
202        write!(f, "{},{} {}×{}", Num(self.origin.x), Num(self.origin.y), Num(self.size.width), Num(self.size.height))
203    }
204}
205
206/// Formats a float compactly: no trailing `.0`, at most two decimals.
207pub struct Num(pub f32);
208
209impl fmt::Display for Num {
210    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
211        let rounded = (self.0 * 100.0).round() / 100.0;
212        if rounded == rounded.trunc() { write!(f, "{}", rounded as i64) } else { write!(f, "{rounded}") }
213    }
214}