diff --git a/linux/changelog.d/4137.added.md b/linux/changelog.d/4137.added.md new file mode 100644 index 0000000000..b498d0b520 --- /dev/null +++ b/linux/changelog.d/4137.added.md @@ -0,0 +1 @@ +Preferences can set the format Copy writes when several rows are selected, as TSV, JSON, CSV or Markdown. diff --git a/linux/crates/app/src/ui/data_grid/copy/mod.rs b/linux/crates/app/src/ui/data_grid/copy/mod.rs index 222b60e39f..decb23cc3f 100644 --- a/linux/crates/app/src/ui/data_grid/copy/mod.rs +++ b/linux/crates/app/src/ui/data_grid/copy/mod.rs @@ -5,6 +5,7 @@ use gtk4::{self as gtk, glib}; use tablepro_core::column::{ColumnInfo, ResultColumn}; use tablepro_core::edit::CellInput; use tablepro_core::{FilterRule, Value}; +use tablepro_linux_platform::settings::RowCopyFormat; use super::grid_actions::{add, add_for_column, enable}; use super::selection::Resolved; @@ -165,7 +166,6 @@ impl ShownColumns { } } -/// Copy: the target cell's value when one row is in play, else the rows. fn copy(grid: &DataGrid) { let Some(target) = grid.target() else { return; @@ -173,7 +173,22 @@ fn copy(grid: &DataGrid) { let column = target.cell.as_ref().map(|(_, column)| *column); match (copy_scope(target.rows.len(), column), &target.cell) { (CopyScope::Value(column), Some((row, _))) => copy_value(grid, row, column), - _ => copy_rows(grid, &target.rows, CopyKind::Rows), + _ => copy_rows(grid, &target.rows, preferred_format(grid)), + } +} + +fn preferred_format(grid: &DataGrid) -> CopyKind { + match grid + .config() + .map(|config| config.settings.row_copy_format()) + .unwrap_or_default() + { + RowCopyFormat::Rows => CopyKind::Rows, + RowCopyFormat::RowsWithHeaders => CopyKind::RowsWithHeaders, + RowCopyFormat::Json => CopyKind::Json, + RowCopyFormat::Csv => CopyKind::Csv, + RowCopyFormat::CsvWithHeaders => CopyKind::CsvWithHeaders, + RowCopyFormat::Markdown => CopyKind::Markdown, } } diff --git a/linux/crates/app/src/ui/data_grid/copy/tests.rs b/linux/crates/app/src/ui/data_grid/copy/tests.rs index 8a46da5545..afab86f749 100644 --- a/linux/crates/app/src/ui/data_grid/copy/tests.rs +++ b/linux/crates/app/src/ui/data_grid/copy/tests.rs @@ -25,6 +25,7 @@ use crate::ui::data_grid::{DataGrid, GridCell, GridRequest}; use crate::ui::export::ExportSource; const BEFORE: &str = "nothing copied yet"; +const TWO_TEXT_ROWS_JSON: &str = "[\n{\"id\":1,\"name\":\"A\"},\n{\"id\":2,\"name\":\"B\"}\n]"; /// What a copy test drives: a grid and the requests it made. trait Copies { @@ -747,3 +748,49 @@ fn the_text_copies_take_the_columns_on_screen() { assert_eq!(lines, "{\"id\":1,\"name\":\"Ada\"}\n{\"id\":2,\"name\":\"Grace\"}\n"); assert_eq!(one_row, "1, 'Ada', 2, 'Grace'"); } + +#[gtk4::test] +fn copy_writes_several_rows_in_the_preferred_format() { + use tablepro_linux_platform::settings::RowCopyFormat; + + let grid = Grid::open(two_text_columns(), two_text_values()); + select_all(&grid); + let settings = &grid.grid.config().expect("a configured grid").settings; + for format in RowCopyFormat::ALL { + settings.set_row_copy_format(format.nick()).expect("store the format"); + + let text = copied(&grid, || activate(&grid.grid, "copy")); + + match format { + RowCopyFormat::Rows => assert_eq!(text, "1\tA\n2\tB\n"), + RowCopyFormat::RowsWithHeaders => assert_eq!(text, "id\tname\n1\tA\n2\tB\n"), + RowCopyFormat::Csv => assert_eq!(text, "1,A\n2,B\n"), + RowCopyFormat::CsvWithHeaders => assert_eq!(text, "id,name\n1,A\n2,B\n"), + RowCopyFormat::Markdown => assert_eq!(text, "| id | name |\n| --- | --- |\n| 1 | A |\n| 2 | B |"), + RowCopyFormat::Json => assert_eq!(text, TWO_TEXT_ROWS_JSON), + } + } +} + +#[gtk4::test] +fn preferred_row_format_preserves_single_cell_copy_and_explicit_formats() { + let grid = Grid::open(two_text_columns(), two_text_values()); + grid.grid + .config() + .expect("a configured grid") + .settings + .set_row_copy_format("json") + .expect("store the format"); + grid.grid.selection().select_item(0, true); + grid.focus(0, 1); + + assert_eq!(copied(&grid, || activate(&grid.grid, "copy")), "A"); + + select_all(&grid); + assert_eq!( + copied(&grid, || activate(&grid.grid, "copy-csv-headers")), + "id,name\n1,A\n2,B\n" + ); + let text = copied(&grid, || activate(&grid.grid, "copy")); + assert_eq!(text, TWO_TEXT_ROWS_JSON); +} diff --git a/linux/crates/app/src/ui/preferences_dialog/data_browser_group.rs b/linux/crates/app/src/ui/preferences_dialog/data_browser_group.rs index b811995241..95a8eae58c 100644 --- a/linux/crates/app/src/ui/preferences_dialog/data_browser_group.rs +++ b/linux/crates/app/src/ui/preferences_dialog/data_browser_group.rs @@ -4,7 +4,7 @@ use gtk4::prelude::*; use gtk4::subclass::prelude::*; use libadwaita as adw; use tablepro_linux_platform::AppSettings; -use tablepro_linux_platform::settings::{GridRowHeight, keys}; +use tablepro_linux_platform::settings::{GridRowHeight, RowCopyFormat, keys}; mod imp { use libadwaita::prelude::*; @@ -30,6 +30,8 @@ mod imp { #[template_child] pub row_numbers_row: TemplateChild, #[template_child] + pub row_copy_format_row: TemplateChild, + #[template_child] pub row_height_row: TemplateChild, #[template_child] pub null_display_row: TemplateChild, @@ -112,23 +114,41 @@ impl DataBrowserGroup { Some(description.to_str().to_variant()) }) .build(); - gio_settings - .bind(keys::GRID_ROW_HEIGHT, &imp.row_height_row.get(), "selected") - .mapping(|variant, _| { - let height = variant.str()?; - let index = GridRowHeight::ALL - .iter() - .position(|candidate| candidate.nick() == height)?; - Some((index as u32).to_value()) - }) - .set_mapping(|value, _| { - let index = value.get::().ok()?; - Some(GridRowHeight::ALL.get(index as usize)?.nick().to_variant()) - }) - .build(); + bind_choice( + &imp.row_height_row.get(), + gio_settings, + keys::GRID_ROW_HEIGHT, + GridRowHeight::ALL.map(GridRowHeight::nick), + ); + bind_choice( + &imp.row_copy_format_row.get(), + gio_settings, + keys::ROW_COPY_FORMAT, + RowCopyFormat::ALL.map(RowCopyFormat::nick), + ); } } +fn bind_choice( + row: &adw::ComboRow, + settings: &gio::Settings, + key: &'static str, + words: [&'static str; N], +) { + settings + .bind(key, row, "selected") + .mapping(move |variant, _| { + let word = variant.str()?; + let index = words.iter().position(|candidate| *candidate == word)?; + Some((index as u32).to_value()) + }) + .set_mapping(move |value, _| { + let index = value.get::().ok()?; + Some(words.get(index as usize)?.to_variant()) + }) + .build(); +} + #[cfg(test)] mod tests { use libadwaita::prelude::*; @@ -258,4 +278,28 @@ mod tests { assert!(first.imp().grid_system_font_row.is_active()); assert!(!second.imp().grid_font_row.is_sensitive()); } + + #[gtk4::test] + fn copy_format_rows_follow_every_choice_from_both_windows() { + let settings = MemorySettings::new(); + let first = bound_group(&settings); + let second = bound_group(&settings); + + for (index, format) in RowCopyFormat::ALL.into_iter().enumerate() { + first.imp().row_copy_format_row.set_selected(index as u32); + assert_eq!(settings.get().row_copy_format(), format); + assert_eq!(second.imp().row_copy_format_row.selected(), index as u32); + } + for (index, format) in RowCopyFormat::ALL.into_iter().enumerate().rev() { + settings + .get() + .set_row_copy_format(format.nick()) + .expect("store copy format"); + assert_eq!(first.imp().row_copy_format_row.selected(), index as u32); + assert_eq!(second.imp().row_copy_format_row.selected(), index as u32); + } + second.imp().row_copy_format_row.set_selected(2); + assert_eq!(first.imp().row_copy_format_row.selected(), 2); + assert_eq!(settings.get().row_copy_format(), RowCopyFormat::Json); + } } diff --git a/linux/crates/platform/src/settings/grid_preferences.rs b/linux/crates/platform/src/settings/grid_preferences.rs index f23cbb69dc..13aa4d8351 100644 --- a/linux/crates/platform/src/settings/grid_preferences.rs +++ b/linux/crates/platform/src/settings/grid_preferences.rs @@ -47,6 +47,50 @@ impl GridRowHeight { } } +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +pub enum RowCopyFormat { + #[default] + Rows, + RowsWithHeaders, + Json, + Csv, + CsvWithHeaders, + Markdown, +} + +impl RowCopyFormat { + pub const ALL: [Self; 6] = [ + Self::Rows, + Self::RowsWithHeaders, + Self::Json, + Self::Csv, + Self::CsvWithHeaders, + Self::Markdown, + ]; + + pub fn from_nick(nick: &str) -> Self { + match nick { + "rows-headers" => Self::RowsWithHeaders, + "json" => Self::Json, + "csv" => Self::Csv, + "csv-headers" => Self::CsvWithHeaders, + "markdown" => Self::Markdown, + _ => Self::Rows, + } + } + + pub fn nick(self) -> &'static str { + match self { + Self::Rows => "rows", + Self::RowsWithHeaders => "rows-headers", + Self::Json => "json", + Self::Csv => "csv", + Self::CsvWithHeaders => "csv-headers", + Self::Markdown => "markdown", + } + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct GridPreferenceValues { pub show_row_numbers: bool, @@ -54,6 +98,7 @@ pub struct GridPreferenceValues { pub row_height: GridRowHeight, pub null_display: String, pub font: EditorFont, + pub row_copy_format: RowCopyFormat, } impl Default for GridPreferenceValues { @@ -64,6 +109,7 @@ impl Default for GridPreferenceValues { row_height: GridRowHeight::Normal, null_display: "NULL".to_owned(), font: EditorFont::System, + row_copy_format: RowCopyFormat::Rows, } } } @@ -71,6 +117,7 @@ impl Default for GridPreferenceValues { impl GridPreferenceValues { fn read(settings: &gio::Settings) -> Self { Self { + row_copy_format: RowCopyFormat::from_nick(&settings.string(keys::ROW_COPY_FORMAT)), show_row_numbers: settings.boolean(keys::SHOW_ROW_NUMBERS), show_alternate_rows: settings.boolean(keys::SHOW_ALTERNATE_ROWS), row_height: GridRowHeight::from_nick(&settings.string(keys::GRID_ROW_HEIGHT)), @@ -148,6 +195,7 @@ impl GridPreferences { | keys::NULL_DISPLAY | keys::GRID_USE_SYSTEM_FONT | keys::GRID_FONT + | keys::ROW_COPY_FORMAT ) && let Some(preferences) = weak.upgrade() { preferences.update(GridPreferenceValues::read(settings)); diff --git a/linux/crates/platform/src/settings/keys.rs b/linux/crates/platform/src/settings/keys.rs index fd4fe976b9..bd0fe73c6e 100644 --- a/linux/crates/platform/src/settings/keys.rs +++ b/linux/crates/platform/src/settings/keys.rs @@ -11,6 +11,7 @@ pub const USE_SYSTEM_FONT: &str = "use-system-font"; pub const CUSTOM_FONT: &str = "custom-font"; pub const GRID_USE_SYSTEM_FONT: &str = "grid-use-system-font"; pub const GRID_FONT: &str = "grid-font"; +pub const ROW_COPY_FORMAT: &str = "row-copy-format"; pub const STYLE_SCHEME: &str = "style-scheme"; pub const VIM_KEYBINDINGS: &str = "vim-keybindings"; pub const CONNECT_DIALOG_DRIVER: &str = "connect-dialog-driver"; diff --git a/linux/crates/platform/src/settings/mod.rs b/linux/crates/platform/src/settings/mod.rs index 2ececf6c1d..4c58e6f358 100644 --- a/linux/crates/platform/src/settings/mod.rs +++ b/linux/crates/platform/src/settings/mod.rs @@ -14,7 +14,7 @@ use tablepro_core::export::{CsvDecimal, CsvDelimiter, CsvLineBreak, CsvOptions, pub use editor_font::EditorFont; pub use error::SettingsError; -pub use grid_preferences::{GridPreferenceValues, GridPreferences, GridRowHeight}; +pub use grid_preferences::{GridPreferenceValues, GridPreferences, GridRowHeight, RowCopyFormat}; pub use window_geometry::WindowGeometry; /// The provider setting's value while the assistant is off. @@ -115,6 +115,14 @@ impl AppSettings { self.set_string(keys::GRID_ROW_HEIGHT, height) } + pub fn row_copy_format(&self) -> RowCopyFormat { + self.grid_preferences().snapshot().row_copy_format + } + + pub fn set_row_copy_format(&self, format: &str) -> Result<(), SettingsError> { + self.set_string(keys::ROW_COPY_FORMAT, format) + } + pub fn null_display(&self) -> String { self.grid_preferences().snapshot().null_display.clone() } diff --git a/linux/crates/platform/tests/settings.rs b/linux/crates/platform/tests/settings.rs index b30491b437..20a9673678 100644 --- a/linux/crates/platform/tests/settings.rs +++ b/linux/crates/platform/tests/settings.rs @@ -533,3 +533,29 @@ async fn grid_font_uses_the_shared_snapshot_and_preserves_the_editor_font() -> T assert_eq!(calls.get(), 3); Ok(()) } + +#[glib::async_test] +async fn row_copy_format_publishes_every_format_through_the_shared_model() -> TestResult { + use std::cell::RefCell; + use std::rc::Rc; + use tablepro_linux_platform::settings::RowCopyFormat; + + let fixture = Fixture::new()?; + let preferences = fixture.settings.grid_preferences(); + let original = preferences.snapshot(); + let observed = Rc::new(RefCell::new(Vec::new())); + let updates = observed.clone(); + preferences.connect_changed(move |preferences| updates.borrow_mut().push(preferences.snapshot().row_copy_format)); + + assert_eq!(fixture.settings.row_copy_format(), RowCopyFormat::Rows); + for format in RowCopyFormat::ALL { + fixture.settings.set_row_copy_format(format.nick())?; + assert_eq!(fixture.settings.row_copy_format(), format); + assert_eq!(preferences.snapshot().row_copy_format, format); + assert_eq!(RowCopyFormat::from_nick(format.nick()), format); + } + assert_eq!(observed.borrow().as_slice(), &RowCopyFormat::ALL[1..]); + assert_eq!(original.row_copy_format, RowCopyFormat::Rows); + assert_eq!(RowCopyFormat::from_nick("unknown"), RowCopyFormat::Rows); + Ok(()) +} diff --git a/linux/data/app.tablepro.TablePro.gschema.xml.in b/linux/data/app.tablepro.TablePro.gschema.xml.in index c82785d5ae..8915cf7d13 100644 --- a/linux/data/app.tablepro.TablePro.gschema.xml.in +++ b/linux/data/app.tablepro.TablePro.gschema.xml.in @@ -89,6 +89,20 @@ How much room a data grid row takes around its text. + + + + + + + + + + "rows" + Format of copied rows + The format Copy writes when several rows are selected. + + "NULL" Text for a NULL value diff --git a/linux/data/resources/ui/preferences-data-browser-group.ui b/linux/data/resources/ui/preferences-data-browser-group.ui index 40c40db731..3cc5159a53 100644 --- a/linux/data/resources/ui/preferences-data-browser-group.ui +++ b/linux/data/resources/ui/preferences-data-browser-group.ui @@ -43,6 +43,24 @@ + + + Copy Rows As + The format Copy writes when several rows are selected + + + + TSV + TSV with Headers + JSON + CSV + CSV with Headers + Markdown + + + + + Text for NULL diff --git a/linux/docs/state-management.md b/linux/docs/state-management.md index 5aca04ebab..11248f253e 100644 --- a/linux/docs/state-management.md +++ b/linux/docs/state-management.md @@ -93,7 +93,7 @@ A tab belongs to its session, and a session to one window ([decisions/0028-sessi ## Grid preferences -`AppSettings` owns one `GridPreferences` object. It reads the grid keys from GSettings and announces each changed snapshot. Every grid holds that object through its settings and follows its change signal. Preferences rows bind directly to GSettings, including both directions for choices stored as strings. Each grid owns its font CSS provider, scoped to that grid and removed when the grid closes. Its custom font description supplies both the rendered font and column width measurement. +`AppSettings` owns one `GridPreferences` object. It reads the grid keys from GSettings and announces each changed snapshot. Every grid holds that object through its settings and follows its change signal. Preferences rows bind directly to GSettings, including both directions for choices stored as strings. Each grid owns its font CSS provider, scoped to that grid and removed when the grid closes. Its custom font description supplies both the rendered font and column width measurement. Copy reads the row format from the shared snapshot when it runs; a single focused cell keeps its value format, and Copy As keeps the requested format. The snapshot belongs to its settings backend, so isolated test settings cannot change another grid. A display change redraws the existing row slots and resizes automatic columns. It keeps row selection, user column widths and active editor drafts. The formatting helpers receive display choices explicitly and keep no global preference state. diff --git a/linux/docs/storage.md b/linux/docs/storage.md index d70a66a716..03a0d51b62 100644 --- a/linux/docs/storage.md +++ b/linux/docs/storage.md @@ -345,6 +345,7 @@ The schema id is the application id and the path follows it: `app.tablepro.Table | `show-row-numbers` | `b` | `true` | | | `grid-use-system-font` | `b` | `true` | | | `grid-font` | `s` | `Monospace 10` | | +| `row-copy-format` | `s` | `rows` | rows, rows-headers, json, csv, csv-headers, markdown | | `grid-row-height` | `s` | `normal` | compact, normal, comfortable, spacious | | `null-display` | `s` | `NULL` | Displayed as at most 20 characters, without control characters; blank uses NULL | | `confirm-bulk-delete` | `b` | `true` | |