theme-manager: add SpThemeManager and use it for custom css

We want to set some custom backgrounds for the visualizers, and
those need to track with the current theme. SpThemeManager will
watch the systems theme changes (including dark theme) and
update the loaded CSS resources as necessary.
This commit is contained in:
Christian Hergert
2016-10-08 16:28:18 -07:00
parent 7ca8493130
commit 7c55f379bb
9 changed files with 327 additions and 12 deletions

View File

@ -174,6 +174,8 @@ libsysprof_ui_@API_VERSION@_la_SOURCES = \
sp-process-model-row.c \
sp-profiler-menu-button.c \
sp-recording-state-view.c \
sp-theme-manager.c \
sp-theme-manager.h \
sp-visualizer-list.c \
sp-visualizer-list.h \
sp-visualizer-row.c \

View File

@ -0,0 +1,6 @@
visualizers list row {
background-color: #232729;
background-size: 8px 8px;
background-image: repeating-linear-gradient(0deg, #2e2e2e, #2e2e2e 1px, transparent 1px, transparent 8px),
repeating-linear-gradient(-90deg, #2e2e2e, #2e2e2e 1px, transparent 1px, transparent 8px);
}

View File

@ -0,0 +1,7 @@
visualizers list row {
background-color: #f6f7f8;
background-size: 8px 8px;
background-image: repeating-linear-gradient(0deg, #f0f1f2, #f0f1f2 1px, transparent 1px, transparent 8px),
repeating-linear-gradient(-90deg, #f0f1f2, #f0f1f2 1px, transparent 1px, transparent 8px);
}

View File

@ -1,7 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<gresources>
<gresource prefix="/org/gnome/sysprof">
<file compressed="true">css/shared.css</file>
<file compressed="true">css/SpVisualizerView-shared.css</file>
<file compressed="true">css/SpVisualizerView-Adwaita.css</file>
<file compressed="true">css/SpVisualizerView-Adwaita-dark.css</file>
<file compressed="true">ui/sp-callgraph-view.ui</file>
<file compressed="true">ui/sp-empty-state-view.ui</file>

264
lib/sp-theme-manager.c Normal file
View File

@ -0,0 +1,264 @@
/* sp-theme-manager.c
*
* Copyright (C) 2016 Christian Hergert <chergert@redhat.com>
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define G_LOG_DOMAIN "sp-theme-manager"
#include "sp-theme-manager.h"
struct _SpThemeManager
{
GObject parent_instance;
GHashTable *theme_resources;
guint reload_source;
guint registered_signals : 1;
};
typedef struct
{
guint id;
gchar *key;
gchar *theme_name;
gchar *variant;
gchar *resource;
GtkCssProvider *provider;
} ThemeResource;
G_DEFINE_TYPE (SpThemeManager, sp_theme_manager, G_TYPE_OBJECT)
static void
theme_resource_free (gpointer data)
{
ThemeResource *theme_resource = data;
if (theme_resource != NULL)
{
g_clear_pointer (&theme_resource->key, g_free);
g_clear_pointer (&theme_resource->theme_name, g_free);
g_clear_pointer (&theme_resource->variant, g_free);
g_clear_pointer (&theme_resource->resource, g_free);
if (theme_resource->provider != NULL)
{
gtk_style_context_remove_provider_for_screen (gdk_screen_get_default (),
GTK_STYLE_PROVIDER (theme_resource->provider));
g_clear_object (&theme_resource->provider);
}
g_slice_free (ThemeResource, theme_resource);
}
}
static gboolean
theme_resource_matches (ThemeResource *theme_resource,
GtkSettings *settings)
{
g_autofree gchar *theme_name = NULL;
gboolean dark_theme = FALSE;
g_assert (theme_resource != NULL);
g_assert (GTK_IS_SETTINGS (settings));
if (theme_resource->theme_name == NULL)
return TRUE;
g_object_get (settings,
"gtk-theme-name", &theme_name,
"gtk-application-prefer-dark-theme", &dark_theme,
NULL);
if (g_strcmp0 (theme_name, theme_resource->theme_name) == 0)
{
if (dark_theme && g_strcmp0 ("dark", theme_resource->variant) == 0)
return TRUE;
if (!dark_theme && (!theme_resource->variant || g_strcmp0 ("light", theme_resource->variant) == 0))
return TRUE;
}
return FALSE;
}
static gboolean
sp_theme_manager_do_reload (gpointer data)
{
SpThemeManager *self = data;
ThemeResource *theme_resource;
GHashTableIter iter;
GtkSettings *settings;
g_assert (SP_IS_THEME_MANAGER (self));
self->reload_source = 0;
settings = gtk_settings_get_default ();
g_hash_table_iter_init (&iter, self->theme_resources);
while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&theme_resource))
{
if (theme_resource_matches (theme_resource, settings))
{
if (theme_resource->provider == NULL)
{
theme_resource->provider = gtk_css_provider_new ();
gtk_css_provider_load_from_resource (theme_resource->provider, theme_resource->resource);
gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
GTK_STYLE_PROVIDER (theme_resource->provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION - 1);
g_print ("Registered resource file %s\n", theme_resource->resource);
}
}
else
{
if (theme_resource->provider != NULL)
{
gtk_style_context_remove_provider_for_screen (gdk_screen_get_default (),
GTK_STYLE_PROVIDER (theme_resource->provider));
g_clear_object (&theme_resource->provider);
g_print ("Unregistered resource file %s\n", theme_resource->resource);
}
}
}
return G_SOURCE_REMOVE;
}
static void
sp_theme_manager_queue_reload (SpThemeManager *self)
{
g_assert (SP_IS_THEME_MANAGER (self));
if (self->reload_source == 0)
self->reload_source = gdk_threads_add_idle_full (G_PRIORITY_LOW,
sp_theme_manager_do_reload,
self,
NULL);
}
static void
sp_theme_manager_finalize (GObject *object)
{
SpThemeManager *self = (SpThemeManager *)object;
if (self->reload_source != 0)
{
g_source_remove (self->reload_source);
self->reload_source = 0;
}
g_clear_pointer (&self->theme_resources, g_hash_table_unref);
G_OBJECT_CLASS (sp_theme_manager_parent_class)->finalize (object);
}
static void
sp_theme_manager_class_init (SpThemeManagerClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = sp_theme_manager_finalize;
}
static void
sp_theme_manager_init (SpThemeManager *self)
{
self->theme_resources = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, theme_resource_free);
}
/**
* sp_theme_manager_get_default:
*
* Returns: (transfer none): An #SpThemeManager
*/
SpThemeManager *
sp_theme_manager_get_default (void)
{
static SpThemeManager *instance;
if (instance == NULL)
instance = g_object_new (SP_TYPE_THEME_MANAGER, NULL);
return instance;
}
guint
sp_theme_manager_register_resource (SpThemeManager *self,
const gchar *theme_name,
const gchar *variant,
const gchar *resource)
{
ThemeResource *theme_resource;
static guint counter;
guint id;
g_return_val_if_fail (SP_IS_THEME_MANAGER (self), 0);
theme_resource = g_slice_new0 (ThemeResource);
theme_resource->id = id = ++counter;
theme_resource->key = g_strdup_printf ("%s-%s-%d",
theme_name ? theme_name : "shared",
variant ? variant : "light",
theme_resource->id);
theme_resource->theme_name = g_strdup (theme_name);
theme_resource->variant = g_strdup (variant);
theme_resource->resource = g_strdup (resource);
theme_resource->provider = NULL;
g_hash_table_insert (self->theme_resources, theme_resource->key, theme_resource);
if (!self->registered_signals)
{
self->registered_signals = TRUE;
g_signal_connect_object (gtk_settings_get_default (),
"notify::gtk-application-prefer-dark-theme",
G_CALLBACK (sp_theme_manager_queue_reload),
self,
G_CONNECT_SWAPPED);
g_signal_connect_object (gtk_settings_get_default (),
"notify::gtk-theme-name",
G_CALLBACK (sp_theme_manager_queue_reload),
self,
G_CONNECT_SWAPPED);
}
sp_theme_manager_queue_reload (self);
return id;
}
void
sp_theme_manager_unregister (SpThemeManager *self,
guint registration_id)
{
GHashTableIter iter;
ThemeResource *theme_resource;
g_return_if_fail (SP_IS_THEME_MANAGER (self));
g_hash_table_iter_init (&iter, self->theme_resources);
while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&theme_resource))
{
if (theme_resource->id == registration_id)
{
/* Provider is unregistered during destroy */
g_hash_table_iter_remove (&iter);
break;
}
}
}

40
lib/sp-theme-manager.h Normal file
View File

@ -0,0 +1,40 @@
/* sp-theme-manager.h
*
* Copyright (C) 2016 Christian Hergert <chergert@redhat.com>
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SP_THEME_MANAGER_H
#define SP_THEME_MANAGER_H
#include <gtk/gtk.h>
G_BEGIN_DECLS
#define SP_TYPE_THEME_MANAGER (sp_theme_manager_get_type())
G_DECLARE_FINAL_TYPE (SpThemeManager, sp_theme_manager, SP, THEME_MANAGER, GObject)
SpThemeManager *sp_theme_manager_get_default (void);
void sp_theme_manager_unregister (SpThemeManager *self,
guint registration_id);
guint sp_theme_manager_register_resource (SpThemeManager *self,
const gchar *theme_name,
const gchar *variant,
const gchar *resource);
G_END_DECLS
#endif /* SP_THEME_MANAGER_H */

View File

@ -20,6 +20,7 @@
#include <glib/gi18n.h>
#include "sp-theme-manager.h"
#include "sp-visualizer-list.h"
#include "sp-visualizer-row.h"
#include "sp-visualizer-row-private.h"
@ -76,7 +77,6 @@ G_DEFINE_TYPE_EXTENDED (SpVisualizerView, sp_visualizer_view, GTK_TYPE_BIN, 0,
static GParamSpec *properties [N_PROPS];
static guint signals [N_SIGNALS];
static GtkBuildableIface *parent_buildable;
static GtkCssProvider *css_provider;
static void
find_row1 (GtkWidget *widget,
@ -476,6 +476,7 @@ sp_visualizer_view_class_init (SpVisualizerViewClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
SpThemeManager *theme_manager = sp_theme_manager_get_default ();
object_class->finalize = sp_visualizer_view_finalize;
object_class->get_property = sp_visualizer_view_get_property;
@ -523,11 +524,9 @@ sp_visualizer_view_class_init (SpVisualizerViewClass *klass)
gtk_widget_class_set_css_name (widget_class, "visualizers");
css_provider = gtk_css_provider_new ();
gtk_css_provider_load_from_resource (css_provider, "/org/gnome/sysprof/css/shared.css");
gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
GTK_STYLE_PROVIDER (css_provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION-1);
sp_theme_manager_register_resource (theme_manager, NULL, NULL, "/org/gnome/sysprof/css/SpVisualizerView-shared.css");
sp_theme_manager_register_resource (theme_manager, "Adwaita", NULL, "/org/gnome/sysprof/css/SpVisualizerView-Adwaita.css");
sp_theme_manager_register_resource (theme_manager, "Adwaita", "dark", "/org/gnome/sysprof/css/SpVisualizerView-Adwaita-dark.css");
}
static void

View File

@ -15,11 +15,6 @@ visualizers list {
background: @theme_bg_color;
}
visualizers list row {
background: @content_view_bg;
border-bottom: 1px solid alpha(@borders, 0.2);
}
visualizers list row:backdrop,
visualizers list row:last-child {
border-bottom: none;