[gnome-todo] theme-selector: Port to AdwStyleManager



commit bd4cc79e057fdf72aa8f67fc10c84f086feb8bd4
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Fri Oct 8 10:33:09 2021 -0300

    theme-selector: Port to AdwStyleManager

 data/org.gnome.todo.gschema.xml |  1 +
 src/gui/gtd-theme-selector.c    | 61 +++++++++++++++++++++++++++++++----------
 src/gui/gtd-theme-selector.ui   | 13 +++++++++
 3 files changed, 60 insertions(+), 15 deletions(-)
---
diff --git a/data/org.gnome.todo.gschema.xml b/data/org.gnome.todo.gschema.xml
index 52119669..a1107b08 100644
--- a/data/org.gnome.todo.gschema.xml
+++ b/data/org.gnome.todo.gschema.xml
@@ -33,6 +33,7 @@
         </key>
         <key name="style-variant" type="s">
           <choices>
+            <choice value="system"/>
             <choice value="light"/>
             <choice value="dark"/>
           </choices>
diff --git a/src/gui/gtd-theme-selector.c b/src/gui/gtd-theme-selector.c
index dd95df4c..e64b3b8a 100644
--- a/src/gui/gtd-theme-selector.c
+++ b/src/gui/gtd-theme-selector.c
@@ -21,10 +21,13 @@
 #include "gtd-manager.h"
 #include "gtd-theme-selector.h"
 
+#include <adwaita.h>
+
 struct _GtdThemeSelector
 {
   GtkWidget           parent;
 
+  GtkToggleButton    *system;
   GtkToggleButton    *dark;
   GtkToggleButton    *light;
 
@@ -46,14 +49,38 @@ static GParamSpec *properties [N_PROPS];
  * Auxiliary methods
  */
 
+static struct {
+  const gchar *style_variant;
+  AdwColorScheme color_scheme;
+} _style_variant_to_color_scheme[] = {
+  { "system", ADW_COLOR_SCHEME_DEFAULT },
+  { "light", ADW_COLOR_SCHEME_FORCE_LIGHT },
+  { "dark", ADW_COLOR_SCHEME_FORCE_DARK },
+};
+
+static inline AdwColorScheme
+style_variant_to_color_scheme (const gchar *style_variant)
+{
+  AdwColorScheme color_scheme = ADW_COLOR_SCHEME_DEFAULT;
+  gsize i;
+
+  for (i = 0; i < G_N_ELEMENTS (_style_variant_to_color_scheme); i++)
+    {
+      if (g_strcmp0 (style_variant, _style_variant_to_color_scheme[i].style_variant) == 0)
+          color_scheme = _style_variant_to_color_scheme[i].color_scheme;
+    }
+
+  return color_scheme;
+}
+
 static gboolean
-style_variant_to_boolean (GValue   *value,
-                          GVariant *variant,
-                          gpointer  user_data)
+style_variant_to_color_scheme_func (GValue   *value,
+                                    GVariant *variant,
+                                    gpointer  user_data)
 {
-  gboolean is_dark = g_strcmp0 (g_variant_get_string (variant, NULL), "dark") == 0;
+  const gchar *style_variant = g_variant_get_string (variant, NULL);
 
-  g_value_set_boolean (value, is_dark);
+  g_value_set_enum (value, style_variant_to_color_scheme (style_variant));
 
   return TRUE;
 }
@@ -63,20 +90,17 @@ setup_action (GtdThemeSelector *self)
 {
   g_autoptr (GSimpleActionGroup) group = NULL;
   g_autoptr (GAction) action = NULL;
-  GtkSettings *gtk_settings;
+  AdwStyleManager *style_manager;
   GtdManager *manager;
   GSettings *settings;
-  gboolean is_dark;
 
   manager = gtd_manager_get_default ();
   settings = gtd_manager_get_settings (manager);
 
   self->theme = g_settings_get_string (settings, "style-variant");
-  is_dark = g_strcmp0 (self->theme, "dark") == 0;
-  gtk_settings = gtk_settings_get_default ();
-  g_object_set (gtk_settings,
-                "gtk-application-prefer-dark-theme", is_dark,
-                NULL);
+
+  style_manager = adw_style_manager_get_default ();
+  adw_style_manager_set_color_scheme (style_manager, style_variant_to_color_scheme (self->theme));
 
   group = g_simple_action_group_new ();
   action = g_settings_create_action (settings, "style-variant");
@@ -88,10 +112,10 @@ setup_action (GtdThemeSelector *self)
 
   g_settings_bind_with_mapping (settings,
                                 "style-variant",
-                                gtk_settings,
-                                "gtk-application-prefer-dark-theme",
+                                style_manager,
+                                "color-scheme",
                                 G_SETTINGS_BIND_GET,
-                                style_variant_to_boolean,
+                                style_variant_to_color_scheme_func,
                                 NULL, NULL, NULL);
 }
 
@@ -169,6 +193,7 @@ gtd_theme_selector_class_init (GtdThemeSelectorClass *klass)
 
   gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/todo/ui/gtd-theme-selector.ui");
 
+  gtk_widget_class_bind_template_child (widget_class, GtdThemeSelector, system);
   gtk_widget_class_bind_template_child (widget_class, GtdThemeSelector, dark);
   gtk_widget_class_bind_template_child (widget_class, GtdThemeSelector, light);
 
@@ -183,6 +208,12 @@ gtd_theme_selector_init (GtdThemeSelector *self)
   gtk_widget_init_template (GTK_WIDGET (self));
 
   setup_action (self);
+
+  g_object_bind_property (adw_style_manager_get_default (),
+                          "system-supports-color-schemes",
+                          self->system,
+                          "visible",
+                          G_BINDING_SYNC_CREATE);
 }
 
 GtkWidget *
diff --git a/src/gui/gtd-theme-selector.ui b/src/gui/gtd-theme-selector.ui
index 364a2463..5d329bf5 100644
--- a/src/gui/gtd-theme-selector.ui
+++ b/src/gui/gtd-theme-selector.ui
@@ -10,6 +10,19 @@
         <property name="margin-end">24</property>
         <property name="spacing">18</property>
 
+        <child>
+          <object class="GtkCheckButton" id="system">
+            <style>
+              <class name="system"/>
+            </style>
+            <property name="group">light</property>
+            <property name="focus-on-click">false</property>
+            <property name="action-name">settings.style-variant</property>
+            <property name="action-target">'system'</property>
+            <property name="tooltip-text" translatable="yes">System</property>
+          </object>
+        </child>
+
         <child>
           <object class="GtkCheckButton" id="light">
             <style>


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