[gitg] config for fonts



commit 0ed296041d68cddcd9bc98522a754341e6b28e21
Author: Gaurav Agrawal <agrawalgaurav1999 gmail com>
Date:   Thu Oct 17 05:48:20 2019 +0000

    config for fonts

 data/org.gnome.gitg.gschema.xml.in               | 11 +++
 gitg/commit/gitg-commit-dialog.vala              | 32 ++-------
 gitg/gitg-create-tag-dialog.vala                 | 28 +-------
 gitg/preferences/gitg-preferences-interface.vala | 39 ++++++++++
 gitg/resources/ui/gitg-preferences-interface.ui  | 84 +++++++++++++++++++++-
 libgitg/gitg-diff-view-file-renderer-text.vala   | 48 +++----------
 libgitg/gitg-font-manager.vala                   | 91 ++++++++++++++++++++++++
 libgitg/meson.build                              |  1 +
 plugins/files/gitg-files.vala                    | 33 +--------
 9 files changed, 244 insertions(+), 123 deletions(-)
---
diff --git a/data/org.gnome.gitg.gschema.xml.in b/data/org.gnome.gitg.gschema.xml.in
index 2bd0e210..a12bd1b9 100644
--- a/data/org.gnome.gitg.gschema.xml.in
+++ b/data/org.gnome.gitg.gschema.xml.in
@@ -46,6 +46,17 @@
         The activity which gitg activates by default when first launched.
       </description>
     </key>
+    <key name="use-default-font" type="b">
+      <default>true</default>
+      <summary>Use Default Font</summary>
+      <description>Whether to use the system’s default fixed width font for widget's text instead of a font 
specific. If this option is turned off, then the font named in the “Editor Font” option will be used instead 
of the system font.</description>
+    </key>
+    <key name="monospace-font-name" type="s">
+      <!-- Translators: This is a GSettings default value. Do NOT change or localize the quotation marks! -->
+      <default l10n="messages">'Monospace 12'</default>
+      <summary>Editor Font</summary>
+      <description>A custom font that will be used for the text widgets. This will only take effect if the 
“Use Default Font” option is turned off.</description>
+    </key>
     <key name="use-gravatar" type="b">
       <default>true</default>
       <summary>Use Gravatar</summary>
diff --git a/gitg/commit/gitg-commit-dialog.vala b/gitg/commit/gitg-commit-dialog.vala
index ce469423..695ed9ce 100644
--- a/gitg/commit/gitg-commit-dialog.vala
+++ b/gitg/commit/gitg-commit-dialog.vala
@@ -74,14 +74,13 @@ class Dialog : Gtk.Dialog
        private Cancellable? d_cancel_avatar;
        private bool d_constructed;
        private Settings? d_message_settings;
-       private Settings? d_font_settings;
+       private Gitg.FontManager d_font_manager;
        private Settings? d_commit_settings;
        private bool d_enable_spell_checking;
        private string? d_spell_checking_language;
        private Gspell.Checker? d_spell_checker;
        private Ggit.Diff d_diff;
        private bool d_infobar_shown;
-       private Gtk.CssProvider css_provider;
 
        public Ggit.Diff? diff
        {
@@ -394,7 +393,7 @@ class Dialog : Gtk.Dialog
                }
 
                d_message_settings = null;
-               d_font_settings = null;
+               d_font_manager = null;
                d_commit_settings = null;
 
                base.destroy();
@@ -402,15 +401,7 @@ class Dialog : Gtk.Dialog
 
        construct
        {
-               d_font_settings = new Settings("org.gnome.desktop.interface");
-               css_provider = new Gtk.CssProvider();
-               d_source_view_message.get_style_context().add_provider(css_provider, 
Gtk.STYLE_PROVIDER_PRIORITY_SETTINGS);
-
-               update_font_settings();
-
-               d_font_settings.changed["monospace-font-name"].connect((s, k) => {
-                       update_font_settings();
-               });
+               d_font_manager = new Gitg.FontManager(d_source_view_message, false);
 
                var b = d_source_view_message.buffer;
 
@@ -732,7 +723,7 @@ class Dialog : Gtk.Dialog
                                {
                                        break;
                                }
-                               
+
                                if (!toolong.forward_to_line_end())
                                {
                                        break;
@@ -772,21 +763,6 @@ class Dialog : Gtk.Dialog
                Object(repository: repository, author: author, diff: diff, use_header_bar: 1);
        }
 
-       private void update_font_settings()
-       {
-               var fname = d_font_settings.get_string("monospace-font-name");
-               var font_desc = Pango.FontDescription.from_string(fname);
-               var css = "textview { %s }".printf(Dazzle.pango_font_description_to_css(font_desc));
-               try
-               {
-                       css_provider.load_from_data(css);
-               }
-               catch(Error e)
-               {
-                       warning("Error applying font: %s", e.message);
-               }
-       }
-
        public void show_infobar(string          primary_msg,
                                 string          secondary_msg,
                                 Gtk.MessageType type)
diff --git a/gitg/gitg-create-tag-dialog.vala b/gitg/gitg-create-tag-dialog.vala
index e7b201d7..a15b9a9a 100644
--- a/gitg/gitg-create-tag-dialog.vala
+++ b/gitg/gitg-create-tag-dialog.vala
@@ -37,20 +37,11 @@ class CreateTagDialog : Gtk.Dialog
 
        private Gtk.TextTag d_info_tag;
        private bool d_is_showing_user_info;
-       private Settings d_font_settings;
-       private Gtk.CssProvider css_provider;
+       private FontManager d_font_manager;
 
        construct
        {
-               d_font_settings = new Settings("org.gnome.desktop.interface");
-               css_provider = new Gtk.CssProvider();
-               d_text_view_message.get_style_context().add_provider(css_provider, 
Gtk.STYLE_PROVIDER_PRIORITY_SETTINGS);
-
-               update_font_settings();
-
-               d_font_settings.changed["monospace-font-name"].connect((s, k) => {
-                       update_font_settings();
-               });
+               d_font_manager = new FontManager(d_text_view_message, false);
 
                d_entry_tag_name.changed.connect(() => {
                        d_button_create.sensitive = (new_tag_name.length != 0);
@@ -88,21 +79,6 @@ class CreateTagDialog : Gtk.Dialog
                set_default_response(Gtk.ResponseType.OK);
        }
 
-       private void update_font_settings()
-       {
-               var fname = d_font_settings.get_string("monospace-font-name");
-               var font_desc = Pango.FontDescription.from_string(fname);
-               var css = "textview { %s }".printf(Dazzle.pango_font_description_to_css(font_desc));
-               try
-               {
-                       css_provider.load_from_data(css);
-               }
-               catch(Error e)
-               {
-                       warning("Error applying font: %s", e.message);
-               }
-       }
-
        private void show_user_info()
        {
                var buf = d_text_view_message.buffer;
diff --git a/gitg/preferences/gitg-preferences-interface.vala 
b/gitg/preferences/gitg-preferences-interface.vala
index e4dc5fb7..0bb9a2ec 100644
--- a/gitg/preferences/gitg-preferences-interface.vala
+++ b/gitg/preferences/gitg-preferences-interface.vala
@@ -27,6 +27,7 @@ public class PreferencesInterface : Gtk.Grid, GitgExt.Preferences
        private const string version = Gitg.Config.VERSION;
        private bool d_block;
        private Settings? d_settings;
+       private Settings? d_global_settings;
 
        [GtkChild (name = "horizontal_layout_enabled")]
        private Gtk.CheckButton d_horizontal_layout_enabled;
@@ -49,9 +50,19 @@ public class PreferencesInterface : Gtk.Grid, GitgExt.Preferences
        [GtkChild (name = "syntax_scheme_store")]
        private Gtk.ListStore d_syntax_scheme_store;
 
+       [GtkChild (name = "font_button")]
+       private Gtk.FontButton d_font_button;
+
+       [GtkChild (name = "font_button_grid")]
+       private Gtk.Grid d_font_button_grid;
+
+       [GtkChild (name = "default_font_checkbutton")]
+       private Gtk.CheckButton d_default_font_checkbutton;
+
        construct
        {
                d_settings = new Settings(Gitg.Config.APPLICATION_ID + ".preferences.interface");
+               d_global_settings = new Settings("org.gnome.desktop.interface");
 
                d_horizontal_layout_enabled.active = d_settings.get_enum("orientation") == 0;
 
@@ -102,6 +113,34 @@ public class PreferencesInterface : Gtk.Grid, GitgExt.Preferences
                                d_default_style_scheme,
                                "active-id",
                                SettingsBindFlags.GET | SettingsBindFlags.SET);
+
+               d_settings.bind("use-default-font",
+                             d_default_font_checkbutton,
+                             "active",
+                             SettingsBindFlags.GET | SettingsBindFlags.SET);
+
+               d_settings.bind("use-default-font",
+                             d_font_button_grid,
+                             "sensitive",
+                             SettingsBindFlags.GET | SettingsBindFlags.SET | 
SettingsBindFlags.INVERT_BOOLEAN);
+
+               d_settings.bind("monospace-font-name",
+                               d_font_button,
+                               "font",
+                               SettingsBindFlags.GET | SettingsBindFlags.SET);
+
+               d_global_settings.changed["monospace-font-name"].connect((s, k) => {
+                       update_system_font_label();
+               });
+
+               update_system_font_label();
+       }
+
+       private void update_system_font_label()
+       {
+               var system_fontname = d_global_settings.get_string ("monospace-font-name");
+               string label = _("_Use the system fixed width font (%s)").printf(system_fontname);
+               d_default_font_checkbutton.label = label;
        }
 
        public override void dispose()
diff --git a/gitg/resources/ui/gitg-preferences-interface.ui b/gitg/resources/ui/gitg-preferences-interface.ui
index f8fb8fbc..79750e13 100644
--- a/gitg/resources/ui/gitg-preferences-interface.ui
+++ b/gitg/resources/ui/gitg-preferences-interface.ui
@@ -204,6 +204,86 @@
             <property name="top_attach">7</property>
           </packing>
         </child>
+        <child>
+          <object class="GtkLabel">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="halign">start</property>
+            <property name="label" translatable="yes">Font</property>
+            <property name="margin_top">12</property>
+            <attributes>
+              <attribute name="weight" value="bold"/>
+            </attributes>
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">8</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkCheckButton" id="default_font_checkbutton">
+            <property name="label">_Use the system fixed width font (%s)</property>
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="receives_default">False</property>
+            <property name="use_underline">True</property>
+            <property name="draw_indicator">True</property>
+            <accessibility>
+              <relation type="controller-for" target="font_button"/>
+            </accessibility>
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">9</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkGrid" id="font_button_grid">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="margin_start">12</property>
+            <property name="hexpand">True</property>
+            <property name="row_spacing">6</property>
+            <property name="column_spacing">6</property>
+            <child>
+              <object class="GtkLabel">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="halign">start</property>
+                <property name="label" translatable="yes">Editor _font: </property>
+                <property name="use_underline">True</property>
+                <property name="justify">center</property>
+                <property name="mnemonic_widget">font_button</property>
+              </object>
+              <packing>
+                <property name="left_attach">0</property>
+                <property name="top_attach">0</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkFontButton" id="font_button">
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="halign">start</property>
+                <property name="font">Sans 12</property>
+                <property name="title" translatable="yes">Pick the editor font</property>
+                <property name="use_font">True</property>
+                <accessibility>
+                  <relation type="controlled-by" target="default_font_checkbutton"/>
+                </accessibility>
+              </object>
+              <packing>
+                <property name="left_attach">1</property>
+                <property name="top_attach">0</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">10</property>
+          </packing>
+        </child>
         <child>
           <object class="GtkLabel" id="label6">
             <property name="visible">True</property>
@@ -217,7 +297,7 @@
           </object>
           <packing>
             <property name="left_attach">0</property>
-            <property name="top_attach">8</property>
+            <property name="top_attach">11</property>
           </packing>
         </child>
         <child>
@@ -283,7 +363,7 @@
           </object>
           <packing>
             <property name="left_attach">0</property>
-            <property name="top_attach">9</property>
+            <property name="top_attach">12</property>
           </packing>
         </child>
       </object>
diff --git a/libgitg/gitg-diff-view-file-renderer-text.vala b/libgitg/gitg-diff-view-file-renderer-text.vala
index ee545ddf..81749d1f 100644
--- a/libgitg/gitg-diff-view-file-renderer-text.vala
+++ b/libgitg/gitg-diff-view-file-renderer-text.vala
@@ -54,14 +54,13 @@ class Gitg.DiffViewFileRendererText : Gtk.SourceView, DiffSelectable, DiffViewFi
        private Gtk.SourceBuffer? d_new_highlight_buffer;
        private bool d_old_highlight_ready;
        private bool d_new_highlight_ready;
-       private Gtk.CssProvider css_provider;
 
        private Region[] d_regions;
        private bool d_constructed;
 
        private Settings? d_stylesettings;
 
-       private Settings? d_fontsettings;
+       private FontManager d_font_manager;
 
        public bool new_is_workdir { get; construct set; }
 
@@ -82,7 +81,7 @@ class Gitg.DiffViewFileRendererText : Gtk.SourceView, DiffSelectable, DiffViewFi
        }
 
        public new int tab_width
-       { 
+       {
                get { return (int)get_tab_width(); }
                set { set_tab_width((uint)value); }
        }
@@ -114,14 +113,14 @@ class Gitg.DiffViewFileRendererText : Gtk.SourceView, DiffSelectable, DiffViewFi
                        }
                }
        }
-       
+
        private bool d_has_selection;
 
        public bool has_selection
        {
                get { return d_has_selection; }
        }
-       
+
        public bool can_select { get; construct set; }
 
        public PatchSet selection
@@ -151,7 +150,7 @@ class Gitg.DiffViewFileRendererText : Gtk.SourceView, DiffSelectable, DiffViewFi
                                        patches += pset;
                                        continue;
                                }
-                               
+
                                var last = patches[patches.length - 1];
 
                                if (last.new_offset + last.length == pset.new_offset &&
@@ -200,8 +199,7 @@ class Gitg.DiffViewFileRendererText : Gtk.SourceView, DiffSelectable, DiffViewFi
                var settings = Gtk.Settings.get_default();
                settings.notify["gtk-application-prefer-dark-theme"].connect(update_theme);
 
-               css_provider = new Gtk.CssProvider();
-               get_style_context().add_provider(css_provider,Gtk.STYLE_PROVIDER_PRIORITY_SETTINGS);
+               d_font_manager = new FontManager(this, true);
 
                update_theme();
 
@@ -390,26 +388,17 @@ class Gitg.DiffViewFileRendererText : Gtk.SourceView, DiffSelectable, DiffViewFi
                var manager = Gtk.SourceLanguageManager.get_default();
                var language = manager.guess_language(location != null ? location.get_basename() : null, 
content_type);
 
-               if (language == null)
+               var buffer = new Gtk.SourceBuffer(this.buffer.tag_table);
+
+               if (language != null)
                {
-                       return null;
+                       buffer.language = language;
                }
 
-               var buffer = new Gtk.SourceBuffer(this.buffer.tag_table);
-
                var style_scheme_manager = Gtk.SourceStyleSchemeManager.get_default();
 
-               buffer.language = language;
                buffer.highlight_syntax = true;
-               d_fontsettings = try_settings("org.gnome.desktop.interface");
-               if (d_fontsettings != null)
-               {
-                       d_fontsettings.changed["monospace-font-name"].connect((s, k) => {
-                               update_font();
-                       });
 
-                       update_font();
-               }
                d_stylesettings = try_settings(Gitg.Config.APPLICATION_ID + ".preferences.interface");
                if (d_stylesettings != null)
                {
@@ -457,21 +446,6 @@ class Gitg.DiffViewFileRendererText : Gtk.SourceView, DiffSelectable, DiffViewFi
                }
        }
 
-       private void update_font()
-       {
-               var fname = d_fontsettings.get_string("monospace-font-name");
-               var font_desc = Pango.FontDescription.from_string(fname);
-               var css = "textview{%s}".printf(Dazzle.pango_font_description_to_css(font_desc));
-               try
-               {
-                       css_provider.load_from_data(css);
-               }
-               catch(Error e)
-               {
-                       warning("Error applying font: %s", e.message);
-               }
-       }
-
        private Settings? try_settings(string schema_id)
        {
                var source = SettingsSchemaSource.get_default();
@@ -606,7 +580,7 @@ class Gitg.DiffViewFileRendererText : Gtk.SourceView, DiffSelectable, DiffViewFi
                ctx.add_class("diff-lines-gutter-border");
                ctx.render_frame(cr, old_lines_width + new_lines_width, 0, sym_lines_width, win.get_height());
                ctx.restore();
-               
+
                return false;
        }
 
diff --git a/libgitg/gitg-font-manager.vala b/libgitg/gitg-font-manager.vala
new file mode 100644
index 00000000..52f84d78
--- /dev/null
+++ b/libgitg/gitg-font-manager.vala
@@ -0,0 +1,91 @@
+/*
+ * This file is part of gitg
+ *
+ * Copyright (C) 2019 - Alberto Fanjul
+ *
+ * gitg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * gitg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with gitg. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace Gitg
+{
+
+public class FontManager: Object
+{
+       private Settings d_font_settings;
+       private Settings d_global_settings;
+       private Gtk.CssProvider css_provider;
+
+       public FontManager (Gtk.TextView text_view, bool plugin) {
+               if (plugin) {
+                       d_font_settings = try_settings(Gitg.Config.APPLICATION_ID + ".preferences.interface");
+                       d_global_settings = try_settings("org.gnome.desktop.interface");
+               } else {
+                       d_font_settings = new Settings(Gitg.Config.APPLICATION_ID + ".preferences.interface");
+                       d_global_settings = new Settings("org.gnome.desktop.interface");
+               }
+               css_provider = new Gtk.CssProvider();
+               if (d_font_settings != null) {
+                       d_font_settings.changed["use-default-font"].connect((s, k) => {
+                               update_font_settings();
+                       });
+                       d_font_settings.changed["monospace-font-name"].connect((s, k) => {
+                               update_font_settings();
+                       });
+               }
+               if (d_global_settings != null) {
+                       d_global_settings.changed["monospace-font-name"].connect((s, k) => {
+                               update_font_settings();
+                       });
+               }
+               text_view.get_style_context().add_provider(css_provider, 
Gtk.STYLE_PROVIDER_PRIORITY_SETTINGS);
+               update_font_settings();
+       }
+
+       private Settings? try_settings(string schema_id)
+       {
+               var source = SettingsSchemaSource.get_default();
+
+               if (source == null)
+               {
+                       return null;
+               }
+
+               if (source.lookup(schema_id, true) != null)
+               {
+                       return new Settings(schema_id);
+               }
+
+               return null;
+       }
+
+       private void update_font_settings()
+       {
+               var fname = d_font_settings.get_string("monospace-font-name");
+               if (d_font_settings.get_boolean("use-default-font") && d_global_settings != null) {
+                       fname = d_global_settings.get_string("monospace-font-name");
+               }
+
+               var font_desc = Pango.FontDescription.from_string(fname);
+               var css = "textview { %s }".printf(Dazzle.pango_font_description_to_css(font_desc));
+               try
+               {
+                       css_provider.load_from_data(css);
+               }
+               catch(Error e)
+               {
+                       warning("Error applying font: %s", e.message);
+               }
+       }
+}
+}
diff --git a/libgitg/meson.build b/libgitg/meson.build
index e2463f9c..493fef5d 100644
--- a/libgitg/meson.build
+++ b/libgitg/meson.build
@@ -49,6 +49,7 @@ sources = files(
   'gitg-diff-view-lines-renderer.vala',
   'gitg-diff-view-options.vala',
   'gitg-diff-view.vala',
+  'gitg-font-manager.vala',
   'gitg-hook.vala',
   'gitg-init.vala',
   'gitg-label-renderer.vala',
diff --git a/plugins/files/gitg-files.vala b/plugins/files/gitg-files.vala
index 6f0bece8..9adb63d2 100644
--- a/plugins/files/gitg-files.vala
+++ b/plugins/files/gitg-files.vala
@@ -30,7 +30,6 @@ namespace GitgFiles
                private TreeStore d_model;
                private Gtk.Paned d_paned;
                private Gtk.SourceView d_source;
-               private Settings? d_fontsettings;
                private Settings? d_stylesettings;
 
                private Gtk.ScrolledWindow d_scrolled_files;
@@ -39,8 +38,8 @@ namespace GitgFiles
                private Gtk.Viewport d_imagevp;
                private Gtk.Image d_image;
 
-               private Gtk.CssProvider css_provider;
                private Gitg.WhenMapped d_whenMapped;
+               private Gitg.FontManager d_font_manager;
 
                construct
                {
@@ -85,21 +84,6 @@ namespace GitgFiles
                        });
                }
 
-               private void update_font()
-               {
-                       var fname = d_fontsettings.get_string("monospace-font-name");
-                       var font_desc = Pango.FontDescription.from_string(fname);
-                       var css = "textview { %s }".printf(Dazzle.pango_font_description_to_css(font_desc));
-                       try
-                       {
-                               css_provider.load_from_data(css);
-                       }
-                       catch(Error e)
-                       {
-                               warning("Error applying font. %s", e.message);
-                       }
-               }
-
                private void update_style()
                {
                        var scheme = d_stylesettings.get_string("style-scheme");
@@ -149,24 +133,13 @@ namespace GitgFiles
                        d_paned = ret["paned_files"] as Gtk.Paned;
                        d_scrolled = ret["scrolled_window_file"] as Gtk.ScrolledWindow;
 
-                       css_provider = new Gtk.CssProvider();
-                       d_source.get_style_context().add_provider(css_provider, 
Gtk.STYLE_PROVIDER_PRIORITY_SETTINGS);
+                       d_font_manager = new Gitg.FontManager(d_source, true);
+
                        d_imagevp = new Gtk.Viewport(null, null);
                        d_image = new Gtk.Image();
                        d_imagevp.add(d_image);
                        d_imagevp.show_all();
 
-                       d_fontsettings = try_settings("org.gnome.desktop.interface");
-
-                       if (d_fontsettings != null)
-                       {
-                               d_fontsettings.changed["monospace-font-name"].connect((s, k) => {
-                                       update_font();
-                               });
-
-                               update_font();
-                       }
-
                        d_stylesettings = try_settings(Gitg.Config.APPLICATION_ID + ".preferences.interface");
                        if (d_stylesettings != null)
                        {


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