Trait druid::widget::Controller

source ·
pub trait Controller<T, W: Widget<T>> {
    fn event(
        &mut self,
        child: &mut W,
        ctx: &mut EventCtx<'_, '_>,
        event: &Event,
        data: &mut T,
        env: &Env
    ) { ... } fn lifecycle(
        &mut self,
        child: &mut W,
        ctx: &mut LifeCycleCtx<'_, '_>,
        event: &LifeCycle,
        data: &T,
        env: &Env
    ) { ... } fn update(
        &mut self,
        child: &mut W,
        ctx: &mut UpdateCtx<'_, '_>,
        old_data: &T,
        data: &T,
        env: &Env
    ) { ... } }
Expand description

A trait for types that modify behaviour of a child widget.

A Controller is a type that manages a child widget, overriding or customizing its event handling or update behaviour.

A controller can only handle events and update; it cannot effect layout or paint.

Controller is a convenience; anything it can do could also be done by creating a custom [Widget] that owned a child. This is somewhat cumbersome, however, especially when you only want to intercept or modify one or two events.

The methods on Controller are identical to the methods on [Widget], except that they are also passed the controller’s child. The controller is responsible for explicitly forwarding calls on to the child as needed.

A Controller is used with a ControllerHost, which manages the relationship between it and its child; although in general you would use the WidgetExt::controller method instead of instantiating a host directly.

Examples

A TextBox that takes focus on launch:

struct TakeFocus;

impl<T, W: Widget<T>> Controller<T, W> for TakeFocus {
    fn event(&mut self, child: &mut W, ctx: &mut EventCtx, event: &Event, data: &mut T, env: &Env) {
        if let Event::WindowConnected = event {
            ctx.request_focus();
        }
        child.event(ctx, event, data, env)
    }
}

Provided Methods§

Analogous to [Widget::event].

Analogous to [Widget::lifecycle].

Analogous to [Widget::update].

Implementors§