Skip to content

Commit 4454744

Browse files
committed
add ParentFill widget size option
1 parent deb48e9 commit 4454744

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/theme_definition.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,10 @@ pub enum WidthRelative {
547547
/// Width is equal to the parent widget's inner width plus the `x` field of the widget's `size`.
548548
Parent,
549549

550+
/// Widget is set such that this widget fills the parent widget's inner widget, accounting for this
551+
/// widget's position within the parent, plus the `x` field of the widget's `size`
552+
ParentFill,
553+
550554
/// Width is equal to the text layout width plus the `x` field of the widget's `size`.
551555
Text,
552556
}
@@ -562,6 +566,10 @@ pub enum HeightRelative {
562566
/// Height is equal to the parent widget's inner height plus the `y` field of the widget's `size`.
563567
Parent,
564568

569+
/// Height is set such that this widget fills the parent widget's inner height, accounting for this
570+
/// widget's position within the parent, plus the `y` field of the widget's `size`
571+
ParentFill,
572+
565573
/// Height is sized so that the widget's inner height just encompasses all child widgets, plus the `y` field
566574
/// of the widget's `size`. Note that any children of this Widget should not use `Parent` height if this
567575
/// is used.

src/widget.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,19 +431,27 @@ impl<'a> WidgetBuilder<'a> {
431431
let x = match self.data.width_from {
432432
WidthRelative::Children => raw.x, // this will be added to after children are layed out
433433
WidthRelative::Normal => raw.x,
434-
WidthRelative::Parent => raw.x + parent.size.x - parent.border.horizontal(),
434+
WidthRelative::Parent | WidthRelative::ParentFill => raw.x + parent.size.x - parent.border.horizontal(),
435435
WidthRelative::Text => raw.x + self.calculate_single_line_text_width() + 2.0 * widget.border.horizontal(),
436436
};
437+
437438
let y = match self.data.height_from {
438439
HeightRelative::Children => raw.y, // this will be added to after children are layed out
439440
HeightRelative::Normal => raw.y,
440-
HeightRelative::Parent => raw.y + parent.size.y - parent.border.vertical(),
441+
HeightRelative::Parent | HeightRelative::ParentFill => raw.y + parent.size.y - parent.border.vertical(),
441442
HeightRelative::FontLine => raw.y + widget.font.map_or(0.0, |sum| sum.line_height) + widget.border.vertical(),
442443
};
443-
let self_size = Point { x, y } + state_resize;
444+
let mut self_size = Point { x, y } + state_resize;
444445

445446
let pos = pos(parent, self.data.raw_pos, self_size, self.data.align);
446447

448+
if let HeightRelative::ParentFill = self.data.height_from {
449+
self_size.y -= pos.y - parent.pos.y;
450+
}
451+
if let WidthRelative::ParentFill = self.data.width_from {
452+
self_size.x -= pos.x - parent.pos.x;
453+
}
454+
447455
self.widget.pos = pos + state_moved;
448456
self.widget.size = self_size;
449457
self.data.recalc_pos_size = false;

0 commit comments

Comments
 (0)