Skip to main content

Constraint

Enum Constraint 

Source
pub enum Constraint {
    Min(u16),
    Max(u16),
    Length(u16),
    Percentage(u16),
    Ratio(u32, u32),
    Fill(u16),
}
Expand description

A constraint that defines the size of a layout element.

Constraints are the core mechanism for defining how space should be allocated within a Layout. They can specify fixed sizes (length), proportional sizes (percentage, ratio), size limits (min, max), or proportional fill values for layout elements. Relative constraints (percentage, ratio) are calculated relative to the entire space being divided, rather than the space available after applying more fixed constraints (min, max, length).

Constraints are prioritized in the following order:

  1. Constraint::Min
  2. Constraint::Max
  3. Constraint::Length
  4. Constraint::Percentage
  5. Constraint::Ratio
  6. Constraint::Fill

§Size Calculation

  • apply - Apply the constraint to a length and return the resulting size

§Collection Creation

  • from_lengths - Create a collection of length constraints
  • from_ratios - Create a collection of ratio constraints
  • from_percentages - Create a collection of percentage constraints
  • from_maxes - Create a collection of maximum constraints
  • from_mins - Create a collection of minimum constraints
  • from_fills - Create a collection of fill constraints

§Conversion and Construction

§Examples

Constraint provides helper methods to create lists of constraints from various input formats.

use ratatui_core::layout::Constraint;

// Create a layout with specified lengths for each element
let constraints = Constraint::from_lengths([10, 20, 10]);

// Create a centered layout using ratio or percentage constraints
let constraints = Constraint::from_ratios([(1, 4), (1, 2), (1, 4)]);
let constraints = Constraint::from_percentages([25, 50, 25]);

// Create a centered layout with a minimum size constraint for specific elements
let constraints = Constraint::from_mins([0, 100, 0]);

// Create a sidebar layout specifying maximum sizes for the columns
let constraints = Constraint::from_maxes([30, 170]);

// Create a layout with fill proportional sizes for each element
let constraints = Constraint::from_fills([1, 2, 1]);

For comprehensive layout documentation and examples, see the layout module.

Variants§

§

Min(u16)

Applies a minimum size constraint to the element

The element size is set to at least the specified amount.

§Examples

[Percentage(100), Min(20)]

┌────────────────────────────┐┌──────────────────┐
│            30 px           ││       20 px      │
└────────────────────────────┘└──────────────────┘

[Percentage(100), Min(10)]

┌──────────────────────────────────────┐┌────────┐
│                 40 px                ││  10 px │
└──────────────────────────────────────┘└────────┘
§

Max(u16)

Applies a maximum size constraint to the element

The element size is set to at most the specified amount.

§Examples

[Percentage(0), Max(20)]

┌────────────────────────────┐┌──────────────────┐
│            30 px           ││       20 px      │
└────────────────────────────┘└──────────────────┘

[Percentage(0), Max(10)]

┌──────────────────────────────────────┐┌────────┐
│                 40 px                ││  10 px │
└──────────────────────────────────────┘└────────┘
§

Length(u16)

Applies a length constraint to the element

The element size is set to the specified amount.

§Examples

[Length(20), Length(20)]

┌──────────────────┐┌──────────────────┐
│       20 px      ││       20 px      │
└──────────────────┘└──────────────────┘

[Length(20), Length(30)]

┌──────────────────┐┌────────────────────────────┐
│       20 px      ││            30 px           │
└──────────────────┘└────────────────────────────┘
§

Percentage(u16)

Applies a percentage of the available space to the element

Converts the given percentage to a floating-point value and multiplies that with area. This value is rounded back to a integer as part of the layout split calculation.

Note: As this value only accepts a u16, certain percentages that cannot be represented exactly (e.g. 1/3) are not possible. You might want to use Constraint::Ratio or Constraint::Fill in such cases.

§Examples

[Percentage(75), Fill(1)]

┌────────────────────────────────────┐┌──────────┐
│                38 px               ││   12 px  │
└────────────────────────────────────┘└──────────┘

[Percentage(50), Fill(1)]

┌───────────────────────┐┌───────────────────────┐
│         25 px         ││         25 px         │
└───────────────────────┘└───────────────────────┘
§

Ratio(u32, u32)

Applies a ratio of the available space to the element

Converts the given ratio to a floating-point value and multiplies that with area. This value is rounded back to a integer as part of the layout split calculation.

§Examples

[Ratio(1, 2) ; 2]

┌───────────────────────┐┌───────────────────────┐
│         25 px         ││         25 px         │
└───────────────────────┘└───────────────────────┘

[Ratio(1, 4) ; 4]

┌───────────┐┌──────────┐┌───────────┐┌──────────┐
│   13 px   ││   12 px  ││   13 px   ││   12 px  │
└───────────┘└──────────┘└───────────┘└──────────┘
§

Fill(u16)

Applies the scaling factor proportional to all other Constraint::Fill elements to fill excess space

The element will only expand or fill into excess available space, proportionally matching other Constraint::Fill elements while satisfying all other constraints.

§Examples

[Fill(1), Fill(2), Fill(3)]

┌──────┐┌───────────────┐┌───────────────────────┐
│ 8 px ││     17 px     ││         25 px         │
└──────┘└───────────────┘└───────────────────────┘

[Fill(1), Percentage(50), Fill(1)]

┌───────────┐┌───────────────────────┐┌──────────┐
│   13 px   ││         25 px         ││   12 px  │
└───────────┘└───────────────────────┘└──────────┘

Implementations§

Source§

impl Constraint

Source

pub const fn is_min(&self) -> bool

Returns true if the enum is Constraint::Min otherwise false

Source

pub const fn is_max(&self) -> bool

Returns true if the enum is Constraint::Max otherwise false

Source

pub const fn is_length(&self) -> bool

Returns true if the enum is Constraint::Length otherwise false

Source

pub const fn is_percentage(&self) -> bool

Returns true if the enum is Constraint::Percentage otherwise false

Source

pub const fn is_ratio(&self) -> bool

Returns true if the enum is Constraint::Ratio otherwise false

Source

pub const fn is_fill(&self) -> bool

Returns true if the enum is Constraint::Fill otherwise false

Source§

impl Constraint

Source

pub fn apply(&self, length: u16) -> u16

👎Deprecated since 0.26.0: This field will be hidden in the next minor version.
Source

pub fn from_lengths<T>(lengths: T) -> Vec<Constraint>
where T: IntoIterator<Item = u16>,

Convert an iterator of lengths into a vector of constraints

§Examples
use ratatui_core::layout::{Constraint, Layout, Rect};

let constraints = Constraint::from_lengths([1, 2, 3]);
let layout = Layout::default().constraints(constraints).split(area);
Source

pub fn from_ratios<T>(ratios: T) -> Vec<Constraint>
where T: IntoIterator<Item = (u32, u32)>,

Convert an iterator of ratios into a vector of constraints

§Examples
use ratatui_core::layout::{Constraint, Layout, Rect};

let constraints = Constraint::from_ratios([(1, 4), (1, 2), (1, 4)]);
let layout = Layout::default().constraints(constraints).split(area);
Source

pub fn from_percentages<T>(percentages: T) -> Vec<Constraint>
where T: IntoIterator<Item = u16>,

Convert an iterator of percentages into a vector of constraints

§Examples
use ratatui_core::layout::{Constraint, Layout, Rect};

let constraints = Constraint::from_percentages([25, 50, 25]);
let layout = Layout::default().constraints(constraints).split(area);
Source

pub fn from_maxes<T>(maxes: T) -> Vec<Constraint>
where T: IntoIterator<Item = u16>,

Convert an iterator of maxes into a vector of constraints

§Examples
use ratatui_core::layout::{Constraint, Layout, Rect};

let constraints = Constraint::from_maxes([1, 2, 3]);
let layout = Layout::default().constraints(constraints).split(area);
Source

pub fn from_mins<T>(mins: T) -> Vec<Constraint>
where T: IntoIterator<Item = u16>,

Convert an iterator of mins into a vector of constraints

§Examples
use ratatui_core::layout::{Constraint, Layout, Rect};

let constraints = Constraint::from_mins([1, 2, 3]);
let layout = Layout::default().constraints(constraints).split(area);
Source

pub fn from_fills<T>(proportional_factors: T) -> Vec<Constraint>
where T: IntoIterator<Item = u16>,

Convert an iterator of proportional factors into a vector of constraints

§Examples
use ratatui_core::layout::{Constraint, Layout, Rect};

let constraints = Constraint::from_fills([1, 2, 3]);
let layout = Layout::default().constraints(constraints).split(area);

Trait Implementations§

Source§

impl AsRef<Constraint> for Constraint

Source§

fn as_ref(&self) -> &Constraint

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Constraint

Source§

fn clone(&self) -> Constraint

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Constraint

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Constraint

Source§

fn default() -> Constraint

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Constraint

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Constraint, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Constraint

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl From<&Constraint> for Constraint

Source§

fn from(constraint: &Constraint) -> Constraint

Converts to this type from the input type.
Source§

impl From<u16> for Constraint

Source§

fn from(length: u16) -> Constraint

Convert a u16 into a Constraint::Length

This is useful when you want to specify a fixed size for a layout, but don’t want to explicitly create a Constraint::Length yourself.

§Examples
use ratatui_core::layout::{Constraint, Direction, Layout, Rect};

let layout = Layout::new(Direction::Vertical, [1, 2, 3]).split(area);
let layout = Layout::horizontal([1, 2, 3]).split(area);
let layout = Layout::vertical([1, 2, 3]).split(area);
Source§

impl Hash for Constraint

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Constraint

Source§

fn eq(&self, other: &Constraint) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Constraint

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for Constraint

Source§

impl Eq for Constraint

Source§

impl StructuralPartialEq for Constraint

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> MaybeExt for T

Source§

fn take_from(&mut self, maybe: Option<T>)

Merge from maybe by taking.
Source§

fn clone_from(&mut self, maybe: &Option<T>)
where T: Clone,

Merge from maybe by cloning.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToCompactString for T
where T: Display,

Source§

impl<T> ToLine for T
where T: Display,

Source§

fn to_line(&self) -> Line<'_>

Converts the value to a Line.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToSpan for T
where T: Display,

Source§

fn to_span(&self) -> Span<'_>

Converts the value to a Span.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToText for T
where T: Display,

Source§

fn to_text(&self) -> Text<'_>

Converts the value to a Text.
Source§

impl<T> TransformExt for T

Source§

fn transform<Q>(self, transform: impl FnOnce(T) -> Q) -> Q

Source§

fn transform_if(self, condition: bool, transform: impl FnOnce(T) -> T) -> T

Example Read more
Source§

fn modify<Q>(self, modify: impl FnOnce(&mut T) -> Q) -> T

Source§

fn modify_if<Q>(self, condition: bool, modify: impl FnOnce(&mut T) -> Q) -> T

Example Read more
Source§

fn cmp_exch<'a, E>(&'a mut self, expected: E, new: T) -> bool
where &'a mut T: PartialEq<E>,

Example Read more
Source§

fn dbg(self) -> T
where T: Debug,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ActionExt for T
where T: Debug + Clone + PartialEq + SSS,

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> SSS for T
where T: Send + Sync + 'static,

Source§

impl<T> Selection for T