Module clipper

Module clipper 

Source
Expand description

Alternative View widget that renders only visible widgets.

It uses a GenericLayout to find the visible widgets. It only uses a Buffer big enough to render these. They may be only partially visible of course.

This helps with rendering speed and allows rendering more than u16::MAX lines.

It works in several phases:


    /// Create the layout. The layout can be stored long-term
    /// and needs to be rebuilt only if your widget layout changes.

    let clipper = Clipper::new();
    let layout_size = clipper.layout_size(l2[1], &mut state.clipper);

    if !state.clipper.valid_layout(layout_size) {
        let mut cl = GenericLayout::new();
        for i in 0..100 {
            cl.add(state.check_states[i].focus(),
                Rect::new(10, i as u16 *11, 15, 10),
                None,
                Rect::default()
            );
        }
        state.clipper.set_layout(cl);
    }

    /// The given area plus the current scroll offset define the
    /// view area. With the view area a temporary buffer is created
    /// that is big enough to fit all widgets that are at least
    /// partially visible.

    let mut clip_buf = clipper
        .into_buffer(l2[1], &mut state.clipper);

    ///
    /// The widgets are rendered to that buffer.
    ///
    for i in 0..100 {
        // refer by handle
        clip_buf.render(
            state.check_states[i].focus(),
            || {
                Checkbox::new()
                .text(format!("{:?}", i))
            },
            &mut state.check_states[i],
        );
    }

    ///
    /// The last step clips and copies the buffer to the frame buffer.
    ///

    clip_buf.finish(&mut buf, &mut state.clipper);

StatefulWidget

For this to work with StatefulWidgets they must cooperate by implementing the RelocatableState trait. With this trait the widget can clip/hide all areas that it stores in its state.

Form

There is an alternative to scrolling through long lists of widgets. With Form you can split the layout into pages. This avoids clipped widgets and allows the extra feature to stretch some widgets to fill the available vertical space.

See

example

Structs§

Clipper
This widget allows rendering to a temporary buffer and clips it to size for the final rendering.
ClipperBuffer
Second stage: render widgets to the temporary buffer.
ClipperState
Widget state.
ClipperStyle
Clipper styles.

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.