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 5bd4b55
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
60 changes: 60 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,56 @@ 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

/// 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 +342,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);
}
}
30 changes: 30 additions & 0 deletions src/layout/rect/offset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// Not used for now, might be used by `Rect::offset` later of removed
#[derive(Debug, Default, Clone, Copy)]

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

View check run for this annotation

Codecov / codecov/patch

src/layout/rect/offset.rs#L2

Added line #L2 was not covered by tests
pub struct Offset {
pub top: i32,
pub right: i32,
pub bottom: i32,
pub left: i32,
}

impl Offset {
pub fn topleft(top: i32, left: i32) -> Self {
Self {
top,
right: 0,
bottom: 0,
left,
}
}

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

View check run for this annotation

Codecov / codecov/patch

src/layout/rect/offset.rs#L11-L18

Added lines #L11 - L18 were not covered by tests
}

impl From<(i32, i32)> for Offset {
fn from(value: (i32, i32)) -> Self {
Self {
top: value.0,
right: value.1,
bottom: value.0,
left: value.1,
}
}

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

View check run for this annotation

Codecov / codecov/patch

src/layout/rect/offset.rs#L22-L29

Added lines #L22 - L29 were not covered by tests
}

0 comments on commit 5bd4b55

Please sign in to comment.