Module form

Module form 

Source
Expand description

Render widgets based on a GenericLayout.

This is useful, if you have more than 3 input widgets and their accompanying labels in a row. See LayoutForm for details on the layout.

If the layout is split into multiple pages this also renders the page-navigation and handles scrolling through the pages.

use rat_widget::form::{Form, FormState};

// create + configure the form.
let form = Form::new()
    .block(Block::bordered());

let layout_size = form.layout_size(area);
if !state.form.valid_layout(layout_size) {
    // define the layout
    let mut form_layout = LayoutForm::new()
            .spacing(1)
            .flex(Flex::Legacy)
            .line_spacing(1)
            .min_label(10);

    use rat_widget::layout::{FormLabel as L, FormWidget as W};

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

    // calculate the layout and set it.
    state.form.set_layout(form_layout.build_paged(area.as_size()));
 }

 // create a FormBuffer from the parameters that will render
 // the individual widgets.
 let mut form = form
    .into_buffer(area, buf, &mut state.form);

 form.render(state.text1.id(),
    || TextInput::new(),
    &mut state.text1
 );
 form.render(state.text2.id(),
    || TextInput::new(),
    &mut state.text2
 );
 form.render(state.text3.id(),
    || TextInput::new(),
    &mut state.text3
 );

Structs§

Form
Renders other widgets using a GenericLayout. It doesn’t scroll, instead it uses pages.
FormBuffer
Second stage of form rendering.
FormState
Widget state.
FormStyle
All styles for a form.

Functions§

handle_events
Handle all events. Text events are only processed if focus is true. Mouse events are processed if they are in range.
handle_mouse_events
Handle only mouse-events.