Struct LayoutForm

Source
pub struct LayoutForm<W>
where W: Eq + Hash + Clone + Debug,
{ /* private fields */ }
Expand description

Create a layout with a single column of label+widget.

There are a number of possible constraints that influence the exact layout: FormLabel and FormWidget.

  • This layout can page break the form, if there is not enough space on one page. This can be used with SinglePager and friends.

  • Or it can generate an endless layout that will be used with scrolling logic like Clipper.

  • There is currently no functionality to shrink-fit the layout to a given page size.

The widgets can be grouped together and a Block can be set to highlight this grouping. Groups can cascade. Groups will be correctly broken by the page break logic. There is no special handling for orphans and widows.

Other features:

  • Spacing/Line spacing.
  • Supports Flex.
  • Manual page breaks.

if state.layout.area_changed(area) {
    let mut form_layout = LayoutForm::new()
            .spacing(1)
            .flex(Flex::Legacy)
            .line_spacing(1)
            .min_label(10);

    form_layout.widget(
        state.text1.focus(),
        FormLabel::Str("Text 1"),
        // single row
        FormWidget::Width(22)
    );
    form_layout.widget(
        state.text2.focus(),
        FormLabel::Str("Text 2"),
        // stretch to the form-width, preferred with 15, 1 row high.
        FormWidget::StretchX(15, 1)
    );
    form_layout.widget(
        state.text3.focus(),
        FormLabel::Str("Text 3"),
        // stretch to the form-width and fill vertically.
        // preferred width is 15 3 rows high.
        FormWidget::StretchXY(15, 3)
    );

    // calculate the layout and place it.
    state.layout = form_layout.paged(area.as_size(), Padding::default())
        .place(area.as_position());
 }

 let layout = &state.layout;

 // this is not really the intended use, but it works.
 // in reality, you would use [Clipper], [SinglePager],
 // [DualPager] or [Form].

 let lbl1= layout.label_for(state.text1.focus());
 Span::from(layout.label_str_for(state.text1.focus()))
    .render(lbl1, buf);
 let txt1 = layout.widget_for(state.text1.focus());
 TextInput::new()
    .render(txt1, buf, &mut state.text1);

 let lbl2 = layout.label_for(state.text2.focus());
 Span::from(layout.label_str_for(state.text2.focus()))
    .render(lbl2, buf);
 let txt2 = layout.widget_for(state.text2.focus());
 TextInput::new()
    .render(txt2, buf, &mut state.text2);

 let lbl3 = layout.label_for(state.text3.focus());
 Span::from(layout.label_str_for(state.text3.focus()))
    .render(lbl3, buf);
 let txt3 = layout.widget_for(state.text3.focus());
 TextInput::new()
    .render(txt3, buf, &mut state.text3);

Implementations§

Source§

impl<W> LayoutForm<W>
where W: Eq + Hash + Clone + Debug,

Source

pub fn new() -> Self

Source

pub fn spacing(self, spacing: u16) -> Self

Spacing between label and widget.

Source

pub fn line_spacing(self, spacing: u16) -> Self

Empty lines between widgets.

Source

pub fn mirror_odd_border(self) -> Self

Mirror the border given to layout between even and odd pages. The layout starts with page 0 which is even.

Source

pub fn flex(self, flex: Flex) -> Self

Flex.

Source

pub fn min_label(self, width: u16) -> Self

Set a reference label width

Source

pub fn min_widget(self, width: u16) -> Self

Set a reference widget width

Source

pub fn start(&mut self, block: Option<Block<'static>>) -> BlockTag

Start a group/block.

This will create a block that covers all widgets added before calling end().

Groups/blocks can be stacked, but they cannot interleave. An inner group/block must be closed before an outer one.

Source

pub fn end(&mut self, tag: BlockTag)

Closes the group/block with the given tag.

Panic Groups must be closed in reverse start order, otherwise this function will panic. It will also panic if there is no open group for the given tag.

Source

pub fn widget(&mut self, key: W, label: FormLabel, widget: FormWidget)

Add label + widget constraint. Key must be a unique identifier.

Source

pub fn page_break(&mut self)

Add a manual page break after the last widget.

This will panic if the widget list is empty.

Source

pub fn endless(self, width: u16, border: Padding) -> GenericLayout<W>

Calculate a layout without page-breaks using the given layout-width and padding.

Source

pub fn paged(self, page: Size, border: Padding) -> GenericLayout<W>

Calculate the layout for the given page size and padding.

Trait Implementations§

Source§

impl<W> Debug for LayoutForm<W>
where W: Eq + Hash + Clone + Debug + Debug,

Source§

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

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

impl<W> Default for LayoutForm<W>
where W: Eq + Clone + Hash + Debug,

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<W> Freeze for LayoutForm<W>

§

impl<W> RefUnwindSafe for LayoutForm<W>
where W: RefUnwindSafe,

§

impl<W> Send for LayoutForm<W>
where W: Send,

§

impl<W> Sync for LayoutForm<W>
where W: Sync,

§

impl<W> Unpin for LayoutForm<W>
where W: Unpin,

§

impl<W> UnwindSafe for LayoutForm<W>
where W: UnwindSafe,

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<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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.