[libadwaita/wip/exalm/dark: 1/5] Add AdwSettings




commit 6252e579142f7ff1c082937621ec7c5fa08e2115
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Thu Aug 26 14:10:29 2021 +0500

    Add AdwSettings

 src/adw-enums-private.c.in |   1 +
 src/adw-settings-private.h |  36 +++++
 src/adw-settings.c         | 387 +++++++++++++++++++++++++++++++++++++++++++++
 src/meson.build            |   2 +
 4 files changed, 426 insertions(+)
---
diff --git a/src/adw-enums-private.c.in b/src/adw-enums-private.c.in
index 097ec60e..4aa8b6db 100644
--- a/src/adw-enums-private.c.in
+++ b/src/adw-enums-private.c.in
@@ -3,6 +3,7 @@
 #include "config.h"
 #include "adw-enums-private.h"
 #include "adw-animation-private.h"
+#include "adw-settings-private.h"
 
 /*** END file-header ***/
 
diff --git a/src/adw-settings-private.h b/src/adw-settings-private.h
new file mode 100644
index 00000000..1da8caee
--- /dev/null
+++ b/src/adw-settings-private.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2021 Purism SPC
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
+ * Author: Alexander Mikhaylenko <alexander mikhaylenko puri sm>
+ */
+
+#pragma once
+
+#if !defined(_ADWAITA_INSIDE) && !defined(ADWAITA_COMPILATION)
+#error "Only <adwaita.h> can be included directly."
+#endif
+
+#include <glib-object.h>
+#include "adw-enums-private.h"
+
+G_BEGIN_DECLS
+
+typedef enum {
+  ADW_SYSTEM_COLOR_SCHEME_DEFAULT,
+  ADW_SYSTEM_COLOR_SCHEME_PREFER_DARK,
+  ADW_SYSTEM_COLOR_SCHEME_PREFER_LIGHT,
+} AdwSystemColorScheme;
+
+#define ADW_TYPE_SETTINGS (adw_settings_get_type())
+
+G_DECLARE_FINAL_TYPE (AdwSettings, adw_settings, ADW, SETTINGS, GObject)
+
+AdwSettings *adw_settings_get_default (void);
+
+AdwSystemColorScheme adw_settings_get_color_scheme (AdwSettings *self);
+
+gboolean adw_settings_get_high_contrast (AdwSettings *self);
+
+G_END_DECLS
diff --git a/src/adw-settings.c b/src/adw-settings.c
new file mode 100644
index 00000000..07820e71
--- /dev/null
+++ b/src/adw-settings.c
@@ -0,0 +1,387 @@
+/*
+ * Copyright (C) 2021 Purism SPC
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
+ * Author: Alexander Mikhaylenko <alexander mikhaylenko puri sm>
+ */
+
+#include "config.h"
+
+#include "adw-settings-private.h"
+
+#include <gio/gio.h>
+
+#define PORTAL_BUS_NAME "org.freedesktop.portal.Desktop"
+#define PORTAL_OBJECT_PATH "/org/freedesktop/portal/desktop"
+#define PORTAL_SETTINGS_INTERFACE "org.freedesktop.portal.Settings"
+
+struct _AdwSettings
+{
+  GObject parent_instance;
+
+  GDBusProxy *settings_portal;
+  GSettings *settings;
+
+  AdwSystemColorScheme color_scheme;
+  gboolean high_contrast;
+
+  gboolean found_high_contrast;
+  gboolean found_color_scheme;
+  gboolean color_scheme_use_fdo_setting;
+};
+
+G_DEFINE_TYPE (AdwSettings, adw_settings, G_TYPE_OBJECT);
+
+enum {
+  PROP_0,
+  PROP_COLOR_SCHEME,
+  PROP_HIGH_CONTRAST,
+  LAST_PROP,
+};
+
+static GParamSpec *props[LAST_PROP];
+
+static AdwSettings *default_instance;
+
+static void
+set_color_scheme (AdwSettings          *self,
+                  AdwSystemColorScheme  color_scheme)
+{
+  if (color_scheme == self->color_scheme)
+    return;
+
+  self->color_scheme = color_scheme;
+
+  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_COLOR_SCHEME]);
+}
+
+static void
+set_high_contrast (AdwSettings *self,
+                   gboolean     high_contrast)
+{
+  if (high_contrast == self->high_contrast)
+    return;
+
+  self->high_contrast = high_contrast;
+
+  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_HIGH_CONTRAST]);
+}
+
+/* Settings portal */
+
+static gboolean
+read_portal_setting (AdwSettings  *self,
+                     const char   *schema,
+                     const char   *name,
+                     const char   *type,
+                     GVariant    **out)
+{
+  g_autoptr (GError) error = NULL;
+  g_autoptr (GVariant) ret = NULL;
+  g_autoptr (GVariant) child = NULL;
+  g_autoptr (GVariant) child2 = NULL;
+  g_autoptr (GVariantType) out_type = NULL;
+
+  ret = g_dbus_proxy_call_sync (self->settings_portal,
+                                "Read",
+                                g_variant_new ("(ss)", schema, name),
+                                G_DBUS_CALL_FLAGS_NONE,
+                                G_MAXINT,
+                                NULL,
+                                &error);
+  if (error) {
+    g_critical ("Couldn't read the %s setting: %s", name, error->message);
+
+    return FALSE;
+  }
+
+  g_variant_get (ret, "(v)", &child);
+  g_variant_get (child, "v", &child2);
+
+  out_type = g_variant_type_new (type);
+  if (!g_variant_type_equal (g_variant_get_type (child2), out_type)) {
+    g_critical ("Invalid type for %s.%s: expected %s, got %s",
+                schema, name, type, g_variant_get_type_string (child2));
+
+    return FALSE;
+  }
+
+  *out = g_steal_pointer (&child2);
+
+  return TRUE;
+}
+
+static AdwSystemColorScheme
+color_scheme_from_string (const char *str)
+{
+  if (!g_strcmp0 (str, "default"))
+    return ADW_SYSTEM_COLOR_SCHEME_DEFAULT;
+
+  if (!g_strcmp0 (str, "prefer-dark"))
+    return ADW_SYSTEM_COLOR_SCHEME_PREFER_DARK;
+
+  if (!g_strcmp0 (str, "prefer-light"))
+    return ADW_SYSTEM_COLOR_SCHEME_PREFER_LIGHT;
+
+  g_warning ("Invalid color scheme: %s", str);
+
+  return ADW_SYSTEM_COLOR_SCHEME_DEFAULT;
+}
+
+static void
+settings_portal_changed_cb (GDBusProxy  *proxy,
+                            const char  *sender_name,
+                            const char  *signal_name,
+                            GVariant    *parameters,
+                            AdwSettings *self)
+{
+  const char *namespace;
+  const char *name;
+  g_autoptr (GVariant) value = NULL;
+
+  if (g_strcmp0 (signal_name, "SettingChanged"))
+    return;
+
+  g_variant_get (parameters, "(&s&sv)", &namespace, &name, &value);
+
+  if (!g_strcmp0 (namespace, "org.freedesktop.appearance") &&
+      !g_strcmp0 (name, "color-scheme") &&
+      self->color_scheme_use_fdo_setting) {
+    const char *color_scheme = g_variant_get_string (value, NULL);
+
+    set_color_scheme (self, color_scheme_from_string (color_scheme));
+  }
+
+  if (!g_strcmp0 (namespace, "org.gnome.desktop.interface") &&
+      !g_strcmp0 (name, "color-scheme") &&
+      !self->color_scheme_use_fdo_setting) {
+    const char *color_scheme = g_variant_get_string (value, NULL);
+
+    set_color_scheme (self, color_scheme_from_string (color_scheme));
+  }
+
+  if (!g_strcmp0 (namespace, "org.gnome.desktop.interface") &&
+      !g_strcmp0 (name, "high-contrast"))
+    set_high_contrast (self, g_variant_get_boolean (value));
+}
+
+static void
+init_portal (AdwSettings *self)
+{
+  g_autoptr (GError) error = NULL;
+  g_autoptr (GVariant) color_scheme_variant = NULL;
+  g_autoptr (GVariant) high_contrast_variant = NULL;
+  const char *color_scheme;
+
+  self->settings_portal = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+                                                         G_DBUS_PROXY_FLAGS_NONE,
+                                                         NULL,
+                                                         PORTAL_BUS_NAME,
+                                                         PORTAL_OBJECT_PATH,
+                                                         PORTAL_SETTINGS_INTERFACE,
+                                                         NULL,
+                                                         &error);
+  if (error) {
+    g_critical ("Settings portal not found: %s", error->message);
+
+    return;
+  }
+
+  if (read_portal_setting (self, "org.freedesktop.appearance",
+                           "color-scheme", "s", &color_scheme_variant)) {
+    self->found_color_scheme = TRUE;
+    self->color_scheme_use_fdo_setting = TRUE;
+
+    color_scheme = g_variant_get_string (color_scheme_variant, NULL);
+    self->color_scheme = color_scheme_from_string (color_scheme);
+    g_debug ("Found freedesktop color-scheme setting");
+  }
+
+  if (!self->found_color_scheme &&
+      read_portal_setting (self, "org.gnome.desktop.interface",
+                           "color-scheme", "s", &color_scheme_variant)) {
+    self->found_color_scheme = TRUE;
+
+    color_scheme = g_variant_get_string (color_scheme_variant, NULL);
+    self->color_scheme = color_scheme_from_string (color_scheme);
+    g_debug ("Found GNOME color-scheme setting");
+  }
+
+  if (read_portal_setting (self, "org.gnome.desktop.interface",
+                           "high-contrast", "b", &high_contrast_variant)) {
+    self->found_high_contrast = TRUE;
+
+    self->high_contrast = g_variant_get_boolean (high_contrast_variant);
+    g_debug ("Found high-contrast setting");
+  }
+
+  if (!self->found_color_scheme &&
+      !self->found_high_contrast)
+    return;
+
+  g_signal_connect (self->settings_portal, "g-signal",
+                    G_CALLBACK (settings_portal_changed_cb), self);
+}
+
+/* GSettings */
+
+static void
+gsettings_color_scheme_changed_cb (AdwSettings *self)
+{
+  set_color_scheme (self, g_settings_get_enum (self->settings, "color-scheme"));
+}
+
+static void
+gsettings_high_contrast_changed_cb (AdwSettings *self)
+{
+  set_high_contrast (self, g_settings_get_boolean (self->settings, "high-contrast"));
+}
+
+static void
+init_gsettings (AdwSettings *self)
+{
+  GSettingsSchema *schema;
+
+  self->settings = g_settings_new ("org.gnome.desktop.interface");
+
+  g_object_get (self->settings, "settings-schema", &schema, NULL);
+
+  if (!self->found_color_scheme &&
+      g_settings_schema_has_key (schema, "color-scheme")) {
+    self->found_color_scheme = TRUE;
+    self->color_scheme = g_settings_get_enum (self->settings, "color-scheme");
+    g_debug ("Found color-scheme setting");
+
+    g_signal_connect_swapped (self->settings,
+                              "changed::color-scheme",
+                              G_CALLBACK (gsettings_color_scheme_changed_cb),
+                              self);
+  }
+
+  if (!self->found_high_contrast &&
+      g_settings_schema_has_key (schema, "high-contrast")) {
+    self->found_high_contrast = TRUE;
+    self->high_contrast = g_settings_get_boolean (self->settings, "high-contrast");
+    g_debug ("Found high-contrast setting\n");
+
+    g_signal_connect_swapped (self->settings,
+                              "changed::high-contrast",
+                              G_CALLBACK (gsettings_high_contrast_changed_cb),
+                              self);
+  }
+}
+
+static void
+adw_settings_constructed (GObject *object)
+{
+  AdwSettings *self = ADW_SETTINGS (object);
+
+  G_OBJECT_CLASS (adw_settings_parent_class)->constructed (object);
+
+  g_debug ("Trying to initialize portal");
+
+  init_portal (self);
+
+  if (!self->found_color_scheme ||
+      !self->found_high_contrast) {
+    g_debug ("Trying to initialize gsettings");
+    init_gsettings (self);
+  }
+
+  if (!self->found_color_scheme)
+    g_warning ("Couldn't find color-scheme");
+
+  if (!self->found_high_contrast)
+    g_warning ("Couldn't find high-contrast");
+}
+
+static void
+adw_settings_dispose (GObject *object)
+{
+  AdwSettings *self = ADW_SETTINGS (object);
+
+  g_clear_object (&self->settings_portal);
+  g_clear_object (&self->settings);
+
+  G_OBJECT_CLASS (adw_settings_parent_class)->dispose (object);
+}
+
+static void
+adw_settings_get_property (GObject    *object,
+                           guint       prop_id,
+                           GValue     *value,
+                           GParamSpec *pspec)
+{
+  AdwSettings *self = ADW_SETTINGS (object);
+
+  switch (prop_id) {
+  case PROP_COLOR_SCHEME:
+    g_value_set_enum (value, adw_settings_get_color_scheme (self));
+    break;
+
+  case PROP_HIGH_CONTRAST:
+    g_value_set_boolean (value, adw_settings_get_high_contrast (self));
+    break;
+
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+  }
+}
+
+static void
+adw_settings_class_init (AdwSettingsClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->constructed = adw_settings_constructed;
+  object_class->dispose = adw_settings_dispose;
+  object_class->get_property = adw_settings_get_property;
+
+  props[PROP_COLOR_SCHEME] =
+    g_param_spec_enum ("color-scheme",
+                       "Color Scheme",
+                       "Color Scheme",
+                       ADW_TYPE_SYSTEM_COLOR_SCHEME,
+                       ADW_SYSTEM_COLOR_SCHEME_DEFAULT,
+                       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
+adw_settings_init (AdwSettings *self)
+{
+}
+
+AdwSettings *
+adw_settings_get_default (void)
+{
+  if (!default_instance)
+    default_instance = g_object_new (ADW_TYPE_SETTINGS, NULL);
+
+  return default_instance;
+}
+
+AdwSystemColorScheme
+adw_settings_get_color_scheme (AdwSettings *self)
+{
+  g_return_val_if_fail (ADW_IS_SETTINGS (self), ADW_SYSTEM_COLOR_SCHEME_DEFAULT);
+
+  return self->color_scheme;
+}
+
+gboolean
+adw_settings_get_high_contrast (AdwSettings *self)
+{
+  g_return_val_if_fail (ADW_IS_SETTINGS (self), FALSE);
+
+  return self->high_contrast;
+}
diff --git a/src/meson.build b/src/meson.build
index 90c7efa2..3f26e2f3 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -22,6 +22,7 @@ adw_public_enum_headers = [
 
 adw_private_enum_headers = [
   'adw-animation-private.h',
+  'adw-settings-private.h',
 ]
 
 version_data = configuration_data()
@@ -152,6 +153,7 @@ src_sources = [
   'adw-preferences-page.c',
   'adw-preferences-row.c',
   'adw-preferences-window.c',
+  'adw-settings.c',
   'adw-shadow-helper.c',
   'adw-squeezer.c',
   'adw-status-page.c',


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