diff --git a/linux/changelog.d/4110.fixed.md b/linux/changelog.d/4110.fixed.md new file mode 100644 index 0000000000..6cefb609b2 --- /dev/null +++ b/linux/changelog.d/4110.fixed.md @@ -0,0 +1 @@ +The row numbers stay at the left edge while the columns scroll sideways. diff --git a/linux/changelog.d/4143.fixed.md b/linux/changelog.d/4143.fixed.md new file mode 100644 index 0000000000..ef09f82fc1 --- /dev/null +++ b/linux/changelog.d/4143.fixed.md @@ -0,0 +1 @@ +The row numbers that stay in view while columns scroll sideways carry the alternate row backgrounds too. diff --git a/linux/crates/app/src/ui/data_grid/imp.rs b/linux/crates/app/src/ui/data_grid/imp.rs index 1b043907fd..bdb68cf824 100644 --- a/linux/crates/app/src/ui/data_grid/imp.rs +++ b/linux/crates/app/src/ui/data_grid/imp.rs @@ -51,6 +51,14 @@ pub struct DataGrid { #[template_child] pub column_view: TemplateChild, #[template_child] + pub row_gutter: TemplateChild, + #[template_child] + pub gutter_header: TemplateChild, + #[template_child] + pub gutter_scroll: TemplateChild, + #[template_child] + pub gutter_list: TemplateChild, + #[template_child] pub empty_page: TemplateChild, #[template_child] pub detail_pane: TemplateChild, @@ -86,6 +94,7 @@ pub struct DataGrid { pub editing: Cell, /// The window's focus handler, while the grid is in a window. pub focus_watch: RefCell, glib::SignalHandlerId)>>, + pub(super) pinned_numbers: std::rc::Rc, pub(super) font_watch: RefCell>, pub(super) stripes_watch: RefCell>, } diff --git a/linux/crates/app/src/ui/data_grid/mod.rs b/linux/crates/app/src/ui/data_grid/mod.rs index f1b2db8c4b..233d573213 100644 --- a/linux/crates/app/src/ui/data_grid/mod.rs +++ b/linux/crates/app/src/ui/data_grid/mod.rs @@ -17,6 +17,7 @@ mod grid_store; mod imp; mod keys; mod layout_binding; +mod pinned_numbers; mod pointer; mod references; mod reversed_list; @@ -86,6 +87,7 @@ impl DataGrid { keys::install(&grid); pointer::install(&grid); row_number_column::install_row_factory(&grid); + pinned_numbers::install(&grid); find_bar::install(&grid); imp.detail_pane.attach(&grid); grid.set_columns(columns, sorting, rules); diff --git a/linux/crates/app/src/ui/data_grid/pinned_numbers.rs b/linux/crates/app/src/ui/data_grid/pinned_numbers.rs new file mode 100644 index 0000000000..9eee825312 --- /dev/null +++ b/linux/crates/app/src/ui/data_grid/pinned_numbers.rs @@ -0,0 +1,221 @@ +//! The row numbers stay in view while the columns scroll sideways, as +//! they do in the macOS grid. GtkColumnView has no frozen column, so once +//! the columns have scrolled, a list of the numbers alone is drawn over +//! the first column, which holds their place, and follows the rows' +//! vertical scroll. + +use std::cell::{Cell, RefCell}; +use std::rc::Rc; + +use gtk4::prelude::*; +use gtk4::subclass::prelude::*; +use gtk4::{self as gtk, glib}; + +use super::row_number_column::{Watches, row_number, unwatch}; +use super::{DataGrid, GridRow}; + +/// A pixel of sideways scroll is not yet a column out of place. +const SCROLLED_PX: f64 = 0.5; + +/// What the gutter keeps between its rows being drawn and measured. +#[derive(Default)] +pub(super) struct Pinned { + /// The rows' height in the column view, which the numbers' rows + /// take, so the two lists stay level however far they scroll. + row_height: Cell, + cells: RefCell>>, +} + +pub(super) fn install(grid: &DataGrid) { + let imp = grid.imp(); + imp.gutter_header + .set_label(&crate::i18n::pgettext("row number column", "#")); + imp.row_gutter.update_state(&[gtk::accessible::State::Hidden(true)]); + imp.gutter_list.set_model(Some(&imp.selection.0)); + imp.gutter_list.set_factory(Some(&factory(grid))); + + let follow = |adjustment: >k::Adjustment| { + let weak = grid.downgrade(); + adjustment.connect_value_changed(move |_| { + if let Some(grid) = weak.upgrade() { + sync(&grid); + } + }); + }; + follow(&imp.scrolled.hadjustment()); + follow(&imp.scrolled.vadjustment()); + // The numbers' own list measures after it is shown, so the offset it + // was given then may have been clamped. + let weak = grid.downgrade(); + imp.gutter_scroll.vadjustment().connect_changed(move |_| { + if let Some(grid) = weak.upgrade() { + sync(&grid); + } + }); +} + +fn factory(grid: &DataGrid) -> gtk::SignalListItemFactory { + let factory = gtk::SignalListItemFactory::new(); + let pinned = grid.imp().pinned_numbers.clone(); + { + let pinned = pinned.clone(); + factory.connect_setup(move |_, item| { + let Some(item) = item.downcast_ref::() else { + return; + }; + item.set_property("focusable", false); + let icon = gtk::Image::builder().pixel_size(16).visible(false).build(); + icon.set_accessible_role(gtk::AccessibleRole::Presentation); + let number = gtk::Inscription::builder() + .xalign(1.0) + .hexpand(true) + .valign(gtk::Align::Center) + .build(); + number.add_css_class("numeric"); + number.add_css_class("dimmed"); + let content = gtk::Box::new(gtk::Orientation::Horizontal, 4); + content.add_css_class("row-gutter-cell"); + content.set_height_request(pinned.row_height.get()); + content.append(&icon); + content.append(&number); + item.set_child(Some(&content)); + pinned + .cells + .borrow_mut() + .push(content.upcast::().downgrade()); + }); + } + let watches: Watches = Rc::default(); + let weak = grid.downgrade(); + { + let watches = watches.clone(); + factory.connect_bind(move |_, item| { + let (Some(item), Some(grid)) = (item.downcast_ref::(), weak.upgrade()) else { + return; + }; + let Some(row) = item.item().and_downcast::() else { + return; + }; + let show = { + let (grid, item) = (grid.downgrade(), item.downgrade()); + move || { + if let (Some(grid), Some(item)) = (grid.upgrade(), item.upgrade()) { + show_item(&grid, &item); + } + } + }; + show(); + let on_row = { + let show = show.clone(); + row.connect_revision_notify(move |_| show()) + }; + let on_position = item.connect_notify_local(Some("position"), move |_, _| show()); + watches.borrow_mut().insert( + item.clone().upcast(), + vec![ + (row.upcast_ref::().downgrade(), on_row), + (item.upcast_ref::().downgrade(), on_position), + ], + ); + }); + } + factory.connect_unbind(move |_, item| unwatch(&watches, item.upcast_ref())); + factory +} + +fn show_item(grid: &DataGrid, item: >k::ListItem) { + let (Some(row), Some(content)) = (item.item().and_downcast::(), item.child()) else { + return; + }; + let (Some(icon), Some(number)) = ( + content.first_child().and_downcast::(), + content.last_child().and_downcast::(), + ) else { + return; + }; + // The stripe of the row beside it, which the rows' own cells carry. + let position = item.position(); + let odd = position != gtk::INVALID_LIST_POSITION && !position.is_multiple_of(2); + super::grid_cell::set_class(&content, "tp-stripe", odd); + let state = grid.tracked().row_state(&row); + icon.set_icon_name(state.icon()); + icon.set_visible(state.icon().is_some()); + number.set_text( + row_number(grid, &row, item.position()) + .map(|n| n.to_string()) + .as_deref(), + ); +} + +/// Show the numbers once the columns have scrolled, level with the rows +/// and as wide as the column they cover. +pub(super) fn sync(grid: &DataGrid) { + let imp = grid.imp(); + let scrolled = imp.scrolled.hadjustment().value() > SCROLLED_PX; + imp.row_gutter.set_visible(scrolled); + if !scrolled { + return; + } + if let Some(column) = imp.columns.borrow().row_numbers() { + imp.row_gutter.set_width_request(column.fixed_width()); + } + let view = grid.column_view(); + if let Some(height) = header_height(&view) { + imp.gutter_header.set_height_request(height); + } + // The separator under a row is drawn beside it, not in it. + if let Some(height) = row_height(&view).map(|height| height + i32::from(view.shows_row_separators())) + && imp.pinned_numbers.row_height.replace(height) != height + { + imp.pinned_numbers + .cells + .borrow_mut() + .retain(|cell| match cell.upgrade() { + Some(cell) => { + cell.set_height_request(height); + true + } + None => false, + }); + } + // A horizontal scrollbar that takes room of its own is not covered. + let bar = imp.scrolled.hscrollbar(); + let below = if imp.scrolled.is_overlay_scrolling() || !bar.is_visible() { + 0 + } else { + bar.height() + }; + imp.row_gutter.set_margin_bottom(below); + imp.gutter_scroll + .vadjustment() + .set_value(imp.scrolled.vadjustment().value()); +} + +fn header_height(view: >k::ColumnView) -> Option { + let mut child = view.first_child(); + while let Some(widget) = child { + if widget.css_name() == "header" && widget.height() > 0 { + return Some(widget.height()); + } + child = widget.next_sibling(); + } + None +} + +/// The height of any row the view has drawn. +fn row_height(view: >k::ColumnView) -> Option { + fn find(widget: >k::Widget) -> Option { + if widget.css_name() == "row" && widget.height() > 0 { + return Some(widget.height()); + } + let mut child = widget.first_child(); + while let Some(next) = child { + if let Some(height) = find(&next) { + return Some(height); + } + child = next.next_sibling(); + } + None + } + find(view.upcast_ref()) +} diff --git a/linux/crates/app/src/ui/data_grid/row_number_column.rs b/linux/crates/app/src/ui/data_grid/row_number_column.rs index 1ce4720d78..d660a822ac 100644 --- a/linux/crates/app/src/ui/data_grid/row_number_column.rs +++ b/linux/crates/app/src/ui/data_grid/row_number_column.rs @@ -12,7 +12,7 @@ use super::{DataGrid, GridRow, grid_menu}; const CHROME_PX: i32 = 16 + 4 + 13; /// The handlers a bound gutter cell or row keeps, dropped at unbind. -type Watches = Rc, glib::SignalHandlerId)>>>>; +pub(super) type Watches = Rc, glib::SignalHandlerId)>>>>; /// The first column: each row's number and the icon of its pending /// change. GtkColumnView has no row headers, so it is a column like the @@ -116,7 +116,7 @@ fn show_cell(grid: &DataGrid, cell: >k::ColumnViewCell) { } /// A saved row's number counts from the first row read; a draft has none. -fn row_number(grid: &DataGrid, row: &GridRow, position: u32) -> Option { +pub(super) fn row_number(grid: &DataGrid, row: &GridRow, position: u32) -> Option { if row.draft_id().is_some() || position == gtk::INVALID_LIST_POSITION { return None; } @@ -124,7 +124,7 @@ fn row_number(grid: &DataGrid, row: &GridRow, position: u32) -> Option { Some(grid.row_base() + u64::from(position.saturating_sub(drafts)) + 1) } -fn unwatch(watches: &Watches, item: &glib::Object) { +pub(super) fn unwatch(watches: &Watches, item: &glib::Object) { if let Some(handlers) = watches.borrow_mut().remove(item) { for (object, handler) in handlers { if let Some(object) = object.upgrade() { @@ -146,6 +146,7 @@ pub fn fit(grid: &DataGrid) { let metrics = grid.column_view().create_pango_context().metrics(None, None); let digit_px = pango::units_to_double(metrics.approximate_digit_width()); column.set_fixed_width((f64::from(digits.max(2)) * digit_px).ceil() as i32 + CHROME_PX); + super::pinned_numbers::sync(grid); } /// Each row names itself and its pending change to a screen reader, and diff --git a/linux/crates/app/src/ui/data_grid/stripes.rs b/linux/crates/app/src/ui/data_grid/stripes.rs index b190b95f60..62d25a3722 100644 --- a/linux/crates/app/src/ui/data_grid/stripes.rs +++ b/linux/crates/app/src/ui/data_grid/stripes.rs @@ -35,12 +35,19 @@ impl DataGrid { glib::clone!( #[weak] view, - move |settings, key| set_class(&view, STRIPED, settings.boolean(key)) + #[weak(rename_to = gutter)] + self.imp().row_gutter.get(), + move |settings, key| { + let striped = settings.boolean(key); + set_class(&view, STRIPED, striped); + set_class(&gutter, STRIPED, striped); + } ), ); self.imp() .stripes_watch .replace(Some(HandlerGuard::new(settings.gio(), handler))); set_class(&view, STRIPED, settings.show_alternate_rows()); + set_class(&self.imp().row_gutter.get(), STRIPED, settings.show_alternate_rows()); } } diff --git a/linux/crates/app/src/ui/data_grid/tests/gutter.rs b/linux/crates/app/src/ui/data_grid/tests/gutter.rs index 6f7fd150b7..471e83c72f 100644 --- a/linux/crates/app/src/ui/data_grid/tests/gutter.rs +++ b/linux/crates/app/src/ui/data_grid/tests/gutter.rs @@ -221,3 +221,89 @@ fn the_gutter_neither_sorts_nor_saves_its_width() { "a sort by the first data column named another" ); } + +/// Columns scrolled sideways leave the first column behind, and the row +/// numbers are drawn again where it was, at the rows' own height. +#[gtk4::test] +fn the_row_numbers_stay_in_view_when_the_columns_scroll_sideways() { + use gtk4::subclass::prelude::ObjectSubclassIsExt; + use tablepro_core::Value; + + let mut columns = vec![crate::ui::data_grid::test_grid::named("id", "integer", true)]; + columns.extend((0..30).map(|n| crate::ui::data_grid::test_grid::named(&format!("column_{n}"), "text", false))); + let rows: Vec> = (1..=3) + .map(|id| { + std::iter::once(Value::Int(id)) + .chain((0..30).map(|n| Value::Text(format!("value {id}-{n}")))) + .collect() + }) + .collect(); + let grid = Grid::open(columns, rows); + let imp = grid.grid.imp(); + let (gutter, columns) = (imp.row_gutter.get(), imp.scrolled.hadjustment()); + assert!( + !gutter.is_visible(), + "the numbers were drawn twice with nothing scrolled" + ); + assert!(columns.upper() > columns.page_size(), "the grid has nowhere to scroll"); + + columns.set_value(columns.upper()); + drain_main_context(); + + assert!(gutter.is_visible()); + let listed = || { + descendants(&imp.gutter_list.get()) + .into_iter() + .filter(|widget| widget.css_name() == "row" && widget.height() > 0) + .count() + }; + crate::test_support::wait_until(crate::ui::data_grid::test_grid::WAIT, || listed() == 3) + .expect("the gutter lists the rows"); + + columns.set_value(0.0); + drain_main_context(); + assert!(!gutter.is_visible()); +} + +/// The pinned numbers are striped like the rows beside them, and follow +/// the preference. +#[gtk4::test] +fn the_pinned_row_numbers_carry_the_stripes_of_their_rows() { + use gtk4::subclass::prelude::ObjectSubclassIsExt; + use tablepro_core::Value; + + let mut columns = vec![crate::ui::data_grid::test_grid::named("id", "integer", true)]; + columns.extend((0..30).map(|n| crate::ui::data_grid::test_grid::named(&format!("column_{n}"), "text", false))); + let rows: Vec> = (1..=3) + .map(|id| { + std::iter::once(Value::Int(id)) + .chain((0..30).map(|n| Value::Text(format!("value {id}-{n}")))) + .collect() + }) + .collect(); + let grid = Grid::open(columns, rows); + let imp = grid.grid.imp(); + let striped = || imp.row_gutter.has_css_class("tp-striped"); + assert!(striped(), "on by default, as the rows are"); + + imp.scrolled.hadjustment().set_value(imp.scrolled.hadjustment().upper()); + drain_main_context(); + let bands = || { + descendants(&imp.gutter_list.get()) + .into_iter() + .filter(|widget| widget.has_css_class("row-gutter-cell")) + .filter(|widget| widget.has_css_class("tp-stripe")) + .count() + }; + crate::test_support::wait_until(crate::ui::data_grid::test_grid::WAIT, || bands() == 1) + .expect("one of three rows is striped, the second"); + + grid.grid + .config() + .expect("a configured grid") + .settings + .set_show_alternate_rows(false) + .expect("store the flag"); + drain_main_context(); + assert!(!striped()); +} diff --git a/linux/data/resources/style.css b/linux/data/resources/style.css index 4c92a7f3e9..1f3bf6b384 100644 --- a/linux/data/resources/style.css +++ b/linux/data/resources/style.css @@ -28,6 +28,26 @@ columnview.data-table > listview > row > cell { padding: 0; } +/* The row numbers over the first column, in the colour of the rows + beneath them, so the columns pass under them as they scroll. */ +.row-gutter { + background-color: var(--view-bg-color); + border-right: 1px solid color-mix(in srgb, var(--view-fg-color) 15%, transparent); +} + +.row-gutter listview { + background: none; +} + +.row-gutter listview > row { + padding: 0; + min-height: 0; +} + +.row-gutter listview > row > .row-gutter-cell { + padding: 0 6px; +} + columnview.data-table > listview > row > cell > * { padding: 2px 6px; } @@ -38,10 +58,20 @@ columnview.tp-striped > listview > row:not(:selected) > cell > .tp-stripe { background-color: color-mix(in srgb, var(--view-fg-color) 4%, transparent); } +/* The pinned row numbers carry the same stripes, so a row reads as one + band across them. */ +.row-gutter.tp-striped listview > row:not(:selected) > .tp-stripe { + background-color: color-mix(in srgb, var(--view-fg-color) 4%, transparent); +} + @media (prefers-contrast: more) { columnview.tp-striped > listview > row:not(:selected) > cell > .tp-stripe { background-color: color-mix(in srgb, var(--view-fg-color) 8%, transparent); } + + .row-gutter.tp-striped listview > row:not(:selected) > .tp-stripe { + background-color: color-mix(in srgb, var(--view-fg-color) 8%, transparent); + } } /* Connection colour tags. The palette is libadwaita's accent set, kept diff --git a/linux/data/resources/ui/data-grid.ui b/linux/data/resources/ui/data-grid.ui index 4ddaaa06aa..c5a9ce6d9b 100644 --- a/linux/data/resources/ui/data-grid.ui +++ b/linux/data/resources/ui/data-grid.ui @@ -102,6 +102,40 @@ + + + + vertical + start + false + + + + 1 + + + + + + true + never + external + + + false + + + + + + No Rows diff --git a/linux/po/POTFILES.in b/linux/po/POTFILES.in index 23a2291ef9..97ad59f0f5 100644 --- a/linux/po/POTFILES.in +++ b/linux/po/POTFILES.in @@ -275,6 +275,7 @@ crates/app/src/ui/data_grid/imp.rs crates/app/src/ui/data_grid/keys.rs crates/app/src/ui/data_grid/layout_binding.rs crates/app/src/ui/data_grid/mod.rs +crates/app/src/ui/data_grid/pinned_numbers.rs crates/app/src/ui/data_grid/pointer.rs crates/app/src/ui/data_grid/references/mod.rs crates/app/src/ui/data_grid/references/tests.rs