[gnome-text-editor] settings: allow setting line-height from GSettings



commit b17017f460234a3e130f44535a290a93a61bd590
Author: Christian Hergert <chergert redhat com>
Date:   Thu Jan 13 10:51:42 2022 -0800

    settings: allow setting line-height from GSettings
    
    gsettings set org.gnome.TextEditor line-height 2.0 will get you double
    spacing line-height. This just uses the CSS line-height attribute (from
    newer GTK releases which support it). The default is 1.2 as we've had
    since the early days of Text Editor.
    
    We're not currently putting this in the preferences dialog and there is
    no plan to, but we can certainly have an override for it in GSettings.
    
    Fixes #285

 data/org.gnome.TextEditor.gschema.xml |  6 +++++
 src/editor-page.c                     |  3 +++
 src/editor-preferences-dialog.c       | 44 ++++++++++++++++++++++++-----------
 src/editor-source-view.c              | 27 +++++++++++++++++++--
 4 files changed, 65 insertions(+), 15 deletions(-)
---
diff --git a/data/org.gnome.TextEditor.gschema.xml b/data/org.gnome.TextEditor.gschema.xml
index 6df68be..5ada0ff 100644
--- a/data/org.gnome.TextEditor.gschema.xml
+++ b/data/org.gnome.TextEditor.gschema.xml
@@ -147,5 +147,11 @@
       <summary>Enable Snippets</summary>
       <description>Enable the use of snippets registered with GtkSourceView from within the 
editor.</description>
     </key>
+    <key name="line-height" type="d">
+      <default>1.2</default>
+      <range min="0.5" max="10"/>
+      <summary>Line Height</summary>
+      <description>The line height to use for the selected font.</description>
+    </key>
   </schema>
 </schemalist>
diff --git a/src/editor-page.c b/src/editor-page.c
index 462608a..4440034 100644
--- a/src/editor-page.c
+++ b/src/editor-page.c
@@ -421,6 +421,9 @@ editor_page_constructed (GObject *object)
   g_settings_bind (app->settings, "enable-snippets",
                    self->view, "enable-snippets",
                    G_SETTINGS_BIND_GET);
+  g_settings_bind (app->settings, "line-height",
+                   self->view, "line-height",
+                   G_SETTINGS_BIND_GET);
 
   _editor_page_vim_init (self);
 }
diff --git a/src/editor-preferences-dialog.c b/src/editor-preferences-dialog.c
index 04bfc1d..5494d88 100644
--- a/src/editor-preferences-dialog.c
+++ b/src/editor-preferences-dialog.c
@@ -314,31 +314,44 @@ update_custom_font_cb (EditorPreferencesDialog *self,
                        const char              *key,
                        GSettings               *settings)
 {
+  g_autofree char *custom_font = NULL;
+  g_autoptr(GString) str = NULL;
+  gboolean use_system_font;
+  double line_height;
+  char line_height_str[G_ASCII_DTOSTR_BUF_SIZE];
+
   g_assert (EDITOR_IS_PREFERENCES_DIALOG (self));
   g_assert (G_IS_SETTINGS (settings));
 
-  if (!g_settings_get_boolean (settings, "use-system-font"))
+  line_height = g_settings_get_double (settings, "line-height");
+  use_system_font = g_settings_get_boolean (settings, "use-system-font");
+  custom_font = g_settings_get_string (settings, "custom-font");
+
+  str = g_string_new ("textview {\n");
+
+  if (!use_system_font)
     {
-      g_autofree char *custom_font = g_settings_get_string (settings, "custom-font");
       PangoFontDescription *font_desc = pango_font_description_from_string (custom_font);
-      g_autofree char *css = _editor_font_description_to_css (font_desc);
 
-      g_clear_pointer (&font_desc, pango_font_description_free);
-
-      if (css != NULL)
+      if (font_desc != NULL)
         {
-          g_autoptr(GString) str = g_string_new (NULL);
-
-          g_string_append_printf (str, "textview { %s }", css);
+          g_autofree char *css = _editor_font_description_to_css (font_desc);
 
-          /* Use -1 instead of str->len to avoid a string copy */
-          gtk_css_provider_load_from_data (self->css_provider, str->str, -1);
+          if (css != NULL)
+            g_string_append_printf (str, "  %s\n", css);
 
-          return;
+          pango_font_description_free (font_desc);
         }
     }
 
-  gtk_css_provider_load_from_data (self->css_provider, "", -1);
+  g_ascii_dtostr (line_height_str, sizeof line_height_str, line_height);
+  line_height_str[6] = 0;
+  g_string_append_printf (str, "  line-height: %s;\n", line_height_str);
+
+  g_string_append (str, "}");
+
+  /* Use -1 instead of str->len to avoid a string copy */
+  gtk_css_provider_load_from_data (self->css_provider, str->str, -1);
 }
 
 static void
@@ -452,6 +465,11 @@ editor_preferences_dialog_init (EditorPreferencesDialog *self)
                            G_CALLBACK (update_custom_font_cb),
                            self,
                            G_CONNECT_SWAPPED);
+  g_signal_connect_object (self->settings,
+                           "changed::line-height",
+                           G_CALLBACK (update_custom_font_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
 
   g_signal_connect_object (g_application_get_default (),
                            "notify::style-scheme",
diff --git a/src/editor-source-view.c b/src/editor-source-view.c
index e397fc0..4cea2a0 100644
--- a/src/editor-source-view.c
+++ b/src/editor-source-view.c
@@ -40,6 +40,7 @@ struct _EditorSourceView
   GMenuModel *spelling_menu;
   char *spelling_word;
   int font_scale;
+  double line_height;
 };
 
 G_DEFINE_TYPE (EditorSourceView, editor_source_view, GTK_SOURCE_TYPE_VIEW)
@@ -48,6 +49,7 @@ enum {
   PROP_0,
   PROP_FONT_DESC,
   PROP_FONT_SCALE,
+  PROP_LINE_HEIGHT,
   PROP_ZOOM_LEVEL,
   N_PROPS
 };
@@ -63,6 +65,7 @@ editor_source_view_update_css (EditorSourceView *self)
   g_autoptr(GString) str = NULL;
   g_autofree char *font_css = NULL;
   int size = 11; /* 11pt */
+  char line_height_str[G_ASCII_DTOSTR_BUF_SIZE];
 
   g_assert (EDITOR_IS_SOURCE_VIEW (self));
 
@@ -94,13 +97,17 @@ editor_source_view_update_css (EditorSourceView *self)
     }
 
   str = g_string_new ("textview {\n");
+
   if (font_desc)
     {
-
       font_css = _editor_font_description_to_css (font_desc);
       g_string_append (str, font_css);
-      g_string_append (str, "\nline-height:1.2;\n");
     }
+
+  g_ascii_dtostr (line_height_str, sizeof line_height_str, self->line_height);
+  line_height_str[6] = 0;
+  g_string_append_printf (str, "\nline-height: %s;\n", line_height_str);
+
   g_string_append (str, "}\n");
 
   gtk_css_provider_load_from_data (self->css_provider, str->str, -1);
@@ -691,6 +698,10 @@ editor_source_view_get_property (GObject    *object,
       g_value_set_int (value, self->font_scale);
       break;
 
+    case PROP_LINE_HEIGHT:
+      g_value_set_double (value, self->line_height);
+      break;
+
     case PROP_ZOOM_LEVEL:
       g_value_set_double (value, editor_source_view_get_zoom_level (self));
       break;
@@ -720,6 +731,11 @@ editor_source_view_set_property (GObject      *object,
       g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ZOOM_LEVEL]);
       break;
 
+    case PROP_LINE_HEIGHT:
+      self->line_height = g_value_get_double (value);
+      editor_source_view_update_css (self);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -739,6 +755,13 @@ editor_source_view_class_init (EditorSourceViewClass *klass)
 
   text_view_class->snapshot_layer = editor_source_view_snapshot_layer;
 
+  properties [PROP_LINE_HEIGHT] =
+    g_param_spec_double ("line-height",
+                         "Line height",
+                         "The line height of all lines",
+                         0.5, 10.0, 1.2,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   properties [PROP_FONT_DESC] =
     g_param_spec_boxed ("font-desc",
                          "Font Description",


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]