Commit 268f3209 authored by Jente Hidskes's avatar Jente Hidskes Committed by Alberto Fanjul

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.
parent 1d306388
Pipeline #35180 passed with stages
in 12 minutes and 50 seconds
......@@ -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)
{
......
......@@ -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
......
......@@ -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)
{
......
......@@ -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)
{
......
/*
*
* 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
......@@ -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'
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment