Lines
100 %
Functions
50 %
Branches
//! Where a new window opens: sized to its content and kept on the desktop.
use ratatui::layout::Rect;
use super::Desktop;
use super::place::{Place, cell, within};
impl<Id: Copy + Eq> Desktop<Id> {
/// The rect of a new window of `size`, at most the desktop's, one cell down and right of the
/// front window's drawn place and kept inside `area`; the corner of `area` with no window.
#[must_use]
pub fn fitted_rect(&self, (width, height): (u16, u16), area: Rect) -> Rect {
let width = width.min(area.width);
let height = height.min(area.height);
let (x, y) = self
.front()
.and_then(|id| self.place_of(id, area))
.map_or((i32::from(area.x), i32::from(area.y)), |front| {
(front.x + 1, front.y + 1)
});
let x = within(
x,
i32::from(area.x),
i32::from(area.right()) - i32::from(width),
);
let y = within(
y,
i32::from(area.y),
i32::from(area.bottom()) - i32::from(height),
Rect::new(cell(x), cell(y), width, height)
}
/// Sizes `id` to `size`, at most `area`'s, and moves it back inside `area` when it would
/// overhang; nothing when it is not open. A size equal to the stored one keeps the place.
pub fn set_size(&mut self, id: Id, (width, height): (u16, u16), area: Rect) {
let Some(window) = self.window_mut(id) else {
return;
};
if (width, height) == (window.place.width, window.place.height) {
let right = i32::from(area.right()) - i32::from(width);
let bottom = i32::from(area.bottom()) - i32::from(height);
window.place = Place {
x: within(window.place.x, i32::from(area.x), right),
y: within(window.place.y, i32::from(area.y), bottom),
width,
height,