Skip to content

Commit

Permalink
feat(Rect): Rect manipulation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin271 committed Nov 25, 2023
1 parent c862aa5 commit 3da3d47
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/layout/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use std::{

use crate::prelude::*;

mod offset;

pub use offset::*;

/// A simple rectangle used in the computation of the layout and to give widgets a hint about the
/// area they are supposed to render to.
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
Expand Down Expand Up @@ -106,6 +110,62 @@ impl Rect {
}
}

/// Adjusts the size of the `Rect`.
///
/// - Positive `width` grows the width, negative shrinks.
/// - Positive `height` grows the height, negative shrinks.
///
/// # See also
///
/// See also [`Rect::relocate`] to move the `Rect`.
pub fn resize<I, J>(self, width: I, height: J) -> Rect
where
I: Into<i32>,
J: Into<i32>,
{
Rect {
width: i32::from(self.width)
.saturating_add(width.into())
.clamp(0, u16::MAX as i32) as u16,
height: i32::from(self.height)
.saturating_add(height.into())
.clamp(0, u16::MAX as i32) as u16,
..self
}
}

Check warning on line 135 in src/layout/rect.rs

View check run for this annotation

Codecov / codecov/patch

src/layout/rect.rs#L121-L135

Added lines #L121 - L135 were not covered by tests

/// Moves the `Rect` without modifying its size.
///
/// Moves the `Rect` according to `x` or `y` without modifying its [`Rect::width`] or
/// [`Rect::height`].
/// - Positive `x` moves the whole `Rect` to the right, negative to the left.
/// - Positive `y` moves the whole `Rect` to the bottom, negative to the top.
///
/// # See also
///
/// See also [`Rect::resize`] to resize.
pub fn relocate<I, J>(self, x: I, y: J) -> Rect
where
I: Into<i32>,
J: Into<i32>,
{
Rect {
x: i32::from(self.x)
.saturating_add(x.into())
.clamp(0, u16::MAX as i32) as u16,
y: i32::from(self.y)
.saturating_add(y.into())
.clamp(0, u16::MAX as i32) as u16,
..self
}
}

Check warning on line 161 in src/layout/rect.rs

View check run for this annotation

Codecov / codecov/patch

src/layout/rect.rs#L147-L161

Added lines #L147 - L161 were not covered by tests

/// Apply the given [`Offset`] to this Rect.
pub fn offset(self, offset: Offset) -> Rect {
self.relocate(offset.top, offset.left)
.resize(offset.right, offset.bottom)
}

Check warning on line 167 in src/layout/rect.rs

View check run for this annotation

Codecov / codecov/patch

src/layout/rect.rs#L164-L167

Added lines #L164 - L167 were not covered by tests

/// Returns a new rect that contains both the current one and the given one.
pub fn union(self, other: Rect) -> Rect {
let x1 = min(self.x, other.x);
Expand Down Expand Up @@ -288,4 +348,10 @@ mod tests {
const _BOTTOM: u16 = RECT.bottom();
assert!(RECT.intersects(RECT));
}

#[test]
fn saturate_add_neg() {
let t: i32 = -1;
assert_eq!(t.clamp(0, i32::from(u16::MAX)) as u16, 0u16);
}
}
16 changes: 16 additions & 0 deletions src/layout/rect/offset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// Amounts by which to move a [`Rect`](super::Rect)'s borders.
///
/// Positive numbers move to the right/bottom and negative to the left/top.
///
/// See [`Rect::offset`](super::Rect::offset)
#[derive(Debug, Default, Clone, Copy)]

Check warning on line 6 in src/layout/rect/offset.rs

View check run for this annotation

Codecov / codecov/patch

src/layout/rect/offset.rs#L6

Added line #L6 was not covered by tests
pub struct Offset {
/// How much to move the left border
pub left: i32,
/// How much to move the top border
pub top: i32,
/// How much to move the right border
pub right: i32,
/// How much to move the bottom border
pub bottom: i32,
}

0 comments on commit 3da3d47

Please sign in to comment.