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.
This commit is contained in:
Jente Hidskes 2018-07-22 22:40:59 +02:00 committed by Alberto Fanjul
parent 1d306388c4
commit 268f3209dc
6 changed files with 60 additions and 45 deletions

View File

@ -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)
{

View File

@ -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

View File

@ -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)
{

View File

@ -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)
{

55
libgitg/gitg-theme.vala Normal file
View File

@ -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

View File

@ -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'
)