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




commit 046b59231d36b444d4fd6128a3318bc68be27dbd
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 01fbc7b8..825167d4 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
@@ -402,7 +397,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);
@@ -423,7 +418,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);
@@ -464,17 +460,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 f2fd2f14..e7323542 100644
--- a/examples/adw-demo-window.ui
+++ b/examples/adw-demo-window.ui
@@ -31,8 +31,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]