1use std::fmt;
2
3use mitsuami_reactive::{IntoValue, Value};
4
5#[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#[derive(Clone, Copy, Debug, PartialEq)]
26pub enum WindowSize {
27 Fixed(Size),
28 FitHeight(f32),
32 FollowHeight(f32),
37 FollowHeightUntilResized(f32),
40 Screen {
44 width: f32,
45 height: f32,
46 },
47}
48
49#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
51pub enum Placement {
52 #[default]
54 System,
55 Centred,
58}
59
60impl IntoValue<Placement> for Placement {
61 fn into_value(self) -> Value<Placement> {
62 Value::Static(self)
63 }
64}
65
66#[derive(Clone, Copy, Debug, PartialEq)]
72pub struct WindowFrame {
73 pub position: Option<Point>,
79 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#[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#[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 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 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 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
206pub 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}