Skip to main content

MeasurableWidget

Trait MeasurableWidget 

Source
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 space
  • has_intrinsic_size() returns true if measure() provides meaningful constraints

§Invariants

Implementations must ensure:

  1. Monotonicity: min <= preferred <= max.unwrap_or(∞)
  2. Purity: Same inputs produce identical outputs (no side effects)
  3. 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§

Source

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.

Source

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 (call measure())
  • false: Widget fills available space (skip measure())
§Default Implementation

Returns false for backwards compatibility with existing widgets.

Implementors§