Trait kas::Widget

source ·
pub trait Widget: Layout {
    type Data;

    // Provided methods
    fn as_node<'a>(&'a mut self, data: &'a Self::Data) -> Node<'a> { ... }
    fn for_child_node(
        &mut self,
        data: &Self::Data,
        index: usize,
        closure: Box<dyn FnOnce(Node<'_>) + '_>
    ) { ... }
}
Expand description

The Widget trait

The primary widget trait covers event handling over super trait Layout which governs layout, drawing, child enumeration and identification. Most methods of Widget are hidden and only for use within the Kas library.

Widget is dyn-safe given a type parameter, e.g. dyn Widget<Data = ()>. Layout is dyn-safe without a type parameter. Node is a dyn-safe abstraction over a &dyn Widget<Data = T> plus a &T data parameter.

Widget lifecycle

  1. The widget is configured (Events::configure) and immediately updated (Events::update).
  2. The widget has its size-requirements checked by calling Layout::size_rules for each axis.
  3. Layout::set_rect is called to position elements. This may use data cached by size_rules.
  4. The widget is updated again after any data change (see ConfigCx::update).
  5. The widget is ready for event-handling and drawing (Events::handle_event, Layout::find_id, Layout::draw).

Widgets are responsible for ensuring that their children may observe this lifecycle. Usually this simply involves inclusion of the child in layout operations. Steps of the lifecycle may be postponed until a widget becomes visible.

Implementing Widget

To implement a widget, use the #widget macro within an impl_scope. This is the only supported method of implementing Widget.

Explicit (partial) implementations of Widget, Layout and Events are optional. The #widget macro completes implementations.

Synopsis:

impl_scope! {
    #[widget {
        // macro properties (all optional)
        Data = T;
        layout = self.foo;
    }]
    struct MyWidget {
        core: widget_core!(),
        #[widget] foo: impl Widget<Data = T> = make_foo(),
        // ...
    }

    // Optional implementations:
    impl Layout for Self { /* ... */ }
    impl Events for Self { /* ... */ }
    impl Self { /* ... */ }
}

Details may be categorised as follows:

For examples, check the source code of widgets in the widgets library or examples apps. (Check that the code uses the same Kas version since the widget traits are not yet stable.)

Required Associated Types§

source

type Data

Input data type

Widget expects data of this type to be provided by reference when calling any event-handling operation on this widget.

This type may be specified using a #widget macro property in case this trait is not explicitly implemented.

Provided Methods§

source

fn as_node<'a>(&'a mut self, data: &'a Self::Data) -> Node<'a>

Erase type

This method is implemented by the #[widget] macro.

source

fn for_child_node( &mut self, data: &Self::Data, index: usize, closure: Box<dyn FnOnce(Node<'_>) + '_> )

Call closure on child with given index, if index < self.num_children().

Widgets with no children or using the #[widget] attribute on fields do not need to implement this. Widgets with an explicit implementation of Layout::num_children also need to implement this.

It is recommended to use the methods on Node instead of calling this method.

Implementations on Foreign Types§

source§

impl<T> Widget for &mut T
where T: Widget + ?Sized,

§

type Data = <T as Widget>::Data

source§

fn as_node<'a>(&'a mut self, data: &'a <&mut T as Widget>::Data) -> Node<'a>

source§

fn for_child_node( &mut self, data: &<&mut T as Widget>::Data, index: usize, closure: Box<dyn FnOnce(Node<'_>) + '_> )

source§

impl<T> Widget for Box<T>
where T: Widget + ?Sized,

§

type Data = <T as Widget>::Data

source§

fn as_node<'a>(&'a mut self, data: &'a <Box<T> as Widget>::Data) -> Node<'a>

source§

fn for_child_node( &mut self, data: &<Box<T> as Widget>::Data, index: usize, closure: Box<dyn FnOnce(Node<'_>) + '_> )

Implementors§

source§

impl Widget for Svg

§

type Data = ()

source§

impl Widget for TextEdit

§

type Data = ()

source§

impl Widget for AccessLabel

§

type Data = ()

source§

impl Widget for EventConfig

§

type Data = ()

source§

impl Widget for Filler

§

type Data = ()

source§

impl Widget for GripPart

§

type Data = ()

source§

impl Widget for Image

§

type Data = ()

source§

impl Widget for Mark

§

type Data = ()

source§

impl Widget for Tab

§

type Data = ()

source§

impl<A> Widget for MenuToggle<A>

§

type Data = A

source§

impl<A> Widget for CheckBox<A>

§

type Data = A

source§

impl<A> Widget for CheckButton<A>

§

type Data = A

source§

impl<A> Widget for RadioBox<A>

§

type Data = A

source§

impl<A> Widget for RadioButton<A>

§

type Data = A

source§

impl<A> Widget for Separator<A>

§

type Data = A

source§

impl<A, D> Widget for ProgressBar<A, D>
where D: Directional,

§

type Data = A

source§

impl<A, F, W> Widget for FilterList<A, F, W>
where A: ListData + 'static, F: Filter<<A as SharedData>::Item, Value = String>, W: Widget<Data = UnsafeFilteredList<A>>,

§

type Data = A

source§

impl<A, F, W, G> Widget for FilterBoxList<A, F, W, G>
where A: ListData + 'static, F: Filter<<A as SharedData>::Item, Value = String>, W: Widget<Data = UnsafeFilteredList<A>>, G: EditGuard<Data = ()>,

§

type Data = A

source§

impl<A, T> Widget for ScrollText<A, T>
where T: Default + FormattableText + 'static,

§

type Data = A

source§

impl<A, T> Widget for Spinner<A, T>
where T: SpinnerValue,

§

type Data = A

source§

impl<A, T> Widget for Text<A, T>
where T: Default + FormattableText + 'static,

§

type Data = A

source§

impl<A, T, D> Widget for Slider<A, T, D>
where T: SliderValue, D: Directional,

§

type Data = A

source§

impl<A, V> Widget for MatrixView<A, V>
where A: MatrixData, V: Driver<<A as SharedData>::Item, A>,

§

type Data = A

source§

impl<A, V> Widget for ComboBox<A, V>
where V: Clone + Debug + Eq + 'static,

§

type Data = A

source§

impl<A, V, D> Widget for ListView<A, V, D>
where A: ListData, V: Driver<<A as SharedData>::Item, A>, D: Directional,

§

type Data = A

source§

impl<A, W> Widget for MapAny<A, W>
where W: Widget<Data = ()>,

§

type Data = A

source§

impl<A, W, F> Widget for Map<A, W, F>
where W: Widget, F: for<'a> Fn(&'a A) -> &'a <W as Widget>::Data,

§

type Data = A

source§

impl<A, W, S> Widget for Adapt<A, W, S>
where W: Widget<Data = S>, S: Debug,

§

type Data = A

source§

impl<D> Widget for ScrollBar<D>
where D: Directional,

§

type Data = ()

source§

impl<Data> Widget for Window<Data>
where Data: 'static,

§

type Data = Data

source§

impl<Data> Widget for SubMenu<Data>

§

type Data = Data

source§

impl<Data, D> Widget for MenuBar<Data, D>
where D: Directional,

§

type Data = Data

source§

impl<G> Widget for EditBox<G>
where G: EditGuard,

§

type Data = <G as EditGuard>::Data

source§

impl<G> Widget for EditField<G>
where G: EditGuard,

§

type Data = <G as EditGuard>::Data

source§

impl<M> Widget for MenuEntry<M>
where M: Clone + Debug + 'static,

§

type Data = ()

source§

impl<M> Widget for MarkButton<M>
where M: Clone + Debug + 'static,

§

type Data = ()

source§

impl<P> Widget for Canvas<P>
where P: CanvasProgram,

§

type Data = ()

source§

impl<T> Widget for MessageBox<T>
where T: FormattableText + 'static,

§

type Data = ()

source§

impl<T> Widget for Label<T>
where T: FormattableText + 'static,

§

type Data = ()

source§

impl<T> Widget for ScrollLabel<T>
where T: FormattableText + 'static,

§

type Data = ()

source§

impl<W> Widget for Popup<W>
where W: Widget,

§

type Data = <W as Widget>::Data

source§

impl<W> Widget for AdaptEvents<W>
where W: Widget,

§

type Data = <W as Widget>::Data

source§

impl<W> Widget for Reserve<W>
where W: Widget,

§

type Data = <W as Widget>::Data

source§

impl<W> Widget for Button<W>
where W: Widget,

§

type Data = <W as Widget>::Data

source§

impl<W> Widget for Frame<W>
where W: Widget,

§

type Data = <W as Widget>::Data

source§

impl<W> Widget for Grid<W>
where W: Widget,

§

type Data = <W as Widget>::Data

source§

impl<W> Widget for NavFrame<W>
where W: Widget,

§

type Data = <W as Widget>::Data

source§

impl<W> Widget for ScrollBarRegion<W>
where W: Widget,

source§

impl<W> Widget for ScrollBars<W>
where W: Scrollable + Widget,

§

type Data = <W as Widget>::Data

source§

impl<W> Widget for ScrollRegion<W>
where W: Widget,

§

type Data = <W as Widget>::Data

source§

impl<W> Widget for Stack<W>
where W: Widget,

§

type Data = <W as Widget>::Data

source§

impl<W> Widget for TabStack<W>
where W: Widget,

§

type Data = <W as Widget>::Data

source§

impl<W, D> Widget for WithLabel<W, D>
where W: Widget, D: Directional,

§

type Data = <W as Widget>::Data

source§

impl<W, D> Widget for List<W, D>
where W: Widget, D: Directional,

§

type Data = <W as Widget>::Data

source§

impl<W, D> Widget for Splitter<W, D>
where W: Widget, D: Directional,

§

type Data = <W as Widget>::Data