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
Paragraphknows how wide its text is - A
Blockknows its minimum border/padding requirements - A
Listknows 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:
min <= preferred <= max.unwrap_or(∞)for both width and heightmeasure()must be pure: same input → same outputmeasure()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§
- Size
Constraints - Size constraints returned by measure operations.
Traits§
- Measurable
Widget - A widget that can report its intrinsic dimensions.