Struct druid::WidgetPod

source ·
pub struct WidgetPod<T, W> { /* private fields */ }
Expand description

A container for one widget in the hierarchy.

Generally, container widgets don’t contain other widgets directly, but rather contain a WidgetPod, which has additional state needed for layout and for the widget to participate in event flow.

WidgetPod will translate internal Druid events to regular events, synthesize additional events of interest, and stop propagation when it makes sense.

This struct also contains the previous data for a widget, which is essential for the update method, both to decide when the update needs to propagate, and to provide the previous data so that a widget can process a diff between the old value and the new.

Implementations§

source§

impl<T, W: Widget<T>> WidgetPod<T, W>

source

pub fn new(inner: W) -> WidgetPod<T, W>

Create a new widget pod.

In a widget hierarchy, each widget is wrapped in a WidgetPod so it can participate in layout and event flow. The process of adding a child widget to a container should call this method.

Examples found in repository?
examples/timer.rs (line 124)
121
122
123
124
125
126
127
128
129
130
131
132
133
134
pub fn main() {
    let window = WindowDesc::new(TimerWidget {
        timer_id: TimerToken::INVALID,
        simple_box: WidgetPod::new(SimpleBox),
        pos: Point::ZERO,
    })
    .with_min_size((200., 200.))
    .title(LocalizedString::new("timer-demo-window-title").with_placeholder("Look at it go!"));

    AppLauncher::with_window(window)
        .log_to_console()
        .launch(0u32)
        .expect("launch failed");
}
More examples
Hide additional examples
examples/input_region.rs (line 80)
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    pub fn new() -> Self {
        let info_label = Label::new(INFO_TEXT)
            .with_line_break_mode(LineBreaking::WordWrap)
            .padding(20.0)
            .background(Color::rgba(0.2, 0.2, 0.2, 1.0));
        let toggle_input_region = Button::new("Toggle Input Region")
            .on_click(|ctx, data: &mut bool, _: &Env| {
                *data = !*data;
                tracing::debug!("Setting input region toggle to: {}", *data);
                ctx.request_layout();
            })
            .lens(AppState::limit_input_region);
        let toggle_titlebar = Button::new("Toggle TitleBar")
            .on_click(|ctx, data: &mut bool, _: &Env| {
                *data = !*data;
                tracing::debug!("Setting titlebar visibility to: {}", *data);
                ctx.window().show_titlebar(*data);
                ctx.request_layout();
            })
            .lens(AppState::show_titlebar);
        let toggle_always_on_top = Button::new("Toggle Always On Top")
            .on_click(|ctx, data: &mut bool, _: &Env| {
                *data = !*data;
                tracing::debug!("Setting always on top to: {}", *data);
                ctx.window().set_always_on_top(*data);
            })
            .lens(AppState::always_on_top);
        let controls_flex = Flex::row()
            .with_child(toggle_input_region)
            .with_child(toggle_titlebar)
            .with_child(toggle_always_on_top);
        Self {
            info_label: WidgetPod::new(info_label),
            controls: WidgetPod::new(controls_flex),
        }
    }
source

pub fn is_initialized(&self) -> bool

Returns true if the widget has received LifeCycle::WidgetAdded.

source

pub fn has_focus(&self) -> bool

Returns true if widget or any descendent is focused

source

pub fn is_active(&self) -> bool

The “active” (aka pressed) status of a widget.

Active status generally corresponds to a mouse button down. Widgets with behavior similar to a button will call set_active on mouse down and then up.

The active status can only be set manually. Druid doesn’t automatically set it to false on mouse release or anything like that.

There is no special handling of the active status for multi-pointer devices.

When a widget is active, it gets mouse events even when the mouse is dragged away.

source

pub fn has_active(&self) -> bool

Returns true if any descendant is active.

source

pub fn is_hot(&self) -> bool

The “hot” (aka hover) status of a widget.

A widget is “hot” when the mouse is hovered over it. Some Widgets (eg buttons) will change their appearance when hot as a visual indication that they will respond to mouse interaction.

The hot status is automatically computed from the widget’s layout rect. In a container hierarchy, all widgets with layout rects containing the mouse position have hot status. The hot status cannot be set manually.

There is no special handling of the hot status for multi-pointer devices.

Note: a widget can be hot while another is active (for example, when clicking a button and dragging the cursor to another widget).

source

pub fn id(&self) -> WidgetId

Get the identity of the widget.

source

pub fn layout_requested(&self) -> bool

This widget or any of its children have requested layout.

source

pub fn set_origin(&mut self, ctx: &mut impl ChangeCtx, origin: Point)

Set the origin of this widget, in the parent’s coordinate space.

A container widget should call the Widget::layout method on its children in its own Widget::layout implementation, and then call set_origin to position those children.

The changed origin won’t be fully in effect until LifeCycle::ViewContextChanged has finished propagating. Specifically methods that depend on the widget’s origin in relation to the window will return stale results during the period after calling set_origin but before LifeCycle::ViewContextChanged has finished propagating.

The widget container can also call set_origin from other context, but calling set_origin after the widget received LifeCycle::ViewContextChanged and before the next event results in an inconsistent state of the widget tree.

The child will receive the LifeCycle::Size event informing them of the final Size.

Examples found in repository?
examples/timer.rs (line 79)
77
78
79
80
81
    fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, data: &u32, env: &Env) -> Size {
        self.simple_box.layout(ctx, &bc.loosen(), data, env);
        self.simple_box.set_origin(ctx, self.pos);
        bc.constrain((500.0, 500.0))
    }
More examples
Hide additional examples
examples/input_region.rs (line 137)
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    fn layout(
        &mut self,
        ctx: &mut druid::LayoutCtx,
        bc: &druid::BoxConstraints,
        data: &AppState,
        env: &druid::Env,
    ) -> druid::Size {
        let mut interactable_area = Region::EMPTY;
        let smaller_bc = BoxConstraints::new(
            Size::new(0.0, 0.0),
            Size::new(bc.max().width - 100.0, bc.max().height - 100.0),
        );
        let full_bc = BoxConstraints::new(Size::new(0.0, 0.0), bc.max());
        let _label_size = self.info_label.layout(ctx, &smaller_bc, data, env);
        let controls_size = self.controls.layout(ctx, &full_bc, data, env);

        let text_origin_point = Point::new(50.0, 50.0 + controls_size.height);
        self.info_label.set_origin(ctx, text_origin_point);
        let controls_origin_point = Point::new(EXAMPLE_BORDER_SIZE, EXAMPLE_BORDER_SIZE);
        self.controls.set_origin(ctx, controls_origin_point);

        // Add side rects to clarify the dimensions of the window.
        let left_rect = Rect::new(0.0, 0.0, EXAMPLE_BORDER_SIZE, bc.max().height);
        let right_rect = Rect::new(
            bc.max().width - EXAMPLE_BORDER_SIZE,
            0.0,
            bc.max().width,
            bc.max().height,
        );
        let bottom_rect = Rect::new(
            0.0,
            bc.max().height - EXAMPLE_BORDER_SIZE,
            bc.max().width,
            bc.max().height,
        );
        interactable_area.add_rect(left_rect);
        interactable_area.add_rect(right_rect);
        interactable_area.add_rect(bottom_rect);
        interactable_area.add_rect(self.info_label.layout_rect());
        interactable_area.add_rect(self.controls.layout_rect());

        if data.limit_input_region {
            ctx.window().set_input_region(Some(interactable_area));
        } else {
            ctx.window().set_input_region(None);
        }

        bc.max()
    }
source

pub fn layout_rect(&self) -> Rect

Returns the layout Rect.

This will be a Rect with a Size determined by the child’s layout method, and the origin that was set by set_origin.

Examples found in repository?
examples/timer.rs (line 38)
37
38
39
40
41
42
43
44
45
46
47
    fn adjust_box_pos(&mut self, container_size: Size) {
        let box_size = self.simple_box.layout_rect().size();
        self.pos.x += 2.;
        if self.pos.x + box_size.width > container_size.width {
            self.pos.x = 0.;
            self.pos.y += box_size.height;
            if self.pos.y + box_size.height > container_size.height {
                self.pos.y = 0.;
            }
        }
    }
More examples
Hide additional examples
examples/input_region.rs (line 158)
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    fn layout(
        &mut self,
        ctx: &mut druid::LayoutCtx,
        bc: &druid::BoxConstraints,
        data: &AppState,
        env: &druid::Env,
    ) -> druid::Size {
        let mut interactable_area = Region::EMPTY;
        let smaller_bc = BoxConstraints::new(
            Size::new(0.0, 0.0),
            Size::new(bc.max().width - 100.0, bc.max().height - 100.0),
        );
        let full_bc = BoxConstraints::new(Size::new(0.0, 0.0), bc.max());
        let _label_size = self.info_label.layout(ctx, &smaller_bc, data, env);
        let controls_size = self.controls.layout(ctx, &full_bc, data, env);

        let text_origin_point = Point::new(50.0, 50.0 + controls_size.height);
        self.info_label.set_origin(ctx, text_origin_point);
        let controls_origin_point = Point::new(EXAMPLE_BORDER_SIZE, EXAMPLE_BORDER_SIZE);
        self.controls.set_origin(ctx, controls_origin_point);

        // Add side rects to clarify the dimensions of the window.
        let left_rect = Rect::new(0.0, 0.0, EXAMPLE_BORDER_SIZE, bc.max().height);
        let right_rect = Rect::new(
            bc.max().width - EXAMPLE_BORDER_SIZE,
            0.0,
            bc.max().width,
            bc.max().height,
        );
        let bottom_rect = Rect::new(
            0.0,
            bc.max().height - EXAMPLE_BORDER_SIZE,
            bc.max().width,
            bc.max().height,
        );
        interactable_area.add_rect(left_rect);
        interactable_area.add_rect(right_rect);
        interactable_area.add_rect(bottom_rect);
        interactable_area.add_rect(self.info_label.layout_rect());
        interactable_area.add_rect(self.controls.layout_rect());

        if data.limit_input_region {
            ctx.window().set_input_region(Some(interactable_area));
        } else {
            ctx.window().set_input_region(None);
        }

        bc.max()
    }
source

pub fn paint_rect(&self) -> Rect

Get the widget’s paint Rect.

This is the Rect that widget has indicated it needs to paint in. This is the same as the layout_rect with the paint_insets applied; in the general case it is the same as the layout_rect.

source

pub fn paint_insets(&self) -> Insets

Return the paint Insets for this widget.

If these Insets are nonzero, they describe the area beyond a widget’s layout rect where it needs to paint.

These are generally zero; exceptions are widgets that do things like paint a drop shadow.

A widget can set its insets by calling set_paint_insets during its layout method.

source

pub fn compute_parent_paint_insets(&self, parent_size: Size) -> Insets

Given a parents layout size, determine the appropriate paint Insets for the parent.

This is a convenience method to be used from the layout method of a Widget that manages a child; it allows the parent to correctly propagate a child’s desired paint rect, if it extends beyond the bounds of the parent’s layout rect.

source

pub fn baseline_offset(&self) -> f64

The distance from the bottom of this widget to the baseline.

source§

impl<T: Data, W: Widget<T>> WidgetPod<T, W>

source

pub fn paint_raw(&mut self, ctx: &mut PaintCtx<'_, '_, '_>, data: &T, env: &Env)

Paint a child widget.

Generally called by container widgets as part of their Widget::paint method.

Note that this method does not apply the offset of the layout rect. If that is desired, use paint instead.

source

pub fn paint(&mut self, ctx: &mut PaintCtx<'_, '_, '_>, data: &T, env: &Env)

Paint the widget, translating it by the origin of its layout rectangle.

This will recursively paint widgets, stopping if a widget’s layout rect is outside of the currently visible region.

Examples found in repository?
examples/timer.rs (line 84)
83
84
85
    fn paint(&mut self, ctx: &mut PaintCtx, data: &u32, env: &Env) {
        self.simple_box.paint(ctx, data, env);
    }
More examples
Hide additional examples
examples/input_region.rs (line 189)
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    fn paint(&mut self, ctx: &mut druid::PaintCtx, data: &AppState, env: &druid::Env) {
        let window_area = ctx.size();
        let left_rect = Rect::new(0.0, 0.0, EXAMPLE_BORDER_SIZE, window_area.height);
        let right_rect = Rect::new(
            window_area.width - EXAMPLE_BORDER_SIZE,
            0.0,
            window_area.width,
            window_area.height,
        );
        let bottom_rect = Rect::new(
            0.0,
            window_area.height - EXAMPLE_BORDER_SIZE,
            window_area.width,
            window_area.height,
        );

        ctx.fill(left_rect, &Color::rgba(1.0, 0., 0., 0.7));
        ctx.fill(right_rect, &Color::rgba(1.0, 0., 0., 0.7));
        ctx.fill(bottom_rect, &Color::rgba(1.0, 0., 0., 0.7));
        self.info_label.paint(ctx, data, env);
        self.controls.paint(ctx, data, env);
    }
source

pub fn paint_always(&mut self, ctx: &mut PaintCtx<'_, '_, '_>, data: &T, env: &Env)

Paint the widget, even if its layout rect is outside of the currently visible region.

source

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

Compute layout of a widget.

Generally called by container widgets as part of their layout method.

Examples found in repository?
examples/timer.rs (line 78)
77
78
79
80
81
    fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, data: &u32, env: &Env) -> Size {
        self.simple_box.layout(ctx, &bc.loosen(), data, env);
        self.simple_box.set_origin(ctx, self.pos);
        bc.constrain((500.0, 500.0))
    }
More examples
Hide additional examples
examples/input_region.rs (line 133)
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    fn layout(
        &mut self,
        ctx: &mut druid::LayoutCtx,
        bc: &druid::BoxConstraints,
        data: &AppState,
        env: &druid::Env,
    ) -> druid::Size {
        let mut interactable_area = Region::EMPTY;
        let smaller_bc = BoxConstraints::new(
            Size::new(0.0, 0.0),
            Size::new(bc.max().width - 100.0, bc.max().height - 100.0),
        );
        let full_bc = BoxConstraints::new(Size::new(0.0, 0.0), bc.max());
        let _label_size = self.info_label.layout(ctx, &smaller_bc, data, env);
        let controls_size = self.controls.layout(ctx, &full_bc, data, env);

        let text_origin_point = Point::new(50.0, 50.0 + controls_size.height);
        self.info_label.set_origin(ctx, text_origin_point);
        let controls_origin_point = Point::new(EXAMPLE_BORDER_SIZE, EXAMPLE_BORDER_SIZE);
        self.controls.set_origin(ctx, controls_origin_point);

        // Add side rects to clarify the dimensions of the window.
        let left_rect = Rect::new(0.0, 0.0, EXAMPLE_BORDER_SIZE, bc.max().height);
        let right_rect = Rect::new(
            bc.max().width - EXAMPLE_BORDER_SIZE,
            0.0,
            bc.max().width,
            bc.max().height,
        );
        let bottom_rect = Rect::new(
            0.0,
            bc.max().height - EXAMPLE_BORDER_SIZE,
            bc.max().width,
            bc.max().height,
        );
        interactable_area.add_rect(left_rect);
        interactable_area.add_rect(right_rect);
        interactable_area.add_rect(bottom_rect);
        interactable_area.add_rect(self.info_label.layout_rect());
        interactable_area.add_rect(self.controls.layout_rect());

        if data.limit_input_region {
            ctx.window().set_input_region(Some(interactable_area));
        } else {
            ctx.window().set_input_region(None);
        }

        bc.max()
    }
source

pub fn event( &mut self, ctx: &mut EventCtx<'_, '_>, event: &Event, data: &mut T, env: &Env )

Propagate an event.

Generally the event method of a container widget will call this method on all its children. Here is where a great deal of the event flow logic resides, particularly whether to continue propagating the event.

Examples found in repository?
examples/input_region.rs (line 94)
87
88
89
90
91
92
93
94
95
96
    fn event(
        &mut self,
        ctx: &mut druid::EventCtx,
        event: &druid::Event,
        data: &mut AppState,
        env: &druid::Env,
    ) {
        self.info_label.event(ctx, event, data, env);
        self.controls.event(ctx, event, data, env);
    }
More examples
Hide additional examples
examples/timer.rs (line 66)
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut u32, env: &Env) {
        match event {
            Event::WindowConnected => {
                // Start the timer when the application launches
                self.timer_id = ctx.request_timer(TIMER_INTERVAL);
            }
            Event::Timer(id) => {
                if *id == self.timer_id {
                    self.adjust_box_pos(ctx.size());
                    ctx.request_layout();
                    self.timer_id = ctx.request_timer(TIMER_INTERVAL);
                }
            }
            _ => (),
        }
        self.simple_box.event(ctx, event, data, env);
    }
source

pub fn lifecycle( &mut self, ctx: &mut LifeCycleCtx<'_, '_>, event: &LifeCycle, data: &T, env: &Env )

Propagate a LifeCycle event.

Examples found in repository?
examples/timer.rs (line 70)
69
70
71
    fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, data: &u32, env: &Env) {
        self.simple_box.lifecycle(ctx, event, data, env);
    }
More examples
Hide additional examples
examples/input_region.rs (line 105)
98
99
100
101
102
103
104
105
106
107
    fn lifecycle(
        &mut self,
        ctx: &mut druid::LifeCycleCtx,
        event: &druid::LifeCycle,
        data: &AppState,
        env: &druid::Env,
    ) {
        self.info_label.lifecycle(ctx, event, data, env);
        self.controls.lifecycle(ctx, event, data, env);
    }
source

pub fn update(&mut self, ctx: &mut UpdateCtx<'_, '_>, data: &T, env: &Env)

Propagate a data update.

Generally called by container widgets as part of their update method.

Examples found in repository?
examples/timer.rs (line 74)
73
74
75
    fn update(&mut self, ctx: &mut UpdateCtx, _old_data: &u32, data: &u32, env: &Env) {
        self.simple_box.update(ctx, data, env);
    }
More examples
Hide additional examples
examples/input_region.rs (line 116)
109
110
111
112
113
114
115
116
117
118
    fn update(
        &mut self,
        ctx: &mut druid::UpdateCtx,
        _old_data: &AppState,
        data: &AppState,
        env: &druid::Env,
    ) {
        self.info_label.update(ctx, data, env);
        self.controls.update(ctx, data, env);
    }
source§

impl<T, W: Widget<T> + 'static> WidgetPod<T, W>

source

pub fn boxed(self) -> WidgetPod<T, Box<dyn Widget<T>>>

Box the contained widget.

Convert a WidgetPod containing a widget of a specific concrete type into a dynamically boxed widget.

source§

impl<T, W> WidgetPod<T, W>

source

pub fn widget(&self) -> &W

Return a reference to the inner widget.

source

pub fn widget_mut(&mut self) -> &mut W

Return a mutable reference to the inner widget.

Auto Trait Implementations§

§

impl<T, W> !RefUnwindSafe for WidgetPod<T, W>

§

impl<T, W> !Send for WidgetPod<T, W>

§

impl<T, W> !Sync for WidgetPod<T, W>

§

impl<T, W> Unpin for WidgetPod<T, W>where T: Unpin, W: Unpin,

§

impl<T, W> !UnwindSafe for WidgetPod<T, W>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> RoundFrom<T> for T

§

fn round_from(x: T) -> T

Performs the conversion.
§

impl<T, U> RoundInto<U> for Twhere U: RoundFrom<T>,

§

fn round_into(self) -> U

Performs the conversion.
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more