//! Core text-line and sizing primitives. //! //! `Constraint`, `Size`, and `Line` are the shared building blocks most widgets //! depend on. This module keeps text-line composition and basic widget sizing //! close together because width-aware rendering needs both. use std::fmt; use std::ops::Range; use std::vec; use super::cell::Text; use crate::widget::Widget; use super::{Buffer, Color, Filled, Label, Rect, Style}; /// Rendering constraint. #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct Constraint { /// Minimum space the widget can take. pub min: Size, /// Maximum space the widget can take. pub max: Size, } impl Default for Constraint { fn default() -> Self { Self::UNBOUNDED } } impl Constraint { /// Can satisfy any size of object. pub const UNBOUNDED: Self = Self { min: Size::MIN, max: Size::MAX, }; /// Creates a new constraint. pub fn new(min: Size, max: Size) -> Self { assert!(min.cols <= max.cols && min.rows <= max.rows); Self { min, max } } /// A constraint with only a maximum size. pub fn max(max: Size) -> Self { Self { min: Size::MIN, max, } } /// A constraint that can only be satisfied by a single column size. /// The rows are unconstrained. pub fn tight(cols: usize) -> Self { Self { min: Size::new(cols, 1), max: Size::new(cols, usize::MAX), } } /// Returns a finite size that satisfies this constraint. pub fn bounded_size(self) -> Size { Size::new( bounded(self.min.cols, self.max.cols), bounded(self.min.rows, self.max.rows), ) .constrain(self) } } /// Chooses a finite bound from a minimum and maximum constraint. fn bounded(min: usize, max: usize) -> usize { if max == usize::MAX { min } else { max } } /// A line of text that has styling and can be displayed. #[derive(Clone, Default, Debug)] pub struct Line { items: Vec