improv(grid): add justify_content and fix padding

This commit is contained in:
Michael Aaron Murphy 2024-05-30 11:41:08 +02:00 committed by Michael Murphy
parent 463e07ec4b
commit 289db87373
2 changed files with 22 additions and 5 deletions

View file

@ -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<Message>(
renderer: &Renderer,
@ -23,6 +24,7 @@ pub fn resolve<Message>(
padding: Padding,
column_alignment: Alignment,
row_alignment: Alignment,
justify_content: Option<AlignContent>,
column_spacing: f32,
row_spacing: f32,
tree: &mut [Tree],
@ -44,23 +46,29 @@ pub fn resolve<Message>(
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<Message>(
_ => length(size.height),
},
},
justify_self,
..Style::default()
});
@ -108,6 +118,8 @@ pub fn resolve<Message>(
},
}),
justify_content,
padding: Rect {
left: length(padding.left),
right: length(padding.right),
@ -187,5 +199,5 @@ pub fn resolve<Message>(
height: grid_layout.content_size.height,
};
Node::with_children(grid_size.expand(padding), nodes)
Node::with_children(grid_size, nodes)
}

View file

@ -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<crate::widget::JustifyContent>,
/// 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<Message, crate::Theme, Renderer> 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,