diff --git a/linux/changelog.d/4135.added.md b/linux/changelog.d/4135.added.md new file mode 100644 index 0000000000..1a239ffb1d --- /dev/null +++ b/linux/changelog.d/4135.added.md @@ -0,0 +1 @@ +The data grid has its own cell font setting in Preferences under Data Grid. diff --git a/linux/crates/app/src/ui/data_grid/cell_font.rs b/linux/crates/app/src/ui/data_grid/cell_font.rs new file mode 100644 index 0000000000..b98a843bf3 --- /dev/null +++ b/linux/crates/app/src/ui/data_grid/cell_font.rs @@ -0,0 +1,290 @@ +use std::sync::atomic::{AtomicU64, Ordering}; + +use gtk4::glib::translate::IntoGlib; +use gtk4::prelude::*; +use gtk4::{self as gtk, gdk, pango}; +use tablepro_linux_platform::EditorFont; + +pub(super) struct CellFont { + display: gdk::Display, + provider: gtk::CssProvider, + class: String, + applied: Option, +} + +impl CellFont { + pub(super) fn new(view: >k::ColumnView) -> Self { + static NEXT_CLASS: AtomicU64 = AtomicU64::new(1); + let class = format!("tp-grid-font-{}", NEXT_CLASS.fetch_add(1, Ordering::Relaxed)); + view.add_css_class(&class); + let display = view.display(); + let provider = gtk::CssProvider::new(); + gtk::style_context_add_provider_for_display(&display, &provider, gtk::STYLE_PROVIDER_PRIORITY_APPLICATION); + Self { + display, + provider, + class, + applied: None, + } + } + + pub(super) fn apply(&mut self, font: &EditorFont) -> bool { + if self.applied.as_ref() == Some(font) { + return false; + } + let css = match font { + EditorFont::System => String::new(), + EditorFont::Custom(description) => font_css(&self.class, &custom_font(description)), + }; + self.provider.load_from_string(&css); + self.applied = Some(font.clone()); + true + } +} + +impl Drop for CellFont { + fn drop(&mut self) { + gtk::style_context_remove_provider_for_display(&self.display, &self.provider); + } +} + +pub(super) fn custom_font(description: &str) -> pango::FontDescription { + let mut font = pango::FontDescription::from_string(description); + // The same fields must reach CSS and width measurement. + font.unset_fields( + !(pango::FontMask::FAMILY + | pango::FontMask::SIZE + | pango::FontMask::WEIGHT + | pango::FontMask::STYLE + | pango::FontMask::STRETCH), + ); + if font.family().is_none_or(|family| family.trim().is_empty()) { + font.set_family("Monospace"); + } + if font.size() <= 0 { + font.set_size(10 * pango::SCALE); + } + font.set_variant(pango::Variant::Normal); + font.set_weight(font.weight()); + font.set_style(font.style()); + font.set_stretch(font.stretch()); + font +} + +fn font_css(class: &str, font: &pango::FontDescription) -> String { + let mut declarations = Vec::new(); + if let Some(family) = font.family() { + let escaped = crate::ui::editor_font_provider::escape_css_string(&family); + declarations.push(format!("font-family: \"{escaped}\";")); + } + if font.size() > 0 { + let size = f64::from(font.size()) / f64::from(pango::SCALE); + let unit = if font.is_size_absolute() { "px" } else { "pt" }; + declarations.push(format!("font-size: {size}{unit};")); + } + declarations.push(format!("font-weight: {};", font.weight().into_glib())); + let style = match font.style() { + pango::Style::Italic => "italic", + pango::Style::Oblique => "oblique", + _ => "normal", + }; + declarations.push(format!("font-style: {style}; font-variant: normal;")); + let stretch = match font.stretch() { + pango::Stretch::UltraCondensed => "ultra-condensed", + pango::Stretch::ExtraCondensed => "extra-condensed", + pango::Stretch::Condensed => "condensed", + pango::Stretch::SemiCondensed => "semi-condensed", + pango::Stretch::SemiExpanded => "semi-expanded", + pango::Stretch::Expanded => "expanded", + pango::Stretch::ExtraExpanded => "extra-expanded", + pango::Stretch::UltraExpanded => "ultra-expanded", + _ => "normal", + }; + declarations.push(format!("font-stretch: {stretch};")); + format!("columnview.{class} .monospace {{ {} }}", declarations.join(" ")) +} + +#[cfg(test)] +mod tests { + use gtk4::subclass::prelude::*; + use tablepro_core::Value; + + use super::*; + use crate::test_support::{MemorySettings, descendants, drain_main_context, wait_until}; + use crate::ui::data_grid::test_grid::{Grid, GridSpec, WAIT, named, persisted, two_text_columns, two_text_values}; + + fn grid(settings: &MemorySettings) -> Grid { + Grid::build( + GridSpec { + settings: Some(settings.get().clone()), + ..GridSpec::table(two_text_columns()) + }, + persisted(&two_text_values()), + ) + } + + fn shown_font(grid: &Grid) -> pango::FontDescription { + grid.cell_at(0, 1) + .display() + .expect("display") + .pango_context() + .font_description() + .expect("font") + } + + #[gtk4::test] + fn grid_fonts_follow_shared_settings_without_affecting_other_grids_or_editors() { + let settings = MemorySettings::new(); + let isolated = MemorySettings::new(); + settings + .get() + .set_editor_font(&EditorFont::Custom("Monospace 13".into())) + .expect("editor font"); + crate::ui::editor_font_provider::apply(settings.get()); + let editor_css = crate::ui::editor_font_provider::installed_css(); + let first = grid(&settings); + let second = grid(&settings); + let third = grid(&isolated); + let original = shown_font(&third); + + settings + .get() + .set_grid_font(&EditorFont::Custom("Sans Bold Italic 20".into())) + .expect("cell font"); + + wait_until(WAIT, || { + [&first, &second].iter().all(|grid| { + let font = shown_font(grid); + font.weight() == pango::Weight::Bold && font.style() == pango::Style::Italic + }) + }) + .expect("both grids use the chosen font"); + assert_eq!(shown_font(&third), original); + assert_eq!(crate::ui::editor_font_provider::installed_css(), editor_css); + + settings.get().set_grid_font(&EditorFont::System).expect("system font"); + wait_until(WAIT, || { + shown_font(&first) == original && shown_font(&second) == original + }) + .expect("both grids return to the desktop font"); + } + + #[gtk4::test] + fn a_styled_cell_font_sizes_numeric_columns_for_the_rendered_glyphs() { + let settings = MemorySettings::new(); + settings + .get() + .set_grid_font(&EditorFont::Custom("Sans Bold Italic 18".into())) + .expect("cell font"); + let shown = Grid::build( + GridSpec { + settings: Some(settings.get().clone()), + ..GridSpec::table(vec![named("id", "integer", true), named("value", "bigint", false)]) + }, + persisted(&[vec![Value::Int(1), Value::Int(8_888_888_888_888)]]), + ); + let display = shown.cell_at(0, 1).display().expect("display").clone(); + wait_until(WAIT, || { + let layout = display.create_pango_layout(display.text().as_deref()); + let attrs = pango::AttrList::new(); + attrs.insert(pango::AttrFontFeatures::new("tnum")); + layout.set_attributes(Some(&attrs)); + display.width() > 0 && layout.pixel_size().0 <= display.width() + }) + .expect("the chosen styled font fits"); + let font = shown_font(&shown); + assert_eq!(font.weight(), pango::Weight::Bold); + assert_eq!(font.style(), pango::Style::Italic); + } + + #[gtk4::test] + fn an_inline_edit_keeps_its_draft_and_follows_the_cell_font() { + let settings = MemorySettings::new(); + let shown = grid(&settings); + let cell = shown.focus(0, 1); + assert!(shown.grid.edit().edit_cell(&cell)); + let editor = descendants(&cell) + .into_iter() + .find_map(|widget| widget.downcast::().ok()) + .expect("inline editor"); + editor.set_text("a draft"); + + settings + .get() + .set_grid_font(&EditorFont::Custom("Sans Bold Italic 20".into())) + .expect("cell font"); + + wait_until(WAIT, || { + editor + .pango_context() + .font_description() + .is_some_and(|font| font.weight() == pango::Weight::Bold && font.style() == pango::Style::Italic) + }) + .expect("the open editor follows the font"); + assert_eq!(editor.text(), "a draft"); + assert!(shown.grid.is_editing()); + } + + #[gtk4::test] + fn a_custom_font_change_preserves_the_width_the_user_chose() { + let settings = MemorySettings::new(); + let shown = grid(&settings); + let column = shown + .grid + .imp() + .columns + .borrow() + .view_column(1) + .expect("column") + .clone(); + column.set_fixed_width(400); + + settings + .get() + .set_grid_font(&EditorFont::Custom("Sans Bold Italic 20".into())) + .expect("cell font"); + wait_until(WAIT, || shown_font(&shown).weight() == pango::Weight::Bold).expect("font applied"); + + assert_eq!(column.fixed_width(), 400); + } + + #[gtk4::test] + fn closing_a_grid_releases_its_font_provider() { + let settings = MemorySettings::new(); + let shown = grid(&settings); + let provider = shown + .grid + .imp() + .cell_font + .borrow() + .as_ref() + .expect("font owner") + .provider + .downgrade(); + + drop(shown); + drain_main_context(); + + assert!(provider.upgrade().is_none()); + } + + #[test] + fn regular_custom_fonts_reset_style_and_weight() { + let css = font_css("scope", &custom_font("Monospace 10")); + assert!(css.contains("font-weight: 400;")); + assert!(css.contains("font-style: normal;")); + assert!(css.contains("font-stretch: normal;")); + assert!(css.contains("font-variant: normal;")); + } + + #[test] + fn custom_font_css_escapes_family_controls_and_keeps_absolute_sizes() { + let mut font = custom_font("Monospace Bold Italic 10"); + font.set_family("A\"B\\C\nD"); + font.set_absolute_size(15.5 * f64::from(pango::SCALE)); + let css = font_css("scope", &font); + assert!(css.contains("A\\\"B\\\\C\\a D"), "{css}"); + assert!(css.contains("font-size: 15.5px;")); + assert!(!css.contains('\n')); + } +} diff --git a/linux/crates/app/src/ui/data_grid/columns.rs b/linux/crates/app/src/ui/data_grid/columns.rs index 5c416d7fba..955e5adf31 100644 --- a/linux/crates/app/src/ui/data_grid/columns.rs +++ b/linux/crates/app/src/ui/data_grid/columns.rs @@ -16,6 +16,7 @@ use crate::services::grid::{ number_width_chars, sampled_texts, typical_texts, typical_width_chars, }; use crate::ui::handler_guard::HandlerGuard; +use tablepro_linux_platform::EditorFont; use tablepro_linux_platform::settings::GridPreferences; /// Rows sampled for a column's opening width. @@ -277,7 +278,7 @@ impl DataGrid { /// before it had a window measured in no theme's font. pub(super) fn rooted(&self) { let measured_in = self.imp().columns.borrow().measured_in.clone(); - if measured_in.is_some_and(|fonts| fonts != fonts_now(&self.column_view())) { + if measured_in.is_some_and(|fonts| fonts != self.fonts_now()) { self.fonts_changed(); } } @@ -294,9 +295,20 @@ impl DataGrid { self.imp().font_watch.replace(Some(HandlerGuard::new(&style, handler))); } + pub(super) fn cell_font(&self) -> Option { + match self.config()?.settings.grid_font() { + EditorFont::Custom(description) => Some(description), + EditorFont::System => None, + } + } + + fn fonts_now(&self) -> String { + fonts_now(&self.column_view(), self.cell_font().as_deref()) + } + fn size_columns(&self, measure: Measure) { let widths = self.opening_widths(measure); - self.imp().columns.borrow_mut().measured_in = Some(fonts_now(&self.column_view())); + self.imp().columns.borrow_mut().measured_in = Some(self.fonts_now()); for (index, view_column, width) in widths { self.imp().columns.borrow_mut().auto_widths.insert(index, width); view_column.set_fixed_width(width); @@ -309,7 +321,7 @@ impl DataGrid { let by_type = set.pages == Pages::Empty; let view = self.column_view(); let context = view.create_pango_context(); - let font = value_font(&context); + let font = value_font(&context, self.cell_font().as_deref()); let metrics = context.metrics(Some(&font), None); let char_px = pango::units_to_double(metrics.approximate_char_width()); let mut widths = Vec::new(); @@ -588,7 +600,7 @@ impl DataGrid { }; let view = self.column_view(); let context = view.create_pango_context(); - let metrics = context.metrics(Some(&value_font(&context)), None); + let metrics = context.metrics(Some(&value_font(&context, self.cell_font().as_deref())), None); let step = (pango::units_to_double(metrics.approximate_digit_width()) * f64::from(STEP_DIGITS)).ceil() as i32; let width = column.fixed_width(); let width = match wider { @@ -899,12 +911,17 @@ fn same_names(layout: &ColumnLayout, columns: &[GridColumn]) -> bool { names.len() == columns.len() && names.iter().zip(columns).all(|(name, column)| *name == column.name) } -// Libadwaita's monospace class overrides only family and size. -pub(super) fn value_font(context: &pango::Context) -> pango::FontDescription { - let mut monospace = pango::FontDescription::from_string(&adw::StyleManager::default().monospace_font_name()); - monospace.unset_fields(!(pango::FontMask::FAMILY | pango::FontMask::SIZE)); +pub(super) fn value_font(context: &pango::Context, custom: Option<&str>) -> pango::FontDescription { + let selected = match custom { + Some(description) => super::cell_font::custom_font(description), + None => { + let mut system = pango::FontDescription::from_string(&adw::StyleManager::default().monospace_font_name()); + system.unset_fields(!(pango::FontMask::FAMILY | pango::FontMask::SIZE)); + system + } + }; let mut font = context.font_description().unwrap_or_default(); - font.merge(Some(&monospace), true); + font.merge(Some(&selected), true); font } @@ -924,13 +941,17 @@ fn header_width(context: &pango::Context, title: &str) -> i32 { /// The grid's font, the text size in it, and the monospace font. A /// widget's own context follows a change only at the next frame, so a /// fresh one is read. -fn fonts_now(view: >k::ColumnView) -> String { +fn fonts_now(view: >k::ColumnView, custom: Option<&str>) -> String { let grid = view .create_pango_context() .font_description() .map(|font| font.to_string()) .unwrap_or_default(); - format!("{grid}; {}", adw::StyleManager::default().monospace_font_name()) + let cells = custom.map_or_else( + || adw::StyleManager::default().monospace_font_name().to_string(), + str::to_owned, + ); + format!("{grid}; {cells}") } /// How wide `text` is in the tabular figures libadwaita's `numeric` @@ -951,3 +972,18 @@ fn drawn_width(context: &pango::Context, font: &pango::FontDescription, text: &s layout.set_font_description(Some(font)); layout.pixel_size().0 } + +#[cfg(test)] +mod font_tests { + use super::*; + + #[test] + fn custom_font_measurement_preserves_weight_style_and_stretch() { + let context = pango::Context::new(); + let font = value_font(&context, Some("Sans Bold Italic Condensed 18")); + assert_eq!(font.weight(), pango::Weight::Bold); + assert_eq!(font.style(), pango::Style::Italic); + assert_eq!(font.stretch(), pango::Stretch::Condensed); + assert_eq!(font.size(), 18 * pango::SCALE); + } +} diff --git a/linux/crates/app/src/ui/data_grid/display_settings.rs b/linux/crates/app/src/ui/data_grid/display_settings.rs index dbbb524862..10f1fd29de 100644 --- a/linux/crates/app/src/ui/data_grid/display_settings.rs +++ b/linux/crates/app/src/ui/data_grid/display_settings.rs @@ -30,6 +30,12 @@ impl DataGrid { }; let values = config.settings.grid_preferences().snapshot(); let view = self.column_view(); + let font_changed = { + let mut installed = self.imp().cell_font.borrow_mut(); + installed + .get_or_insert_with(|| super::cell_font::CellFont::new(&view)) + .apply(&values.font) + }; for height in GridRowHeight::ALL { set_class(&view, height.css_class(), height == values.row_height); } @@ -42,6 +48,8 @@ impl DataGrid { if changed { self.imp().applied_null.replace(Some(values.null_display.clone())); self.redraw(); + } + if changed || font_changed { self.fonts_changed(); } } diff --git a/linux/crates/app/src/ui/data_grid/imp.rs b/linux/crates/app/src/ui/data_grid/imp.rs index 10275e0d52..17d90683d0 100644 --- a/linux/crates/app/src/ui/data_grid/imp.rs +++ b/linux/crates/app/src/ui/data_grid/imp.rs @@ -95,6 +95,7 @@ pub struct DataGrid { pub(super) font_watch: RefCell>, pub(super) display_watch: RefCell>, pub(super) applied_null: RefCell>, + pub(super) cell_font: RefCell>, } #[glib::object_subclass] @@ -134,6 +135,7 @@ impl ObjectImpl for DataGrid { self.summary.stop(); self.obj().stop_watching_focus(); drop(self.font_watch.take()); + drop(self.cell_font.take()); drop(self.display_watch.take()); if let Some(menus) = self.menus.get() { menus.unparent(); diff --git a/linux/crates/app/src/ui/data_grid/mod.rs b/linux/crates/app/src/ui/data_grid/mod.rs index 70d3a25ddd..14d7fef63a 100644 --- a/linux/crates/app/src/ui/data_grid/mod.rs +++ b/linux/crates/app/src/ui/data_grid/mod.rs @@ -1,6 +1,7 @@ //! The data grid every surface shows rows in: the table tab, the SQL //! editor's results and a key-value engine's key tab. +mod cell_font; mod client_sorter; mod column_chooser; mod columns; @@ -91,9 +92,9 @@ impl DataGrid { pinned_numbers::install(&grid); find_bar::install(&grid); imp.detail_pane.attach(&grid); + grid.watch_display_settings(); grid.set_columns(columns, sorting, rules); grid.watch_fonts(); - grid.watch_display_settings(); grid } diff --git a/linux/crates/app/src/ui/data_grid/row_detail/fields.rs b/linux/crates/app/src/ui/data_grid/row_detail/fields.rs index ce300f2e15..b06cfdb1c5 100644 --- a/linux/crates/app/src/ui/data_grid/row_detail/fields.rs +++ b/linux/crates/app/src/ui/data_grid/row_detail/fields.rs @@ -328,7 +328,11 @@ impl RowDetailPane { if wanted.is_empty() || column.name.to_lowercase().contains(&wanted) { return true; } - let value = field_value(&grid.tracked(), &row, &column); + let Some(config) = grid.config() else { + return false; + }; + let preferences = config.settings.grid_preferences().snapshot(); + let value = field_value(&grid.tracked(), &row, &column, &preferences.null_display); !value.null && value.text.to_lowercase().contains(&wanted) } diff --git a/linux/crates/app/src/ui/data_grid/row_number_column.rs b/linux/crates/app/src/ui/data_grid/row_number_column.rs index be50ece427..f675c8ed71 100644 --- a/linux/crates/app/src/ui/data_grid/row_number_column.rs +++ b/linux/crates/app/src/ui/data_grid/row_number_column.rs @@ -140,7 +140,10 @@ pub fn fit(grid: &DataGrid) { // A fresh context, since the view's own follows a font change only at // the next frame. let context = grid.column_view().create_pango_context(); - let metrics = context.metrics(Some(&super::columns::value_font(&context)), None); + let metrics = context.metrics( + Some(&super::columns::value_font(&context, grid.cell_font().as_deref())), + 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); diff --git a/linux/crates/app/src/ui/editor_font_provider.rs b/linux/crates/app/src/ui/editor_font_provider.rs index 5d6cf7f61b..143e67a193 100644 --- a/linux/crates/app/src/ui/editor_font_provider.rs +++ b/linux/crates/app/src/ui/editor_font_provider.rs @@ -65,10 +65,17 @@ fn editor_font_css(font: &pango::FontDescription) -> String { ) } -/// A family name reaches CSS inside double quotes, so a quote or a -/// backslash in it would end the string early. -fn escape_css_string(value: &str) -> String { - value.replace('\\', "\\\\").replace('"', "\\\"") +// Font names can come from external settings and must stay inside the CSS string. +pub(super) fn escape_css_string(value: &str) -> String { + value + .chars() + .map(|ch| match ch { + '\\' => "\\\\".to_owned(), + '"' => "\\\"".to_owned(), + ch if ch.is_control() => format!("\\{:x} ", u32::from(ch)), + ch => ch.to_string(), + }) + .collect() } #[cfg(test)] 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 611b65ca31..b811995241 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 @@ -22,6 +22,12 @@ mod imp { #[template_child] pub confirm_row: TemplateChild, #[template_child] + pub grid_system_font_row: TemplateChild, + #[template_child] + pub grid_font_row: TemplateChild, + #[template_child] + pub grid_font_button: TemplateChild, + #[template_child] pub row_numbers_row: TemplateChild, #[template_child] pub row_height_row: TemplateChild, @@ -91,6 +97,21 @@ impl DataBrowserGroup { gio_settings .bind(keys::NULL_DISPLAY, &imp.null_display_row.get(), "text") .build(); + gio_settings + .bind(keys::GRID_USE_SYSTEM_FONT, &imp.grid_system_font_row.get(), "active") + .build(); + gio_settings + .bind(keys::GRID_USE_SYSTEM_FONT, &imp.grid_font_row.get(), "sensitive") + .flags(gio::SettingsBindFlags::GET | gio::SettingsBindFlags::INVERT_BOOLEAN) + .build(); + gio_settings + .bind(keys::GRID_FONT, &imp.grid_font_button.get(), "font-desc") + .mapping(|variant, _| Some(gtk4::pango::FontDescription::from_string(variant.str()?).to_value())) + .set_mapping(|value, _| { + let description = value.get::().ok()?; + Some(description.to_str().to_variant()) + }) + .build(); gio_settings .bind(keys::GRID_ROW_HEIGHT, &imp.row_height_row.get(), "selected") .mapping(|variant, _| { @@ -208,4 +229,33 @@ mod tests { assert_eq!(group.imp().null_display_row.text(), "界".repeat(20)); assert_eq!(settings.get().null_display(), "界".repeat(20)); } + #[gtk4::test] + fn cell_font_rows_bind_both_ways_across_windows() { + use tablepro_linux_platform::EditorFont; + let settings = MemorySettings::new(); + let first = bound_group(&settings); + let second = bound_group(&settings); + assert!(!first.imp().grid_font_row.is_sensitive()); + assert_eq!( + first.imp().grid_font_row.activatable_widget(), + Some(first.imp().grid_font_button.get().upcast()) + ); + + first.imp().grid_system_font_row.set_active(false); + first + .imp() + .grid_font_button + .set_font_desc(>k4::pango::FontDescription::from_string("Sans Bold 15")); + assert_eq!(settings.get().grid_font(), EditorFont::Custom("Sans Bold 15".into())); + assert!(!second.imp().grid_system_font_row.is_active()); + assert!(second.imp().grid_font_row.is_sensitive()); + assert_eq!( + first.imp().grid_font_button.font_desc(), + second.imp().grid_font_button.font_desc() + ); + + settings.get().set_grid_font(&EditorFont::System).expect("system font"); + assert!(first.imp().grid_system_font_row.is_active()); + assert!(!second.imp().grid_font_row.is_sensitive()); + } } diff --git a/linux/crates/platform/src/settings/grid_preferences.rs b/linux/crates/platform/src/settings/grid_preferences.rs index 6889059db9..f23cbb69dc 100644 --- a/linux/crates/platform/src/settings/grid_preferences.rs +++ b/linux/crates/platform/src/settings/grid_preferences.rs @@ -5,7 +5,7 @@ use std::sync::OnceLock; use gio::prelude::*; use glib::subclass::prelude::*; -use super::keys; +use super::{EditorFont, keys}; #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] pub enum GridRowHeight { @@ -53,6 +53,7 @@ pub struct GridPreferenceValues { pub show_alternate_rows: bool, pub row_height: GridRowHeight, pub null_display: String, + pub font: EditorFont, } impl Default for GridPreferenceValues { @@ -62,6 +63,7 @@ impl Default for GridPreferenceValues { show_alternate_rows: true, row_height: GridRowHeight::Normal, null_display: "NULL".to_owned(), + font: EditorFont::System, } } } @@ -73,6 +75,11 @@ impl GridPreferenceValues { show_alternate_rows: settings.boolean(keys::SHOW_ALTERNATE_ROWS), row_height: GridRowHeight::from_nick(&settings.string(keys::GRID_ROW_HEIGHT)), null_display: null_display_text(&settings.string(keys::NULL_DISPLAY)), + font: if settings.boolean(keys::GRID_USE_SYSTEM_FONT) { + EditorFont::System + } else { + EditorFont::Custom(settings.string(keys::GRID_FONT).into()) + }, } } } @@ -135,7 +142,12 @@ impl GridPreferences { let handler = settings.connect_changed(None, move |settings, key| { if matches!( key, - keys::SHOW_ROW_NUMBERS | keys::SHOW_ALTERNATE_ROWS | keys::GRID_ROW_HEIGHT | keys::NULL_DISPLAY + keys::SHOW_ROW_NUMBERS + | keys::SHOW_ALTERNATE_ROWS + | keys::GRID_ROW_HEIGHT + | keys::NULL_DISPLAY + | keys::GRID_USE_SYSTEM_FONT + | keys::GRID_FONT ) && 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 0b9d74d3ef..fd4fe976b9 100644 --- a/linux/crates/platform/src/settings/keys.rs +++ b/linux/crates/platform/src/settings/keys.rs @@ -9,6 +9,8 @@ pub const HISTORY_RETENTION_DAYS: &str = "history-retention-days"; pub const QUERY_TIMEOUT_SECS: &str = "query-timeout-secs"; 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 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 df83a22e82..2ececf6c1d 100644 --- a/linux/crates/platform/src/settings/mod.rs +++ b/linux/crates/platform/src/settings/mod.rs @@ -172,6 +172,20 @@ impl AppSettings { } } + pub fn grid_font(&self) -> EditorFont { + self.grid_preferences().snapshot().font.clone() + } + + pub fn set_grid_font(&self, font: &EditorFont) -> Result<(), SettingsError> { + match font { + EditorFont::System => self.set_boolean(keys::GRID_USE_SYSTEM_FONT, true), + EditorFont::Custom(description) => { + self.set_string(keys::GRID_FONT, description)?; + self.set_boolean(keys::GRID_USE_SYSTEM_FONT, false) + } + } + } + pub fn style_scheme(&self) -> String { self.settings.string(keys::STYLE_SCHEME).into() } diff --git a/linux/crates/platform/tests/settings.rs b/linux/crates/platform/tests/settings.rs index 35d889ca53..b30491b437 100644 --- a/linux/crates/platform/tests/settings.rs +++ b/linux/crates/platform/tests/settings.rs @@ -505,3 +505,31 @@ async fn an_explicit_adwaita_choice_is_distinct_from_the_system_default() -> Tes assert_eq!(fixture.settings.style_scheme(), ""); Ok(()) } + +#[glib::async_test] +async fn grid_font_uses_the_shared_snapshot_and_preserves_the_editor_font() -> TestResult { + use std::cell::Cell; + use std::rc::Rc; + + let fixture = Fixture::new()?; + let preferences = fixture.settings.grid_preferences(); + let calls = Rc::new(Cell::new(0)); + let observed = calls.clone(); + preferences.connect_changed(move |_| observed.set(observed.get() + 1)); + let editor_font = fixture.settings.editor_font(); + let custom = EditorFont::Custom("Sans Bold Italic 15".into()); + + fixture.settings.set_grid_font(&custom)?; + assert_eq!(preferences.snapshot().font, custom); + assert_eq!(fixture.settings.grid_font(), custom); + assert_eq!(calls.get(), 1); + assert_eq!(fixture.settings.editor_font(), editor_font); + + fixture.settings.set_grid_font(&EditorFont::System)?; + assert_eq!(preferences.snapshot().font, EditorFont::System); + assert_eq!(calls.get(), 2); + fixture.settings.set_grid_font(&custom)?; + assert_eq!(preferences.snapshot().font, custom); + assert_eq!(calls.get(), 3); + Ok(()) +} diff --git a/linux/data/app.tablepro.TablePro.gschema.xml.in b/linux/data/app.tablepro.TablePro.gschema.xml.in index 53b0cb4554..c82785d5ae 100644 --- a/linux/data/app.tablepro.TablePro.gschema.xml.in +++ b/linux/data/app.tablepro.TablePro.gschema.xml.in @@ -127,6 +127,18 @@ Pango font description the SQL editor uses when the system font is turned off. + + true + Use the system monospace font in the data grid + Whether the data grid follows the desktop monospace font instead of a custom one. + + + + 'Monospace 10' + Custom data grid font + Pango font description the data grid uses when the system font is turned off. + + '' Editor style scheme diff --git a/linux/data/resources/ui/preferences-data-browser-group.ui b/linux/data/resources/ui/preferences-data-browser-group.ui index bb541a71b6..40c40db731 100644 --- a/linux/data/resources/ui/preferences-data-browser-group.ui +++ b/linux/data/resources/ui/preferences-data-browser-group.ui @@ -50,6 +50,28 @@ Up to 20 characters. Empty text uses NULL. + + + Use the System Monospace Font for Cells + + + + + Cell Font + grid_font_button + + + + Cell Font + + center + + + + + + + Confirm Before Deleting Rows diff --git a/linux/docs/state-management.md b/linux/docs/state-management.md index e7825026c2..ba4233eb59 100644 --- a/linux/docs/state-management.md +++ b/linux/docs/state-management.md @@ -89,7 +89,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. +`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. 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 f7e63a9198..d70a66a716 100644 --- a/linux/docs/storage.md +++ b/linux/docs/storage.md @@ -343,6 +343,8 @@ The schema id is the application id and the path follows it: `app.tablepro.Table | `default-page-size` | `u` | `1000` | 100 to 100000 | | `show-alternate-rows` | `b` | `true` | | | `show-row-numbers` | `b` | `true` | | +| `grid-use-system-font` | `b` | `true` | | +| `grid-font` | `s` | `Monospace 10` | | | `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` | | diff --git a/linux/po/POTFILES.in b/linux/po/POTFILES.in index 464cf46144..879e1aa5b7 100644 --- a/linux/po/POTFILES.in +++ b/linux/po/POTFILES.in @@ -245,6 +245,7 @@ crates/app/src/ui/credential_prompt/presenter.rs crates/app/src/ui/credential_prompt/prompt_form.rs crates/app/src/ui/credential_prompt/prompt_text.rs crates/app/src/ui/credential_prompt/prompter.rs +crates/app/src/ui/data_grid/cell_font.rs crates/app/src/ui/data_grid/client_sorter.rs crates/app/src/ui/data_grid/column_chooser.rs crates/app/src/ui/data_grid/columns.rs