iced_core/
size.rs

1use crate::{Length, Radians, Vector};
2
3/// An amount of space in 2 dimensions.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
5pub struct Size<T = f32> {
6    /// The width.
7    pub width: T,
8    /// The height.
9    pub height: T,
10}
11
12impl<T> Size<T> {
13    /// Creates a new  [`Size`] with the given width and height.
14    pub const fn new(width: T, height: T) -> Self {
15        Size { width, height }
16    }
17}
18
19impl Size {
20    /// A [`Size`] with zero width and height.
21    pub const ZERO: Size = Size::new(0., 0.);
22
23    /// A [`Size`] with a width and height of 1 unit.
24    pub const UNIT: Size = Size::new(1., 1.);
25
26    /// A [`Size`] with infinite width and height.
27    pub const INFINITE: Size = Size::new(f32::INFINITY, f32::INFINITY);
28
29    /// Returns the minimum of each component of this size and another.
30    pub fn min(self, other: Self) -> Self {
31        Size {
32            width: self.width.min(other.width),
33            height: self.height.min(other.height),
34        }
35    }
36
37    /// Returns the maximum of each component of this size and another.
38    pub fn max(self, other: Self) -> Self {
39        Size {
40            width: self.width.max(other.width),
41            height: self.height.max(other.height),
42        }
43    }
44
45    /// Expands this [`Size`] by the given amount.
46    pub fn expand(self, other: impl Into<Size>) -> Self {
47        let other = other.into();
48
49        Size {
50            width: self.width + other.width,
51            height: self.height + other.height,
52        }
53    }
54
55    /// Rotates this [`Size`] and returns the minimum [`Size`]
56    /// containing it.
57    pub fn rotate(self, rotation: Radians) -> Size {
58        let radians = f32::from(rotation);
59
60        Size {
61            width: (self.width * radians.cos()).abs() + (self.height * radians.sin()).abs(),
62            height: (self.width * radians.sin()).abs() + (self.height * radians.cos()).abs(),
63        }
64    }
65
66    /// Applies an aspect ratio to this [`Size`] without
67    /// exceeding its bounds.
68    pub const fn ratio(self, aspect_ratio: f32) -> Size {
69        Size {
70            width: (self.height * aspect_ratio).min(self.width),
71            height: (self.width / aspect_ratio).min(self.height),
72        }
73    }
74}
75
76impl Size<Length> {
77    /// Returns true if either `width` or `height` are 0-sized.
78    #[inline]
79    pub fn is_void(&self) -> bool {
80        matches!(self.width, Length::Fixed(0.0)) || matches!(self.height, Length::Fixed(0.0))
81    }
82}
83
84impl<T> From<[T; 2]> for Size<T> {
85    fn from([width, height]: [T; 2]) -> Self {
86        Size { width, height }
87    }
88}
89
90impl<T> From<(T, T)> for Size<T> {
91    fn from((width, height): (T, T)) -> Self {
92        Self { width, height }
93    }
94}
95
96impl From<(u32, u32)> for Size {
97    fn from((width, height): (u32, u32)) -> Self {
98        Size::new(width as f32, height as f32)
99    }
100}
101
102impl<T> From<Vector<T>> for Size<T> {
103    fn from(vector: Vector<T>) -> Self {
104        Size {
105            width: vector.x,
106            height: vector.y,
107        }
108    }
109}
110
111impl<T> From<Size<T>> for [T; 2] {
112    fn from(size: Size<T>) -> Self {
113        [size.width, size.height]
114    }
115}
116
117impl<T> From<Size<T>> for Vector<T> {
118    fn from(size: Size<T>) -> Self {
119        Vector::new(size.width, size.height)
120    }
121}
122
123impl<T> std::ops::Add for Size<T>
124where
125    T: std::ops::Add<Output = T>,
126{
127    type Output = Size<T>;
128
129    fn add(self, rhs: Self) -> Self::Output {
130        Size {
131            width: self.width + rhs.width,
132            height: self.height + rhs.height,
133        }
134    }
135}
136
137impl<T> std::ops::Sub for Size<T>
138where
139    T: std::ops::Sub<Output = T>,
140{
141    type Output = Size<T>;
142
143    fn sub(self, rhs: Self) -> Self::Output {
144        Size {
145            width: self.width - rhs.width,
146            height: self.height - rhs.height,
147        }
148    }
149}
150
151impl<T> std::ops::Mul<T> for Size<T>
152where
153    T: std::ops::Mul<Output = T> + Copy,
154{
155    type Output = Size<T>;
156
157    fn mul(self, rhs: T) -> Self::Output {
158        Size {
159            width: self.width * rhs,
160            height: self.height * rhs,
161        }
162    }
163}
164
165impl<T> std::ops::Div<T> for Size<T>
166where
167    T: std::ops::Div<Output = T> + Copy,
168{
169    type Output = Size<T>;
170
171    fn div(self, rhs: T) -> Self::Output {
172        Size {
173            width: self.width / rhs,
174            height: self.height / rhs,
175        }
176    }
177}
178
179impl<T> std::ops::Mul<Vector<T>> for Size<T>
180where
181    T: std::ops::Mul<Output = T> + Copy,
182{
183    type Output = Size<T>;
184
185    fn mul(self, scale: Vector<T>) -> Self::Output {
186        Size {
187            width: self.width * scale.x,
188            height: self.height * scale.y,
189        }
190    }
191}