Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions linux/changelog.d/4110.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The row numbers stay at the left edge while the columns scroll sideways.
1 change: 1 addition & 0 deletions linux/changelog.d/4143.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The row numbers that stay in view while columns scroll sideways carry the alternate row backgrounds too.
9 changes: 9 additions & 0 deletions linux/crates/app/src/ui/data_grid/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ pub struct DataGrid {
#[template_child]
pub column_view: TemplateChild<gtk4::ColumnView>,
#[template_child]
pub row_gutter: TemplateChild<gtk4::Box>,
#[template_child]
pub gutter_header: TemplateChild<gtk4::Label>,
#[template_child]
pub gutter_scroll: TemplateChild<gtk4::ScrolledWindow>,
#[template_child]
pub gutter_list: TemplateChild<gtk4::ListView>,
#[template_child]
pub empty_page: TemplateChild<adw::StatusPage>,
#[template_child]
pub detail_pane: TemplateChild<RowDetailPane>,
Expand Down Expand Up @@ -86,6 +94,7 @@ pub struct DataGrid {
pub editing: Cell<bool>,
/// The window's focus handler, while the grid is in a window.
pub focus_watch: RefCell<Option<(glib::WeakRef<gtk4::Window>, glib::SignalHandlerId)>>,
pub(super) pinned_numbers: std::rc::Rc<super::pinned_numbers::Pinned>,
pub(super) font_watch: RefCell<Option<HandlerGuard>>,
pub(super) stripes_watch: RefCell<Option<HandlerGuard>>,
}
Expand Down
2 changes: 2 additions & 0 deletions linux/crates/app/src/ui/data_grid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod grid_store;
mod imp;
mod keys;
mod layout_binding;
mod pinned_numbers;
mod pointer;
mod references;
mod reversed_list;
Expand Down Expand Up @@ -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);
Expand Down
221 changes: 221 additions & 0 deletions linux/crates/app/src/ui/data_grid/pinned_numbers.rs
Original file line number Diff line number Diff line change
@@ -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<i32>,
cells: RefCell<Vec<glib::WeakRef<gtk::Widget>>>,
}

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: &gtk::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::<gtk::ListItem>() 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::<gtk::Widget>().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::<gtk::ListItem>(), weak.upgrade()) else {
return;
};
let Some(row) = item.item().and_downcast::<GridRow>() 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::<glib::Object>().downgrade(), on_row),
(item.upcast_ref::<glib::Object>().downgrade(), on_position),
],
);
});
}
factory.connect_unbind(move |_, item| unwatch(&watches, item.upcast_ref()));
factory
}

fn show_item(grid: &DataGrid, item: &gtk::ListItem) {
let (Some(row), Some(content)) = (item.item().and_downcast::<GridRow>(), item.child()) else {
return;
};
let (Some(icon), Some(number)) = (
content.first_child().and_downcast::<gtk::Image>(),
content.last_child().and_downcast::<gtk::Inscription>(),
) 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: &gtk::ColumnView) -> Option<i32> {
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: &gtk::ColumnView) -> Option<i32> {
fn find(widget: &gtk::Widget) -> Option<i32> {
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())
}
7 changes: 4 additions & 3 deletions linux/crates/app/src/ui/data_grid/row_number_column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RefCell<HashMap<glib::Object, Vec<(glib::WeakRef<glib::Object>, glib::SignalHandlerId)>>>>;
pub(super) type Watches = Rc<RefCell<HashMap<glib::Object, Vec<(glib::WeakRef<glib::Object>, 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
Expand Down Expand Up @@ -116,15 +116,15 @@ fn show_cell(grid: &DataGrid, cell: &gtk::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<u64> {
pub(super) fn row_number(grid: &DataGrid, row: &GridRow, position: u32) -> Option<u64> {
if row.draft_id().is_some() || position == gtk::INVALID_LIST_POSITION {
return None;
}
let drafts = grid.store().draft_count();
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() {
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion linux/crates/app/src/ui/data_grid/stripes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Loading