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

source

pub const UNBOUNDED: BoxConstraints = _

An unbounded box constraints object.

Can be satisfied by any nonnegative size.

source

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?
examples/input_region.rs (lines 128-131)
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 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.

source

pub fn loosen(&self) -> BoxConstraints

Create a “loose” version of the constraints.

Make a version with zero minimum size, but the same maximum size.

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))
    }
source

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?
examples/scroll.rs (line 58)
56
57
58
59
    fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, _: &T, _: &Env) -> Size {
        ctx.set_paint_insets(INSETS);
        bc.constrain(Size::new(100., 100.))
    }
More examples
Hide additional examples
examples/anim.rs (line 71)
64
65
66
67
68
69
70
71
72
    fn layout(
        &mut self,
        _layout_ctx: &mut LayoutCtx,
        bc: &BoxConstraints,
        _data: &(),
        _env: &Env,
    ) -> Size {
        bc.constrain((100.0, 100.0))
    }
examples/sub_window.rs (line 271)
264
265
266
267
268
269
270
271
272
    fn layout(
        &mut self,
        _ctx: &mut LayoutCtx,
        bc: &BoxConstraints,
        _data: &(),
        _env: &Env,
    ) -> Size {
        bc.constrain(Size::new(800.0, 600.0))
    }
examples/timer.rs (line 80)
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))
    }
examples/custom_widget.rs (line 70)
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)
        }
    }
source

pub fn max(&self) -> Size

Returns the max size of these constraints.

Examples found in repository?
examples/invalidation.rs (line 134)
127
128
129
130
131
132
133
134
135
    fn layout(
        &mut self,
        _ctx: &mut LayoutCtx,
        bc: &BoxConstraints,
        _data: &Vector<Circle>,
        _env: &Env,
    ) -> Size {
        bc.max()
    }
More examples
Hide additional examples
examples/game_of_life.rs (line 289)
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,
        }
    }
examples/custom_widget.rs (line 67)
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)
        }
    }
examples/input_region.rs (line 130)
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 min(&self) -> Size

Returns the min size of these constraints.

source

pub fn is_width_bounded(&self) -> bool

Whether there is an upper bound on the width.

Examples found in repository?
examples/custom_widget.rs (line 66)
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)
        }
    }
source

pub fn is_height_bounded(&self) -> bool

Whether there is an upper bound on the height.

Examples found in repository?
examples/custom_widget.rs (line 66)
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)
        }
    }
source

pub fn debug_check(&self, name: &str)

Check to see if these constraints are legit.

Logs a warning if BoxConstraints are invalid.

source

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.

source

pub fn contains(&self, size: impl Into<Size>) -> bool

Test whether these constraints contain the given Size.

source

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.

source

pub fn unbound_max(&self, axis: Axis) -> Self

Sets the max on a given axis to infinity.

source

pub fn unbound_max_width(&self) -> Self

Sets max width to infinity.

source

pub fn unbound_max_height(&self) -> Self

Sets max height to infinity.

source

pub fn shrink_max_to(&self, axis: Axis, dim: f64) -> Self

Shrinks the max dimension on the given axis. Does NOT shrink beyond min.

source

pub fn shrink_max_width_to(&self, dim: f64) -> Self

Shrinks the max width to dim. Does NOT shrink beyond min width.

source

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

source§

fn clone(&self) -> BoxConstraints

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for BoxConstraints

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq<BoxConstraints> for BoxConstraints

source§

fn eq(&self, other: &BoxConstraints) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for BoxConstraints

source§

impl StructuralPartialEq for BoxConstraints

Auto Trait Implementations§

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> AnyEq for Twhere T: Any + PartialEq<T>,

source§

fn equals(&self, other: &(dyn Any + 'static)) -> bool

source§

fn as_any(&self) -> &(dyn Any + 'static)

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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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