Implement alignment support for table::column

This commit is contained in:
Héctor Ramón Jiménez 2025-07-16 04:06:38 +02:00
parent 1e89439aff
commit 6e71c7dd6f
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
3 changed files with 67 additions and 18 deletions

View file

@ -1,11 +1,13 @@
#![allow(missing_docs, missing_debug_implementations)]
use crate::core;
use crate::core::alignment;
use crate::core::layout;
use crate::core::mouse;
use crate::core::renderer;
use crate::core::widget;
use crate::core::{
Background, Element, Layout, Length, Pixels, Rectangle, Size, Widget,
Alignment, Background, Element, Layout, Length, Pixels, Rectangle, Size,
Widget,
};
pub fn table<'a, R, T, Message, Theme, Renderer>(
@ -33,6 +35,8 @@ where
header: header.into(),
view: Box::new(move |data| view(data).into()),
width: Length::Shrink,
align_x: alignment::Horizontal::Left,
align_y: alignment::Vertical::Top,
}
}
@ -40,7 +44,7 @@ pub struct Table<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer>
where
Theme: Catalog,
{
columns: Vec<Length>,
columns: Vec<Column_>,
cells: Vec<Element<'a, Message, Theme, Renderer>>,
width: Length,
height: Length,
@ -51,6 +55,12 @@ where
class: Theme::Class<'a>,
}
struct Column_ {
width: Length,
align_x: alignment::Horizontal,
align_y: alignment::Vertical,
}
impl<'a, Message, Theme, Renderer> Table<'a, Message, Theme, Renderer>
where
Theme: Catalog,
@ -88,13 +98,17 @@ where
width = width.enclose(column.width);
column.width
Column_ {
width: column.width,
align_x: column.align_x,
align_y: column.align_y,
}
})
.collect();
if width == Length::Shrink {
if let Some(first) = columns.first_mut() {
*first = Length::Fill;
first.width = Length::Fill;
}
}
@ -225,7 +239,7 @@ where
let column = i / rows;
let row = i % rows;
let width = self.columns[column];
let width = self.columns[column].width;
let size = cell.as_widget().size();
if row == 0 {
@ -307,7 +321,7 @@ where
let size = cell.as_widget().size();
let width = self.columns[column];
let width = self.columns[column].width;
let width_factor = width.fill_factor();
if row == 0 {
@ -387,7 +401,16 @@ where
}
}
let Column_ {
align_x, align_y, ..
} = &self.columns[column];
cell.move_to_mut((x, y));
cell.align_mut(
Alignment::from(*align_x),
Alignment::from(*align_y),
Size::new(metrics.columns[column], metrics.rows[row]),
);
y += metrics.rows[row] + spacing_y;
}
@ -505,6 +528,8 @@ pub struct Column<
header: Element<'a, Message, Theme, Renderer>,
view: Box<dyn Fn(T) -> Element<'a, Message, Theme, Renderer> + 'a>,
width: Length,
align_x: alignment::Horizontal,
align_y: alignment::Vertical,
}
impl<'a, T, Message, Theme, Renderer> Column<'a, T, Message, Theme, Renderer> {
@ -512,6 +537,24 @@ impl<'a, T, Message, Theme, Renderer> Column<'a, T, Message, Theme, Renderer> {
self.width = width.into();
self
}
/// Sets the alignment for the horizontal axis of the [`Column`].
pub fn align_x(
mut self,
alignment: impl Into<alignment::Horizontal>,
) -> Self {
self.align_x = alignment.into();
self
}
/// Sets the alignment for the vertical axis of the [`Column`].
pub fn align_y(
mut self,
alignment: impl Into<alignment::Vertical>,
) -> Self {
self.align_y = alignment.into();
self
}
}
#[derive(Debug, Clone, Copy)]