improv(grid): add justify_content and fix padding
This commit is contained in:
parent
463e07ec4b
commit
289db87373
2 changed files with 22 additions and 5 deletions
|
|
@ -10,8 +10,9 @@ 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::{AlignItems, Dimension, Display, GridPlacement, Style};
|
||||||
use taffy::style_helpers::{auto, length};
|
use taffy::style_helpers::{auto, length};
|
||||||
use taffy::TaffyTree;
|
use taffy::{AlignContent, TaffyTree};
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
#[allow(clippy::too_many_lines)]
|
#[allow(clippy::too_many_lines)]
|
||||||
pub fn resolve<Message>(
|
pub fn resolve<Message>(
|
||||||
renderer: &Renderer,
|
renderer: &Renderer,
|
||||||
|
|
@ -23,6 +24,7 @@ pub fn resolve<Message>(
|
||||||
padding: Padding,
|
padding: Padding,
|
||||||
column_alignment: Alignment,
|
column_alignment: Alignment,
|
||||||
row_alignment: Alignment,
|
row_alignment: Alignment,
|
||||||
|
justify_content: Option<AlignContent>,
|
||||||
column_spacing: f32,
|
column_spacing: f32,
|
||||||
row_spacing: f32,
|
row_spacing: f32,
|
||||||
tree: &mut [Tree],
|
tree: &mut [Tree],
|
||||||
|
|
@ -44,23 +46,29 @@ pub fn resolve<Message>(
|
||||||
nodes.push(child_node);
|
nodes.push(child_node);
|
||||||
|
|
||||||
let c_size = child_widget.size();
|
let c_size = child_widget.size();
|
||||||
let (width, justify_self) = match c_size.width {
|
let (width, flex_grow, justify_self) = match c_size.width {
|
||||||
Length::Fill | Length::FillPortion(_) => (Dimension::Auto, Some(AlignItems::Stretch)),
|
Length::Fill | Length::FillPortion(_) => {
|
||||||
_ => (length(size.width), None),
|
(Dimension::Auto, 1.0, Some(AlignItems::Stretch))
|
||||||
|
}
|
||||||
|
_ => (length(size.width), 0.0, 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 {
|
||||||
|
flex_grow,
|
||||||
|
|
||||||
grid_column: Line {
|
grid_column: Line {
|
||||||
start: GridPlacement::Line((assignment.column as i16).into()),
|
start: GridPlacement::Line((assignment.column as i16).into()),
|
||||||
end: GridPlacement::Line(
|
end: GridPlacement::Line(
|
||||||
(assignment.column as i16 + assignment.width as i16).into(),
|
(assignment.column as i16 + assignment.width as i16).into(),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
|
||||||
grid_row: Line {
|
grid_row: Line {
|
||||||
start: GridPlacement::Line((assignment.row as i16).into()),
|
start: GridPlacement::Line((assignment.row as i16).into()),
|
||||||
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,
|
width,
|
||||||
height: match c_size.height {
|
height: match c_size.height {
|
||||||
|
|
@ -68,7 +76,9 @@ pub fn resolve<Message>(
|
||||||
_ => length(size.height),
|
_ => length(size.height),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
justify_self,
|
justify_self,
|
||||||
|
|
||||||
..Style::default()
|
..Style::default()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -108,6 +118,8 @@ pub fn resolve<Message>(
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
justify_content,
|
||||||
|
|
||||||
padding: Rect {
|
padding: Rect {
|
||||||
left: length(padding.left),
|
left: length(padding.left),
|
||||||
right: length(padding.right),
|
right: length(padding.right),
|
||||||
|
|
@ -187,5 +199,5 @@ pub fn resolve<Message>(
|
||||||
height: grid_layout.content_size.height,
|
height: grid_layout.content_size.height,
|
||||||
};
|
};
|
||||||
|
|
||||||
Node::with_children(grid_size.expand(padding), nodes)
|
Node::with_children(grid_size, nodes)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,9 @@ pub struct Grid<'a, Message> {
|
||||||
column_alignment: Alignment,
|
column_alignment: Alignment,
|
||||||
/// Alignment across rows
|
/// Alignment across rows
|
||||||
row_alignment: Alignment,
|
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.
|
/// Sets the space between each column of items.
|
||||||
column_spacing: u16,
|
column_spacing: u16,
|
||||||
/// Sets the space between each item in a row.
|
/// Sets the space between each item in a row.
|
||||||
|
|
@ -50,6 +53,7 @@ impl<'a, Message> Grid<'a, Message> {
|
||||||
padding: Padding::ZERO,
|
padding: Padding::ZERO,
|
||||||
column_alignment: Alignment::Start,
|
column_alignment: Alignment::Start,
|
||||||
row_alignment: Alignment::Start,
|
row_alignment: Alignment::Start,
|
||||||
|
justify_content: None,
|
||||||
column_spacing: 4,
|
column_spacing: 4,
|
||||||
row_spacing: 4,
|
row_spacing: 4,
|
||||||
width: Length::Shrink,
|
width: Length::Shrink,
|
||||||
|
|
@ -138,6 +142,7 @@ impl<'a, Message: 'static + Clone> Widget<Message, crate::Theme, Renderer> for G
|
||||||
self.padding,
|
self.padding,
|
||||||
self.column_alignment,
|
self.column_alignment,
|
||||||
self.row_alignment,
|
self.row_alignment,
|
||||||
|
self.justify_content,
|
||||||
f32::from(self.column_spacing),
|
f32::from(self.column_spacing),
|
||||||
f32::from(self.row_spacing),
|
f32::from(self.row_spacing),
|
||||||
&mut tree.children,
|
&mut tree.children,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue