[libadwaita/wip/exalm/dark: 3/5] application: Proxy AdwStyleManager API




commit 27b770b3f56e84c8c45bed36211abd1655107c7b
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Thu Aug 26 14:22:13 2021 +0500

    application: Proxy AdwStyleManager API

 src/adw-application.c | 184 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/adw-application.h |  12 ++++
 2 files changed, 196 insertions(+)
---
diff --git a/src/adw-application.c b/src/adw-application.c
index be283290..e3e60b33 100644
--- a/src/adw-application.c
+++ b/src/adw-application.c
@@ -6,6 +6,7 @@
 
 #include "config.h"
 #include "adw-application.h"
+
 #include "adw-main-private.h"
 
 /**
@@ -48,10 +49,23 @@ typedef struct
   GtkStyleProvider *dark_style_provider;
   GtkStyleProvider *hc_style_provider;
   GtkStyleProvider *hc_dark_style_provider;
+
+  AdwStyleManager *style_manager;
+  AdwColorScheme color_scheme;
 } AdwApplicationPrivate;
 
 G_DEFINE_TYPE_WITH_PRIVATE (AdwApplication, adw_application, GTK_TYPE_APPLICATION)
 
+enum {
+  PROP_0,
+  PROP_COLOR_SCHEME,
+  PROP_DARK,
+  PROP_HIGH_CONTRAST,
+  LAST_PROP,
+};
+
+static GParamSpec *props[LAST_PROP];
+
 static inline void
 style_provider_set_enabled (GtkStyleProvider *provider,
                             gboolean          enabled)
@@ -159,15 +173,45 @@ init_styling (AdwApplication *self)
   update_stylesheet (self);
 }
 
+static void
+notify_color_scheme_cb (AdwApplication *self)
+{
+  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_COLOR_SCHEME]);
+}
+
+static void
+notify_dark_cb (AdwApplication *self)
+{
+  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_DARK]);
+}
+
+static void
+notify_high_contrast_cb (AdwApplication *self)
+{
+  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_HIGH_CONTRAST]);
+}
+
 static void
 adw_application_startup (GApplication *application)
 {
   AdwApplication *self = ADW_APPLICATION (application);
+  AdwApplicationPrivate *priv = adw_application_get_instance_private (self);
 
   G_APPLICATION_CLASS (adw_application_parent_class)->startup (application);
 
   adw_init ();
 
+  priv->style_manager = adw_style_manager_get_default ();
+
+  g_signal_connect_swapped (priv->style_manager, "notify::color-scheme",
+                            G_CALLBACK (notify_color_scheme_cb), self);
+  g_signal_connect_swapped (priv->style_manager, "notify::dark",
+                            G_CALLBACK (notify_dark_cb), self);
+  g_signal_connect_swapped (priv->style_manager, "notify::high-contrast",
+                            G_CALLBACK (notify_high_contrast_cb), self);
+
+  adw_style_manager_set_color_scheme (priv->style_manager, priv->color_scheme);
+
   init_providers (self);
   init_styling (self);
 }
@@ -183,9 +227,60 @@ adw_application_dispose (GObject *object)
   g_clear_object (&priv->hc_style_provider);
   g_clear_object (&priv->hc_dark_style_provider);
 
+  if (priv->style_manager) {
+    g_signal_handlers_disconnect_by_func (priv->style_manager, G_CALLBACK (notify_color_scheme_cb), self);
+    g_signal_handlers_disconnect_by_func (priv->style_manager, G_CALLBACK (notify_dark_cb), self);
+    g_signal_handlers_disconnect_by_func (priv->style_manager, G_CALLBACK (notify_high_contrast_cb), self);
+    priv->style_manager = NULL;
+  }
+
   G_OBJECT_CLASS (adw_application_parent_class)->dispose (object);
 }
 
+static void
+adw_application_get_property (GObject    *object,
+                              guint       prop_id,
+                              GValue     *value,
+                              GParamSpec *pspec)
+{
+  AdwApplication *self = ADW_APPLICATION (object);
+
+  switch (prop_id) {
+  case PROP_COLOR_SCHEME:
+    g_value_set_enum (value, adw_application_get_color_scheme (self));
+    break;
+
+  case PROP_DARK:
+    g_value_set_boolean (value, adw_application_get_dark (self));
+    break;
+
+  case PROP_HIGH_CONTRAST:
+    g_value_set_boolean (value, adw_application_get_high_contrast (self));
+    break;
+
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+  }
+}
+
+static void
+adw_application_set_property (GObject      *object,
+                              guint         prop_id,
+                              const GValue *value,
+                              GParamSpec   *pspec)
+{
+  AdwApplication *self = ADW_APPLICATION (object);
+
+  switch (prop_id) {
+  case PROP_COLOR_SCHEME:
+    adw_application_set_color_scheme (self, g_value_get_enum (value));
+    break;
+
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+  }
+}
+
 static void
 adw_application_class_init (AdwApplicationClass *klass)
 {
@@ -193,8 +288,34 @@ adw_application_class_init (AdwApplicationClass *klass)
   GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
 
   object_class->dispose = adw_application_dispose;
+  object_class->get_property = adw_application_get_property;
+  object_class->set_property = adw_application_set_property;
 
   application_class->startup = adw_application_startup;
+
+  props[PROP_COLOR_SCHEME] =
+    g_param_spec_enum ("color-scheme",
+                       "Color Scheme",
+                       "Color Scheme",
+                       ADW_TYPE_COLOR_SCHEME,
+                       ADW_COLOR_SCHEME_LIGHT,
+                       G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
+
+  props[PROP_DARK] =
+    g_param_spec_boolean ("dark",
+                          "Dark",
+                          "Dark",
+                          FALSE,
+                          G_PARAM_READABLE);
+
+  props[PROP_HIGH_CONTRAST] =
+    g_param_spec_boolean ("high-contrast",
+                          "High Contrast",
+                          "High Contrast",
+                          FALSE,
+                          G_PARAM_READABLE);
+
+  g_object_class_install_properties (object_class, LAST_PROP, props);
 }
 
 static void
@@ -228,3 +349,66 @@ adw_application_new (const char        *application_id,
                        "flags", flags,
                        NULL);
 }
+
+AdwColorScheme
+adw_application_get_color_scheme (AdwApplication *self)
+{
+  AdwApplicationPrivate *priv;
+
+  g_return_val_if_fail (ADW_IS_APPLICATION (self), ADW_COLOR_SCHEME_LIGHT);
+
+  priv = adw_application_get_instance_private (self);
+
+  return priv->color_scheme;
+}
+
+void
+adw_application_set_color_scheme (AdwApplication *self,
+                                  AdwColorScheme  color_scheme)
+{
+  AdwApplicationPrivate *priv;
+
+  g_return_if_fail (ADW_IS_APPLICATION (self));
+
+  priv = adw_application_get_instance_private (self);
+
+  if (color_scheme == priv->color_scheme)
+    return;
+
+  priv->color_scheme = color_scheme;
+
+  if (priv->style_manager)
+    adw_style_manager_set_color_scheme (priv->style_manager, color_scheme);
+  else
+    notify_color_scheme_cb (self);
+}
+
+gboolean
+adw_application_get_dark (AdwApplication *self)
+{
+  AdwApplicationPrivate *priv;
+
+  g_return_val_if_fail (ADW_IS_APPLICATION (self), FALSE);
+
+  priv = adw_application_get_instance_private (self);
+
+  if (!priv->style_manager)
+    return FALSE;
+
+  return adw_style_manager_get_dark (priv->style_manager);
+}
+
+gboolean
+adw_application_get_high_contrast (AdwApplication *self)
+{
+  AdwApplicationPrivate *priv;
+
+  g_return_val_if_fail (ADW_IS_APPLICATION (self), FALSE);
+
+  priv = adw_application_get_instance_private (self);
+
+  if (!priv->style_manager)
+    return FALSE;
+
+  return adw_style_manager_get_high_contrast (priv->style_manager);
+}
diff --git a/src/adw-application.h b/src/adw-application.h
index 2c5d73de..776934b7 100644
--- a/src/adw-application.h
+++ b/src/adw-application.h
@@ -13,6 +13,7 @@
 #include "adw-version.h"
 
 #include <gtk/gtk.h>
+#include "adw-style-manager.h"
 
 G_BEGIN_DECLS
 
@@ -37,4 +38,15 @@ ADW_AVAILABLE_IN_ALL
 AdwApplication *adw_application_new (const char        *application_id,
                                      GApplicationFlags  flags) G_GNUC_WARN_UNUSED_RESULT;
 
+ADW_AVAILABLE_IN_ALL
+AdwColorScheme adw_application_get_color_scheme (AdwApplication *self);
+ADW_AVAILABLE_IN_ALL
+void           adw_application_set_color_scheme (AdwApplication *self,
+                                                 AdwColorScheme  color_scheme);
+
+ADW_AVAILABLE_IN_ALL
+gboolean adw_application_get_dark          (AdwApplication *self);
+ADW_AVAILABLE_IN_ALL
+gboolean adw_application_get_high_contrast (AdwApplication *self);
+
 G_END_DECLS


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