Lines
100 %
Functions
90.63 %
Branches
//! Opening windows: the startup set, the TV number and the cascade slot a new window gets.
use super::App;
use super::windows::{desktop_area, new_view};
use crate::draw::body_area;
use crate::view::ViewId;
use crate::window::{Placement, View, WindowId};
use ratatui::layout::Rect;
use tvui::Place;
/// The size of a window once unzoomed.
const WINDOW_SIZE: (u16, u16) = (60, 16);
/// The screen the cascade assumes until the terminal reports its size.
const FALLBACK_SCREEN: Rect = Rect::new(0, 0, 80, 24);
/// The columns and rows a window gives up at the least, so a small desktop still cascades.
const CASCADE_ROOM: u16 = 3;
/// TV numbers windows 1 to 9.
const MAX_NUMBER: u8 = 9;
impl App {
/// Opens `view` at the next cascade slot with the lowest free number. It is zoomed when the
/// front window is or when there is none.
pub(crate) fn open_new_window(&mut self, view: View) -> WindowId {
let placement = Placement {
rect: self.cascade_rect(),
zoomed: self.front_zoomed().unwrap_or(true),
};
let number = self.free_number();
self.open_window(view, placement, number)
}
/// Opens a new window of `kind` on top and loads it.
pub(crate) fn open_view(&mut self, kind: ViewId) {
let id = self.open_new_window(new_view(kind));
self.activate_window(id);
fn front_zoomed(&self) -> Option<bool> {
let mut front = self.desktop.iter_back_to_front().rev();
front.find(|slot| slot.caps.zoom).map(|slot| slot.zoomed)
/// The lowest of 1 to 9 no window has.
fn free_number(&self) -> Option<u8> {
(1..=MAX_NUMBER).find(|number| self.windows.iter().all(|win| win.number != Some(*number)))
/// The next ordinal of a window of `kind`: one past the highest open one.
pub(super) fn next_ordinal(&self, kind: ViewId) -> u32 {
let same = self.windows.iter().filter(|win| win.view.kind() == kind);
same.map(|win| win.ordinal).max().map_or(1, |last| last + 1)
/// The part of the terminal the windows sit on.
pub(super) fn desktop_rect(&self) -> Rect {
let fallback = body_area(FALLBACK_SCREEN);
self.terminal.env().map_or(fallback, desktop_area)
/// The rect one step down and right of the front window's, wrapping back to the desktop
/// corner when it would leave the desktop. A desktop too small for the usual size gets
/// smaller windows, so that there is always a slot.
fn cascade_rect(&self) -> Rect {
let area = self.desktop_rect();
let width = WINDOW_SIZE.0.min(area.width.saturating_sub(CASCADE_ROOM));
let height = WINDOW_SIZE.1.min(area.height.saturating_sub(CASCADE_ROOM));
let front = self
.front_window()
.and_then(|id| self.desktop.restored_place(id, area));
let (x, y) = front.map_or((i32::from(area.x), i32::from(area.y)), |place| {
(place.x + 1, place.y + 1)
});
let next = Place {
x,
y,
width,
height,
match Place::from(next.visible_in(area)) == next {
true => next.visible_in(area),
false => Rect::new(area.x, area.y, width, height),