[][src]Trait druid::Widget

pub trait Widget<T> {
    fn paint(
        &mut self,
        paint_ctx: &mut PaintCtx,
        base_state: &BaseState,
        data: &T,
        env: &Env
    );
fn layout(
        &mut self,
        ctx: &mut LayoutCtx,
        bc: &BoxConstraints,
        data: &T,
        env: &Env
    ) -> Size;
fn event(
        &mut self,
        event: &Event,
        ctx: &mut EventCtx,
        data: &mut T,
        env: &Env
    );
fn update(
        &mut self,
        ctx: &mut UpdateCtx,
        old_data: Option<&T>,
        data: &T,
        env: &Env
    ); }

The trait implemented by all widgets.

All appearance and behavior for a widget is encapsulated in an object that implements this trait.

The trait is parametrized by a type (T) for associated data. All trait methods are provided with access to this data, and in the case of event the reference is mutable, so that events can directly update the data.

Whenever the application data changes, the framework traverses the widget hierarchy with an update method. The framework needs to know whether the data has actually changed or not, which is why T has a Data bound.

All the trait methods are provided with a corresponding context. The widget can request things and cause actions by calling methods on that context.

In addition, all trait methods are provided with an environment (Env).

Container widgets will generally not call Widget methods directly on their child widgets, but rather will own their widget wrapped in a WidgetPod, and call the corresponding method on that. The WidgetPod contains state and logic for these traversals. On the other hand, particularly light-weight containers might contain their child Widget directly (when no layout or event flow logic is needed), and in those cases will call these methods.

As a general pattern, container widgets will call the corresponding WidgetPod method on all their children. The WidgetPod applies logic to determine whether to recurse, as needed.

Required methods

fn paint(
    &mut self,
    paint_ctx: &mut PaintCtx,
    base_state: &BaseState,
    data: &T,
    env: &Env
)

Paint the widget appearance.

The widget calls methods on the render_ctx field of the paint_ctx in order to paint its appearance. paint_ctx auto derefs to render_ctx for convenience.

Container widgets can paint a background before recursing to their children, or annotations (for example, scrollbars) by painting afterwards. In addition, they can apply masks and transforms on the render context, which is especially useful for scrolling.

fn layout(
    &mut self,
    ctx: &mut LayoutCtx,
    bc: &BoxConstraints,
    data: &T,
    env: &Env
) -> Size

Compute layout.

A leaf widget should determine its size (subject to the provided constraints) and return it.

A container widget will recursively call WidgetPod::layout on its child widgets, providing each of them an appropriate box constraint, compute layout, then call set_layout_rect on each of its children. Finally, it should return the size of the container. The container can recurse in any order, which can be helpful to, for example, compute the size of non-flex widgets first, to determine the amount of space available for the flex widgets.

For efficiency, a container should only invoke layout of a child widget once, though there is nothing enforcing this.

The layout strategy is strongly inspired by Flutter.

fn event(&mut self, event: &Event, ctx: &mut EventCtx, data: &mut T, env: &Env)

Handle an event.

A number of different events (in the Event enum) are handled in this method call. A widget can handle these events in a number of ways: requesting things from the EventCtx, mutating the data, or submitting a Command.

fn update(
    &mut self,
    ctx: &mut UpdateCtx,
    old_data: Option<&T>,
    data: &T,
    env: &Env
)

Handle a change of data.

This method is called whenever the data changes. When the appearance of the widget depends on data, call invalidate so that it's scheduled for repaint.

The previous value of the data is provided in case the widget wants to compute a fine-grained delta. Before any paint operation, this method will be called with None for old_data. Thus, this method can also be used to build resources that will be retained for painting.

Loading content...

Implementations on Foreign Types

impl<T> Widget<T> for Box<dyn Widget<T>>[src]

Loading content...

Implementors

impl<T, U, L, W> Widget<T> for LensWrap<U, L, W> where
    T: Data,
    U: Data,
    L: Lens<T, U>,
    W: Widget<U>, 
[src]

impl<T: Data> Widget<T> for Align<T>[src]

impl<T: Data> Widget<T> for Button<T>[src]

impl<T: Data> Widget<T> for Flex<T>[src]

impl<T: Data> Widget<T> for Label<T>[src]

impl<T: Data> Widget<T> for Padding<T>[src]

impl<T: Data> Widget<T> for SizedBox<T>[src]

impl<T: Data, F: FnMut(&T, &Env) -> String> Widget<T> for DynLabel<T, F>[src]

impl<T: Data, W: Widget<T>> Widget<T> for Scroll<T, W>[src]

Loading content...