pub trait MeasurableWidget {
// Provided methods
fn measure(&self, available: Size) -> SizeConstraints { ... }
fn has_intrinsic_size(&self) -> bool { ... }
}Expand description
A widget that can report its intrinsic dimensions.
Implement this trait for widgets whose size depends on their content. Widgets that simply fill available space can use the default implementation.
§Semantics
measure(&self, available)returns the size constraints given the available spacehas_intrinsic_size()returnstrueif measure() provides meaningful constraints
§Invariants
Implementations must ensure:
- Monotonicity:
min <= preferred <= max.unwrap_or(∞) - Purity: Same inputs produce identical outputs (no side effects)
- Performance: O(content_length) worst case
§Example
ⓘ
use ftui_core::geometry::Size;
use ftui_widgets::{MeasurableWidget, SizeConstraints};
struct Icon {
glyph: char,
}
impl MeasurableWidget for Icon {
fn measure(&self, _available: Size) -> SizeConstraints {
// Icons are always 1x1 (or 2x1 for wide chars)
let mut buf = [0u8; 4];
let glyph = self.glyph.encode_utf8(&mut buf);
let width = ftui_text::grapheme_width(glyph) as u16;
SizeConstraints::exact(Size::new(width, 1))
}
fn has_intrinsic_size(&self) -> bool {
true
}
}Provided Methods§
Sourcefn measure(&self, available: Size) -> SizeConstraints
fn measure(&self, available: Size) -> SizeConstraints
Measure the widget given available space.
§Arguments
available: Maximum space the widget could occupy. Use this for:- Text wrapping calculations (wrap at available.width)
- Proportional sizing (e.g., “50% of available width”)
§Returns
SizeConstraints describing the widget’s min/preferred/max sizes.
§Default Implementation
Returns SizeConstraints::ZERO, indicating the widget fills available space.
Sourcefn has_intrinsic_size(&self) -> bool
fn has_intrinsic_size(&self) -> bool
Quick check: does this widget have content-dependent sizing?
Widgets returning false can skip measure() calls when only chrome
(borders, padding) matters. This is a performance optimization.
§Returns
true: Widget size depends on content (callmeasure())false: Widget fills available space (skipmeasure())
§Default Implementation
Returns false for backwards compatibility with existing widgets.