[gnome-panel] move theme handling to GpApplication



commit 3bd161954e3d4a6a479c31c13f0876b40ac049f2
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Sat Dec 28 15:04:15 2019 +0200

    move theme handling to GpApplication

 gnome-panel/gp-application.c | 210 ++++++++++++++++++++++++++++++++++++++++++-
 gnome-panel/main.c           | 144 -----------------------------
 2 files changed, 209 insertions(+), 145 deletions(-)
---
diff --git a/gnome-panel/gp-application.c b/gnome-panel/gp-application.c
index 5af2dfa7a..3982fa291 100644
--- a/gnome-panel/gp-application.c
+++ b/gnome-panel/gp-application.c
@@ -18,21 +18,229 @@
 #include "config.h"
 #include "gp-application.h"
 
+#include <gtk/gtk.h>
+
+#include "panel-enums-gsettings.h"
+
+typedef struct
+{
+  const char *name;
+  const char *dir;
+  const char *variant;
+  gboolean    has_dark_variant;
+} GpSupportedTheme;
+
 struct _GpApplication
 {
-  GObject parent;
+  GObject           parent;
+
+  GSettings        *general_settings;
+
+  gulong            theme_name_id;
+  gulong            prefer_dark_id;
+
+  GtkStyleProvider *provider;
 };
 
 G_DEFINE_TYPE (GpApplication, gp_application, G_TYPE_OBJECT)
 
+static GpSupportedTheme supported_themes[] =
+{
+  { "Adwaita", "Adwaita", NULL, TRUE },
+  { "Adwaita-dark", "Adwaita", "dark", FALSE },
+  { "HighContrast", "HighContrast", NULL, FALSE },
+  { "HighContrastInverse", "HighContrast", "inverse", FALSE },
+  { NULL, NULL, FALSE, FALSE }
+};
+
+static char *
+get_theme_resource (GpSupportedTheme *theme,
+                    gboolean          prefer_dark)
+{
+  char *filename;
+  const char *resource_base;
+  char *resource;
+
+  if (theme->variant != NULL)
+    filename = g_strdup_printf ("gnome-panel-%s.css", theme->variant);
+  else if (theme->has_dark_variant && prefer_dark)
+    filename = g_strdup ("gnome-panel-dark.css");
+  else
+    filename = g_strdup ("gnome-panel.css");
+
+  resource_base = "/org/gnome/gnome-panel/theme";
+  resource = g_strdup_printf ("%s/%s/%s", resource_base, theme->dir, filename);
+  g_free (filename);
+
+  return resource;
+}
+
+static gboolean
+is_theme_supported (const char        *theme_name,
+                    GpSupportedTheme **theme)
+{
+  int i;
+
+  for (i = 0; supported_themes[i].name != NULL; i++)
+    {
+      if (g_strcmp0 (supported_themes[i].name, theme_name) == 0)
+        {
+          *theme = &supported_themes[i];
+          return TRUE;
+        }
+    }
+
+  return FALSE;
+}
+
+static void
+update_theme (GpApplication *self)
+{
+  GdkScreen *screen;
+  GtkSettings *settings;
+  char *theme_name;
+  gboolean prefer_dark;
+  GpSupportedTheme *theme;
+  char *resource;
+  guint priority;
+  GtkCssProvider *css;
+
+  screen = gdk_screen_get_default ();
+  settings = gtk_settings_get_default ();
+
+  if (self->provider != NULL)
+    {
+      gtk_style_context_remove_provider_for_screen (screen, self->provider);
+      g_clear_object (&self->provider);
+    }
+
+  g_object_get (settings,
+                "gtk-theme-name", &theme_name,
+                "gtk-application-prefer-dark-theme", &prefer_dark,
+                NULL);
+
+  if (is_theme_supported (theme_name, &theme))
+    {
+      resource = get_theme_resource (theme, prefer_dark);
+      priority = GTK_STYLE_PROVIDER_PRIORITY_APPLICATION;
+    }
+  else
+    {
+      resource = g_strdup ("/org/gnome/gnome-panel/theme/fallback.css");
+      priority = GTK_STYLE_PROVIDER_PRIORITY_FALLBACK;
+    }
+
+  css = gtk_css_provider_new ();
+  self->provider = GTK_STYLE_PROVIDER (css);
+
+  gtk_css_provider_load_from_resource (css, resource);
+  gtk_style_context_add_provider_for_screen (screen, self->provider, priority);
+
+  g_free (theme_name);
+  g_free (resource);
+}
+
+static void
+gtk_theme_name_changed_cb (GtkSettings   *settings,
+                           GParamSpec    *pspec,
+                           GpApplication *self)
+{
+  update_theme (self);
+}
+
+static void
+prefer_dark_changed_cb (GtkSettings   *settings,
+                        GParamSpec    *pspec,
+                        GpApplication *self)
+{
+  update_theme (self);
+}
+
+static void
+theme_variant_changed_cb (GSettings     *settings,
+                          const char    *key,
+                          GpApplication *self)
+{
+  PanelThemeVariant variant;
+
+  variant = g_settings_get_enum (settings, key);
+
+  if (variant == PANEL_THEME_VARIANT_SYSTEM)
+    {
+      gtk_settings_reset_property (gtk_settings_get_default (),
+                                   "gtk-application-prefer-dark-theme");
+    }
+  else
+    {
+      g_object_set (gtk_settings_get_default (),
+                    "gtk-application-prefer-dark-theme",
+                    variant == PANEL_THEME_VARIANT_DARK,
+                    NULL);
+    }
+
+  update_theme (self);
+}
+
+static void
+gp_application_dispose (GObject *object)
+{
+  GpApplication *self;
+
+  self = GP_APPLICATION (object);
+
+  if (self->theme_name_id != 0)
+    {
+      g_signal_handler_disconnect (gtk_settings_get_default (),
+                                   self->theme_name_id);
+
+      self->theme_name_id = 0;
+    }
+
+  if (self->prefer_dark_id != 0)
+    {
+      g_signal_handler_disconnect (gtk_settings_get_default (),
+                                   self->prefer_dark_id);
+
+      self->prefer_dark_id = 0;
+    }
+
+  g_clear_object (&self->general_settings);
+  g_clear_object (&self->provider);
+
+  G_OBJECT_CLASS (gp_application_parent_class)->dispose (object);
+}
+
 static void
 gp_application_class_init (GpApplicationClass *self_class)
 {
+  GObjectClass *object_class;
+
+  object_class = G_OBJECT_CLASS (self_class);
+
+  object_class->dispose = gp_application_dispose;
 }
 
 static void
 gp_application_init (GpApplication *self)
 {
+  self->general_settings = g_settings_new ("org.gnome.gnome-panel.general");
+
+  g_signal_connect (self->general_settings,
+                    "changed::theme-variant",
+                    G_CALLBACK (theme_variant_changed_cb),
+                    self);
+
+  self->theme_name_id = g_signal_connect (gtk_settings_get_default (),
+                                          "notify::gtk-theme-name",
+                                          G_CALLBACK (gtk_theme_name_changed_cb),
+                                          self);
+
+  self->prefer_dark_id = g_signal_connect (gtk_settings_get_default (),
+                                           "notify::gtk-application-prefer-dark-theme",
+                                           G_CALLBACK (prefer_dark_changed_cb),
+                                           self);
+
+  update_theme (self);
 }
 
 GpApplication *
diff --git a/gnome-panel/main.c b/gnome-panel/main.c
index a77159087..8c944d658 100644
--- a/gnome-panel/main.c
+++ b/gnome-panel/main.c
@@ -30,143 +30,12 @@
 static gboolean  replace = FALSE;
 static gboolean  version = FALSE;
 
-static GSettings *g_settings = NULL;
-static GtkSettings *gtk_settings = NULL;
-static GtkStyleProvider *provider = NULL;
-
 static const GOptionEntry options[] = {
   { "replace", 0, 0, G_OPTION_ARG_NONE, &replace, N_("Replace a currently running panel"), NULL },
   { "version", 0, 0, G_OPTION_ARG_NONE, &version, N_("Print version"), NULL},
   { NULL }
 };
 
-typedef struct
-{
-  const char *name;
-  const char *dir;
-  const char *variant;
-  gboolean    has_dark_variant;
-} GpSupportedTheme;
-
-static GpSupportedTheme supported_themes[] =
-{
-  { "Adwaita", "Adwaita", NULL, TRUE },
-  { "Adwaita-dark", "Adwaita", "dark", FALSE },
-  { "HighContrast", "HighContrast", NULL, FALSE },
-  { "HighContrastInverse", "HighContrast", "inverse", FALSE },
-  { NULL, NULL, FALSE, FALSE }
-};
-
-static char *
-get_theme_resource (GpSupportedTheme *theme,
-                    gboolean          prefer_dark)
-{
-  char *filename;
-  const char *resource_base;
-  char *resource;
-
-  if (theme->variant != NULL)
-    filename = g_strdup_printf ("gnome-panel-%s.css", theme->variant);
-  else if (theme->has_dark_variant && prefer_dark)
-    filename = g_strdup ("gnome-panel-dark.css");
-  else
-    filename = g_strdup ("gnome-panel.css");
-
-  resource_base = "/org/gnome/gnome-panel/theme";
-  resource = g_strdup_printf ("%s/%s/%s", resource_base, theme->dir, filename);
-  g_free (filename);
-
-  return resource;
-}
-
-static gboolean
-is_theme_supported (const char        *theme_name,
-                    GpSupportedTheme **theme)
-{
-  int i;
-
-  for (i = 0; supported_themes[i].name != NULL; i++)
-    {
-      if (g_strcmp0 (supported_themes[i].name, theme_name) == 0)
-        {
-          *theme = &supported_themes[i];
-          return TRUE;
-        }
-    }
-
-  return FALSE;
-}
-
-static void
-theme_changed (GtkSettings *settings,
-               GParamSpec  *pspec,
-               gpointer     user_data)
-{
-  GdkScreen *screen;
-  gchar *theme_name;
-  gboolean dark_theme;
-  GpSupportedTheme *theme;
-  guint priority;
-  gchar *resource;
-  GtkCssProvider *css;
-
-  screen = gdk_screen_get_default ();
-
-  if (provider != NULL)
-    {
-      gtk_style_context_remove_provider_for_screen (screen, provider);
-      g_clear_object (&provider);
-    }
-
-  g_object_get (settings,
-                "gtk-theme-name", &theme_name,
-                "gtk-application-prefer-dark-theme", &dark_theme,
-                NULL);
-
-  if (is_theme_supported (theme_name, &theme))
-    {
-      resource = get_theme_resource (theme, dark_theme);
-      priority = GTK_STYLE_PROVIDER_PRIORITY_APPLICATION;
-    }
-  else
-    {
-      resource = g_strdup ("/org/gnome/gnome-panel/theme/fallback.css");
-      priority = GTK_STYLE_PROVIDER_PRIORITY_FALLBACK;
-    }
-
-  css = gtk_css_provider_new ();
-  provider = GTK_STYLE_PROVIDER (css);
-
-  gtk_css_provider_load_from_resource (css, resource);
-  gtk_style_context_add_provider_for_screen (screen, provider, priority);
-
-  g_free (theme_name);
-  g_free (resource);
-}
-
-static void
-theme_variant_changed_cb (GSettings   *settings,
-                          const gchar *key,
-                          gpointer     user_data)
-{
-  PanelThemeVariant variant;
-
-  variant = g_settings_get_enum (settings, key);
-
-  if (variant == PANEL_THEME_VARIANT_SYSTEM)
-    {
-      gtk_settings_reset_property (gtk_settings,
-                                   "gtk-application-prefer-dark-theme");
-    }
-  else
-    {
-      g_object_set (gtk_settings, "gtk-application-prefer-dark-theme",
-                    variant == PANEL_THEME_VARIANT_DARK, NULL);
-    }
-
-  theme_changed (gtk_settings, NULL, NULL);
-}
-
 static gboolean
 on_term_signal (gpointer user_data)
 {
@@ -250,24 +119,11 @@ main (int argc, char **argv)
         * connecting to the session manager */
        panel_session_register_client (session);
 
-       g_settings = g_settings_new (PANEL_GENERAL_SCHEMA);
-       g_signal_connect (g_settings, "changed::" PANEL_GENERAL_THEME_VARIANT_KEY,
-                         G_CALLBACK (theme_variant_changed_cb), NULL);
-
-       gtk_settings = gtk_settings_get_default ();
-       g_signal_connect (gtk_settings, "notify::gtk-theme-name",
-                         G_CALLBACK (theme_changed), NULL);
-       g_signal_connect (gtk_settings, "notify::gtk-application-prefer-dark-theme",
-                         G_CALLBACK (theme_changed), NULL);
-
-       theme_variant_changed_cb (g_settings, PANEL_GENERAL_THEME_VARIANT_KEY, NULL);
-
        application = gp_application_new ();
 
        gtk_main ();
 
        g_object_unref (application);
-       g_object_unref (g_settings);
        g_object_unref (session);
 
        toplevels_to_destroy = g_slist_copy (panel_toplevel_list_toplevels ());


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