[gtranslator/font-size] Font change in preferences



commit 2d74bd59ad1c2c13ffaf1320f5845d1ee419f940
Author: Daniel GarcĂ­a Moreno <dani danigm net>
Date:   Thu Sep 10 09:36:53 2020 +0200

    Font change in preferences
    
    Instead of change just the font size with three buttons it's better to
    provide a way to change the font and font size in the preferences
    editor.
    
    The editor font is a preference so it's better placed there. This patch
    provides a font-chooser button so now it's possible to change the font
    and font size at once in the preferences dialog.
    
    See https://gitlab.gnome.org/GNOME/gtranslator/-/issues/120

 data/org.gnome.Gtranslator.gschema.xml.in |  4 +-
 src/gtr-preferences-dialog.c              | 38 +++++++++++++
 src/gtr-preferences-dialog.ui             | 11 ++++
 src/gtr-settings.c                        | 14 ++---
 src/gtr-settings.h                        |  2 +-
 src/gtr-tab.c                             | 91 -------------------------------
 src/gtr-tab.ui                            | 82 ----------------------------
 src/gtr-view.c                            | 22 +++++---
 src/gtr-view.h                            |  2 +-
 9 files changed, 75 insertions(+), 191 deletions(-)
---
diff --git a/data/org.gnome.Gtranslator.gschema.xml.in b/data/org.gnome.Gtranslator.gschema.xml.in
index 690b5ec0..8c9b110e 100644
--- a/data/org.gnome.Gtranslator.gschema.xml.in
+++ b/data/org.gnome.Gtranslator.gschema.xml.in
@@ -86,8 +86,8 @@
         If true, check the spelling of translated messages.
       </description>
     </key>
-    <key name="font-size" type="i">
-      <default>0</default>
+    <key name="font" type="s">
+      <default>''</default>
     </key>
   </schema>
   <schema gettext-domain="@GETTEXT_PACKAGE@" id="org.gnome.gtranslator.preferences.ui" 
path="/org/gnome/gtranslator/preferences/ui/">
diff --git a/src/gtr-preferences-dialog.c b/src/gtr-preferences-dialog.c
index 6aa0f351..04c013ce 100644
--- a/src/gtr-preferences-dialog.c
+++ b/src/gtr-preferences-dialog.c
@@ -65,6 +65,7 @@ typedef struct
   /* Editor->Text display */
   GtkWidget *highlight_syntax_checkbutton;
   GtkWidget *visible_whitespace_checkbutton;
+  GtkWidget *font_button;
 
   /* Editor->Contents */
   GtkWidget *unmark_fuzzy_when_changed_checkbutton;
@@ -193,6 +194,33 @@ setup_editor_pages (GtrPreferencesDialog * dlg)
   setup_editor_contents (dlg);
 }
 
+static void
+on_font_set (GtkWidget *widget, GtrPreferencesDialog *dlg)
+{
+  GtrPreferencesDialogPrivate *priv = gtr_preferences_dialog_get_instance_private (dlg);
+  g_autofree char *font = NULL;
+
+  font = gtk_font_chooser_get_font (GTK_FONT_CHOOSER (priv->font_button));
+  g_settings_set_string (priv->editor_settings, GTR_SETTINGS_FONT, font);
+}
+
+static char *
+get_default_font () {
+  g_autoptr(GSettings) settings = NULL;
+  g_autoptr(GSettingsSchema) schema = NULL;
+  GSettingsSchemaSource *source = g_settings_schema_source_get_default ();
+  char *font = NULL;
+
+  schema = g_settings_schema_source_lookup (source, "org.gnome.desktop.interface", TRUE);
+  if (!schema || !g_settings_schema_has_key (schema, "monospace-font-name"))
+    return NULL;
+
+  settings = g_settings_new ("org.gnome.desktop.interface");
+  font = g_settings_get_string (settings, "monospace-font-name");
+  return font;
+}
+
+
 /***************Profile pages****************/
 static void
 on_profile_dialog_response_cb (GtrProfileDialog     *profile_dialog,
@@ -613,6 +641,7 @@ gtr_preferences_dialog_init (GtrPreferencesDialog * dlg)
     NULL
   };
   GtrPreferencesDialogPrivate *priv = gtr_preferences_dialog_get_instance_private (dlg);
+  g_autofree char *font = NULL;
 
   priv->ui_settings = g_settings_new ("org.gnome.gtranslator.preferences.ui");
   priv->editor_settings = g_settings_new ("org.gnome.gtranslator.preferences.editor");
@@ -647,6 +676,7 @@ gtr_preferences_dialog_init (GtrPreferencesDialog * dlg)
   priv->create_backup_checkbutton = GTK_WIDGET (gtk_builder_get_object (builder, 
"create_backup_checkbutton"));
   priv->highlight_syntax_checkbutton = GTK_WIDGET (gtk_builder_get_object (builder, 
"highlight_checkbutton"));
   priv->visible_whitespace_checkbutton = GTK_WIDGET (gtk_builder_get_object (builder, 
"visible_whitespace_checkbutton"));
+  priv->font_button = GTK_WIDGET (gtk_builder_get_object (builder, "font_button"));
   priv->unmark_fuzzy_when_changed_checkbutton = GTK_WIDGET (gtk_builder_get_object (builder, 
"unmark_fuzzy_checkbutton"));
   priv->spellcheck_checkbutton = GTK_WIDGET (gtk_builder_get_object (builder, "spellcheck_checkbutton"));
   priv->profile_treeview = GTK_WIDGET (gtk_builder_get_object (builder, "profile_treeview"));
@@ -670,6 +700,14 @@ gtr_preferences_dialog_init (GtrPreferencesDialog * dlg)
   setup_files_pages (dlg);
   setup_editor_pages (dlg);
   setup_profile_pages (dlg);
+
+  font = g_settings_get_string (priv->editor_settings, GTR_SETTINGS_FONT);
+  if (!strlen (font))
+    font = get_default_font ();
+
+  gtk_font_chooser_set_font (GTK_FONT_CHOOSER (priv->font_button), font);
+
+  g_signal_connect (priv->font_button, "font-set", G_CALLBACK (on_font_set), dlg);
 }
 
 static void
diff --git a/src/gtr-preferences-dialog.ui b/src/gtr-preferences-dialog.ui
index 0054af64..d8211fe8 100644
--- a/src/gtr-preferences-dialog.ui
+++ b/src/gtr-preferences-dialog.ui
@@ -362,6 +362,17 @@
                                 <property name="position">1</property>
                               </packing>
                             </child>
+                            <child>
+                              <object class="GtkFontButton" id="font_button">
+                                <property name="visible">True</property>
+                                <property name="can_focus">True</property>
+                              </object>
+                              <packing>
+                                <property name="expand">False</property>
+                                <property name="fill">False</property>
+                                <property name="position">2</property>
+                              </packing>
+                            </child>
                           </object>
                         </child>
                       </object>
diff --git a/src/gtr-settings.c b/src/gtr-settings.c
index eae56a7a..1e51b6c0 100644
--- a/src/gtr-settings.c
+++ b/src/gtr-settings.c
@@ -180,19 +180,19 @@ on_spellcheck_changed (GSettings * settings,
 }
 
 static void
-on_font_size_changed (GSettings * settings,
-                      const gchar * key, GtrSettings * gs)
+on_font_changed (GSettings * settings,
+                 const gchar * key, GtrSettings * gs)
 {
   GList *views, *l;
-  int size;
+  g_autofree char *font = NULL;
 
-  size = g_settings_get_int (settings, key);
+  font = g_settings_get_string (settings, GTR_SETTINGS_FONT);
 
   views = gtr_application_get_views (GTR_APP, TRUE, TRUE);
 
   for (l = views; l != NULL; l = g_list_next (l))
     {
-      gtr_view_set_font_size (GTR_VIEW (l->data), size);
+      gtr_view_set_font (GTR_VIEW (l->data), font);
     }
 
   g_list_free (views);
@@ -247,8 +247,8 @@ gtr_settings_init (GtrSettings * gs)
                     "changed::color-scheme",
                     G_CALLBACK (on_scheme_changed), gs);
   g_signal_connect (priv->editor,
-                    "changed::font-size",
-                    G_CALLBACK (on_font_size_changed), gs);
+                    "changed::font",
+                    G_CALLBACK (on_font_changed), gs);
 }
 
 static void
diff --git a/src/gtr-settings.h b/src/gtr-settings.h
index c0bccee0..1b3d559d 100644
--- a/src/gtr-settings.h
+++ b/src/gtr-settings.h
@@ -65,7 +65,7 @@ GSettings               *gtr_settings_new                       (void);
 #define GTR_SETTINGS_VISIBLE_WHITESPACE                        "visible-whitespace"
 #define GTR_SETTINGS_UNMARK_FUZZY_WHEN_CHANGED         "unmark-fuzzy-when-changed"
 #define GTR_SETTINGS_SPELLCHECK                                "spellcheck"
-#define GTR_SETTINGS_FONT_SIZE                         "font-size"
+#define GTR_SETTINGS_FONT                              "font"
 #define GTR_SETTINGS_PANEL_SWITCHER_STYLE              "panel-switcher-style"
 #define GTR_SETTINGS_COLOR_SCHEME                      "color-scheme"
 #define GTR_SETTINGS_SORT_ORDER                                "sort-order"
diff --git a/src/gtr-tab.c b/src/gtr-tab.c
index 712cb136..c203426b 100644
--- a/src/gtr-tab.c
+++ b/src/gtr-tab.c
@@ -120,11 +120,6 @@ typedef struct
   GtrSearchBar   *search_bar;
   GtkSearchEntry *search;
 
-  /* Text zoom */
-  GtkWidget *zoom_in;
-  GtkWidget *zoom_original;
-  GtkWidget *zoom_out;
-
 } GtrTabPrivate;
 
 G_DEFINE_TYPE_WITH_PRIVATE (GtrTab, gtr_tab, GTK_TYPE_BOX)
@@ -150,35 +145,6 @@ static guint signals[LAST_SIGNAL];
 
 static gboolean gtr_tab_autosave (GtrTab * tab);
 
-static int DEFAULT_FONT_SIZE = 18;
-
-static void
-update_default_font_size () {
-  gchar **font_parts = NULL;
-  int i = 0;
-  g_autofree char *font = NULL;
-  g_autoptr(GSettings) settings = NULL;
-  g_autoptr(GSettingsSchema) schema = NULL;
-  GSettingsSchemaSource *source = g_settings_schema_source_get_default ();
-
-  DEFAULT_FONT_SIZE = 12;
-
-  schema = g_settings_schema_source_lookup (source, "org.gnome.desktop.interface", TRUE);
-  if (!schema || !g_settings_schema_has_key (schema, "font-name"))
-    return;
-
-  settings = g_settings_new ("org.gnome.desktop.interface");
-  font = g_settings_get_string (settings, "font-name");
-  font_parts = g_strsplit (font, " ", -1);
-
-  while (font_parts[i])
-    i++;
-
-  DEFAULT_FONT_SIZE = atoi (font_parts[i ? i - 1 : 0]);
-
-  g_strfreev (font_parts);
-}
-
 //---------------------------Search Bar Revealer------------------//
 
 void
@@ -261,51 +227,6 @@ gtr_page_notify_child_revealed (GtrTab *tab,
 
 //----------------------------------------------------------------//
 
-static gboolean
-zoom_in (GtkWidget *widget, GtrTab *tab)
-{
-  GtrTabPrivate *priv;
-  int size;
-
-  priv = gtr_tab_get_instance_private (tab);
-
-  size = g_settings_get_int (priv->editor_settings, GTR_SETTINGS_FONT_SIZE);
-  size = size == 0 ? DEFAULT_FONT_SIZE : size;
-
-  if (size <= 32)
-    g_settings_set_int (priv->editor_settings, GTR_SETTINGS_FONT_SIZE, size + 1);
-
-  return TRUE;
-}
-
-static gboolean
-zoom_original (GtkWidget *widget, GtrTab *tab)
-{
-  GtrTabPrivate *priv;
-
-  priv = gtr_tab_get_instance_private (tab);
-  g_settings_set_int (priv->editor_settings, GTR_SETTINGS_FONT_SIZE, 0);
-
-  return TRUE;
-}
-
-static gboolean
-zoom_out (GtkWidget *widget, GtrTab *tab)
-{
-  GtrTabPrivate *priv;
-  int size;
-
-  priv = gtr_tab_get_instance_private (tab);
-
-  size = g_settings_get_int (priv->editor_settings, GTR_SETTINGS_FONT_SIZE);
-  size = size == 0 ? DEFAULT_FONT_SIZE : size;
-
-  if (size >= 4)
-    g_settings_set_int (priv->editor_settings, GTR_SETTINGS_FONT_SIZE, size - 1);
-
-  return TRUE;
-}
-
 static gboolean
 show_hide_revealer (GtkWidget *widget, GdkEvent *ev, GtrTab *tab)
 {
@@ -853,15 +774,6 @@ gtr_tab_init (GtrTab * tab)
 
   g_signal_connect (priv->progress_eventbox, "button-press-event",
                     G_CALLBACK (show_hide_revealer), tab);
-
-  update_default_font_size();
-
-  g_signal_connect (priv->zoom_in, "clicked",
-                    G_CALLBACK (zoom_in), tab);
-  g_signal_connect (priv->zoom_original, "clicked",
-                    G_CALLBACK (zoom_original), tab);
-  g_signal_connect (priv->zoom_out, "clicked",
-                    G_CALLBACK (zoom_out), tab);
 }
 
 static void
@@ -1047,9 +959,6 @@ gtr_tab_class_init (GtrTabClass * klass)
   gtk_widget_class_bind_template_child_private (widget_class, GtrTab, overlay);
   gtk_widget_class_bind_template_child_private (widget_class, GtrTab, search_bar);
   gtk_widget_class_bind_template_child_private (widget_class, GtrTab, search_revealer);
-  gtk_widget_class_bind_template_child_private (widget_class, GtrTab, zoom_in);
-  gtk_widget_class_bind_template_child_private (widget_class, GtrTab, zoom_original);
-  gtk_widget_class_bind_template_child_private (widget_class, GtrTab, zoom_out);
   gtk_widget_class_bind_template_callback (widget_class, gtr_page_notify_child_revealed);
   gtk_widget_class_bind_template_callback (widget_class, gtr_page_stop_search);
 
diff --git a/src/gtr-tab.ui b/src/gtr-tab.ui
index 910c984e..49a144c9 100644
--- a/src/gtr-tab.ui
+++ b/src/gtr-tab.ui
@@ -110,88 +110,6 @@
                                           </style>
                                        </object>
                                     </child>
-
-                                    <child>
-                                      <object class="GtkButtonBox">
-                                        <property name="visible">True</property>
-                                        <property name="can_focus">False</property>
-                                        <property name="layout_style">start</property>
-                                        <property name="halign">end</property>
-                                        <style>
-                                         <class name="linked"/>
-                                         <class name="horizontal"/>
-                                        </style>
-                                        <child>
-                                          <object class="GtkButton" id="zoom_in">
-                                            <property name="tooltip_text" translatable="yes">Increase font 
size</property>
-                                            <property name="visible">True</property>
-                                            <property name="can_focus">True</property>
-                                            <property name="receives_default">True</property>
-                                            <child>
-                                              <object class="GtkImage">
-                                                <property name="visible">True</property>
-                                                <property name="can_focus">False</property>
-                                                <property name="icon_name">zoom-in-symbolic</property>
-                                              </object>
-                                            </child>
-                                          </object>
-                                          <packing>
-                                            <property name="expand">True</property>
-                                            <property name="fill">True</property>
-                                            <property name="position">0</property>
-                                            <property name="non_homogeneous">True</property>
-                                          </packing>
-                                        </child>
-                                        <child>
-                                          <object class="GtkButton" id="zoom_original">
-                                            <property name="tooltip_text" translatable="yes">Set default 
font size</property>
-                                            <property name="visible">True</property>
-                                            <property name="can_focus">True</property>
-                                            <property name="receives_default">True</property>
-                                            <child>
-                                              <object class="GtkImage">
-                                                <property name="visible">True</property>
-                                                <property name="can_focus">False</property>
-                                                <property name="icon_name">zoom-original-symbolic</property>
-                                              </object>
-                                            </child>
-                                          </object>
-                                          <packing>
-                                            <property name="expand">True</property>
-                                            <property name="fill">True</property>
-                                            <property name="position">1</property>
-                                            <property name="non_homogeneous">True</property>
-                                          </packing>
-                                        </child>
-                                        <child>
-                                          <object class="GtkButton" id="zoom_out">
-                                            <property name="tooltip_text" translatable="yes">Decrease font 
size</property>
-                                            <property name="visible">True</property>
-                                            <property name="can_focus">True</property>
-                                            <property name="receives_default">True</property>
-                                            <child>
-                                              <object class="GtkImage">
-                                                <property name="visible">True</property>
-                                                <property name="can_focus">False</property>
-                                                <property name="icon_name">zoom-out-symbolic</property>
-                                              </object>
-                                            </child>
-                                          </object>
-                                          <packing>
-                                            <property name="expand">True</property>
-                                            <property name="fill">True</property>
-                                            <property name="position">2</property>
-                                            <property name="non_homogeneous">True</property>
-                                          </packing>
-                                        </child>
-                                      </object>
-                                      <packing>
-                                        <property name="expand">True</property>
-                                        <property name="fill">True</property>
-                                        <property name="position">4</property>
-                                      </packing>
-                                    </child>
-
                                  </object>
                                  <packing>
                                     <property name="expand">False</property>
diff --git a/src/gtr-view.c b/src/gtr-view.c
index 55d4815f..1e6fc7df 100644
--- a/src/gtr-view.c
+++ b/src/gtr-view.c
@@ -40,6 +40,7 @@
 #include <gtk/gtk.h>
 
 #include <gtksourceview/gtksource.h>
+#include <dazzle.h>
 
 #include <gspell/gspell.h>
 
@@ -107,6 +108,8 @@ gtr_view_init (GtrView * view)
   gchar *ui_dir;
   GtrViewPrivate *priv;
 
+  g_autofree char *font = NULL;
+
   priv = gtr_view_get_instance_private (view);
 
   priv->provider = gtk_css_provider_new ();
@@ -150,8 +153,8 @@ gtr_view_init (GtrView * view)
                                       g_settings_get_boolean (priv->editor_settings,
                                                               GTR_SETTINGS_VISIBLE_WHITESPACE));
 
-  gtr_view_set_font_size (view, g_settings_get_int (priv->editor_settings,
-                                                    GTR_SETTINGS_FONT_SIZE));
+  font = g_settings_get_string (priv->editor_settings, GTR_SETTINGS_FONT);
+  gtr_view_set_font (view, font);
 
   /* Set scheme color according to preferences */
   gtr_view_reload_scheme_color (view);
@@ -910,15 +913,20 @@ gtr_view_reload_scheme_color (GtrView * view)
 }
 
 void
-gtr_view_set_font_size (GtrView *view, int size)
+gtr_view_set_font (GtrView *view, char *font)
 {
+  PangoFontDescription *font_desc = NULL;
+  g_autofree char *str = NULL;
   g_autofree char *css = NULL;
   GtrViewPrivate *priv = gtr_view_get_instance_private (view);
+  g_autoptr (GtkWidget) button = gtk_font_button_new ();
 
-  if (size == 0)
-    css = g_strdup ("textview { }");
-  else
-    css = g_strdup_printf ("textview { font-size: %dpt; }", size);
+  gtk_font_chooser_set_font (GTK_FONT_CHOOSER (button), font);
+  font_desc = gtk_font_chooser_get_font_desc (GTK_FONT_CHOOSER (button));
+  str = dzl_pango_font_description_to_css (font_desc);
+  css = g_strdup_printf ("textview { %s }", str ?: "");
 
   gtk_css_provider_load_from_data (priv->provider, css, -1, NULL);
+
+  pango_font_description_free (font_desc);
 }
diff --git a/src/gtr-view.h b/src/gtr-view.h
index 6c8d56e7..c9cf6f19 100644
--- a/src/gtr-view.h
+++ b/src/gtr-view.h
@@ -115,7 +115,7 @@ gtr_view_get_type (void)
      void gtr_view_set_language (GtrView *view,
                                  const gchar *lang);
 
-     void gtr_view_set_font_size (GtrView *view, int size);
+     void gtr_view_set_font (GtrView *view, char *font);
 
 
 /* Search macros */


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