diff --git a/src/widget/grid/layout.rs b/src/widget/grid/layout.rs index d61d13c9..6423c377 100644 --- a/src/widget/grid/layout.rs +++ b/src/widget/grid/layout.rs @@ -10,8 +10,9 @@ use iced_core::{Alignment, Length, Padding, Point, Size}; use taffy::geometry::{Line, Rect}; use taffy::style::{AlignItems, Dimension, Display, GridPlacement, Style}; use taffy::style_helpers::{auto, length}; -use taffy::TaffyTree; +use taffy::{AlignContent, TaffyTree}; +#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_lines)] pub fn resolve( renderer: &Renderer, @@ -23,6 +24,7 @@ pub fn resolve( padding: Padding, column_alignment: Alignment, row_alignment: Alignment, + justify_content: Option, column_spacing: f32, row_spacing: f32, tree: &mut [Tree], @@ -44,23 +46,29 @@ pub fn resolve( nodes.push(child_node); let c_size = child_widget.size(); - let (width, justify_self) = match c_size.width { - Length::Fill | Length::FillPortion(_) => (Dimension::Auto, Some(AlignItems::Stretch)), - _ => (length(size.width), None), + let (width, flex_grow, justify_self) = match c_size.width { + Length::Fill | Length::FillPortion(_) => { + (Dimension::Auto, 1.0, Some(AlignItems::Stretch)) + } + _ => (length(size.width), 0.0, None), }; // Attach widget as leaf to be later assigned to grid. let leaf = taffy.new_leaf(Style { + flex_grow, + grid_column: Line { start: GridPlacement::Line((assignment.column as i16).into()), end: GridPlacement::Line( (assignment.column as i16 + assignment.width as i16).into(), ), }, + grid_row: Line { start: GridPlacement::Line((assignment.row as i16).into()), end: GridPlacement::Line((assignment.row as i16 + assignment.height as i16).into()), }, + size: taffy::geometry::Size { width, height: match c_size.height { @@ -68,7 +76,9 @@ pub fn resolve( _ => length(size.height), }, }, + justify_self, + ..Style::default() }); @@ -108,6 +118,8 @@ pub fn resolve( }, }), + justify_content, + padding: Rect { left: length(padding.left), right: length(padding.right), @@ -187,5 +199,5 @@ pub fn resolve( height: grid_layout.content_size.height, }; - Node::with_children(grid_size.expand(padding), nodes) + Node::with_children(grid_size, nodes) } diff --git a/src/widget/grid/widget.rs b/src/widget/grid/widget.rs index 6c16b79f..7ba1cfcc 100644 --- a/src/widget/grid/widget.rs +++ b/src/widget/grid/widget.rs @@ -26,6 +26,9 @@ pub struct Grid<'a, Message> { column_alignment: Alignment, /// Alignment across rows row_alignment: Alignment, + /// Defines how the content will be justified. + #[setters(into, strip_option)] + justify_content: Option, /// Sets the space between each column of items. column_spacing: u16, /// Sets the space between each item in a row. @@ -50,6 +53,7 @@ impl<'a, Message> Grid<'a, Message> { padding: Padding::ZERO, column_alignment: Alignment::Start, row_alignment: Alignment::Start, + justify_content: None, column_spacing: 4, row_spacing: 4, width: Length::Shrink, @@ -138,6 +142,7 @@ impl<'a, Message: 'static + Clone> Widget for G self.padding, self.column_alignment, self.row_alignment, + self.justify_content, f32::from(self.column_spacing), f32::from(self.row_spacing), &mut tree.children,