mirror of
https://github.com/ratatui/ratatui.git
synced 2025-10-02 15:25:54 +00:00
feat(Rect): add offset
method (#533)
The offset method creates a new Rect that is moved by the amount specified in the x and y direction. These values can be positive or negative. This is useful for manual layout tasks. ```rust let rect = area.offset(Offset { x: 10, y -10 }); ```
This commit is contained in:
parent
fe632d70cb
commit
1229b96e42
@ -6,6 +6,10 @@ use std::{
|
|||||||
|
|
||||||
use crate::prelude::*;
|
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
|
/// 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.
|
/// area they are supposed to render to.
|
||||||
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
|
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
|
||||||
@ -106,6 +110,26 @@ impl Rect {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Moves the `Rect` without modifying its size.
|
||||||
|
///
|
||||||
|
/// Moves the `Rect` according to the given offset without modifying its [`width`](Rect::width)
|
||||||
|
/// or [`height`](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 [`Offset`] for details.
|
||||||
|
pub fn offset(self, offset: Offset) -> Rect {
|
||||||
|
Rect {
|
||||||
|
x: i32::from(self.x)
|
||||||
|
.saturating_add(offset.x)
|
||||||
|
.clamp(0, (u16::MAX - self.width) as i32) as u16,
|
||||||
|
y: i32::from(self.y)
|
||||||
|
.saturating_add(offset.y)
|
||||||
|
.clamp(0, (u16::MAX - self.height) as i32) as u16,
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns a new rect that contains both the current one and the given one.
|
/// Returns a new rect that contains both the current one and the given one.
|
||||||
pub fn union(self, other: Rect) -> Rect {
|
pub fn union(self, other: Rect) -> Rect {
|
||||||
let x1 = min(self.x, other.x);
|
let x1 = min(self.x, other.x);
|
||||||
@ -207,6 +231,39 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn offset() {
|
||||||
|
assert_eq!(
|
||||||
|
Rect::new(1, 2, 3, 4).offset(Offset { x: 5, y: 6 }),
|
||||||
|
Rect::new(6, 8, 3, 4),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn negative_offset() {
|
||||||
|
assert_eq!(
|
||||||
|
Rect::new(4, 3, 3, 4).offset(Offset { x: -2, y: -1 }),
|
||||||
|
Rect::new(2, 2, 3, 4),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn negative_offset_saturate() {
|
||||||
|
assert_eq!(
|
||||||
|
Rect::new(1, 2, 3, 4).offset(Offset { x: -5, y: -6 }),
|
||||||
|
Rect::new(0, 0, 3, 4),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Offsets a [`Rect`] making it go outside [`u16::MAX`], it should keep its size.
|
||||||
|
#[test]
|
||||||
|
fn offset_saturate_max() {
|
||||||
|
assert_eq!(
|
||||||
|
Rect::new(u16::MAX - 500, u16::MAX - 500, 100, 100).offset(Offset { x: 1000, y: 1000 }),
|
||||||
|
Rect::new(u16::MAX - 100, u16::MAX - 100, 100, 100),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn union() {
|
fn union() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
12
src/layout/rect/offset.rs
Normal file
12
src/layout/rect/offset.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
/// Amounts by which to move a [`Rect`](super::Rect).
|
||||||
|
///
|
||||||
|
/// Positive numbers move to the right/bottom and negative to the left/top.
|
||||||
|
///
|
||||||
|
/// See [`Rect::offset`](super::Rect::offset)
|
||||||
|
#[derive(Debug, Default, Clone, Copy)]
|
||||||
|
pub struct Offset {
|
||||||
|
/// How much to move on the X axis
|
||||||
|
pub x: i32,
|
||||||
|
/// How much to move on the Y axis
|
||||||
|
pub y: i32,
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user