Skip to content

Commit

Permalink
Put Widget methods in lifecycle order
Browse files Browse the repository at this point in the history
  • Loading branch information
ul authored and cmyr committed Dec 4, 2019
1 parent 26b7ad1 commit 61770e5
Show file tree
Hide file tree
Showing 23 changed files with 817 additions and 817 deletions.
44 changes: 22 additions & 22 deletions druid/examples/anim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,6 @@ struct AnimWidget {
}

impl Widget<u32> for AnimWidget {
fn paint(
&mut self,
paint_ctx: &mut PaintCtx,
_base_state: &BaseState,
_data: &u32,
_env: &Env,
) {
let center = Point::new(50.0, 50.0);
let ambit = center + 45.0 * Vec2::from_angle((0.75 + self.t) * 2.0 * PI);
paint_ctx.stroke(Line::new(center, ambit), &Color::WHITE, 1.0);
}

fn layout(
&mut self,
_layout_ctx: &mut LayoutCtx,
bc: &BoxConstraints,
_data: &u32,
_env: &Env,
) -> Size {
bc.constrain((100.0, 100.0))
}

fn event(&mut self, ctx: &mut EventCtx, event: &Event, _data: &mut u32, _env: &Env) {
match event {
Event::MouseDown(_) => {
Expand All @@ -70,6 +48,28 @@ impl Widget<u32> for AnimWidget {
}

fn update(&mut self, _ctx: &mut UpdateCtx, _old_data: Option<&u32>, _data: &u32, _env: &Env) {}

fn layout(
&mut self,
_layout_ctx: &mut LayoutCtx,
bc: &BoxConstraints,
_data: &u32,
_env: &Env,
) -> Size {
bc.constrain((100.0, 100.0))
}

fn paint(
&mut self,
paint_ctx: &mut PaintCtx,
_base_state: &BaseState,
_data: &u32,
_env: &Env,
) {
let center = Point::new(50.0, 50.0);
let ambit = center + 45.0 * Vec2::from_angle((0.75 + self.t) * 2.0 * PI);
paint_ctx.stroke(Line::new(center, ambit), &Color::WHITE, 1.0);
}
}

fn main() {
Expand Down
54 changes: 27 additions & 27 deletions druid/examples/custom_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,33 @@ use druid::{
struct CustomWidget;

impl Widget<String> for CustomWidget {
fn event(&mut self, _ctx: &mut EventCtx, _event: &Event, _data: &mut String, _env: &Env) {}

fn update(
&mut self,
_ctx: &mut UpdateCtx,
_old_data: Option<&String>,
_data: &String,
_env: &Env,
) {
}

fn layout(
&mut self,
_layout_ctx: &mut LayoutCtx,
bc: &BoxConstraints,
_data: &String,
_env: &Env,
) -> Size {
// BoxConstraints are passed by the parent widget.
// This method can return any Size within those constraints:
// bc.constrain(my_size)
//
// To check if a dimension is infinite or not (e.g. scrolling):
// bc.is_width_bounded() / bc.is_height_bounded()
bc.max()
}

// The paint method gets called last, after an event flow.
// It goes event -> update -> layout -> paint, and each method can influence the next.
// Basically, anything that changes the appearance of a widget causes a paint.
Expand Down Expand Up @@ -97,33 +124,6 @@ impl Widget<String> for CustomWidget {
InterpolationMode::Bilinear,
);
}

fn layout(
&mut self,
_layout_ctx: &mut LayoutCtx,
bc: &BoxConstraints,
_data: &String,
_env: &Env,
) -> Size {
// BoxConstraints are passed by the parent widget.
// This method can return any Size within those constraints:
// bc.constrain(my_size)
//
// To check if a dimension is infinite or not (e.g. scrolling):
// bc.is_width_bounded() / bc.is_height_bounded()
bc.max()
}

fn event(&mut self, _ctx: &mut EventCtx, _event: &Event, _data: &mut String, _env: &Env) {}

fn update(
&mut self,
_ctx: &mut UpdateCtx,
_old_data: Option<&String>,
_data: &String,
_env: &Env,
) {
}
}

fn main() {
Expand Down
44 changes: 22 additions & 22 deletions druid/examples/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,6 @@ struct TimerWidget {
}

impl Widget<u32> for TimerWidget {
fn paint(
&mut self,
paint_ctx: &mut PaintCtx,
_base_state: &BaseState,
_data: &u32,
_env: &Env,
) {
if self.on {
paint_ctx.stroke(Line::new((10.0, 10.0), (10.0, 50.0)), &Color::WHITE, 1.0);
}
}

fn layout(
&mut self,
_layout_ctx: &mut LayoutCtx,
bc: &BoxConstraints,
_data: &u32,
_env: &Env,
) -> Size {
bc.constrain((100.0, 100.0))
}

fn event(&mut self, ctx: &mut EventCtx, event: &Event, _data: &mut u32, _env: &Env) {
match event {
Event::MouseDown(_) => {
Expand All @@ -72,6 +50,28 @@ impl Widget<u32> for TimerWidget {
}

fn update(&mut self, _ctx: &mut UpdateCtx, _old_data: Option<&u32>, _data: &u32, _env: &Env) {}

fn layout(
&mut self,
_layout_ctx: &mut LayoutCtx,
bc: &BoxConstraints,
_data: &u32,
_env: &Env,
) -> Size {
bc.constrain((100.0, 100.0))
}

fn paint(
&mut self,
paint_ctx: &mut PaintCtx,
_base_state: &BaseState,
_data: &u32,
_env: &Env,
) {
if self.on {
paint_ctx.stroke(Line::new((10.0, 10.0), (10.0, 50.0)), &Color::WHITE, 1.0);
}
}
}

fn main() {
Expand Down
24 changes: 12 additions & 12 deletions druid/src/lens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,6 @@ where
L: Lens<T, U>,
W: Widget<U>,
{
fn paint(&mut self, paint_ctx: &mut PaintCtx, base_state: &BaseState, data: &T, env: &Env) {
let inner = &mut self.inner;
self.lens
.with(data, |data| inner.paint(paint_ctx, base_state, data, env));
}

fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, data: &T, env: &Env) -> Size {
let inner = &mut self.inner;
self.lens
.with(data, |data| inner.layout(ctx, bc, data, env))
}

fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut T, env: &Env) {
let inner = &mut self.inner;
self.lens
Expand All @@ -253,6 +241,18 @@ where
lens.with(data, |data| inner.update(ctx, None, data, env));
}
}

fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, data: &T, env: &Env) -> Size {
let inner = &mut self.inner;
self.lens
.with(data, |data| inner.layout(ctx, bc, data, env))
}

fn paint(&mut self, paint_ctx: &mut PaintCtx, base_state: &BaseState, data: &T, env: &Env) {
let inner = &mut self.inner;
self.lens
.with(data, |data| inner.paint(paint_ctx, base_state, data, env));
}
}

/// Lens accessing a member of some type using accessor functions
Expand Down
84 changes: 42 additions & 42 deletions druid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,40 +172,6 @@ pub struct BaseState {
/// [`Data`]: trait.Data.html
/// [`WidgetPod`]: struct.WidgetPod.html
pub trait Widget<T> {
/// 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 paint(&mut self, paint_ctx: &mut PaintCtx, base_state: &BaseState, data: &T, env: &Env);

/// 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.
///
/// [`WidgetPod::layout`]: struct.WidgetPod.html#method.layout
/// [`set_layout_rect`]: struct.LayoutCtx.html#method.set_layout_rect
fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, data: &T, env: &Env) -> Size;

/// Handle an event.
///
/// A number of different events (in the [`Event`] enum) are handled in this
Expand Down Expand Up @@ -234,26 +200,60 @@ pub trait Widget<T> {
// Consider a no-op default impl. One reason against is that containers might
// inadvertently forget to propagate.
fn update(&mut self, ctx: &mut UpdateCtx, old_data: Option<&T>, data: &T, env: &Env);

/// 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.
///
/// [`WidgetPod::layout`]: struct.WidgetPod.html#method.layout
/// [`set_layout_rect`]: struct.LayoutCtx.html#method.set_layout_rect
fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, data: &T, env: &Env) -> Size;

/// 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 paint(&mut self, paint_ctx: &mut PaintCtx, base_state: &BaseState, data: &T, env: &Env);
}

// TODO: explore getting rid of this (ie be consistent about using
// `dyn Widget` only).
impl<T> Widget<T> for Box<dyn Widget<T>> {
fn paint(&mut self, paint_ctx: &mut PaintCtx, base_state: &BaseState, data: &T, env: &Env) {
self.deref_mut().paint(paint_ctx, base_state, data, env);
}

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

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

fn update(&mut self, ctx: &mut UpdateCtx, old_data: Option<&T>, data: &T, env: &Env) {
self.deref_mut().update(ctx, old_data, data, env);
}

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

fn paint(&mut self, paint_ctx: &mut PaintCtx, base_state: &BaseState, data: &T, env: &Env) {
self.deref_mut().paint(paint_ctx, base_state, data, env);
}
}

/// A context passed to paint methods of widgets.
Expand Down
16 changes: 8 additions & 8 deletions druid/src/widget/align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@ impl<T: Data> Align<T> {
}

impl<T: Data> Widget<T> for Align<T> {
fn paint(&mut self, paint_ctx: &mut PaintCtx, _base_state: &BaseState, data: &T, env: &Env) {
self.child.paint_with_offset(paint_ctx, data, env);
fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut T, env: &Env) {
self.child.event(ctx, event, data, env)
}

fn update(&mut self, ctx: &mut UpdateCtx, _old_data: Option<&T>, data: &T, env: &Env) {
self.child.update(ctx, data, env);
}

fn layout(
Expand Down Expand Up @@ -121,11 +125,7 @@ impl<T: Data> Widget<T> for Align<T> {
my_size
}

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

fn update(&mut self, ctx: &mut UpdateCtx, _old_data: Option<&T>, data: &T, env: &Env) {
self.child.update(ctx, data, env);
fn paint(&mut self, paint_ctx: &mut PaintCtx, _base_state: &BaseState, data: &T, env: &Env) {
self.child.paint_with_offset(paint_ctx, data, env);
}
}
Loading

0 comments on commit 61770e5

Please sign in to comment.