Lines
100 %
Functions
96.97 %
Branches
//! The TurboVision window chrome: the frame, its title, number and icons. The `*_area` fns
//! give the cells the widget draws, so a hit test and the drawing cannot disagree.
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::Style;
use ratatui::text::Line;
use ratatui::widgets::{Block, BorderType, Widget};
use crate::{Role, Theme};
const ICON_WIDTH: u16 = 3;
const GROW_WIDTH: u16 = 2;
const CLOSE_FROM_LEFT: u16 = 2;
const ZOOM_FROM_RIGHT: u16 = 5;
const NUMBER_FROM_RIGHT: u16 = 7;
/// The columns of the top row the title leaves free for the icons and the number.
const TITLE_MARGIN: u16 = 10;
const ICONS_MIN_WIDTH: u16 = 12;
const LABELS_MIN_WIDTH: u16 = TITLE_MARGIN + 1;
/// Splits a window title: the name before it may be shortened, the tail after it never is.
const TITLE_TAIL: char = '\u{1f}';
/// The title of a window made of `name` and a `tail`, such as its ordinal and its filter.
#[must_use]
pub fn joined_title(name: &str, tail: &str) -> String {
match tail.is_empty() {
true => name.to_string(),
false => format!("{name}{TITLE_TAIL}{tail}"),
}
/// How a frame is drawn.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum FrameMode {
/// Single-line, no icons.
#[default]
Passive,
/// The front window: double-line with icons.
Active,
/// A window being moved or sized: single-line, all in the icon colour, with its icons.
Dragging,
/// What a window can do besides move and close, as TV's `wfGrow`, `wfZoom` and `ofTileable`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Caps {
pub grow: bool,
pub zoom: bool,
pub tile: bool,
impl Caps {
pub const ALL: Self = Self {
grow: true,
zoom: true,
tile: true,
};
/// Moves and closes only, and stays out of tile and cascade.
pub const FIXED: Self = Self {
grow: false,
zoom: false,
tile: false,
impl Default for Caps {
fn default() -> Self {
Self::ALL
/// What the frame shows of a window besides its title.
pub struct FrameState {
pub mode: FrameMode,
pub caps: Caps,
pub zoomed: bool,
/// The window number, a digit 1..=9.
pub number: Option<u8>,
/// The area inside the frame.
pub fn inner(area: Rect) -> Rect {
Block::bordered().inner(area)
fn has_icons(area: Rect) -> bool {
area.width >= ICONS_MIN_WIDTH && area.height >= 2
fn has_labels(area: Rect) -> bool {
area.width >= LABELS_MIN_WIDTH && area.height >= 2
impl FrameState {
/// The role of the frame lines, which the title takes unless the window is active.
pub(crate) fn frame_role(self) -> Role {
match self.mode {
FrameMode::Active => Role::PanelFrame,
FrameMode::Passive => Role::PanelFramePassive,
FrameMode::Dragging => Role::FrameIcon,
fn shows_icons(area: Rect, state: FrameState) -> bool {
state.mode != FrameMode::Passive && has_icons(area)
/// The `[â– ]` close icon; `None` where none is drawn: a passive or too small window.
pub fn close_area(area: Rect, state: FrameState) -> Option<Rect> {
let x = area.x.saturating_add(CLOSE_FROM_LEFT);
shows_icons(area, state).then(|| Rect::new(x, area.y, ICON_WIDTH, 1))
/// The `[↑]` zoom icon; `None` where none is drawn.
pub fn zoom_area(area: Rect, state: FrameState) -> Option<Rect> {
let x = area.right().saturating_sub(ZOOM_FROM_RIGHT);
(state.caps.zoom && shows_icons(area, state)).then(|| Rect::new(x, area.y, ICON_WIDTH, 1))
/// The `─┘` resize mark in the bottom-right corner; `None` where none is drawn.
pub fn grow_area(area: Rect, state: FrameState) -> Option<Rect> {
let x = area.right().saturating_sub(GROW_WIDTH);
let y = area.bottom().saturating_sub(1);
(state.caps.grow && shows_icons(area, state)).then(|| Rect::new(x, y, GROW_WIDTH, 1))
/// The cells of the title text, centred in the top row; `None` when no room is left for it.
pub fn title_area(area: Rect, title: &str) -> Option<Rect> {
if !has_labels(area) {
return None;
let plain = title.replace(TITLE_TAIL, "");
let wanted = u16::try_from(width(&plain)).unwrap_or(u16::MAX);
let len = wanted.min(area.width - TITLE_MARGIN);
let x = area.x.saturating_add((area.width - len) / 2);
(len > 0).then(|| Rect::new(x, area.y, len, 1))
/// `title` as `cells` cells show it. A title with a tail keeps the tail whole, where the ordinal
/// and the filter that tell windows apart are, and shortens the name before it with `…`; a
/// tail wider than the title area is cut from the right. A title without one is cut from the
/// right, as TV does, by the caller's clipping.
fn fitted(title: &str, cells: usize) -> String {
let (name, tail) = title.split_once(TITLE_TAIL).unwrap_or((title, ""));
let (name_width, tail_width) = (width(name), width(tail));
if tail.is_empty() || name_width + tail_width <= cells {
return format!("{name}{tail}");
if tail_width >= cells {
return tail.trim_start().to_string();
let kept = head(name, cells - tail_width - 1);
format!("{kept}\u{2026}{tail}")
fn width(text: &str) -> usize {
Line::from(text).width()
/// The longest start of `text` that fits `cells`.
fn head(text: &str, cells: usize) -> String {
let mut used = 0;
let fits = |c: &char| {
used += width(&c.to_string());
used <= cells
text.chars().take_while(fits).collect()
fn number_area(area: Rect) -> Option<Rect> {
let x = area.right().saturating_sub(NUMBER_FROM_RIGHT);
has_labels(area).then(|| Rect::new(x, area.y, 1, 1))
/// A window's frame and fill. Icons, number and title are dropped as the rect shrinks.
/// A caller that joins things to the frame lines draws them between the two render steps.
pub struct Chrome<'a> {
theme: &'a Theme,
title: &'a str,
state: FrameState,
/// An icon of three cells: the brackets in the frame style and the glyph in the icon style.
pub(crate) fn draw_icon(buf: &mut Buffer, at: Rect, glyph: &str, [frame, icon]: [Style; 2]) {
buf.set_string(at.x, at.y, "[", frame);
buf.set_string(at.x.saturating_add(1), at.y, glyph, icon);
buf.set_string(at.x.saturating_add(2), at.y, "]", frame);
impl<'a> Chrome<'a> {
pub fn new(theme: &'a Theme, title: &'a str, state: FrameState) -> Self {
Self {
theme,
title,
state,
fn frame_style(&self) -> Style {
self.theme.style(self.state.frame_role())
fn title_style(&self) -> Style {
match self.state.mode {
FrameMode::Active => self.theme.style(Role::PanelTitle),
_ => self.frame_style(),
fn render_title(&self, area: Rect, buf: &mut Buffer) {
let Some(text) = title_area(area, self.title) else {
return;
let style = self.title_style();
buf.set_string(text.x.saturating_sub(1), text.y, " ", style);
buf.set_string(text.right(), text.y, " ", style);
buf.set_string(text.x, text.y, " ".repeat(usize::from(text.width)), style);
let shown = fitted(self.title, usize::from(text.width));
buf.set_stringn(text.x, text.y, shown, usize::from(text.width), style);
fn render_number(&self, area: Rect, buf: &mut Buffer) {
if let Some(digit) = self
.state
.number
.and_then(|n| char::from_digit(n.into(), 10))
&& let Some(cell) = number_area(area)
{
buf.set_string(cell.x, cell.y, digit.to_string(), self.frame_style());
fn render_icon(&self, area: Rect, glyph: &str, buf: &mut Buffer) {
let icon = self.theme.style(Role::FrameIcon);
draw_icon(buf, area, glyph, [self.frame_style(), icon]);
fn render_icons(&self, area: Rect, buf: &mut Buffer) {
if let Some(close) = close_area(area, self.state) {
self.render_icon(close, "\u{25A0}", buf);
if let Some(zoom) = zoom_area(area, self.state) {
let glyph = if self.state.zoomed {
"\u{2195}"
} else {
"\u{2191}"
self.render_icon(zoom, glyph, buf);
if let Some(grow) = grow_area(area, self.state) {
buf.set_string(grow.x, grow.y, "\u{2500}\u{2518}", icon);
impl Chrome<'_> {
/// The frame lines and the fill, below everything else the chrome draws.
pub fn render_frame(&self, area: Rect, buf: &mut Buffer) {
let area = area.intersection(buf.area);
let border_type = match self.state.mode {
FrameMode::Active => BorderType::Double,
_ => BorderType::Plain,
Block::bordered()
.border_type(border_type)
.border_style(self.frame_style())
.style(self.theme.style(Role::Panel))
.render(area, buf);
/// The number, title and icons, over the frame lines and anything joined to them.
pub fn render_labels(&self, area: Rect, buf: &mut Buffer) {
self.render_number(area, buf);
self.render_title(area, buf);
self.render_icons(area, buf);
impl Widget for Chrome<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
self.render_frame(area, buf);
self.render_labels(area, buf);
#[cfg(test)]
mod tests;