From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 4C7ED1FF0AB for ; Wed, 07 Oct 2026 10:16:53 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 0D94720B9E; Wed, 07 Oct 2026 10:16:53 +0200 (CEST) From: Dominik Csapak To: yew-devel@lists.proxmox.com Subject: [PATCH yew-widget-toolkit v3] widget: data table: implement auto column sizing based on sample records Date: Wed, 7 Oct 2026 10:15:57 +0200 Message-ID: <20261007081647.947344-1-d.csapak@proxmox.com> X-Mailer: git-send-email 2.47.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.386 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 27SDBJKFS5SS2DO45LL2ZPAPAAEXLROY X-Message-ID-Hash: 27SDBJKFS5SS2DO45LL2ZPAPAAEXLROY X-MailFrom: d.csapak@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Yew framework devel list at Proxmox List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: When a column size is not explicitly given (like "100px" or "15em"), the default size is implicitly determined by the header text. In cases where the content should always be shown (like in an action column) and depends on the theme/density (for padding/width/etc.) this lead to an bad tradeoff: Either a fixed size is used and it's sometimes too wide or too narrow, or some css expression is used that dynamically determines the size. Both are not ideal, so introduce 'sample records' that the data table can use to (invisibly) render a set of dummy rows which can dynamically determine the correct size. This can be used for e.g. setting an example that should always fit, or having a fixed set of ActionIcons, etc. When multiple records are given, the largest one for each column wins. Since too many rendered records can impact the performance, limit the considered records and assert the number in debug mode. Reuse VIRTUAL_SCROLL_TRIGGER as the limit, since that is also the cut-off for the visible rows. Signed-off-by: Dominik Csapak --- changes from v2: * limit the amount of sample records to VIRTUAL_SCROLL_TRIGGER (and debug assert in create) * document this limit and make the variable const and public so it's visible in the documentation. src/widget/data_table/data_table.rs | 99 ++++++++++++++++++++++++-- src/widget/data_table/header_widget.rs | 45 ++++++++++++ src/widget/data_table/mod.rs | 2 +- 3 files changed, 140 insertions(+), 6 deletions(-) diff --git a/src/widget/data_table/data_table.rs b/src/widget/data_table/data_table.rs index f00d9ab..5add94e 100644 --- a/src/widget/data_table/data_table.rs +++ b/src/widget/data_table/data_table.rs @@ -21,10 +21,13 @@ use crate::props::{ use crate::state::{DataStore, Selection, SelectionObserver}; use crate::widget::{Column, Container, get_unique_element_id}; +use pwt_macros::builder; + use super::{ - CellConfiguration, DataTableColumn, DataTableHeader, DataTableKeyboardEvent, - DataTableMouseEvent, DataTableRow, DataTableRowRenderCallback, HeaderWidget, IndexedHeader, - IntoOptionalDataTableRowRenderCallback, create_indexed_header_list, + CellConfiguration, DataTableCellRenderArgs, DataTableColumn, DataTableHeader, + DataTableKeyboardEvent, DataTableMouseEvent, DataTableRow, DataTableRowRenderCallback, + HeaderWidget, IndexedHeader, IntoOptionalDataTableRowRenderCallback, + create_indexed_header_list, }; pub enum HeaderMsg { @@ -109,6 +112,7 @@ pub enum MultiSelectMode { /// * `End`: moves focus to the last cell in the row that contains focus. #[derive(Properties, Derivative)] #[derivative(Clone(bound = ""), PartialEq(bound = ""))] +#[builder] pub struct DataTable { /// Yew key property. #[prop_or_default] @@ -173,6 +177,21 @@ pub struct DataTable { #[prop_or(22)] pub min_row_height: usize, + /// Sample records used for sizing the columns. + /// + /// Default column widths are computed from the header, because the table rows are rendered + /// separately (and only partially with virtual scrolling). These records are rendered + /// invisibly inside the header, using the column render functions, so content based widths + /// like "auto" or "max-content" account for them. For flex widths, they act as minimum width. + /// + /// To make it easier to handle with enum records (often used by trees), this is a list, so + /// multiple variants can be used simultaneously. + /// + /// NOTE: Only up to [`VIRTUAL_SCROLL_TRIGGER`] records are supported for performance reasons. + #[prop_or_default] + #[builder] + pub sample_records: Vec, + /// Selection object. #[prop_or_default] pub selection: Option, @@ -240,7 +259,9 @@ impl CssPaddingBuilder for DataTable {} impl CssMarginBuilder for DataTable {} impl WidgetStyleBuilder for DataTable {} -static VIRTUAL_SCROLL_TRIGGER: usize = 30; +/// The automatic cut-off for virtual scrolling. When the DataTable can be scrolled +/// and there are more rows than this limit, virtual scrolling is automatically enabled. +pub const VIRTUAL_SCROLL_TRIGGER: usize = 30; impl DataTable { /// Create a new instance. @@ -1049,6 +1070,67 @@ impl PwtDataTable { }; } + // Render the cells of the sizing records with the column renderers, wrapped like table cells, + // so that they get the same font and padding when the header measures them. The cells of + // one column are stacked, so the widest one determines the column width. + fn render_column_sizers(&self, props: &DataTable) -> Vec> { + if props.sample_records.is_empty() { + return Vec::new(); + } + + let records: Vec<(Key, &S::Record)> = props + .sample_records + .iter() + .take(VIRTUAL_SCROLL_TRIGGER) + .map(|record| (props.store.extract_key(record), record)) + .collect(); + + let mut column_index = 0; + self.columns + .iter() + .enumerate() + .map(|(column_num, column)| { + if let Some(true) = self.column_hidden.get(column_num) { + return None; + } + + let mut sizer = Container::new().class("pwt-datatable-content"); + for (row_index, (record_key, record)) in records.iter().enumerate() { + let mut args = DataTableCellRenderArgs { + selection: props.selection.clone(), + record: *record, + record_key, + row_index, + column_index, + selected: false, + config: (*self.cell_config).clone(), + is_expanded: false, + is_leaf: true, + level: 0, + }; + + let cell = column.apply_render(&mut args); + + sizer.add_child( + Container::new() + .class(args.config.class) + .styles(args.config.style) + .style("white-space", "nowrap") + // cell widths get reduced by the border width, see + // render_empty_row_with_sizes + .style( + "border-inline-end", + props.bordered.then_some("1px solid transparent"), + ) + .with_child(html! {
{cell}
}), + ); + } + column_index += 1; + Some(sizer.into()) + }) + .collect() + } + fn init_headers(&mut self, props: &DataTable) { let headers = create_indexed_header_list(&props.headers); @@ -1080,6 +1162,12 @@ impl Component for PwtDataTable { props.cell_configuration.clone() }; + debug_assert!( + props.sample_records.len() < VIRTUAL_SCROLL_TRIGGER, + "More than {VIRTUAL_SCROLL_TRIGGER} sample records can have negative performance \ + anything more than that will not be rendered." + ); + let _store_observer = props .store .add_listener(ctx.link().callback(|_| Msg::DataChange)); @@ -1764,7 +1852,8 @@ impl Component for PwtDataTable { .focusable(props.header_focusable && props.show_header) .selection_status(self.selection_status) .header_class(header_class) - .reserve_scroll_space(self.scrollbar_size.unwrap_or_default()), + .reserve_scroll_space(self.scrollbar_size.unwrap_or_default()) + .column_sizers(self.render_column_sizers(props)), ) .into_html_with_ref(self.header_scroll_ref.clone()), ) diff --git a/src/widget/data_table/header_widget.rs b/src/widget/data_table/header_widget.rs index b6f06f6..a588b21 100644 --- a/src/widget/data_table/header_widget.rs +++ b/src/widget/data_table/header_widget.rs @@ -43,6 +43,11 @@ pub struct HeaderWidget { pub focusable: bool, reserve_scroll_space: f64, + + /// Invisible content per column (indexed like the column list), used to size the column + /// tracks. + #[prop_or_default] + pub column_sizers: Vec>, } impl HeaderWidget { @@ -95,6 +100,17 @@ impl HeaderWidget { pub fn set_reserve_scroll_space(&mut self, reserve_scroll_space: f64) { self.reserve_scroll_space = reserve_scroll_space; } + + /// Builder style method to set the column sizers. + pub fn column_sizers(mut self, column_sizers: Vec>) -> Self { + self.set_column_sizers(column_sizers); + self + } + + /// Method to set the column sizers. + pub fn set_column_sizers(&mut self, column_sizers: Vec>) { + self.column_sizers = column_sizers; + } } pub enum Msg { @@ -633,6 +649,35 @@ impl Component for PwtHeaderWidget { &mut header_row, ); + // Put the sizers into their column tracks, so that content based track sizes account for + // them. They have no height and are invisible, so they do not change the header layout. + // Header cells end at row line 10, so use the row after that to avoid interfering with + // the auto placed group headers. + let mut grid_column = 0; + for col_idx in 0..self.state.columns().len() { + if self.state.get_column_hidden(col_idx) { + continue; + } + grid_column += 1; + if let Some(Some(sizer)) = props.column_sizers.get(col_idx) { + header_row.push( + Container::new() + .key(Key::from(format!("sizer-{col_idx}"))) + .attribute("role", "none") + .attribute("aria-hidden", "true") + .attribute( + "style", + format!( + "grid-row: 10;grid-column-start: {grid_column};\ + height: 0;overflow: clip;visibility: hidden;" + ), + ) + .with_child(sizer.clone()) + .into(), + ); + } + } + let column_count = self.state.visible_column_count(); // add some space at the end to make room for the tables vertical scrollbar diff --git a/src/widget/data_table/mod.rs b/src/widget/data_table/mod.rs index 2ab03b6..42b8c21 100644 --- a/src/widget/data_table/mod.rs +++ b/src/widget/data_table/mod.rs @@ -45,7 +45,7 @@ mod data_table; pub(crate) use data_table::HeaderMsg; #[doc(hidden)] pub use data_table::PwtDataTable; -pub use data_table::{DataTable, MultiSelectMode, RowSelectionStatus}; +pub use data_table::{DataTable, MultiSelectMode, RowSelectionStatus, VIRTUAL_SCROLL_TRIGGER}; use yew::prelude::*; -- 2.47.3