pub struct BoxConstraints { /* private fields */ }Expand description
Constraints for layout.
The layout strategy for Druid is strongly inspired by Flutter, and this struct is similar to the Flutter BoxConstraints class.
At the moment, it represents simply a minimum and maximum size.
A widget’s layout method should choose an appropriate size that
meets these constraints.
Further, a container widget should compute appropriate constraints for each of its child widgets, and pass those down when recursing.
The constraints are always rounded away from zero to integers to enable pixel perfect layout.
Implementations§
Source§impl BoxConstraints
impl BoxConstraints
Sourcepub const UNBOUNDED: BoxConstraints
pub const UNBOUNDED: BoxConstraints
An unbounded box constraints object.
Can be satisfied by any nonnegative size.
Sourcepub fn new(min: Size, max: Size) -> BoxConstraints
pub fn new(min: Size, max: Size) -> BoxConstraints
Create a new box constraints object.
Create constraints based on minimum and maximum size.
The given sizes are also rounded away from zero, so that the layout is aligned to integers.
Examples found in repository?
120 fn layout(
121 &mut self,
122 ctx: &mut druid::LayoutCtx,
123 bc: &druid::BoxConstraints,
124 data: &AppState,
125 env: &druid::Env,
126 ) -> druid::Size {
127 let mut interactable_area = Region::EMPTY;
128 let smaller_bc = BoxConstraints::new(
129 Size::new(0.0, 0.0),
130 Size::new(bc.max().width - 100.0, bc.max().height - 100.0),
131 );
132 let full_bc = BoxConstraints::new(Size::new(0.0, 0.0), bc.max());
133 let _label_size = self.info_label.layout(ctx, &smaller_bc, data, env);
134 let controls_size = self.controls.layout(ctx, &full_bc, data, env);
135
136 let text_origin_point = Point::new(50.0, 50.0 + controls_size.height);
137 self.info_label.set_origin(ctx, text_origin_point);
138 let controls_origin_point = Point::new(EXAMPLE_BORDER_SIZE, EXAMPLE_BORDER_SIZE);
139 self.controls.set_origin(ctx, controls_origin_point);
140
141 // Add side rects to clarify the dimensions of the window.
142 let left_rect = Rect::new(0.0, 0.0, EXAMPLE_BORDER_SIZE, bc.max().height);
143 let right_rect = Rect::new(
144 bc.max().width - EXAMPLE_BORDER_SIZE,
145 0.0,
146 bc.max().width,
147 bc.max().height,
148 );
149 let bottom_rect = Rect::new(
150 0.0,
151 bc.max().height - EXAMPLE_BORDER_SIZE,
152 bc.max().width,
153 bc.max().height,
154 );
155 interactable_area.add_rect(left_rect);
156 interactable_area.add_rect(right_rect);
157 interactable_area.add_rect(bottom_rect);
158 interactable_area.add_rect(self.info_label.layout_rect());
159 interactable_area.add_rect(self.controls.layout_rect());
160
161 if data.limit_input_region {
162 ctx.window().set_input_region(Some(interactable_area));
163 } else {
164 ctx.window().set_input_region(None);
165 }
166
167 bc.max()
168 }Sourcepub fn tight(size: Size) -> BoxConstraints
pub fn tight(size: Size) -> BoxConstraints
Create a “tight” box constraints object.
A “tight” constraint can only be satisfied by a single size.
The given size is also rounded away from zero, so that the layout is aligned to integers.
Sourcepub fn loosen(&self) -> BoxConstraints
pub fn loosen(&self) -> BoxConstraints
Create a “loose” version of the constraints.
Make a version with zero minimum size, but the same maximum size.
Sourcepub fn constrain(&self, size: impl Into<Size>) -> Size
pub fn constrain(&self, size: impl Into<Size>) -> Size
Clamp a given size so that it fits within the constraints.
The given size is also rounded away from zero, so that the layout is aligned to integers.
Examples found in repository?
More examples
77 fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, data: &u32, env: &Env) -> Size {
78 self.simple_box.layout(ctx, &bc.loosen(), data, env);
79 self.simple_box.set_origin(ctx, self.pos);
80 bc.constrain((500.0, 500.0))
81 }
82
83 fn paint(&mut self, ctx: &mut PaintCtx, data: &u32, env: &Env) {
84 self.simple_box.paint(ctx, data, env);
85 }
86}
87
88struct SimpleBox;
89
90impl Widget<u32> for SimpleBox {
91 fn event(&mut self, _ctx: &mut EventCtx, _event: &Event, _data: &mut u32, _env: &Env) {}
92
93 fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, _data: &u32, _env: &Env) {
94 if let LifeCycle::HotChanged(_) = event {
95 ctx.request_paint();
96 }
97 }
98
99 fn update(&mut self, _ctx: &mut UpdateCtx, _old_data: &u32, _data: &u32, _env: &Env) {}
100
101 fn layout(
102 &mut self,
103 _ctx: &mut LayoutCtx,
104 bc: &BoxConstraints,
105 _data: &u32,
106 _env: &Env,
107 ) -> Size {
108 bc.constrain((50.0, 50.0))
109 }48 fn layout(
49 &mut self,
50 _layout_ctx: &mut LayoutCtx,
51 bc: &BoxConstraints,
52 _data: &String,
53 _env: &Env,
54 ) -> Size {
55 // BoxConstraints are passed by the parent widget.
56 // This method can return any Size within those constraints:
57 // bc.constrain(my_size)
58 //
59 // To check if a dimension is infinite or not (e.g. scrolling):
60 // bc.is_width_bounded() / bc.is_height_bounded()
61 //
62 // bx.max() returns the maximum size of the widget. Be careful
63 // using this, since always make sure the widget is bounded.
64 // If bx.max() is used in a scrolling widget things will probably
65 // not work correctly.
66 if bc.is_width_bounded() && bc.is_height_bounded() {
67 bc.max()
68 } else {
69 let size = Size::new(100.0, 100.0);
70 bc.constrain(size)
71 }
72 }Sourcepub fn max(&self) -> Size
pub fn max(&self) -> Size
Returns the max size of these constraints.
Examples found in repository?
More examples
282 fn layout(
283 &mut self,
284 _layout_ctx: &mut LayoutCtx,
285 bc: &BoxConstraints,
286 _data: &AppData,
287 _env: &Env,
288 ) -> Size {
289 let max_size = bc.max();
290 let min_side = max_size.height.min(max_size.width);
291 Size {
292 width: min_side,
293 height: min_side,
294 }
295 }48 fn layout(
49 &mut self,
50 _layout_ctx: &mut LayoutCtx,
51 bc: &BoxConstraints,
52 _data: &String,
53 _env: &Env,
54 ) -> Size {
55 // BoxConstraints are passed by the parent widget.
56 // This method can return any Size within those constraints:
57 // bc.constrain(my_size)
58 //
59 // To check if a dimension is infinite or not (e.g. scrolling):
60 // bc.is_width_bounded() / bc.is_height_bounded()
61 //
62 // bx.max() returns the maximum size of the widget. Be careful
63 // using this, since always make sure the widget is bounded.
64 // If bx.max() is used in a scrolling widget things will probably
65 // not work correctly.
66 if bc.is_width_bounded() && bc.is_height_bounded() {
67 bc.max()
68 } else {
69 let size = Size::new(100.0, 100.0);
70 bc.constrain(size)
71 }
72 }120 fn layout(
121 &mut self,
122 ctx: &mut druid::LayoutCtx,
123 bc: &druid::BoxConstraints,
124 data: &AppState,
125 env: &druid::Env,
126 ) -> druid::Size {
127 let mut interactable_area = Region::EMPTY;
128 let smaller_bc = BoxConstraints::new(
129 Size::new(0.0, 0.0),
130 Size::new(bc.max().width - 100.0, bc.max().height - 100.0),
131 );
132 let full_bc = BoxConstraints::new(Size::new(0.0, 0.0), bc.max());
133 let _label_size = self.info_label.layout(ctx, &smaller_bc, data, env);
134 let controls_size = self.controls.layout(ctx, &full_bc, data, env);
135
136 let text_origin_point = Point::new(50.0, 50.0 + controls_size.height);
137 self.info_label.set_origin(ctx, text_origin_point);
138 let controls_origin_point = Point::new(EXAMPLE_BORDER_SIZE, EXAMPLE_BORDER_SIZE);
139 self.controls.set_origin(ctx, controls_origin_point);
140
141 // Add side rects to clarify the dimensions of the window.
142 let left_rect = Rect::new(0.0, 0.0, EXAMPLE_BORDER_SIZE, bc.max().height);
143 let right_rect = Rect::new(
144 bc.max().width - EXAMPLE_BORDER_SIZE,
145 0.0,
146 bc.max().width,
147 bc.max().height,
148 );
149 let bottom_rect = Rect::new(
150 0.0,
151 bc.max().height - EXAMPLE_BORDER_SIZE,
152 bc.max().width,
153 bc.max().height,
154 );
155 interactable_area.add_rect(left_rect);
156 interactable_area.add_rect(right_rect);
157 interactable_area.add_rect(bottom_rect);
158 interactable_area.add_rect(self.info_label.layout_rect());
159 interactable_area.add_rect(self.controls.layout_rect());
160
161 if data.limit_input_region {
162 ctx.window().set_input_region(Some(interactable_area));
163 } else {
164 ctx.window().set_input_region(None);
165 }
166
167 bc.max()
168 }Sourcepub fn is_width_bounded(&self) -> bool
pub fn is_width_bounded(&self) -> bool
Whether there is an upper bound on the width.
Examples found in repository?
48 fn layout(
49 &mut self,
50 _layout_ctx: &mut LayoutCtx,
51 bc: &BoxConstraints,
52 _data: &String,
53 _env: &Env,
54 ) -> Size {
55 // BoxConstraints are passed by the parent widget.
56 // This method can return any Size within those constraints:
57 // bc.constrain(my_size)
58 //
59 // To check if a dimension is infinite or not (e.g. scrolling):
60 // bc.is_width_bounded() / bc.is_height_bounded()
61 //
62 // bx.max() returns the maximum size of the widget. Be careful
63 // using this, since always make sure the widget is bounded.
64 // If bx.max() is used in a scrolling widget things will probably
65 // not work correctly.
66 if bc.is_width_bounded() && bc.is_height_bounded() {
67 bc.max()
68 } else {
69 let size = Size::new(100.0, 100.0);
70 bc.constrain(size)
71 }
72 }Sourcepub fn is_height_bounded(&self) -> bool
pub fn is_height_bounded(&self) -> bool
Whether there is an upper bound on the height.
Examples found in repository?
48 fn layout(
49 &mut self,
50 _layout_ctx: &mut LayoutCtx,
51 bc: &BoxConstraints,
52 _data: &String,
53 _env: &Env,
54 ) -> Size {
55 // BoxConstraints are passed by the parent widget.
56 // This method can return any Size within those constraints:
57 // bc.constrain(my_size)
58 //
59 // To check if a dimension is infinite or not (e.g. scrolling):
60 // bc.is_width_bounded() / bc.is_height_bounded()
61 //
62 // bx.max() returns the maximum size of the widget. Be careful
63 // using this, since always make sure the widget is bounded.
64 // If bx.max() is used in a scrolling widget things will probably
65 // not work correctly.
66 if bc.is_width_bounded() && bc.is_height_bounded() {
67 bc.max()
68 } else {
69 let size = Size::new(100.0, 100.0);
70 bc.constrain(size)
71 }
72 }Sourcepub fn debug_check(&self, name: &str)
pub fn debug_check(&self, name: &str)
Check to see if these constraints are legit.
Logs a warning if BoxConstraints are invalid.
Sourcepub fn shrink(&self, diff: impl Into<Size>) -> BoxConstraints
pub fn shrink(&self, diff: impl Into<Size>) -> BoxConstraints
Shrink min and max constraints by size
The given size is also rounded away from zero, so that the layout is aligned to integers.
Sourcepub fn contains(&self, size: impl Into<Size>) -> bool
pub fn contains(&self, size: impl Into<Size>) -> bool
Test whether these constraints contain the given Size.
Sourcepub fn constrain_aspect_ratio(&self, aspect_ratio: f64, width: f64) -> Size
pub fn constrain_aspect_ratio(&self, aspect_ratio: f64, width: f64) -> Size
Find the Size within these BoxConstraints that minimises the difference between the
returned Size’s aspect ratio and aspect_ratio, where aspect ratio is defined as
height / width.
If multiple Sizes give the optimal aspect_ratio, then the one with the width nearest
the supplied width will be used. Specifically, if width == 0.0 then the smallest possible
Size will be chosen, and likewise if width == f64::INFINITY, then the largest Size
will be chosen.
Use this function when maintaining an aspect ratio is more important than minimizing the distance between input and output size width and height.
Sourcepub fn unbound_max(&self, axis: Axis) -> Self
pub fn unbound_max(&self, axis: Axis) -> Self
Sets the max on a given axis to infinity.
Sourcepub fn unbound_max_width(&self) -> Self
pub fn unbound_max_width(&self) -> Self
Sets max width to infinity.
Sourcepub fn unbound_max_height(&self) -> Self
pub fn unbound_max_height(&self) -> Self
Sets max height to infinity.
Sourcepub fn shrink_max_to(&self, axis: Axis, dim: f64) -> Self
pub fn shrink_max_to(&self, axis: Axis, dim: f64) -> Self
Shrinks the max dimension on the given axis. Does NOT shrink beyond min.
Sourcepub fn shrink_max_width_to(&self, dim: f64) -> Self
pub fn shrink_max_width_to(&self, dim: f64) -> Self
Shrinks the max width to dim. Does NOT shrink beyond min width.
Sourcepub fn shrink_max_height_to(&self, dim: f64) -> Self
pub fn shrink_max_height_to(&self, dim: f64) -> Self
Shrinks the max height to dim. Does NOT shrink beyond min height.
Trait Implementations§
Source§impl Clone for BoxConstraints
impl Clone for BoxConstraints
Source§fn clone(&self) -> BoxConstraints
fn clone(&self) -> BoxConstraints
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more