Skip to content

Commit

Permalink
Allow the initial BoxConstraints to not fill the entire screen.
Browse files Browse the repository at this point in the history
Also fix issue with INIT_FLAGS
  • Loading branch information
Philipp-M committed Feb 16, 2024
1 parent e505e09 commit a506749
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl<T: Send + 'static, V: View<T> + 'static> App<T, V> {
widget_state: &mut self.root_state,
cx_state,
};
let bc = BoxConstraints::tight(self.size);
let bc = BoxConstraints::tight(self.size).loosen();
root_pod.layout(&mut layout_cx, &bc);
root_pod.set_origin(&mut layout_cx, Point::ORIGIN);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ expression: buffer
Buffer {
area: Rect { x: 0, y: 0, width: 15, height: 5 },
content: [
"┌─────────────┐",
"│some text ",
" ",
" ",
"└─────────────┘",
"┌─────────",
"│some text ",
"└─────────┘ ",
" ",
" ",
],
styles: [
x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ Buffer {
content: [
"┌─────┐",
"│some │",
"│ │",
"│ │",
"└─────┘",
" ",
" ",
],
styles: [
x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
Expand Down
14 changes: 14 additions & 0 deletions src/widget/box_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ impl BoxConstraints {
)
}

pub fn tighten_max_height(&self) -> Self {
BoxConstraints::new(
Size::new(self.min().width, self.max().height),
Size::new(self.max().width, self.max().height),
)
}

pub fn tighten_max_width(&self) -> Self {
BoxConstraints::new(
Size::new(self.max().width, self.min().height),
Size::new(self.max().width, self.max().height),
)
}

/// Create a "loose" version of the constraints.
///
/// Make a version with zero minimum size, but the same maximum size.
Expand Down
3 changes: 2 additions & 1 deletion src/widget/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ bitflags! {
const INIT_FLAGS = Self::REQUEST_UPDATE.bits()
| Self::REQUEST_LAYOUT.bits()
| Self::REQUEST_PAINT.bits()
| Self::TREE_CHANGED.bits();
| Self::TREE_CHANGED.bits()
| Self::VIEW_CONTEXT_CHANGED.bits();
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/widget/fill_max_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ impl Widget for FillMaxSize {
fn layout(&mut self, cx: &mut LayoutCx, bc: &BoxConstraints) -> Size {
let mut bc = *bc;
if self.fill.contains(Fill::WIDTH) && bc.is_width_bounded() {
bc = bc.constrain_width_to(bc.max().width * self.percent);
bc = bc
.constrain_width_to(bc.max().width * self.percent)
.tighten_max_width();
}
if self.fill.contains(Fill::HEIGHT) && bc.is_height_bounded() {
bc = bc.constrain_height_to(bc.max().height * self.percent);
bc = bc
.constrain_height_to(bc.max().height * self.percent)
.tighten_max_height();
}
self.content.layout(cx, &bc)
}
Expand Down

0 comments on commit a506749

Please sign in to comment.