diff options
author | 2024-04-14 13:43:10 +0200 | |
---|---|---|
committer | 2024-04-14 13:43:10 +0200 | |
commit | 105b8bd5ad6ade1f203a0d8b0b93bd06f61f621a (patch) | |
tree | 4a5efa021b0dbe7d87ff57993c118753a32ea241 /core/src/rectangle.rs | |
parent | ee105e3bee1bc676dcf3324693984ccda8e4e733 (diff) | |
parent | dbbbadfc950dfdfd02c7abbbf993e0685ca0f64a (diff) | |
download | iced-105b8bd5ad6ade1f203a0d8b0b93bd06f61f621a.tar.gz iced-105b8bd5ad6ade1f203a0d8b0b93bd06f61f621a.tar.bz2 iced-105b8bd5ad6ade1f203a0d8b0b93bd06f61f621a.zip |
Merge pull request #2382 from iced-rs/wgpu/better-architecture
Improved architecture for `iced_wgpu` and `iced_tiny_skia`
Diffstat (limited to 'core/src/rectangle.rs')
-rw-r--r-- | core/src/rectangle.rs | 47 |
1 files changed, 31 insertions, 16 deletions
diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index c1c2eeac..2ab50137 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -16,24 +16,32 @@ pub struct Rectangle<T = f32> { pub height: T, } -impl Rectangle<f32> { - /// Creates a new [`Rectangle`] with its top-left corner in the given - /// [`Point`] and with the provided [`Size`]. - pub fn new(top_left: Point, size: Size) -> Self { +impl<T> Rectangle<T> +where + T: Default, +{ + /// Creates a new [`Rectangle`] with its top-left corner at the origin + /// and with the provided [`Size`]. + pub fn with_size(size: Size<T>) -> Self { Self { - x: top_left.x, - y: top_left.y, + x: T::default(), + y: T::default(), width: size.width, height: size.height, } } +} - /// Creates a new [`Rectangle`] with its top-left corner at the origin - /// and with the provided [`Size`]. - pub fn with_size(size: Size) -> Self { +impl Rectangle<f32> { + /// A rectangle starting at [`Point::ORIGIN`] with infinite width and height. + pub const INFINITE: Self = Self::new(Point::ORIGIN, Size::INFINITY); + + /// Creates a new [`Rectangle`] with its top-left corner in the given + /// [`Point`] and with the provided [`Size`]. + pub const fn new(top_left: Point, size: Size) -> Self { Self { - x: 0.0, - y: 0.0, + x: top_left.x, + y: top_left.y, width: size.width, height: size.height, } @@ -139,13 +147,20 @@ impl Rectangle<f32> { } /// Snaps the [`Rectangle`] to __unsigned__ integer coordinates. - pub fn snap(self) -> Rectangle<u32> { - Rectangle { + pub fn snap(self) -> Option<Rectangle<u32>> { + let width = self.width as u32; + let height = self.height as u32; + + if width < 1 || height < 1 { + return None; + } + + Some(Rectangle { x: self.x as u32, y: self.y as u32, - width: self.width as u32, - height: self.height as u32, - } + width, + height, + }) } /// Expands the [`Rectangle`] a given amount. |