[libadwaita/wip/exalm/dark: 29/29] demo: Rework the dark mode button




commit d22d710fde8efd56abe316384cf595b7464bfc83
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Sun Aug 29 20:46:53 2021 +0500

    demo: Rework the dark mode button
    
    Only show it if the system doesn't support color schemes.

 examples/adw-demo-window.c  | 45 ++++++++++++++++++---------------------------
 examples/adw-demo-window.ui | 11 +++++++++--
 2 files changed, 27 insertions(+), 29 deletions(-)
---
diff --git a/examples/adw-demo-window.c b/examples/adw-demo-window.c
index f48c9db7..001a0378 100644
--- a/examples/adw-demo-window.c
+++ b/examples/adw-demo-window.c
@@ -11,7 +11,7 @@ struct _AdwDemoWindow
 
   AdwLeaflet *content_box;
   GtkBox *right_box;
-  GtkImage *theme_variant;
+  GtkWidget *color_scheme_button;
   GtkStackSidebar *sidebar;
   GtkStack *stack;
   AdwComboRow *leaflet_transition_row;
@@ -31,27 +31,22 @@ struct _AdwDemoWindow
 
 G_DEFINE_TYPE (AdwDemoWindow, adw_demo_window, ADW_TYPE_APPLICATION_WINDOW)
 
-static void
-theme_variant_button_clicked_cb (AdwDemoWindow *self)
+static char *
+get_color_scheme_icon_name (gpointer user_data,
+                            gboolean dark)
 {
-  GtkSettings *settings = gtk_settings_get_default ();
-  gboolean prefer_dark_theme;
-
-  g_object_get (settings, "gtk-application-prefer-dark-theme", &prefer_dark_theme, NULL);
-  g_object_set (settings, "gtk-application-prefer-dark-theme", !prefer_dark_theme, NULL);
+  return g_strdup (dark ? "light-mode-symbolic" : "dark-mode-symbolic");
 }
 
-static gboolean
-prefer_dark_theme_to_icon_name_cb (GBinding     *binding,
-                                   const GValue *from_value,
-                                   GValue       *to_value,
-                                   gpointer      user_data)
+static void
+color_scheme_button_clicked_cb (AdwDemoWindow *self)
 {
-  g_value_set_string (to_value,
-                      g_value_get_boolean (from_value) ? "light-mode-symbolic" :
-                                                         "dark-mode-symbolic");
+  AdwApplication *app = ADW_APPLICATION (g_application_get_default ());
 
-  return TRUE;
+  if (adw_application_get_dark (app))
+    adw_application_set_color_scheme (app, ADW_COLOR_SCHEME_LIGHT);
+  else
+    adw_application_set_color_scheme (app, ADW_COLOR_SCHEME_DARK);
 }
 
 static void
@@ -398,7 +393,7 @@ adw_demo_window_class_init (AdwDemoWindowClass *klass)
   gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/Adwaita/Demo/ui/adw-demo-window.ui");
   gtk_widget_class_bind_template_child (widget_class, AdwDemoWindow, content_box);
   gtk_widget_class_bind_template_child (widget_class, AdwDemoWindow, right_box);
-  gtk_widget_class_bind_template_child (widget_class, AdwDemoWindow, theme_variant);
+  gtk_widget_class_bind_template_child (widget_class, AdwDemoWindow, color_scheme_button);
   gtk_widget_class_bind_template_child (widget_class, AdwDemoWindow, sidebar);
   gtk_widget_class_bind_template_child (widget_class, AdwDemoWindow, stack);
   gtk_widget_class_bind_template_child (widget_class, AdwDemoWindow, leaflet_transition_row);
@@ -419,7 +414,8 @@ adw_demo_window_class_init (AdwDemoWindowClass *klass)
   gtk_widget_class_bind_template_callback (widget_class, leaflet_transition_name);
   gtk_widget_class_bind_template_callback (widget_class, notify_leaflet_transition_cb);
   gtk_widget_class_bind_template_callback (widget_class, leaflet_go_next_row_activated_cb);
-  gtk_widget_class_bind_template_callback (widget_class, theme_variant_button_clicked_cb);
+  gtk_widget_class_bind_template_callback (widget_class, get_color_scheme_icon_name);
+  gtk_widget_class_bind_template_callback (widget_class, color_scheme_button_clicked_cb);
   gtk_widget_class_bind_template_callback (widget_class, view_switcher_demo_clicked_cb);
   gtk_widget_class_bind_template_callback (widget_class, notify_carousel_orientation_cb);
   gtk_widget_class_bind_template_callback (widget_class, notify_carousel_indicators_cb);
@@ -460,17 +456,12 @@ avatar_page_init (AdwDemoWindow *self)
 static void
 adw_demo_window_init (AdwDemoWindow *self)
 {
-  GtkSettings *settings = gtk_settings_get_default ();
+  AdwStyleManager *manager = adw_style_manager_get_default ();
 
   gtk_widget_init_template (GTK_WIDGET (self));
 
-  g_object_bind_property_full (settings, "gtk-application-prefer-dark-theme",
-                               self->theme_variant, "icon-name",
-                               G_BINDING_SYNC_CREATE,
-                               prefer_dark_theme_to_icon_name_cb,
-                               NULL,
-                               NULL,
-                               NULL);
+  gtk_widget_set_visible (self->color_scheme_button,
+                          !adw_style_manager_get_system_supports_color_scheme (manager));
 
   avatar_page_init (self);
 
diff --git a/examples/adw-demo-window.ui b/examples/adw-demo-window.ui
index 8e6db78f..4ddda518 100644
--- a/examples/adw-demo-window.ui
+++ b/examples/adw-demo-window.ui
@@ -44,8 +44,15 @@
                   <object class="AdwHeaderBar">
                     <property name="show-end-title-buttons" bind-source="content_box" bind-property="folded" 
bind-flags="sync-create"/>
                     <child type="start">
-                      <object class="GtkButton" id="theme_variant">
-                        <signal name="clicked" handler="theme_variant_button_clicked_cb" swapped="yes"/>
+                      <object class="GtkButton" id="color_scheme_button">
+                        <binding name="icon-name">
+                          <closure type="gchararray" function="get_color_scheme_icon_name">
+                            <lookup name="dark" type="AdwApplication">
+                              <lookup name="application">AdwDemoWindow</lookup>
+                            </lookup>
+                          </closure>
+                        </binding>
+                        <signal name="clicked" handler="color_scheme_button_clicked_cb" swapped="yes"/>
                       </object>
                     </child>
                     <child type="end">


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