Skip to main content

StatefulWidget

Trait StatefulWidget 

Source
pub trait StatefulWidget {
    type State;

    // Required method
    fn render(&self, surface: &mut Surface<'_>, state: &mut Self::State);
}
Expand description

Like Widget, but for widgets that read (and may update) externally owned state (a selection index, a scroll offset) that outlives a single render call. See crate::ListState.

§Examples

use retroglyph_core::color::Style;
use retroglyph_core::grid::{Grid, Rect};
use retroglyph_ui::{Surface, StatefulWidget};

struct Counter;

impl StatefulWidget for Counter {
    type State = u32;

    fn render(&self, surface: &mut Surface<'_>, state: &mut Self::State) {
        *state += 1;
        surface.put((0, 0), 'x', Style::new());
    }
}

let area = Rect::new(0, 0, 4, 1);
let mut grid = Grid::new(4, 1);
let mut renders = 0;
Counter.render(&mut Surface::new(&mut grid, area, 0), &mut renders);
assert_eq!(renders, 1);

Required Associated Types§

Source

type State

The externally owned state this widget reads and/or updates while rendering.

Required Methods§

Source

fn render(&self, surface: &mut Surface<'_>, state: &mut Self::State)

Draw this widget into surface, filling surface.area(), using and/or updating state.

See Widget::render’s doc for why placement should come from Surface::width/Surface::height/surface.area().at_origin(), not surface.area()’s own left/top.

Implementors§