#![doc = include_str!("../readme.md")]
use ratatui::layout::{Position, Rect};
pub trait RelocatableState {
fn relocate(&mut self, shift: (i16, i16), clip: Rect);
}
#[macro_export]
macro_rules! impl_relocatable_state {
($($n:ident),* for $ty:ty) => {
impl $crate::RelocatableState for $ty {
fn relocate(&mut self, shift: (i16, i16), clip: Rect) {
$(self.$n.relocate(shift, clip);)*
}
}
};
}
pub fn relocate_areas(area: &mut [Rect], shift: (i16, i16), clip: Rect) {
for a in area {
*a = relocate_area(*a, shift, clip)
}
}
pub fn relocate_area(area: Rect, shift: (i16, i16), clip: Rect) -> Rect {
let relocated = relocate(area, shift);
let clipped = clipped(relocated, clip);
if !clipped.is_empty() {
clipped
} else {
Rect::default()
}
}
pub fn relocate_positions(pos: &mut [Position], shift: (i16, i16), clip: Rect) {
for p in pos {
*p = relocate_position(*p, shift, clip).unwrap_or_default()
}
}
pub fn relocate_position(pos: Position, shift: (i16, i16), clip: Rect) -> Option<Position> {
let reloc = relocate(Rect::new(pos.x, pos.y, 0, 0), shift);
let reloc_pos = Position::new(reloc.x, reloc.y);
if clip.contains(reloc_pos) {
Some(reloc_pos)
} else {
None
}
}
pub fn relocate_dark_offset(area: Rect, shift: (i16, i16), clip: Rect) -> (u16, u16) {
let relocated = relocate(area, shift);
let clipped = clipped(relocated, clip);
(clipped.x - relocated.x, clipped.y - relocated.y)
}
pub fn rect_dbg(area: Rect) -> String {
use std::fmt::Write;
let mut buf = String::new();
_ = write!(buf, "{}:{}+{}+{}", area.x, area.y, area.width, area.height);
buf
}
#[inline]
fn relocate(area: Rect, shift: (i16, i16)) -> Rect {
let x0 = area.left().saturating_add_signed(shift.0);
let x1 = area.right().saturating_add_signed(shift.0);
let y0 = area.top().saturating_add_signed(shift.1);
let y1 = area.bottom().saturating_add_signed(shift.1);
Rect::new(x0, y0, x1 - x0, y1 - y0)
}
#[inline]
fn clipped(area: Rect, clip: Rect) -> Rect {
area.intersection(clip)
}