[libadwaita/wip/exalm/demo-cleanups: 15/20] demo: Split toasts page into a separate class




commit c9c1541612e0bf502e1b636c874f8b4cc57ff98a
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Thu Dec 9 19:00:26 2021 +0500

    demo: Split toasts page into a separate class

 demo/adw-demo-window.c                    |  95 ++-------------------
 demo/adw-demo-window.ui                   |  62 +-------------
 demo/adwaita-demo.gresources.xml          |   1 +
 demo/meson.build                          |   1 +
 demo/pages/toasts/adw-demo-page-toasts.c  | 136 ++++++++++++++++++++++++++++++
 demo/pages/toasts/adw-demo-page-toasts.h  |  13 +++
 demo/pages/toasts/adw-demo-page-toasts.ui |  70 +++++++++++++++
 7 files changed, 230 insertions(+), 148 deletions(-)
---
diff --git a/demo/adw-demo-window.c b/demo/adw-demo-window.c
index ba3a2dcb..08a4ad4a 100644
--- a/demo/adw-demo-window.c
+++ b/demo/adw-demo-window.c
@@ -10,6 +10,7 @@
 #include "pages/lists/adw-demo-page-lists.h"
 #include "pages/styles/adw-demo-page-styles.h"
 #include "pages/tab-view/adw-demo-page-tab-view.h"
+#include "pages/toasts/adw-demo-page-toasts.h"
 #include "pages/view-switcher/adw-demo-page-view-switcher.h"
 #include "pages/welcome/adw-demo-page-welcome.h"
 
@@ -18,14 +19,12 @@ struct _AdwDemoWindow
   AdwApplicationWindow parent_instance;
 
   AdwLeaflet *content_box;
-  AdwToastOverlay *toast_overlay;
   GtkBox *right_box;
   GtkWidget *color_scheme_button;
   GtkStackSidebar *sidebar;
   GtkStack *stack;
   AdwLeaflet *subpage_leaflet;
-  int toast_undo_items;
-  AdwToast *undo_toast;
+  AdwDemoPageToasts *toasts_page;
   GtkStack *animation_preferences_stack;
   AdwAnimation *timed_animation;
   GtkWidget *timed_animation_sample;
@@ -380,85 +379,10 @@ notify_spring_params_change (AdwDemoWindow *self)
   adw_spring_animation_set_spring_params (ADW_SPRING_ANIMATION (self->spring_animation), spring_params);
 }
 
-static void
-add_toast_cb (AdwDemoWindow *self)
-{
-  adw_toast_overlay_add_toast (self->toast_overlay,
-                               adw_toast_new (_("Simple Toast")));
-}
-
-static void
-dismissed_cb (AdwDemoWindow *self)
-{
-  self->undo_toast = NULL;
-  self->toast_undo_items = 0;
-
-  gtk_widget_action_set_enabled (GTK_WIDGET (self), "toast.dismiss", FALSE);
-}
-
-static void
-add_toast_with_button_cb (AdwDemoWindow *self)
-{
-  g_autofree char *title = NULL;
-
-  self->toast_undo_items++;
-
-  if (!self->undo_toast) {
-    title = g_strdup_printf (_("‘%s’ deleted"), "Lorem Ipsum");
-
-    self->undo_toast = adw_toast_new (title);
-
-    adw_toast_set_priority (self->undo_toast, ADW_TOAST_PRIORITY_HIGH);
-    adw_toast_set_button_label (self->undo_toast, _("_Undo"));
-    adw_toast_set_action_name (self->undo_toast, "toast.undo");
-
-    g_signal_connect_swapped (self->undo_toast, "dismissed", G_CALLBACK (dismissed_cb), self);
-
-    adw_toast_overlay_add_toast (self->toast_overlay, self->undo_toast);
-
-    gtk_widget_action_set_enabled (GTK_WIDGET (self), "toast.dismiss", TRUE);
-
-    return;
-  }
-
-  title =
-    g_strdup_printf (ngettext ("<span font_features='tnum=1'>%d</span> item deleted",
-                               "<span font_features='tnum=1'>%d</span> items deleted",
-                               self->toast_undo_items), self->toast_undo_items);
-
-  adw_toast_set_title (self->undo_toast, title);
-}
-
-static void
-add_toast_with_long_title_cb (AdwDemoWindow *self)
-{
-  adw_toast_overlay_add_toast (self->toast_overlay,
-                               adw_toast_new (_("Lorem ipsum dolor sit amet, "
-                                                "consectetur adipiscing elit, "
-                                                "sed do eiusmod tempor incididunt "
-                                                "ut labore et dolore magnam aliquam "
-                                                "quaerat voluptatem.")));
-}
-
 static void
 toast_undo_cb (AdwDemoWindow *self)
 {
-  g_autofree char *title =
-    g_strdup_printf (ngettext ("Undoing deleting <span font_features='tnum=1'>%d</span> item…",
-                               "Undoing deleting <span font_features='tnum=1'>%d</span> items…",
-                               self->toast_undo_items), self->toast_undo_items);
-  AdwToast *toast = adw_toast_new (title);
-
-  adw_toast_set_priority (toast, ADW_TOAST_PRIORITY_HIGH);
-
-  adw_toast_overlay_add_toast (self->toast_overlay, toast);
-}
-
-static void
-toast_dismiss_cb (AdwDemoWindow *self)
-{
-  if (self->undo_toast)
-    adw_toast_dismiss (self->undo_toast);
+  adw_demo_page_toasts_undo (self->toasts_page);
 }
 
 static void
@@ -472,9 +396,6 @@ adw_demo_window_class_init (AdwDemoWindowClass *klass)
 
   gtk_widget_class_add_binding_action (widget_class, GDK_KEY_q, GDK_CONTROL_MASK, "window.close", NULL);
 
-  gtk_widget_class_install_action (widget_class, "toast.undo", NULL, (GtkWidgetActionActivateFunc) 
toast_undo_cb);
-  gtk_widget_class_install_action (widget_class, "toast.dismiss", NULL, (GtkWidgetActionActivateFunc) 
toast_dismiss_cb);
-
   gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/Adwaita1/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);
@@ -482,7 +403,7 @@ adw_demo_window_class_init (AdwDemoWindowClass *klass)
   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, subpage_leaflet);
-  gtk_widget_class_bind_template_child (widget_class, AdwDemoWindow, toast_overlay);
+  gtk_widget_class_bind_template_child (widget_class, AdwDemoWindow, toasts_page);
   gtk_widget_class_bind_template_child (widget_class, AdwDemoWindow, animation_preferences_stack);
   gtk_widget_class_bind_template_child (widget_class, AdwDemoWindow, timed_animation_sample);
   gtk_widget_class_bind_template_child (widget_class, AdwDemoWindow, timed_animation_button_box);
@@ -503,9 +424,6 @@ adw_demo_window_class_init (AdwDemoWindowClass *klass)
   gtk_widget_class_bind_template_callback (widget_class, leaflet_next_page_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, add_toast_cb);
-  gtk_widget_class_bind_template_callback (widget_class, add_toast_with_button_cb);
-  gtk_widget_class_bind_template_callback (widget_class, add_toast_with_long_title_cb);
   gtk_widget_class_bind_template_callback (widget_class, animations_easing_name);
   gtk_widget_class_bind_template_callback (widget_class, timed_animation_reset);
   gtk_widget_class_bind_template_callback (widget_class, timed_animation_play_pause);
@@ -530,6 +448,8 @@ adw_demo_window_class_init (AdwDemoWindowClass *klass)
                          G_PARAM_READWRITE);
 
   g_object_class_install_properties (object_class, LAST_PROP, props);
+
+  gtk_widget_class_install_action (widget_class, "toast.undo", NULL, (GtkWidgetActionActivateFunc) 
toast_undo_cb);
 }
 
 static void
@@ -608,6 +528,7 @@ adw_demo_window_init (AdwDemoWindow *self)
   g_type_ensure (ADW_TYPE_DEMO_PAGE_LISTS);
   g_type_ensure (ADW_TYPE_DEMO_PAGE_STYLES);
   g_type_ensure (ADW_TYPE_DEMO_PAGE_TAB_VIEW);
+  g_type_ensure (ADW_TYPE_DEMO_PAGE_TOASTS);
   g_type_ensure (ADW_TYPE_DEMO_PAGE_VIEW_SWITCHER);
   g_type_ensure (ADW_TYPE_DEMO_PAGE_WELCOME);
 
@@ -623,7 +544,5 @@ adw_demo_window_init (AdwDemoWindow *self)
 
   adw_leaflet_set_visible_child (self->content_box, GTK_WIDGET (self->right_box));
 
-  gtk_widget_action_set_enabled (GTK_WIDGET (self), "toast.dismiss", FALSE);
-
   animation_page_init (self);
 }
diff --git a/demo/adw-demo-window.ui b/demo/adw-demo-window.ui
index 539bee7c..9d769d8d 100644
--- a/demo/adw-demo-window.ui
+++ b/demo/adw-demo-window.ui
@@ -202,66 +202,8 @@
                           <object class="GtkStackPage">
                             <property name="title" translatable="yes">Toasts</property>
                             <property name="child">
-                              <object class="AdwStatusPage">
-                                <property 
name="icon-name">preferences-system-notifications-symbolic</property>
-                                <property name="title" translatable="yes">Toasts</property>
-                                <property name="description" translatable="yes">Transient in-app 
notifications.</property>
-                                <property name="child">
-                                  <object class="AdwClamp">
-                                    <property name="maximum-size">400</property>
-                                    <property name="tightening-threshold">300</property>
-                                    <property name="child">
-                                      <object class="AdwPreferencesGroup">
-                                        <child>
-                                          <object class="AdwActionRow">
-                                            <property name="title" translatable="yes">Simple Toast</property>
-                                            <child>
-                                              <object class="GtkButton">
-                                                <property name="valign">center</property>
-                                                <property name="label" translatable="yes">Show</property>
-                                                <signal name="clicked" handler="add_toast_cb" 
swapped="true"/>
-                                              </object>
-                                            </child>
-                                          </object>
-                                        </child>
-                                        <child>
-                                          <object class="AdwActionRow">
-                                            <property name="title" translatable="yes">Toast With an 
Action</property>
-                                            <child>
-                                              <object class="GtkButton">
-                                                <property name="valign">center</property>
-                                                <property name="icon-name">user-trash-symbolic</property>
-                                                <property name="action-name">toast.dismiss</property>
-                                                <style>
-                                                  <class name="flat"/>
-                                                </style>
-                                              </object>
-                                            </child>
-                                            <child>
-                                              <object class="GtkButton">
-                                                <property name="valign">center</property>
-                                                <property name="label" translatable="yes">Show</property>
-                                                <signal name="clicked" handler="add_toast_with_button_cb" 
swapped="true"/>
-                                              </object>
-                                            </child>
-                                          </object>
-                                        </child>
-                                        <child>
-                                          <object class="AdwActionRow">
-                                            <property name="title" translatable="yes">Toast With a Long 
Title</property>
-                                            <child>
-                                              <object class="GtkButton">
-                                                <property name="valign">center</property>
-                                                <property name="label" translatable="yes">Show</property>
-                                                <signal name="clicked" 
handler="add_toast_with_long_title_cb" swapped="true"/>
-                                              </object>
-                                            </child>
-                                          </object>
-                                        </child>
-                                      </object>
-                                    </property>
-                                  </object>
-                                </property>
+                              <object class="AdwDemoPageToasts" id="toasts_page">
+                                <signal name="add-toast" handler="adw_toast_overlay_add_toast" 
object="toast_overlay" swapped="true"/>
                               </object>
                             </property>
                           </object>
diff --git a/demo/adwaita-demo.gresources.xml b/demo/adwaita-demo.gresources.xml
index 666a1a14..d4cc311f 100644
--- a/demo/adwaita-demo.gresources.xml
+++ b/demo/adwaita-demo.gresources.xml
@@ -53,6 +53,7 @@
     <file preprocess="xml-stripblanks">pages/styles/adw-style-demo-window.ui</file>
     <file preprocess="xml-stripblanks">pages/tab-view/adw-demo-page-tab-view.ui</file>
     <file preprocess="xml-stripblanks">pages/tab-view/adw-tab-view-demo-window.ui</file>
+    <file preprocess="xml-stripblanks">pages/toasts/adw-demo-page-toasts.ui</file>
     <file preprocess="xml-stripblanks">pages/view-switcher/adw-demo-page-view-switcher.ui</file>
     <file preprocess="xml-stripblanks">pages/view-switcher/adw-view-switcher-demo-window.ui</file>
     <file preprocess="xml-stripblanks">pages/welcome/adw-demo-page-welcome.ui</file>
diff --git a/demo/meson.build b/demo/meson.build
index 3442fae5..6e1bb587 100644
--- a/demo/meson.build
+++ b/demo/meson.build
@@ -24,6 +24,7 @@ adwaita_demo_sources = [
   'pages/styles/adw-style-demo-window.c',
   'pages/tab-view/adw-demo-page-tab-view.c',
   'pages/tab-view/adw-tab-view-demo-window.c',
+  'pages/toasts/adw-demo-page-toasts.c',
   'pages/view-switcher/adw-demo-page-view-switcher.c',
   'pages/view-switcher/adw-view-switcher-demo-window.c',
   'pages/welcome/adw-demo-page-welcome.c',
diff --git a/demo/pages/toasts/adw-demo-page-toasts.c b/demo/pages/toasts/adw-demo-page-toasts.c
new file mode 100644
index 00000000..4b679104
--- /dev/null
+++ b/demo/pages/toasts/adw-demo-page-toasts.c
@@ -0,0 +1,136 @@
+#include "adw-demo-page-toasts.h"
+
+#include <glib/gi18n.h>
+
+struct _AdwDemoPageToasts
+{
+  AdwBin parent_instance;
+
+  AdwToast *undo_toast;
+  int toast_undo_items;
+};
+
+enum {
+  SIGNAL_ADD_TOAST,
+  SIGNAL_LAST_SIGNAL,
+};
+
+static guint signals[SIGNAL_LAST_SIGNAL];
+
+G_DEFINE_TYPE (AdwDemoPageToasts, adw_demo_page_toasts, ADW_TYPE_BIN)
+
+static void
+add_toast (AdwDemoPageToasts *self,
+           AdwToast          *toast)
+{
+  g_signal_emit (self, signals[SIGNAL_ADD_TOAST], 0, toast);
+}
+
+static void
+dismissed_cb (AdwDemoPageToasts *self)
+{
+  self->undo_toast = NULL;
+  self->toast_undo_items = 0;
+
+  gtk_widget_action_set_enabled (GTK_WIDGET (self), "toast.dismiss", FALSE);
+}
+
+static void
+toast_add_cb (AdwDemoPageToasts *self)
+{
+  add_toast (self, adw_toast_new (_("Simple Toast")));
+}
+
+static void
+toast_add_with_button_cb (AdwDemoPageToasts *self)
+{
+  g_autofree char *title = NULL;
+
+  self->toast_undo_items++;
+
+  if (!self->undo_toast) {
+    title = g_strdup_printf (_("‘%s’ deleted"), "Lorem Ipsum");
+
+    self->undo_toast = adw_toast_new (title);
+
+    adw_toast_set_priority (self->undo_toast, ADW_TOAST_PRIORITY_HIGH);
+    adw_toast_set_button_label (self->undo_toast, _("_Undo"));
+    adw_toast_set_action_name (self->undo_toast, "toast.undo");
+
+    g_signal_connect_swapped (self->undo_toast, "dismissed", G_CALLBACK (dismissed_cb), self);
+
+    add_toast (self, self->undo_toast);
+
+    gtk_widget_action_set_enabled (GTK_WIDGET (self), "toast.dismiss", TRUE);
+
+    return;
+  }
+
+  title =
+    g_strdup_printf (ngettext ("<span font_features='tnum=1'>%d</span> item deleted",
+                               "<span font_features='tnum=1'>%d</span> items deleted",
+                               self->toast_undo_items), self->toast_undo_items);
+
+  adw_toast_set_title (self->undo_toast, title);
+}
+
+static void
+toast_add_with_long_title_cb (AdwDemoPageToasts *self)
+{
+  add_toast (self, adw_toast_new (_("Lorem ipsum dolor sit amet, "
+                                    "consectetur adipiscing elit, "
+                                    "sed do eiusmod tempor incididunt "
+                                    "ut labore et dolore magnam aliquam "
+                                    "quaerat voluptatem.")));
+}
+
+static void
+toast_dismiss_cb (AdwDemoPageToasts *self)
+{
+  if (self->undo_toast)
+    adw_toast_dismiss (self->undo_toast);
+}
+
+static void
+adw_demo_page_toasts_class_init (AdwDemoPageToastsClass *klass)
+{
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  signals[SIGNAL_ADD_TOAST] =
+    g_signal_new ("add-toast",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_FIRST,
+                  0,
+                  NULL, NULL, NULL,
+                  G_TYPE_NONE, 1,
+                  ADW_TYPE_TOAST);
+
+  gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/Adwaita1/Demo/ui/pages/toasts/adw-demo-page-toasts.ui");
+
+  gtk_widget_class_install_action (widget_class, "toast.add", NULL, (GtkWidgetActionActivateFunc) 
toast_add_cb);
+  gtk_widget_class_install_action (widget_class, "toast.add-with-button", NULL, 
(GtkWidgetActionActivateFunc) toast_add_with_button_cb);
+  gtk_widget_class_install_action (widget_class, "toast.add-with-long-title", NULL, 
(GtkWidgetActionActivateFunc) toast_add_with_long_title_cb);
+  gtk_widget_class_install_action (widget_class, "toast.dismiss", NULL, (GtkWidgetActionActivateFunc) 
toast_dismiss_cb);
+}
+
+static void
+adw_demo_page_toasts_init (AdwDemoPageToasts *self)
+{
+  gtk_widget_init_template (GTK_WIDGET (self));
+
+  gtk_widget_action_set_enabled (GTK_WIDGET (self), "toast.dismiss", FALSE);
+}
+
+void
+adw_demo_page_toasts_undo (AdwDemoPageToasts *self)
+{
+  g_autofree char *title =
+    g_strdup_printf (ngettext ("Undoing deleting <span font_features='tnum=1'>%d</span> item…",
+                               "Undoing deleting <span font_features='tnum=1'>%d</span> items…",
+                               self->toast_undo_items), self->toast_undo_items);
+  AdwToast *toast = adw_toast_new (title);
+
+  adw_toast_set_priority (toast, ADW_TOAST_PRIORITY_HIGH);
+
+  add_toast (self, toast);
+}
diff --git a/demo/pages/toasts/adw-demo-page-toasts.h b/demo/pages/toasts/adw-demo-page-toasts.h
new file mode 100644
index 00000000..a5325a23
--- /dev/null
+++ b/demo/pages/toasts/adw-demo-page-toasts.h
@@ -0,0 +1,13 @@
+#pragma once
+
+#include <adwaita.h>
+
+G_BEGIN_DECLS
+
+#define ADW_TYPE_DEMO_PAGE_TOASTS (adw_demo_page_toasts_get_type())
+
+G_DECLARE_FINAL_TYPE (AdwDemoPageToasts, adw_demo_page_toasts, ADW, DEMO_PAGE_TOASTS, AdwBin)
+
+void adw_demo_page_toasts_undo (AdwDemoPageToasts *self);
+
+G_END_DECLS
diff --git a/demo/pages/toasts/adw-demo-page-toasts.ui b/demo/pages/toasts/adw-demo-page-toasts.ui
new file mode 100644
index 00000000..cb97e51b
--- /dev/null
+++ b/demo/pages/toasts/adw-demo-page-toasts.ui
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <requires lib="gtk" version="4.0"/>
+  <requires lib="libadwaita" version="1.0"/>
+  <template class="AdwDemoPageToasts" parent="AdwBin">
+    <property name="child">
+      <object class="AdwStatusPage">
+        <property name="icon-name">preferences-system-notifications-symbolic</property>
+        <property name="title" translatable="yes">Toasts</property>
+        <property name="description" translatable="yes">Transient in-app notifications.</property>
+        <property name="child">
+          <object class="AdwClamp">
+            <property name="maximum-size">400</property>
+            <property name="tightening-threshold">300</property>
+            <property name="child">
+              <object class="AdwPreferencesGroup">
+                <child>
+                  <object class="AdwActionRow">
+                    <property name="title" translatable="yes">Simple Toast</property>
+                    <child>
+                      <object class="GtkButton">
+                        <property name="valign">center</property>
+                        <property name="label" translatable="yes">Show</property>
+                        <property name="action-name">toast.add</property>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+                <child>
+                  <object class="AdwActionRow">
+                    <property name="title" translatable="yes">Toast With an Action</property>
+                    <child>
+                      <object class="GtkButton">
+                        <property name="valign">center</property>
+                        <property name="icon-name">user-trash-symbolic</property>
+                        <property name="action-name">toast.dismiss</property>
+                        <style>
+                          <class name="flat"/>
+                        </style>
+                      </object>
+                    </child>
+                    <child>
+                      <object class="GtkButton">
+                        <property name="valign">center</property>
+                        <property name="label" translatable="yes">Show</property>
+                        <property name="action-name">toast.add-with-button</property>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+                <child>
+                  <object class="AdwActionRow">
+                    <property name="title" translatable="yes">Toast With a Long Title</property>
+                    <child>
+                      <object class="GtkButton">
+                        <property name="valign">center</property>
+                        <property name="label" translatable="yes">Show</property>
+                        <property name="action-name">toast.add-with-long-title</property>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+              </object>
+            </property>
+          </object>
+        </property>
+      </object>
+    </property>
+  </template>
+</interface>


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