Add align_x support for row::Wrapping

This commit is contained in:
Héctor Ramón Jiménez 2025-08-03 22:16:45 +02:00
parent c98f3d525a
commit f8a8a85717
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -160,6 +160,7 @@ where
Wrapping {
row: self,
vertical_spacing: None,
align_x: alignment::Horizontal::Left,
}
}
}
@ -368,6 +369,7 @@ pub struct Wrapping<
> {
row: Row<'a, Message, Theme, Renderer>,
vertical_spacing: Option<f32>,
align_x: alignment::Horizontal,
}
impl<Message, Theme, Renderer> Wrapping<'_, Message, Theme, Renderer> {
@ -376,6 +378,15 @@ impl<Message, Theme, Renderer> Wrapping<'_, Message, Theme, Renderer> {
self.vertical_spacing = Some(amount.into().0);
self
}
/// Sets the horizontal alignment of the wrapping [`Row`].
pub fn align_x(
mut self,
align_x: impl Into<alignment::Horizontal>,
) -> Self {
self.align_x = align_x.into();
self
}
}
impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer>
@ -423,9 +434,9 @@ where
Alignment::End => 1.0,
};
let align = |row_start: std::ops::Range<usize>,
row_height: f32,
children: &mut Vec<layout::Node>| {
let align_y = |row_start: std::ops::Range<usize>,
row_height: f32,
children: &mut Vec<layout::Node>| {
if align_factor != 0.0 {
for node in &mut children[row_start] {
let height = node.size().height;
@ -450,7 +461,7 @@ where
if x != 0.0 && x + child_size.width > max_width {
intrinsic_size.width = intrinsic_size.width.max(x - spacing);
align(row_start..i, row_height, &mut children);
align_y(row_start..i, row_height, &mut children);
y += row_height + vertical_spacing;
x = 0.0;
@ -473,7 +484,42 @@ where
}
intrinsic_size.height = y + row_height;
align(row_start..children.len(), row_height, &mut children);
align_y(row_start..children.len(), row_height, &mut children);
let align_factor = match self.align_x {
alignment::Horizontal::Left => 0.0,
alignment::Horizontal::Center => 2.0,
alignment::Horizontal::Right => 1.0,
};
if align_factor != 0.0 {
let total_width = intrinsic_size.width;
let mut row_start = 0;
for i in 0..children.len() {
let bounds = children[i].bounds();
let row_width = bounds.x + bounds.width;
let next_x = children
.get(i + 1)
.map(|node| node.bounds().x)
.unwrap_or_default();
if next_x == 0.0 {
let translation = Vector::new(
(total_width - row_width) / align_factor,
0.0,
);
for node in &mut children[row_start..=i] {
node.translate_mut(translation);
}
row_start = i + 1;
}
}
}
let size =
limits.resolve(self.row.width, self.row.height, intrinsic_size);