Skip to main content

Module measurable

Module measurable 

Source
Expand description

Intrinsic sizing support for content-aware layout. Intrinsic sizing support for widgets.

This module provides the MeasurableWidget trait for widgets that can report their intrinsic dimensions, enabling content-aware layout like Constraint::FitContent.

§Overview

Not all widgets need intrinsic sizing—many simply fill whatever space they’re given. But some widgets have natural dimensions based on their content:

  • A Paragraph knows how wide its text is
  • A Block knows its minimum border/padding requirements
  • A List knows how many items it contains

§Size Constraints

SizeConstraints captures the full sizing semantics:

  • min: Minimum size below which the widget clips or becomes unusable
  • preferred: Size that best displays the content
  • max: Maximum useful size (beyond this, extra space is wasted)

§Example

use ftui_core::geometry::Size;
use ftui_widgets::{MeasurableWidget, SizeConstraints, Widget};

struct Label {
    text: String,
}

impl MeasurableWidget for Label {
    fn measure(&self, _available: Size) -> SizeConstraints {
        let width = ftui_text::display_width(self.text.as_str()) as u16;
        SizeConstraints {
            min: Size::new(1, 1),           // At least show something
            preferred: Size::new(width, 1), // Ideal: full text on one line
            max: Some(Size::new(width, 1)), // No benefit from extra space
        }
    }

    fn has_intrinsic_size(&self) -> bool {
        true // This widget's size depends on content
    }
}

§Invariants

Implementations must maintain these invariants:

  1. min <= preferred <= max.unwrap_or(∞) for both width and height
  2. measure() must be pure: same input → same output
  3. measure() should be O(content_length) worst case

§Backwards Compatibility

Widgets that don’t implement MeasurableWidget explicitly get a default implementation that returns SizeConstraints::ZERO and has_intrinsic_size() = false, indicating they fill available space.

Structs§

SizeConstraints
Size constraints returned by measure operations.

Traits§

MeasurableWidget
A widget that can report its intrinsic dimensions.