[gitg] Refactor dark theme detection into a function



commit 268f3209dc4e4ec304e5ca61d2287186d6a62d8f
Author: Jente Hidskes <hjdskes gmail com>
Date:   Sun Jul 22 22:40:59 2018 +0200

    Refactor dark theme detection into a function
    
    We need to instance a Gtk.Widget to be able to use `get_style_context`.
    Thus, we use the singleton pattern to avoid allocating an object every
    time we call `is_theme_dark()`.
    
    Fixes #133.

 gitg/gitg-window.vala                          | 16 +-------
 libgitg/Makefile.am                            |  1 +
 libgitg/gitg-diff-stat.vala                    | 16 +-------
 libgitg/gitg-diff-view-file-renderer-text.vala | 16 +-------
 libgitg/gitg-theme.vala                        | 55 ++++++++++++++++++++++++++
 libgitg/meson.build                            |  1 +
 6 files changed, 60 insertions(+), 45 deletions(-)
---
diff --git a/gitg/gitg-window.vala b/gitg/gitg-window.vala
index c05dda7a..1c68cb8f 100644
--- a/gitg/gitg-window.vala
+++ b/gitg/gitg-window.vala
@@ -368,21 +368,7 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable
        {
                base.style_updated();
 
-               var settings = Gtk.Settings.get_default();
-               var theme = Environment.get_variable("GTK_THEME");
-
-               var dark = settings.gtk_application_prefer_dark_theme || (theme != null && 
theme.has_suffix(":dark"));
-
-               if (!dark) {
-                       var stylecontext = get_style_context();
-                       Gdk.RGBA rgba;
-                       var background_set = stylecontext.lookup_color("theme_bg_color", out rgba);
-
-                       if (background_set && rgba.red + rgba.green + rgba.blue < 1.0)
-                       {
-                               dark = true;
-                       }
-               }
+               var dark = new Theme().is_theme_dark();
 
                if (dark)
                {
diff --git a/libgitg/Makefile.am b/libgitg/Makefile.am
index 5d94cb40..a985f78a 100644
--- a/libgitg/Makefile.am
+++ b/libgitg/Makefile.am
@@ -100,6 +100,7 @@ libgitg_libgitg_1_0_la_VALASOURCES =                                \
        libgitg/gitg-sidebar.vala                               \
        libgitg/gitg-stage-status-enumerator.vala               \
        libgitg/gitg-stage.vala                                 \
+       libgitg/gitg-theme.vala                                 \
        libgitg/gitg-utils.vala                                 \
        libgitg/gitg-when-mapped.vala
 
diff --git a/libgitg/gitg-diff-stat.vala b/libgitg/gitg-diff-stat.vala
index f83318fe..867fa864 100644
--- a/libgitg/gitg-diff-stat.vala
+++ b/libgitg/gitg-diff-stat.vala
@@ -85,21 +85,7 @@ public class Gitg.DiffStat : Gtk.DrawingArea
 
                d_layout = null;
 
-               var settings = Gtk.Settings.get_default();
-               var theme = Environment.get_variable("GTK_THEME");
-
-               var dark = settings.gtk_application_prefer_dark_theme || (theme != null && 
theme.has_suffix(":dark"));
-
-               if (!dark) {
-                       var stylecontext = get_style_context();
-                       Gdk.RGBA rgba;
-                       var background_set = stylecontext.lookup_color("theme_bg_color", out rgba);
-
-                       if (background_set && rgba.red + rgba.green + rgba.blue < 1.0)
-                       {
-                               dark = true;
-                       }
-               }
+               var dark = new Theme().is_theme_dark();
 
                if (dark)
                {
diff --git a/libgitg/gitg-diff-view-file-renderer-text.vala b/libgitg/gitg-diff-view-file-renderer-text.vala
index d83e9f19..b6735623 100644
--- a/libgitg/gitg-diff-view-file-renderer-text.vala
+++ b/libgitg/gitg-diff-view-file-renderer-text.vala
@@ -545,21 +545,7 @@ class Gitg.DiffViewFileRendererText : Gtk.SourceView, DiffSelectable, DiffViewFi
                var added_attributes = new Gtk.SourceMarkAttributes();
                var removed_attributes = new Gtk.SourceMarkAttributes();
 
-               var settings = Gtk.Settings.get_default();
-               var theme = Environment.get_variable("GTK_THEME");
-
-               var dark = settings.gtk_application_prefer_dark_theme || (theme != null && 
theme.has_suffix(":dark"));
-
-        if (!dark) {
-                       var stylecontext = get_style_context();
-                       Gdk.RGBA rgba;
-                       var background_set = stylecontext.lookup_color("theme_bg_color", out rgba);
-
-                       if (background_set && rgba.red + rgba.green + rgba.blue < 1.0)
-                       {
-                               dark = true;
-                       }
-               }
+               var dark = new Theme().is_theme_dark();
 
                if (dark)
                {
diff --git a/libgitg/gitg-theme.vala b/libgitg/gitg-theme.vala
new file mode 100644
index 00000000..d6f42ddc
--- /dev/null
+++ b/libgitg/gitg-theme.vala
@@ -0,0 +1,55 @@
+/*
+ *
+ * Copyright (C) 2018 - Jente Hidskes <hjdskes gmail com>
+ *
+ * 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 Theme : Gtk.Widget
+{
+       private static GLib.Once<Theme> _instance;
+
+       public static unowned Theme instance ()
+       {
+               return _instance.once (() => { return new Theme (); });
+       }
+
+       public bool is_theme_dark()
+       {
+               var settings = Gtk.Settings.get_default();
+               var theme = Environment.get_variable("GTK_THEME");
+
+               var dark = settings.gtk_application_prefer_dark_theme || (theme != null && 
theme.has_suffix(":dark"));
+
+               if (!dark) {
+                       var stylecontext = get_style_context();
+                       Gdk.RGBA rgba;
+                       var background_set = stylecontext.lookup_color("theme_bg_color", out rgba);
+
+                       if (background_set && rgba.red + rgba.green + rgba.blue < 1.0)
+                       {
+                               dark = true;
+                       }
+               }
+
+               return dark;
+       }
+}
+
+}
+
+// ex:ts=4 noet
diff --git a/libgitg/meson.build b/libgitg/meson.build
index 793f2c21..75a255af 100644
--- a/libgitg/meson.build
+++ b/libgitg/meson.build
@@ -57,6 +57,7 @@ sources = files(
   'gitg-sidebar.vala',
   'gitg-stage-status-enumerator.vala',
   'gitg-stage.vala',
+  'gitg-theme.vala',
   'gitg-utils.vala',
   'gitg-when-mapped.vala'
 )


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