Skip to content

Commit 28a5a90

Browse files
committed
Add ClipBox::content_must_fill
This adds a flag that forces the child to be at least as large as the parent, which is a common behaviour for scroll views. I'm playing around with using this with the textbox, instead of having our own manual scroll implementation.
1 parent 7971080 commit 28a5a90

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

druid/src/widget/clip_box.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ pub struct ClipBox<T, W> {
121121
port: Viewport,
122122
constrain_horizontal: bool,
123123
constrain_vertical: bool,
124+
must_fill: bool,
124125
}
125126

126127
impl<T, W> ClipBox<T, W> {
@@ -151,6 +152,16 @@ impl<T, W> ClipBox<T, W> {
151152
self
152153
}
153154

155+
/// Builder-style method to set whether the child must fill the view.
156+
///
157+
/// If `false` (the default) there is no minimum constraint on the child's
158+
/// size. If `true`, the child is passed the same minimum constraints as
159+
/// the `ClipBox`.
160+
pub fn content_must_fill(mut self, must_fill: bool) -> Self {
161+
self.must_fill = must_fill;
162+
self
163+
}
164+
154165
/// Returns a reference to the child widget.
155166
pub fn child(&self) -> &W {
156167
self.child.widget()
@@ -201,6 +212,16 @@ impl<T, W> ClipBox<T, W> {
201212
pub fn set_constrain_vertical(&mut self, constrain: bool) {
202213
self.constrain_vertical = constrain;
203214
}
215+
216+
/// Set whether the child's size must be greater than or equal the size of
217+
/// the `ClipBox`.
218+
///
219+
/// See [`content_must_fill`] for more details.
220+
///
221+
/// [`content_must_fill`]: ClipBox::content_must_fill
222+
pub fn set_content_must_fill(&mut self, must_fill: bool) {
223+
self.must_fill = must_fill;
224+
}
204225
}
205226

206227
impl<T, W: Widget<T>> ClipBox<T, W> {
@@ -211,6 +232,7 @@ impl<T, W: Widget<T>> ClipBox<T, W> {
211232
port: Default::default(),
212233
constrain_horizontal: false,
213234
constrain_vertical: false,
235+
must_fill: false,
214236
}
215237
}
216238

@@ -310,8 +332,9 @@ impl<T: Data, W: Widget<T>> Widget<T> for ClipBox<T, W> {
310332
} else {
311333
f64::INFINITY
312334
};
335+
let min_child_size = if self.must_fill { bc.min() } else { Size::ZERO };
313336
let child_bc =
314-
BoxConstraints::new(Size::ZERO, Size::new(max_child_width, max_child_height));
337+
BoxConstraints::new(min_child_size, Size::new(max_child_width, max_child_height));
315338

316339
let content_size = self.child.layout(ctx, &child_bc, data, env);
317340
self.port.content_size = content_size;

0 commit comments

Comments
 (0)