Skip to content
Merged
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/4112.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Row Details marks a primary key field with a key, and a field holding an unsaved edit with a pencil.
90 changes: 76 additions & 14 deletions linux/crates/app/src/ui/data_grid/row_detail/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ use gtk4::{self as gtk, glib, pango};
use tablepro_core::Value;

use super::RowDetailPane;
use crate::i18n::gettext;
use crate::services::change_tracker::CellState;
use crate::services::grid::{GridColumn, auto_filled_sentinel, display_text};
use crate::ui::data_grid::tracked_values::cell_description;
use crate::ui::data_grid::{DataGrid, GridRow, ServerValue, TrackedValues};
use crate::ui::handler_guard::HandlerGuard;
use crate::ui::icons;

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

/// A field's row: its name and type on one line, its value on the next.
fn is_primary_key(column: &GridColumn) -> bool {
column.catalog.as_ref().is_some_and(|catalog| catalog.primary_key)
}

// The key has no role of its own, so the label is where a screen reader hears it.
pub(super) fn field_label(column: &GridColumn) -> String {
let mut parts = vec![column.name.clone()];
let type_name = &column.value_type().name;
if !type_name.is_empty() {
parts.push(type_name.clone());
}
if is_primary_key(column) {
parts.push(gettext("primary key"));
}
parts.join(", ")
}

fn marker(icon: &str) -> gtk::Image {
let image = gtk::Image::builder().icon_name(icon).visible(false).build();
image.set_accessible_role(gtk::AccessibleRole::Presentation);
image
}

fn field_row() -> gtk::Box {
let key = marker(icons::PRIMARY_KEY);
// Dimmed as a libadwaita property row's title, so the eye lands on the value.
let name = gtk::Label::builder()
.xalign(0.0)
.hexpand(true)
.ellipsize(pango::EllipsizeMode::End)
.ellipsize(pango::EllipsizeMode::Middle)
.build();
name.add_css_class("dimmed");
let edited = marker(icons::DOCUMENT_EDIT);
// Match the pending value beneath the marker.
edited.add_css_class("warning");
let kind = gtk::Label::builder()
.xalign(1.0)
.ellipsize(pango::EllipsizeMode::End)
.build();
kind.add_css_class("caption");
kind.add_css_class("dimmed");
let type_icon = marker(icons::TEXT_X_GENERIC);
type_icon.set_visible(true);
type_icon.add_css_class("dimmed");
let type_box = gtk::Box::new(gtk::Orientation::Horizontal, 3);
type_box.append(&type_icon);
type_box.append(&kind);
let top = gtk::Box::new(gtk::Orientation::Horizontal, 6);
top.append(&key);
top.append(&name);
top.append(&kind);
top.append(&edited);
top.append(&type_box);
let value = gtk::Inscription::builder()
.hexpand(true)
.min_chars(1)
Expand All @@ -96,6 +134,29 @@ fn field_row() -> gtk::Box {
row
}

struct FieldParts {
key: gtk::Image,
name: gtk::Label,
edited: gtk::Image,
kind: gtk::Label,
value: gtk::Inscription,
}

fn field_parts(row: &gtk::Widget) -> Option<FieldParts> {
let top = row.first_child()?;
let key = top.first_child()?;
let name = key.next_sibling()?;
let edited = name.next_sibling()?;
let kind = edited.next_sibling()?.last_child()?;
Some(FieldParts {
key: key.downcast().ok()?,
name: name.downcast().ok()?,
edited: edited.downcast().ok()?,
kind: kind.downcast().ok()?,
value: row.last_child()?.downcast().ok()?,
})
}

fn set_class(widget: &impl IsA<gtk::Widget>, class: &str, on: bool) {
match on {
true => widget.as_ref().add_css_class(class),
Expand Down Expand Up @@ -192,26 +253,27 @@ impl RowDetailPane {
let Some(row_box) = item.child() else {
return;
};
let top = row_box.first_child();
let (Some(name), Some(kind), Some(shown)) = (
top.as_ref()
.and_then(|top| top.first_child())
.and_downcast::<gtk::Label>(),
top.as_ref()
.and_then(|top| top.last_child())
.and_downcast::<gtk::Label>(),
row_box.last_child().and_downcast::<gtk::Inscription>(),
) else {
let Some(FieldParts {
key,
name,
edited,
kind,
value: shown,
}) = field_parts(&row_box)
else {
return;
};
key.set_visible(is_primary_key(&column));
name.set_label(&column.name);
kind.set_label(&column.value_type().name);
item.set_accessible_label(&column.name);
item.set_accessible_label(&field_label(&column));
let Some(row) = self.current_row() else {
edited.set_visible(false);
shown.set_text(None);
return;
};
let value = field_value(&grid.tracked(), &row, &column);
edited.set_visible(value.modified);
shown.set_text(Some(&value.text));
shown.set_attributes(Some(&value_attributes(&value)));
set_class(&shown, "dimmed", value.placeholder || value.deleted);
Expand Down
76 changes: 75 additions & 1 deletion linux/crates/app/src/ui/data_grid/row_detail/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tablepro_core::{ColumnInfo, Value};
use super::RowDetailPane;
use super::calendar::{day_of, day_start_offset, with_day};
use super::clock::{ClockField, time_of, with_time};
use super::fields::field_order;
use super::fields::{field_label, field_order};
use super::value_editor::value_content;
use crate::services::change_tracker::RowKey;
use crate::services::grid::{CellLock, GridColumn, GridRowId, grid_columns};
Expand Down Expand Up @@ -1424,3 +1424,77 @@ fn a_page_that_takes_the_row_closes_its_clock_and_writes_nothing() {
);
assert!(!pane.imp().clock_popover.is_visible());
}

fn drawn_field(pane: &RowDetailPane, column: usize) -> gtk::ListItem {
pane.imp()
.bound
.borrow()
.iter()
.find(|item| {
item.item()
.and_downcast::<gtk4::glib::BoxedAnyObject>()
.is_some_and(|boxed| *boxed.borrow::<usize>() == column)
})
.cloned()
.expect("the field is drawn")
}

fn marks(pane: &RowDetailPane, column: usize, icon: &str) -> bool {
let row = drawn_field(pane, column).child().expect("the field's row");
descendants(&row)
.into_iter()
.filter_map(|widget| widget.downcast::<gtk::Image>().ok())
.any(|image| image.is_visible() && image.icon_name().as_deref() == Some(icon))
}

#[gtk4::test]
fn a_field_marks_its_primary_key_and_a_pending_edit() {
let (grid, pane) = opened(two_text_columns(), two_text_values());
grid.focus(0, 1);
drain_main_context();
let key_mark = crate::ui::icons::PRIMARY_KEY;
let edit_mark = crate::ui::icons::DOCUMENT_EDIT;
assert!(marks(&pane, 0, key_mark));
assert!(!marks(&pane, 1, key_mark));
assert!(!marks(&pane, 1, edit_mark));
assert!(marks(&pane, 0, crate::ui::icons::TEXT_X_GENERIC));
assert!(marks(&pane, 1, crate::ui::icons::TEXT_X_GENERIC));

let delta = grid.tracker.apply(TrackerEdit::Cell {
row_key: key(1),
col: 1,
original: Value::Text("A".into()),
value: CellInput::Value(Value::Text("changed".into())),
});
grid.grid.apply_delta(&delta);
drain_main_context();
assert!(marks(&pane, 1, edit_mark));
assert!(!marks(&pane, 0, edit_mark));
assert_eq!(
drawn_field(&pane, 1).accessible_description().as_str(),
"changed, Modified"
);

grid.grid.undo();
drain_main_context();
assert!(!marks(&pane, 1, edit_mark), "the pencil outlived the undo");
assert_eq!(drawn_field(&pane, 1).accessible_description().as_str(), "A");
}

#[gtk4::test]
fn a_field_names_its_type_and_its_key_to_a_screen_reader() {
let (grid, pane) = opened(two_text_columns(), two_text_values());
grid.focus(0, 1);
drain_main_context();

assert_eq!(
drawn_field(&pane, 0).accessible_label().as_str(),
"id, integer, primary key"
);
assert_eq!(drawn_field(&pane, 1).accessible_label().as_str(), "name, text");
let result_column = GridColumn {
catalog: None,
..column_of(named("id", "integer", true))
};
assert_eq!(field_label(&result_column), "id, integer");
}
3 changes: 3 additions & 0 deletions linux/crates/app/src/ui/icons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub const PACKAGE: &str = "package-x-generic-symbolic";
pub const PAN_DOWN: &str = "pan-down-symbolic";
pub const PREFERENCES_SYSTEM: &str = "preferences-system-symbolic";
pub const PREFERENCES_SYSTEM_TIME: &str = "preferences-system-time-symbolic";
// Adwaita's only key glyph.
pub const PRIMARY_KEY: &str = "dialog-password-symbolic";
pub const PROCESS_STOP: &str = "process-stop-symbolic";
pub const SECURITY_HIGH: &str = "security-high-symbolic";
pub const STARRED: &str = "starred-symbolic";
Expand Down Expand Up @@ -120,6 +122,7 @@ pub const ALL: &[&str] = &[
PAN_DOWN,
PREFERENCES_SYSTEM,
PREFERENCES_SYSTEM_TIME,
PRIMARY_KEY,
PROCESS_STOP,
SECURITY_HIGH,
SIDEBAR_SHOW,
Expand Down
Loading