Enum ratatui::layout::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 can be used to specify a fixed size, a percentage of the available space, a ratio of the available space, a minimum or maximum size or a fill proportional value for a layout element.

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

§Examples

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

// 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]);

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.

§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<Self>
where T: IntoIterator<Item = u16>,

Convert an iterator of lengths into a vector of constraints

§Examples
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<Self>
where T: IntoIterator<Item = (u32, u32)>,

Convert an iterator of ratios into a vector of constraints

§Examples
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<Self>
where T: IntoIterator<Item = u16>,

Convert an iterator of percentages into a vector of constraints

§Examples
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<Self>
where T: IntoIterator<Item = u16>,

Convert an iterator of maxes into a vector of constraints

§Examples
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<Self>
where T: IntoIterator<Item = u16>,

Convert an iterator of mins into a vector of constraints

§Examples
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<Self>
where T: IntoIterator<Item = u16>,

Convert an iterator of proportional factors into a vector of constraints

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

Trait Implementations§

source§

impl AsRef<Constraint> for Constraint

source§

fn as_ref(&self) -> &Self

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 copy 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

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

impl Default for Constraint

source§

fn default() -> Self

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

impl Display for Constraint

source§

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

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

impl From<&Constraint> for Constraint

source§

fn from(constraint: &Self) -> Self

Converts to this type from the input type.
source§

impl From<u16> for Constraint

source§

fn from(length: u16) -> Self

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
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: Hasher>(&self, state: &mut __H)

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

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

Checks if this value is equivalent to the given key. Read more
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> Same for T

§

type Output = T

Should always be Self
source§

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

source§

fn to_compact_string(&self) -> CompactString

Converts the given value to a CompactString. Read more
source§

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

§

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

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

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

§

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>,

§

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.