Fusuma

Components / Your own widgets

widget

A widget of your own with props: widget(&METER, 10).

Try it

This is the Vector page of the gallery, running in your browser: widget is on it, and it is the real widget. Press Run, then use it.

Live: Vector, where widget is usedOpen full size

A widget of your own with props: widget(&METER, 10).

The widget is a type that implements Widget, kept in a static. Its props are read again on every view (nothing happens when they are what it has), its own state stays in the tree, and what it reports becomes the message of .on_press(..), .on_change(..) or .on_toggle(..).

Signature

pub const fn widget<W: Widget, M>(widget: &'static W, props: W::Props) -> Hosted<W, M>

Methods

  • .style(style: Style) -> Self — The size and look of its node; without it the widget's own measure decides.
  • .width(width: Length) -> Self — A width.
  • .height(height: Length) -> Self — A height.
  • .enabled(enabled: bool) -> Self — Whether it can be used (it is by default).
  • .on_press(message: M) -> Self — Sends message when the widget says it was pressed or chosen.
  • .on_change(f: fn(u16) -> M) -> Self — Sends a message made from the value the widget reports when it changes.
  • .on_toggle(f: fn(bool) -> M) -> Self — Sends a message made from the state the widget reports when it is switched.

Example

use fusuma::ui::{Canvas, MeasureCx, PaintCx, Widget};
use fusuma::render::{Rect, Size};
use fusuma::{column, widget};

/// A bar that is filled to a number of tenths.
struct Meter;
static METER: Meter = Meter;

impl Widget for Meter {
    type Props = u16;
    type State = ();

    fn measure(&self, _: &MeasureCx<'_>, _: &u16, (): &()) -> Size {
        Size::new(10, 1)
    }

    fn paint(&self, cx: &PaintCx<'_, u16, ()>, canvas: &mut dyn Canvas) {
        let size = canvas.size();
        canvas.fill_rect(Rect::new(0, 0, size.w * u32::from(*cx.props) / 10, size.h), 0, cx.theme.colors.primary);
    }
}

let view = column((widget::<Meter, ()>(&METER, 7),));
let built = fusuma::doc::built::<(), _>(&view);
assert_eq!(built.nodes(), 2);

All components · Source