Skip to content

Commit e6dee19

Browse files
authored
feat(linux-grid): mark a field's key, edit and type in the row details (#4112)
1 parent 3d84c78 commit e6dee19

4 files changed

Lines changed: 155 additions & 15 deletions

File tree

‎linux/changelog.d/4112.added.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Row Details marks a primary key field with a key, and a field holding an unsaved edit with a pencil.

‎linux/crates/app/src/ui/data_grid/row_detail/fields.rs‎

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ use gtk4::{self as gtk, glib, pango};
66
use tablepro_core::Value;
77

88
use super::RowDetailPane;
9+
use crate::i18n::gettext;
910
use crate::services::change_tracker::CellState;
1011
use crate::services::grid::{GridColumn, auto_filled_sentinel, display_text};
1112
use crate::ui::data_grid::tracked_values::cell_description;
1213
use crate::ui::data_grid::{DataGrid, GridRow, ServerValue, TrackedValues};
1314
use crate::ui::handler_guard::HandlerGuard;
15+
use crate::ui::icons;
1416

1517
/// The data columns in the order the fields list shows them: the ones on
1618
/// screen as the grid shows them, then the hidden ones.
@@ -62,22 +64,58 @@ pub fn field_value(tracked: &TrackedValues, row: &GridRow, column: &GridColumn)
6264
}
6365
}
6466

65-
/// A field's row: its name and type on one line, its value on the next.
67+
fn is_primary_key(column: &GridColumn) -> bool {
68+
column.catalog.as_ref().is_some_and(|catalog| catalog.primary_key)
69+
}
70+
71+
// The key has no role of its own, so the label is where a screen reader hears it.
72+
pub(super) fn field_label(column: &GridColumn) -> String {
73+
let mut parts = vec![column.name.clone()];
74+
let type_name = &column.value_type().name;
75+
if !type_name.is_empty() {
76+
parts.push(type_name.clone());
77+
}
78+
if is_primary_key(column) {
79+
parts.push(gettext("primary key"));
80+
}
81+
parts.join(", ")
82+
}
83+
84+
fn marker(icon: &str) -> gtk::Image {
85+
let image = gtk::Image::builder().icon_name(icon).visible(false).build();
86+
image.set_accessible_role(gtk::AccessibleRole::Presentation);
87+
image
88+
}
89+
6690
fn field_row() -> gtk::Box {
91+
let key = marker(icons::PRIMARY_KEY);
92+
// Dimmed as a libadwaita property row's title, so the eye lands on the value.
6793
let name = gtk::Label::builder()
6894
.xalign(0.0)
6995
.hexpand(true)
70-
.ellipsize(pango::EllipsizeMode::End)
96+
.ellipsize(pango::EllipsizeMode::Middle)
7197
.build();
98+
name.add_css_class("dimmed");
99+
let edited = marker(icons::DOCUMENT_EDIT);
100+
// Match the pending value beneath the marker.
101+
edited.add_css_class("warning");
72102
let kind = gtk::Label::builder()
73103
.xalign(1.0)
74104
.ellipsize(pango::EllipsizeMode::End)
75105
.build();
76106
kind.add_css_class("caption");
77107
kind.add_css_class("dimmed");
108+
let type_icon = marker(icons::TEXT_X_GENERIC);
109+
type_icon.set_visible(true);
110+
type_icon.add_css_class("dimmed");
111+
let type_box = gtk::Box::new(gtk::Orientation::Horizontal, 3);
112+
type_box.append(&type_icon);
113+
type_box.append(&kind);
78114
let top = gtk::Box::new(gtk::Orientation::Horizontal, 6);
115+
top.append(&key);
79116
top.append(&name);
80-
top.append(&kind);
117+
top.append(&edited);
118+
top.append(&type_box);
81119
let value = gtk::Inscription::builder()
82120
.hexpand(true)
83121
.min_chars(1)
@@ -96,6 +134,29 @@ fn field_row() -> gtk::Box {
96134
row
97135
}
98136

137+
struct FieldParts {
138+
key: gtk::Image,
139+
name: gtk::Label,
140+
edited: gtk::Image,
141+
kind: gtk::Label,
142+
value: gtk::Inscription,
143+
}
144+
145+
fn field_parts(row: &gtk::Widget) -> Option<FieldParts> {
146+
let top = row.first_child()?;
147+
let key = top.first_child()?;
148+
let name = key.next_sibling()?;
149+
let edited = name.next_sibling()?;
150+
let kind = edited.next_sibling()?.last_child()?;
151+
Some(FieldParts {
152+
key: key.downcast().ok()?,
153+
name: name.downcast().ok()?,
154+
edited: edited.downcast().ok()?,
155+
kind: kind.downcast().ok()?,
156+
value: row.last_child()?.downcast().ok()?,
157+
})
158+
}
159+
99160
fn set_class(widget: &impl IsA<gtk::Widget>, class: &str, on: bool) {
100161
match on {
101162
true => widget.as_ref().add_css_class(class),
@@ -192,26 +253,27 @@ impl RowDetailPane {
192253
let Some(row_box) = item.child() else {
193254
return;
194255
};
195-
let top = row_box.first_child();
196-
let (Some(name), Some(kind), Some(shown)) = (
197-
top.as_ref()
198-
.and_then(|top| top.first_child())
199-
.and_downcast::<gtk::Label>(),
200-
top.as_ref()
201-
.and_then(|top| top.last_child())
202-
.and_downcast::<gtk::Label>(),
203-
row_box.last_child().and_downcast::<gtk::Inscription>(),
204-
) else {
256+
let Some(FieldParts {
257+
key,
258+
name,
259+
edited,
260+
kind,
261+
value: shown,
262+
}) = field_parts(&row_box)
263+
else {
205264
return;
206265
};
266+
key.set_visible(is_primary_key(&column));
207267
name.set_label(&column.name);
208268
kind.set_label(&column.value_type().name);
209-
item.set_accessible_label(&column.name);
269+
item.set_accessible_label(&field_label(&column));
210270
let Some(row) = self.current_row() else {
271+
edited.set_visible(false);
211272
shown.set_text(None);
212273
return;
213274
};
214275
let value = field_value(&grid.tracked(), &row, &column);
276+
edited.set_visible(value.modified);
215277
shown.set_text(Some(&value.text));
216278
shown.set_attributes(Some(&value_attributes(&value)));
217279
set_class(&shown, "dimmed", value.placeholder || value.deleted);

‎linux/crates/app/src/ui/data_grid/row_detail/tests.rs‎

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use tablepro_core::{ColumnInfo, Value};
1515
use super::RowDetailPane;
1616
use super::calendar::{day_of, day_start_offset, with_day};
1717
use super::clock::{ClockField, time_of, with_time};
18-
use super::fields::field_order;
18+
use super::fields::{field_label, field_order};
1919
use super::value_editor::value_content;
2020
use crate::services::change_tracker::RowKey;
2121
use crate::services::grid::{CellLock, GridColumn, GridRowId, grid_columns};
@@ -1541,3 +1541,77 @@ fn a_page_that_takes_the_row_closes_its_clock_and_writes_nothing() {
15411541
);
15421542
assert!(!pane.imp().clock_popover.is_visible());
15431543
}
1544+
1545+
fn drawn_field(pane: &RowDetailPane, column: usize) -> gtk::ListItem {
1546+
pane.imp()
1547+
.bound
1548+
.borrow()
1549+
.iter()
1550+
.find(|item| {
1551+
item.item()
1552+
.and_downcast::<gtk4::glib::BoxedAnyObject>()
1553+
.is_some_and(|boxed| *boxed.borrow::<usize>() == column)
1554+
})
1555+
.cloned()
1556+
.expect("the field is drawn")
1557+
}
1558+
1559+
fn marks(pane: &RowDetailPane, column: usize, icon: &str) -> bool {
1560+
let row = drawn_field(pane, column).child().expect("the field's row");
1561+
descendants(&row)
1562+
.into_iter()
1563+
.filter_map(|widget| widget.downcast::<gtk::Image>().ok())
1564+
.any(|image| image.is_visible() && image.icon_name().as_deref() == Some(icon))
1565+
}
1566+
1567+
#[gtk4::test]
1568+
fn a_field_marks_its_primary_key_and_a_pending_edit() {
1569+
let (grid, pane) = opened(two_text_columns(), two_text_values());
1570+
grid.focus(0, 1);
1571+
drain_main_context();
1572+
let key_mark = crate::ui::icons::PRIMARY_KEY;
1573+
let edit_mark = crate::ui::icons::DOCUMENT_EDIT;
1574+
assert!(marks(&pane, 0, key_mark));
1575+
assert!(!marks(&pane, 1, key_mark));
1576+
assert!(!marks(&pane, 1, edit_mark));
1577+
assert!(marks(&pane, 0, crate::ui::icons::TEXT_X_GENERIC));
1578+
assert!(marks(&pane, 1, crate::ui::icons::TEXT_X_GENERIC));
1579+
1580+
let delta = grid.tracker.apply(TrackerEdit::Cell {
1581+
row_key: key(1),
1582+
col: 1,
1583+
original: Value::Text("A".into()),
1584+
value: CellInput::Value(Value::Text("changed".into())),
1585+
});
1586+
grid.grid.apply_delta(&delta);
1587+
drain_main_context();
1588+
assert!(marks(&pane, 1, edit_mark));
1589+
assert!(!marks(&pane, 0, edit_mark));
1590+
assert_eq!(
1591+
drawn_field(&pane, 1).accessible_description().as_str(),
1592+
"changed, Modified"
1593+
);
1594+
1595+
grid.grid.undo();
1596+
drain_main_context();
1597+
assert!(!marks(&pane, 1, edit_mark), "the pencil outlived the undo");
1598+
assert_eq!(drawn_field(&pane, 1).accessible_description().as_str(), "A");
1599+
}
1600+
1601+
#[gtk4::test]
1602+
fn a_field_names_its_type_and_its_key_to_a_screen_reader() {
1603+
let (grid, pane) = opened(two_text_columns(), two_text_values());
1604+
grid.focus(0, 1);
1605+
drain_main_context();
1606+
1607+
assert_eq!(
1608+
drawn_field(&pane, 0).accessible_label().as_str(),
1609+
"id, integer, primary key"
1610+
);
1611+
assert_eq!(drawn_field(&pane, 1).accessible_label().as_str(), "name, text");
1612+
let result_column = GridColumn {
1613+
catalog: None,
1614+
..column_of(named("id", "integer", true))
1615+
};
1616+
assert_eq!(field_label(&result_column), "id, integer");
1617+
}

‎linux/crates/app/src/ui/icons.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ pub const PACKAGE: &str = "package-x-generic-symbolic";
4040
pub const PAN_DOWN: &str = "pan-down-symbolic";
4141
pub const PREFERENCES_SYSTEM: &str = "preferences-system-symbolic";
4242
pub const PREFERENCES_SYSTEM_TIME: &str = "preferences-system-time-symbolic";
43+
// Adwaita's only key glyph.
44+
pub const PRIMARY_KEY: &str = "dialog-password-symbolic";
4345
pub const PROCESS_STOP: &str = "process-stop-symbolic";
4446
pub const SECURITY_HIGH: &str = "security-high-symbolic";
4547
pub const STARRED: &str = "starred-symbolic";
@@ -120,6 +122,7 @@ pub const ALL: &[&str] = &[
120122
PAN_DOWN,
121123
PREFERENCES_SYSTEM,
122124
PREFERENCES_SYSTEM_TIME,
125+
PRIMARY_KEY,
123126
PROCESS_STOP,
124127
SECURITY_HIGH,
125128
SIDEBAR_SHOW,

0 commit comments

Comments
 (0)