Skip to content

Commit 694af59

Browse files
committed
Use widget::prelude::* where appropriate
This will encourage other people to use the prelude, which in turn makes it slightly less annoying when (and if) we change the signatures of widget methods. This includes a few small cleanups that came about while updating imports; most of these involve replacing things like `let rect = Rect::from_origin_size(Point::ZERO, my_size);` with, `let rect = my_size.to_rect();`.
1 parent 9920850 commit 694af59

30 files changed

+80
-168
lines changed

druid/examples/custom_widget.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl Widget<String> for CustomWidget {
6363
// Clear the whole widget with the color of your choice
6464
// (ctx.size() returns the size of the layout rect we're painting in)
6565
let size = ctx.size();
66-
let rect = Rect::from_origin_size(Point::ORIGIN, size);
66+
let rect = size.to_rect();
6767
ctx.fill(rect, &Color::WHITE);
6868

6969
// Note: ctx also has a `clear` method, but that clears the whole context,
@@ -105,11 +105,7 @@ impl Widget<String> for CustomWidget {
105105
.make_image(256, 256, &image_data, ImageFormat::RgbaSeparate)
106106
.unwrap();
107107
// The image is automatically scaled to fit the rect you pass to draw_image
108-
ctx.draw_image(
109-
&image,
110-
Rect::from_origin_size(Point::ORIGIN, size),
111-
InterpolationMode::Bilinear,
112-
);
108+
ctx.draw_image(&image, size.to_rect(), InterpolationMode::Bilinear);
113109
}
114110
}
115111

druid/src/scroll_component.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ impl ScrollComponent {
318318
/// Make sure to call on every event
319319
pub fn event(&mut self, ctx: &mut EventCtx, event: &Event, env: &Env) {
320320
let size = ctx.size();
321-
let viewport = Rect::from_origin_size(Point::ORIGIN, size);
321+
let viewport = size.to_rect();
322322

323323
let scrollbar_is_hovered = match event {
324324
Event::MouseMove(e) | Event::MouseUp(e) | Event::MouseDown(e) => {

druid/src/widget/align.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,8 @@
1414

1515
//! A widget that aligns its child (for example, centering it).
1616
17-
use crate::kurbo::{Rect, Size};
18-
use crate::{
19-
BoxConstraints, Data, Env, Event, EventCtx, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx,
20-
UpdateCtx, Widget, WidgetPod,
21-
};
22-
23-
use crate::piet::UnitPoint;
17+
use crate::widget::prelude::*;
18+
use crate::{Data, Rect, Size, UnitPoint, WidgetPod};
2419

2520
/// A widget that aligns its child.
2621
pub struct Align<T> {

druid/src/widget/button.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
// limitations under the License.
1414

1515
//! A button widget.
16-
use crate::theme;
16+
1717
use crate::widget::prelude::*;
1818
use crate::widget::{Click, ControllerHost, Label, LabelText};
19-
20-
use crate::{Affine, Data, Insets, LinearGradient, Point, Rect, RenderContext, UnitPoint, Widget};
19+
use crate::{theme, Affine, Data, Insets, LinearGradient, UnitPoint};
2120

2221
// the minimum padding added to a button.
2322
// NOTE: these values are chosen to match the existing look of TextBox; these
@@ -165,7 +164,8 @@ impl<T: Data> Widget<T> for Button<T> {
165164
let size = ctx.size();
166165
let stroke_width = env.get(theme::BUTTON_BORDER_WIDTH);
167166

168-
let rounded_rect = Rect::from_origin_size(Point::ORIGIN, size)
167+
let rounded_rect = size
168+
.to_rect()
169169
.inset(-stroke_width / 2.0)
170170
.to_rounded_rect(env.get(theme::BUTTON_BORDER_RADIUS));
171171

druid/src/widget/checkbox.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
use crate::kurbo::{BezPath, Size};
1818
use crate::piet::{LineCap, LineJoin, LinearGradient, RenderContext, StrokeStyle, UnitPoint};
1919
use crate::theme;
20-
use crate::widget::{Label, LabelText};
21-
use crate::{
22-
BoxConstraints, Env, Event, EventCtx, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx, UpdateCtx,
23-
Widget,
24-
};
20+
use crate::widget::{prelude::*, Label, LabelText};
2521

2622
/// A checkbox that toggles a `bool`.
2723
pub struct Checkbox {

druid/src/widget/container.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
//! A widget that provides simple visual styling options to a child.
1616
1717
use super::BackgroundBrush;
18-
use crate::shell::kurbo::{Point, Rect, Size};
19-
use crate::{
20-
BoxConstraints, Color, Data, Env, Event, EventCtx, KeyOrValue, LayoutCtx, LifeCycle,
21-
LifeCycleCtx, PaintCtx, RenderContext, UpdateCtx, Widget, WidgetPod,
22-
};
18+
use crate::kurbo::Point;
19+
use crate::widget::prelude::*;
20+
use crate::{Color, Data, KeyOrValue, Rect, WidgetPod};
2321

2422
struct BorderStyle {
2523
width: KeyOrValue<f64>,

druid/src/widget/controller.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414

1515
//! A widget-controlling widget.
1616
17-
use crate::{
18-
BoxConstraints, Env, Event, EventCtx, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx, Size,
19-
UpdateCtx, Widget, WidgetId,
20-
};
17+
use crate::widget::prelude::*;
2118

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

druid/src/widget/either.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414

1515
//! A widget that switches dynamically between two child views.
1616
17-
use crate::kurbo::{Point, Rect, Size};
18-
use crate::{
19-
BoxConstraints, Data, Env, Event, EventCtx, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx,
20-
UpdateCtx, Widget, WidgetPod,
21-
};
17+
use crate::widget::prelude::*;
18+
use crate::{Data, WidgetPod};
2219

2320
/// A widget that switches between two possible child views.
2421
pub struct Either<T> {
@@ -80,22 +77,14 @@ impl<T: Data> Widget<T> for Either<T> {
8077
fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, data: &T, env: &Env) -> Size {
8178
if self.current {
8279
let size = self.true_branch.layout(ctx, bc, data, env);
83-
self.true_branch.set_layout_rect(
84-
ctx,
85-
data,
86-
env,
87-
Rect::from_origin_size(Point::ORIGIN, size),
88-
);
80+
self.true_branch
81+
.set_layout_rect(ctx, data, env, size.to_rect());
8982
ctx.set_paint_insets(self.true_branch.paint_insets());
9083
size
9184
} else {
9285
let size = self.false_branch.layout(ctx, bc, data, env);
93-
self.false_branch.set_layout_rect(
94-
ctx,
95-
data,
96-
env,
97-
Rect::from_origin_size(Point::ORIGIN, size),
98-
);
86+
self.false_branch
87+
.set_layout_rect(ctx, data, env, size.to_rect());
9988
ctx.set_paint_insets(self.true_branch.paint_insets());
10089
size
10190
}

druid/src/widget/env_scope.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414

1515
//! A widget that accepts a closure to update the environment for its child.
1616
17-
use crate::kurbo::Size;
18-
use crate::{
19-
BoxConstraints, Data, Env, Event, EventCtx, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx,
20-
UpdateCtx, Widget, WidgetPod,
21-
};
17+
use crate::widget::prelude::*;
18+
use crate::{Data, WidgetPod};
2219

2320
/// A widget that accepts a closure to update the environment for its child.
2421
pub struct EnvScope<T, W> {

druid/src/widget/flex.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@
1515
//! A widget that arranges its children in a one-dimensional array.
1616
1717
use crate::kurbo::common::FloatExt;
18-
use crate::kurbo::{Point, Rect, Size};
19-
18+
use crate::widget::prelude::*;
2019
use crate::widget::SizedBox;
21-
use crate::{
22-
BoxConstraints, Data, Env, Event, EventCtx, KeyOrValue, LayoutCtx, LifeCycle, LifeCycleCtx,
23-
PaintCtx, UpdateCtx, Widget, WidgetPod,
24-
};
20+
use crate::{Data, KeyOrValue, Point, Rect, WidgetPod};
2521

2622
/// A container with either horizontal or vertical layout.
2723
///
@@ -632,7 +628,7 @@ impl<T: Data> Widget<T> for Flex<T> {
632628
major_non_flex += self.direction.major(child_size).expand();
633629
minor = minor.max(self.direction.minor(child_size).expand());
634630
// Stash size.
635-
let rect = Rect::from_origin_size(Point::ORIGIN, child_size);
631+
let rect = child_size.to_rect();
636632
child.widget.set_layout_rect(ctx, data, env, rect);
637633
}
638634
}
@@ -659,7 +655,7 @@ impl<T: Data> Widget<T> for Flex<T> {
659655
major_flex += self.direction.major(child_size).expand();
660656
minor = minor.max(self.direction.minor(child_size).expand());
661657
// Stash size.
662-
let rect = Rect::from_origin_size(Point::ORIGIN, child_size);
658+
let rect = child_size.to_rect();
663659
child.widget.set_layout_rect(ctx, data, env, rect);
664660
}
665661
}

0 commit comments

Comments
 (0)