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/4134.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Data grid preferences set whether rows are numbered, how tall a row is and what a NULL shows.
17 changes: 16 additions & 1 deletion linux/crates/app/src/services/grid/display_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ pub fn editable_null_sentinel() -> String {
crate::i18n::gettext("<NULL>")
}

pub fn null_text(text: &str, editable: bool) -> String {
match (editable, text) {
(true, "NULL") => editable_null_sentinel(),
_ => text.to_owned(),
}
}

/// A read-only cell dims its NULL instead, so it needs no brackets.
/// A technical term, so it is not translated.
const READONLY_NULL: &str = "NULL";
Expand Down Expand Up @@ -206,7 +213,7 @@ mod tests {
use tablepro_core::column::{BoolStorage, ColumnKind, ColumnType};
use tablepro_core::value::{JsonText, OffsetTimestamp, SqlTime, Temporal, UndecodableReason, UndecodedValue};

use super::{DISPLAY_MAX_CHARS, DisplayText, display_text};
use super::{DISPLAY_MAX_CHARS, DisplayText, display_text, null_text};

fn text_column() -> ColumnType {
ColumnType::untyped_text("text")
Expand All @@ -217,6 +224,14 @@ mod tests {
display_text(value, &text_column(), &mut scratch).text.to_owned()
}

#[test]
fn a_chosen_null_text_reaches_editable_cells_and_the_default_keeps_its_brackets() {
assert_eq!(null_text("NULL", true), "<NULL>");
assert_eq!(null_text("(none)", true), "(none)");
assert_eq!(null_text("(none)", false), "(none)");
assert_eq!(null_text("NULL", false), "NULL");
}

#[test]
fn display_text_borrows_text_without_allocating() {
let value = Value::Text("hello".into());
Expand Down
1 change: 1 addition & 0 deletions linux/crates/app/src/ui/a11y/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fn template_classes() -> Vec<glib::Type> {
preferences_dialog::DataBrowserGroup::static_type(),
preferences_dialog::EditorPage::static_type(),
preferences_dialog::GeneralPage::static_type(),
preferences_dialog::GridPage::static_type(),
preferences_dialog::HistoryGroup::static_type(),
saved_queries_dialog::SavedQueriesDialog::static_type(),
saved_queries_dialog::SavedQueryRow::static_type(),
Expand Down
18 changes: 13 additions & 5 deletions linux/crates/app/src/ui/data_grid/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use tablepro_core::Value;
use super::grid_cell::{GridCell, TextAttributes};
use super::server_sorter::ServerSorter;
use super::{DataGrid, GridRow, GridSorting, TrackedValues, client_sorter, grid_menu, row_number_column};
use crate::services::grid::display_text::editable_null_sentinel;
use crate::services::grid::{
CellDisplay, ColumnLayout, GridColumn, LayoutSlot, Placement, default_width_chars, header_text, merge_order,
number_width_chars, sampled_texts, typical_texts, typical_width_chars,
};
use crate::ui::handler_guard::HandlerGuard;
use tablepro_linux_platform::settings::GridPreferences;

/// Rows sampled for a column's opening width.
const WIDTH_SAMPLE: usize = 200;
Expand Down Expand Up @@ -49,6 +49,7 @@ pub struct ColumnPaint {
pub column: GridColumn,
pub tracked: TrackedValues,
pub attributes: Rc<TextAttributes>,
pub preferences: GridPreferences,
/// The column lines its values up on the last digit. An untyped column
/// does once every value it holds is a number.
pub numbers: Cell<bool>,
Expand Down Expand Up @@ -159,6 +160,10 @@ impl DataGrid {
column: column.clone(),
tracked: tracked.clone(),
attributes: attributes.clone(),
preferences: self
.config()
.map(|config| config.settings.grid_preferences().clone())
.unwrap_or_default(),
numbers: Cell::new(column.display() == CellDisplay::Number),
});
built.view_columns.push(self.build_column(&paint, sorting));
Expand Down Expand Up @@ -192,6 +197,7 @@ impl DataGrid {
self.size_unsized_columns();
self.watch_layout_changes();
self.wire_sorting(sorting);
self.apply_display_settings();
}

fn build_column(&self, paint: &Rc<ColumnPaint>, sorting: GridSorting) -> gtk::ColumnViewColumn {
Expand Down Expand Up @@ -306,7 +312,6 @@ impl DataGrid {
let font = value_font(&context);
let metrics = context.metrics(Some(&font), None);
let char_px = pango::units_to_double(metrics.approximate_char_width());
let null = editable_null_sentinel();
let mut widths = Vec::new();
for (paint, view_column) in set.paints.iter().zip(&set.view_columns) {
let index = paint.column.index;
Expand Down Expand Up @@ -351,9 +356,12 @@ impl DataGrid {
None => 0,
};
let header = header_width(&context, header_text(column)) + indicator;
// A NULL the user can replace draws as `<NULL>`, wider than the
// fewest cells a column takes.
let placeholder = match paint.tracked.tracks_changes() && values.iter().any(|value| value.is_null()) {
let preferences = paint.preferences.snapshot();
let null = crate::services::grid::display_text::null_text(
&preferences.null_display,
paint.tracked.tracks_changes(),
);
let placeholder = match values.iter().any(|value| value.is_null()) {
true => drawn_width(&context, &font, &null),
false => 0,
};
Expand Down
48 changes: 48 additions & 0 deletions linux/crates/app/src/ui/data_grid/display_settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use gtk4::prelude::*;
use gtk4::subclass::prelude::*;
use tablepro_linux_platform::settings::GridRowHeight;

use super::DataGrid;
use super::grid_cell::set_class;
use crate::ui::handler_guard::HandlerGuard;

impl DataGrid {
pub(super) fn watch_display_settings(&self) {
let Some(config) = self.config() else {
return;
};
let preferences = config.settings.grid_preferences();
let weak = self.downgrade();
let handler = preferences.connect_changed(move |_| {
if let Some(grid) = weak.upgrade() {
grid.apply_display_settings();
}
});
self.imp()
.display_watch
.replace(Some(HandlerGuard::new(preferences, handler)));
self.apply_display_settings();
}

pub(super) fn apply_display_settings(&self) {
let Some(config) = self.config() else {
return;
};
let values = config.settings.grid_preferences().snapshot();
let view = self.column_view();
for height in GridRowHeight::ALL {
set_class(&view, height.css_class(), height == values.row_height);
}
set_class(&view, "tp-striped", values.show_alternate_rows);
if let Some(numbers) = self.row_numbers_column() {
numbers.set_visible(values.show_row_numbers);
super::pinned_numbers::sync(self);
}
let changed = self.imp().applied_null.borrow().as_deref() != Some(&values.null_display);
if changed {
self.imp().applied_null.replace(Some(values.null_display.clone()));
self.redraw();
self.fonts_changed();
}
}
}
10 changes: 5 additions & 5 deletions linux/crates/app/src/ui/data_grid/grid_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::columns::ColumnPaint;
use super::tracked_values::cell_description;
use super::{DataGrid, GridRow, ServerValue};
use crate::services::change_tracker::CellState;
use crate::services::grid::display_text::{DisplayText, auto_filled_sentinel, display_text, editable_null_sentinel};
use crate::services::grid::display_text::{DisplayText, auto_filled_sentinel, display_text, null_text};
use crate::services::grid::{CellLock, GridColumn};

/// Enough for any number, date or short text.
Expand All @@ -36,8 +36,6 @@ pub struct TextAttributes {
deleted: pango::AttrList,
deleted_placeholder: pango::AttrList,
placeholder: pango::AttrList,
/// The stand-in texts, translated once rather than on every bind.
editable_null: String,
auto_filled: String,
}

Expand All @@ -61,7 +59,6 @@ impl TextAttributes {
deleted: list(TextMark::Deleted, false),
deleted_placeholder: list(TextMark::Deleted, true),
placeholder: list(TextMark::Plain, true),
editable_null: editable_null_sentinel(),
auto_filled: auto_filled_sentinel(),
}
}
Expand Down Expand Up @@ -403,7 +400,10 @@ fn shown_text<'a>(
}
(Some(ServerValue::Default), _) => DisplayText::placeholder("DEFAULT"),
(Some(ServerValue::Expression(expression)), _) => DisplayText::placeholder(expression.sql),
(None, Value::Null) if editable => DisplayText::placeholder(&paint.attributes.editable_null),
(None, Value::Null) => {
*scratch = null_text(&paint.preferences.snapshot().null_display, editable);
DisplayText::placeholder(scratch)
}
(None, value) => display_text(value, &paint.column.column_type, scratch),
}
}
Expand Down
4 changes: 3 additions & 1 deletion linux/crates/app/src/ui/data_grid/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ pub struct DataGrid {
/// 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) font_watch: RefCell<Option<HandlerGuard>>,
pub(super) stripes_watch: RefCell<Option<HandlerGuard>>,
pub(super) display_watch: RefCell<Option<HandlerGuard>>,
pub(super) applied_null: RefCell<Option<String>>,
}

#[glib::object_subclass]
Expand Down Expand Up @@ -133,6 +134,7 @@ impl ObjectImpl for DataGrid {
self.summary.stop();
self.obj().stop_watching_focus();
drop(self.font_watch.take());
drop(self.display_watch.take());
if let Some(menus) = self.menus.get() {
menus.unparent();
}
Expand Down
5 changes: 2 additions & 3 deletions linux/crates/app/src/ui/data_grid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod client_sorter;
mod column_chooser;
mod columns;
mod copy;
mod display_settings;
mod edit;
mod find_bar;
mod grid_actions;
Expand Down Expand Up @@ -92,7 +93,7 @@ impl DataGrid {
imp.detail_pane.attach(&grid);
grid.set_columns(columns, sorting, rules);
grid.watch_fonts();
grid.watch_stripes();
grid.watch_display_settings();
grid
}

Expand Down Expand Up @@ -187,8 +188,6 @@ impl DataGrid {
self.imp().columns.borrow().columns()
}

/// Build the columns for another column set, with what may change in
/// its rows. The rows go, since they were read under the old columns.
pub fn set_columns(&self, columns: Arc<[GridColumn]>, sorting: GridSorting, rules: Option<EditRules>) {
self.commit_editors();
self.store().clear();
Expand Down
14 changes: 10 additions & 4 deletions linux/crates/app/src/ui/data_grid/pinned_numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub(super) fn install(grid: &DataGrid) {
return None;
}
let area = header(&grid.column_view()).and_then(|header| header.compute_bounds(overlay));
let Some((area, column)) = area.zip(grid.row_numbers_column()) else {
let Some((area, column)) = area.zip(shown_numbers(&grid)) else {
return Some(gtk::gdk::Rectangle::new(0, 0, 0, 0));
};
let width = column.fixed_width();
Expand All @@ -196,15 +196,21 @@ pub(super) fn install(grid: &DataGrid) {
));
}

// The row numbers preference hides the column without taking it out of the view.
fn shown_numbers(grid: &DataGrid) -> Option<gtk::ColumnViewColumn> {
grid.row_numbers_column().filter(|column| column.is_visible())
}

pub(super) fn sync(grid: &DataGrid) {
let imp = grid.imp();
if let Some(column) = grid.row_numbers_column() {
let numbers = shown_numbers(grid);
if let Some(column) = &numbers {
imp.gutter_backing.set_width_request(column.fixed_width());
}
let visible = imp.scrolled.hadjustment().value() > 0.0;
let visible = numbers.is_some() && imp.scrolled.hadjustment().value() > 0.0;
imp.gutter_backing.set_visible(visible);
// Keep one pointer target while scrolling schedules a new native header allocation.
imp.gutter_header.set_visible(grid.row_numbers_column().is_some());
imp.gutter_header.set_visible(numbers.is_some());
imp.viewport.queue_draw();
grid.edit().queue_editor_allocate();
}
Expand Down
10 changes: 7 additions & 3 deletions linux/crates/app/src/ui/data_grid/row_detail/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ pub struct FieldValue {
pub deleted: bool,
}

pub fn field_value(tracked: &TrackedValues, row: &GridRow, column: &GridColumn) -> FieldValue {
pub fn field_value(tracked: &TrackedValues, row: &GridRow, column: &GridColumn, null_display: &str) -> FieldValue {
let mark = tracked.mark(row, column.index);
let (text, placeholder, null) = tracked.with_value(row, column.index, |value, computed| match (computed, value) {
(Some(ServerValue::Default), _) if column.auto_filled() => (auto_filled_sentinel(), true, false),
(Some(ServerValue::Default), _) => ("DEFAULT".to_owned(), true, false),
(Some(ServerValue::Expression(expression)), _) => (expression.sql.to_owned(), true, false),
(None, Value::Null) => ("NULL".to_owned(), true, true),
(None, Value::Null) => (null_display.to_owned(), true, true),
(None, value) => {
let mut scratch = String::new();
let shown = display_text(value, &column.column_type, &mut scratch);
Expand Down Expand Up @@ -272,7 +272,11 @@ impl RowDetailPane {
shown.set_text(None);
return;
};
let value = field_value(&grid.tracked(), &row, &column);
let Some(config) = grid.config() else {
return;
};
let preferences = config.settings.grid_preferences().snapshot();
let value = field_value(&grid.tracked(), &row, &column, &preferences.null_display);
edited.set_visible(value.modified);
shown.set_text(Some(&value.text));
shown.set_attributes(Some(&value_attributes(&value)));
Expand Down
29 changes: 29 additions & 0 deletions linux/crates/app/src/ui/data_grid/row_detail/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,35 @@ fn a_page_that_takes_the_row_closes_its_clock_and_writes_nothing() {
assert!(!pane.imp().clock_popover.is_visible());
}

#[gtk4::test]
fn null_preferences_update_details_without_replacing_a_draft() {
let (grid, pane) = opened(two_text_columns(), vec![vec![Value::Int(1), Value::Null]]);
grid.focus(0, 1);
let settings = grid.grid.config().expect("configured grid").settings.clone();
settings.set_null_display("missing").expect("store placeholder");
drain_main_context();

assert_eq!(pane.imp().placeholder_label.label(), "missing");
let shown: Vec<String> = descendants(&pane.imp().field_list.get())
.into_iter()
.filter_map(|widget| widget.downcast::<gtk::Inscription>().ok())
.filter_map(|inscription| inscription.text().map(|text| text.to_string()))
.collect();
assert!(shown.contains(&"missing".to_owned()), "field list showed {shown:?}");

edit_field(&grid, &pane, 0, 1);
type_value(&pane, "unfinished");
assert!(grid.grid.is_editing());
settings
.set_null_display("absent")
.expect("change placeholder while editing");
drain_main_context();

assert_eq!(pane.typed_text(), "unfinished");
assert!(grid.grid.is_editing());
assert_eq!(pending(&grid, 1, 1), None);
}

fn drawn_field(pane: &RowDetailPane, column: usize) -> gtk::ListItem {
pane.imp()
.bound
Expand Down
7 changes: 6 additions & 1 deletion linux/crates/app/src/ui/data_grid/row_detail/value_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,12 @@ impl RowDetailPane {
}
let tracked = grid.tracked();
let input = tracked.edit_input(&row, column.index);
let content = value_content(&column, &row.id(), tracked.lock(&column, &row), &input);
let mut content = value_content(&column, &row.id(), tracked.lock(&column, &row), &input);
if matches!(input, CellInput::Value(Value::Null))
&& let Some(config) = grid.config()
{
content.open_placeholder = Some(config.settings.grid_preferences().snapshot().null_display.clone());
}
self.show_value(&column.name, &content);
self.show_picture(match input {
CellInput::Value(Value::Bytes(bytes)) => Some(bytes),
Expand Down
Loading
Loading