fix(grid): width set to Length::Fill causes 0-width layout

This commit is contained in:
Michael Aaron Murphy 2023-10-03 23:44:42 +02:00
parent 2060597959
commit cce4a54e14
No known key found for this signature in database
GPG key ID: B2732D4240C9212C

View file

@ -7,9 +7,9 @@ use iced_core::layout::{Limits, Node};
use iced_core::{Alignment, Length, Padding, Point, Size}; use iced_core::{Alignment, Length, Padding, Point, Size};
use taffy::geometry::{Line, Rect}; use taffy::geometry::{Line, Rect};
use taffy::style::{AlignItems, Dimension, Display, GridPlacement, Style}; use taffy::style::{AlignContent, AlignItems, Dimension, Display, GridPlacement, Style};
use taffy::style_helpers::{auto, length}; use taffy::style_helpers::{auto, length};
use taffy::Taffy; use taffy::{LayoutTree, Taffy};
#[allow(clippy::too_many_lines)] #[allow(clippy::too_many_lines)]
pub fn resolve<Message>( pub fn resolve<Message>(
@ -41,6 +41,11 @@ pub fn resolve<Message>(
nodes.push(child_node); nodes.push(child_node);
let (width, justify_self) = match child_widget.width() {
Length::Fill | Length::FillPortion(_) => (Dimension::Auto, Some(AlignItems::Stretch)),
_ => (length(size.width), None),
};
// Attach widget as leaf to be later assigned to grid. // Attach widget as leaf to be later assigned to grid.
let leaf = taffy.new_leaf(Style { let leaf = taffy.new_leaf(Style {
grid_column: Line { grid_column: Line {
@ -54,16 +59,13 @@ pub fn resolve<Message>(
end: GridPlacement::Line((assignment.row as i16 + assignment.height as i16).into()), end: GridPlacement::Line((assignment.row as i16 + assignment.height as i16).into()),
}, },
size: taffy::geometry::Size { size: taffy::geometry::Size {
width: match child_widget.width() { width,
Length::Fill | Length::FillPortion(_) => Dimension::Auto,
_ => length(size.width),
},
height: match child_widget.height() { height: match child_widget.height() {
Length::Fill | Length::FillPortion(_) => Dimension::Auto, Length::Fill | Length::FillPortion(_) => Dimension::Auto,
_ => length(size.height), _ => length(size.height),
}, },
}, },
justify_self,
..Style::default() ..Style::default()
}); });
@ -153,12 +155,19 @@ pub fn resolve<Message>(
} }
}; };
for (leaf, node) in leafs.into_iter().zip(nodes.iter_mut()) { for (leaf, (child, node)) in leafs.into_iter().zip(items.iter().zip(nodes.iter_mut())) {
if let Ok(leaf_layout) = taffy.layout(leaf) { if let Ok(leaf_layout) = taffy.layout(leaf) {
let location = leaf_layout.location; let child_widget = child.as_widget();
match child_widget.width() {
Length::Fill | Length::FillPortion(_) => {
*node = child_widget.layout(renderer, &limits.width(leaf_layout.size.width));
}
_ => (),
}
node.move_to(Point { node.move_to(Point {
x: location.x, x: leaf_layout.location.x,
y: location.y, y: leaf_layout.location.y,
}); });
} }
} }