Struct druid::BoxConstraints
source · 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 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()
}
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
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))
}
fn paint(&mut self, ctx: &mut PaintCtx, data: &u32, env: &Env) {
self.simple_box.paint(ctx, data, env);
}
}
struct SimpleBox;
impl Widget<u32> for SimpleBox {
fn event(&mut self, _ctx: &mut EventCtx, _event: &Event, _data: &mut u32, _env: &Env) {}
fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, _data: &u32, _env: &Env) {
if let LifeCycle::HotChanged(_) = event {
ctx.request_paint();
}
}
fn update(&mut self, _ctx: &mut UpdateCtx, _old_data: &u32, _data: &u32, _env: &Env) {}
fn layout(
&mut self,
_ctx: &mut LayoutCtx,
bc: &BoxConstraints,
_data: &u32,
_env: &Env,
) -> Size {
bc.constrain((50.0, 50.0))
}
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
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()
//
// bx.max() returns the maximum size of the widget. Be careful
// using this, since always make sure the widget is bounded.
// If bx.max() is used in a scrolling widget things will probably
// not work correctly.
if bc.is_width_bounded() && bc.is_height_bounded() {
bc.max()
} else {
let size = Size::new(100.0, 100.0);
bc.constrain(size)
}
}
sourcepub fn max(&self) -> Size
pub fn max(&self) -> Size
Returns the max size of these constraints.
Examples found in repository?
More examples
282 283 284 285 286 287 288 289 290 291 292 293 294 295
fn layout(
&mut self,
_layout_ctx: &mut LayoutCtx,
bc: &BoxConstraints,
_data: &AppData,
_env: &Env,
) -> Size {
let max_size = bc.max();
let min_side = max_size.height.min(max_size.width);
Size {
width: min_side,
height: min_side,
}
}
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
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()
//
// bx.max() returns the maximum size of the widget. Be careful
// using this, since always make sure the widget is bounded.
// If bx.max() is used in a scrolling widget things will probably
// not work correctly.
if bc.is_width_bounded() && bc.is_height_bounded() {
bc.max()
} else {
let size = Size::new(100.0, 100.0);
bc.constrain(size)
}
}
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()
}
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
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()
//
// bx.max() returns the maximum size of the widget. Be careful
// using this, since always make sure the widget is bounded.
// If bx.max() is used in a scrolling widget things will probably
// not work correctly.
if bc.is_width_bounded() && bc.is_height_bounded() {
bc.max()
} else {
let size = Size::new(100.0, 100.0);
bc.constrain(size)
}
}
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
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()
//
// bx.max() returns the maximum size of the widget. Be careful
// using this, since always make sure the widget is bounded.
// If bx.max() is used in a scrolling widget things will probably
// not work correctly.
if bc.is_width_bounded() && bc.is_height_bounded() {
bc.max()
} else {
let size = Size::new(100.0, 100.0);
bc.constrain(size)
}
}
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 BoxConstraint
s that minimises the difference between the
returned Size
’s aspect ratio and aspect_ratio
, where aspect ratio is defined as
height / width
.
If multiple Size
s 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 moresource§impl Debug for BoxConstraints
impl Debug for BoxConstraints
source§impl PartialEq<BoxConstraints> for BoxConstraints
impl PartialEq<BoxConstraints> for BoxConstraints
source§fn eq(&self, other: &BoxConstraints) -> bool
fn eq(&self, other: &BoxConstraints) -> bool
self
and other
values to be equal, and is used
by ==
.