Lines
98.47 %
Functions
65.87 %
Branches
100 %
//! The window manager as plain data: the windows in z-order with their rects and zoom flags.
//! No events and no app types; the app owns the window state and asks the desktop where each goes.
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use std::slice::Iter;
use crate::{Caps, Role, Theme};
mod arrange;
mod drag;
mod fit;
mod place;
pub use drag::{Change, changed};
pub use place::{Place, copy_visible};
/// TV's `minWinSize`: the least columns and rows a window is sized to.
pub(crate) const MIN_SIZE: (u16, u16) = (16, 6);
/// The desktop background pattern, TV's `defaultBkgrnd` (CP437 0xB0).
const BACKGROUND: &str = "\u{2591}";
/// One window of the desktop. The place is where it sits when not zoomed, in screen-absolute
/// cells (TV stores it relative to the desktop): the app draws the desktop at a fixed offset,
/// and `place_of` limits it against whatever area it is handed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Window<Id> {
pub id: Id,
pub place: Place,
pub zoomed: bool,
pub caps: Caps,
}
impl<Id> Window<Id> {
/// Stores `to`, keeping the size when the window cannot grow.
fn set_place(&mut self, to: Place) {
self.place = match self.caps.grow {
true => to,
false => Place {
x: to.x,
y: to.y,
..self.place
},
};
/// The windows back to front: the last is the front (current) one.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Desktop<Id> {
windows: Vec<Window<Id>>,
impl<Id> Default for Desktop<Id> {
fn default() -> Self {
Self {
windows: Vec::new(),
impl<Id: Copy + Eq> Desktop<Id> {
/// Opens `id` on top; ids are never reused, so `id` is not already open.
pub fn open(&mut self, id: Id, rect: Rect, caps: Caps) {
self.windows.push(Window {
id,
place: Place::from(rect),
zoomed: false,
caps,
});
/// Brings `id` to the front; false when it is not open.
pub fn raise(&mut self, id: Id) -> bool {
let Some(index) = self.position(id) else {
return false;
let window = self.windows.remove(index);
self.windows.push(window);
true
/// Sends `id` behind every other window; false when it is not open.
pub fn lower(&mut self, id: Id) -> bool {
self.windows.insert(0, window);
/// Removes `id`; false when it is not open.
pub fn close(&mut self, id: Id) -> bool {
self.windows.remove(index);
pub fn front(&self) -> Option<Id> {
self.windows.last().map(|window| window.id)
pub fn rearmost(&self) -> Option<Id> {
self.windows.first().map(|window| window.id)
/// Flips the zoom flag of `id`; false when it is not open or cannot zoom.
pub fn toggle_zoom(&mut self, id: Id) -> bool {
let window = &mut self.windows[index];
window.zoomed ^= window.caps.zoom;
window.caps.zoom
/// Lays every tileable window out in TV's tile grid over `area`, unzoomed; false, with nothing
/// moved, when the tileable windows do not fit.
pub fn tile(&mut self, area: Rect) -> bool {
match arrange::tile(self.tileable(), area) {
Some(rects) => self.place(&rects),
None => false,
/// Lays every tileable window out in TV's cascade over `area`, unzoomed; false, with nothing
pub fn cascade(&mut self, area: Rect) -> bool {
match arrange::cascade(self.tileable(), area) {
fn tileable(&self) -> usize {
self.windows
.iter()
.filter(|window| window.caps.tile)
.count()
/// Gives the tileable windows back to front the stored `rects`, unzoomed.
fn place(&mut self, rects: &[Rect]) -> bool {
let tileable = self.windows.iter_mut().filter(|window| window.caps.tile);
for (window, rect) in tileable.zip(rects) {
window.place = Place::from(*rect);
window.zoomed = false;
/// Fits every window to the desktop going from `old` to `new`, as TV does when its owner
/// changes size: see `Place::scaled`.
pub fn resize(&mut self, old: Rect, new: Rect) {
for window in &mut self.windows {
window.set_place(window.place.scaled(old, new));
/// Where `id` is placed in `area`: all of it when zoomed, else its place as TV keeps it
/// (see `restored_place`). The place may reach beyond `area`; `Place::visible_in` says which
/// cells show.
pub fn place_of(&self, id: Id, area: Rect) -> Option<Place> {
let window = self.windows.get(self.position(id)?)?;
match window.zoomed {
true => Some(Place::from(area)),
false => self.restored_place(id, area),
/// Where `id` sits once unzoomed, whether it is zoomed now or not: the size limited to
/// `area`'s, the origin to TV's limits.
pub fn restored_place(&self, id: Id, area: Rect) -> Option<Place> {
Some(window.place.limited(area))
/// True when the windows in front of `id` cover all the cells that show it.
pub fn occluded(&self, id: Id, area: Rect) -> bool {
let Some(place) = self.place_of(id, area) else {
let in_front: Vec<Rect> = self.windows[index + 1..]
.filter_map(|window| self.place_of(window.id, area))
.map(|front| front.visible_in(area))
.collect();
place
.visible_in(area)
.positions()
.all(|position| in_front.iter().any(|front| front.contains(position)))
/// The slot of `id` as it is now, to hand back to `restore`.
pub fn window(&self, id: Id) -> Option<Window<Id>> {
self.windows.get(self.position(id)?).copied()
/// Puts a window saved by `window` back in its slot, rect and zoom as they were; false when
/// it is not open.
pub fn restore(&mut self, saved: Window<Id>) -> bool {
let Some(index) = self.position(saved.id) else {
self.windows[index] = saved;
/// The windows in drawing order: the front one last.
pub fn iter_back_to_front(&self) -> Iter<'_, Window<Id>> {
self.windows.iter()
fn window_mut(&mut self, id: Id) -> Option<&mut Window<Id>> {
let index = self.position(id)?;
self.windows.get_mut(index)
fn position(&self, id: Id) -> Option<usize> {
self.windows.iter().position(|window| window.id == id)
/// Fills `area` with the desktop background pattern; the windows draw over it.
pub fn render_background(area: Rect, buf: &mut Buffer, theme: &Theme) {
let style = theme.style(Role::Desktop);
for position in area.intersection(buf.area).positions() {
if let Some(cell) = buf.cell_mut(position) {
cell.set_symbol(BACKGROUND).set_style(style);
#[cfg(test)]
mod caps_tests;
mod fit_tests;
mod tests;