[libadwaita/wip/exalm/pref-window-toast: 5/6] preferences-window: Add a way to show toasts




commit 7d8ef9b29403adcd4d570f63a28479aa05d0a53d
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Thu Nov 11 19:58:05 2021 +0500

    preferences-window: Add a way to show toasts
    
    Now that AdwToastOverlay exists, we can also have it here.
    
    Fixes https://gitlab.gnome.org/GNOME/libadwaita/-/issues/322

 src/adw-preferences-window.c    |  27 +++++
 src/adw-preferences-window.h    |   5 +
 src/adw-preferences-window.ui   | 218 ++++++++++++++++++++--------------------
 tests/test-preferences-window.c |  16 ++-
 4 files changed, 158 insertions(+), 108 deletions(-)
---
diff --git a/src/adw-preferences-window.c b/src/adw-preferences-window.c
index 8d81dd3a..01476077 100644
--- a/src/adw-preferences-window.c
+++ b/src/adw-preferences-window.c
@@ -15,6 +15,7 @@
 #include "adw-macros-private.h"
 #include "adw-preferences-group-private.h"
 #include "adw-preferences-page-private.h"
+#include "adw-toast-overlay.h"
 #include "adw-view-switcher.h"
 #include "adw-view-switcher-bar.h"
 #include "adw-view-switcher-title.h"
@@ -37,6 +38,7 @@
 
 typedef struct
 {
+  AdwToastOverlay *toast_overlay;
   AdwLeaflet *subpages_leaflet;
   GtkWidget *preferences;
   GtkStack *content_stack;
@@ -579,6 +581,7 @@ adw_preferences_window_class_init (AdwPreferencesWindowClass *klass)
 
   gtk_widget_class_set_template_from_resource (widget_class,
                                                "/org/gnome/Adwaita/ui/adw-preferences-window.ui");
+  gtk_widget_class_bind_template_child_private (widget_class, AdwPreferencesWindow, toast_overlay);
   gtk_widget_class_bind_template_child_private (widget_class, AdwPreferencesWindow, subpages_leaflet);
   gtk_widget_class_bind_template_child_private (widget_class, AdwPreferencesWindow, preferences);
   gtk_widget_class_bind_template_child_private (widget_class, AdwPreferencesWindow, content_stack);
@@ -990,3 +993,27 @@ adw_preferences_window_set_visible_page_name (AdwPreferencesWindow *self,
 
   adw_view_stack_set_visible_child_name (priv->pages_stack, name);
 }
+
+/**
+ * adw_preferences_window_add_toast:
+ * @self: a `AdwPreferencesWindow`
+ * @toast: (transfer full): a toast
+ *
+ * Displays @toast.
+ *
+ * See [method@Adw.ToastOverlay:add_toast].
+ *
+ * Since: 1.0
+ */
+void
+adw_preferences_window_add_toast (AdwPreferencesWindow *self,
+                                  AdwToast             *toast)
+{
+  AdwPreferencesWindowPrivate *priv;
+
+  g_return_if_fail (ADW_IS_PREFERENCES_WINDOW (self));
+
+  priv = adw_preferences_window_get_instance_private (self);
+
+  adw_toast_overlay_add_toast (priv->toast_overlay, toast);
+}
diff --git a/src/adw-preferences-window.h b/src/adw-preferences-window.h
index 21729657..85bfa6d9 100644
--- a/src/adw-preferences-window.h
+++ b/src/adw-preferences-window.h
@@ -14,6 +14,7 @@
 
 #include <gtk/gtk.h>
 #include "adw-preferences-page.h"
+#include "adw-toast.h"
 #include "adw-window.h"
 
 G_BEGIN_DECLS
@@ -75,4 +76,8 @@ ADW_AVAILABLE_IN_ALL
 void        adw_preferences_window_set_visible_page_name (AdwPreferencesWindow *self,
                                                           const char           *name);
 
+ADW_AVAILABLE_IN_ALL
+void adw_preferences_window_add_toast (AdwPreferencesWindow *self,
+                                       AdwToast             *toast);
+
 G_END_DECLS
diff --git a/src/adw-preferences-window.ui b/src/adw-preferences-window.ui
index 7d57226f..f20bb7ba 100644
--- a/src/adw-preferences-window.ui
+++ b/src/adw-preferences-window.ui
@@ -12,32 +12,91 @@
       <class name="preferences"/>
     </style>
     <child>
-      <object class="AdwLeaflet" id="subpages_leaflet">
-        <property name="can-swipe-back" bind-source="AdwPreferencesWindow" bind-property="can-swipe-back" 
bind-flags="sync-create"/>
-        <property name="can-unfold">False</property>
-        <property name="width-request">360</property>
-        <signal name="notify::child-transition-running" 
handler="subpages_leaflet_child_transition_running_cb" swapped="yes"/>
-        <signal name="notify::visible-child" handler="subpages_leaflet_visible_child_cb" swapped="yes"/>
+      <object class="AdwToastOverlay" id="toast_overlay">
         <child>
-          <object class="GtkBox" id="preferences">
-            <property name="orientation">vertical</property>
+          <object class="AdwLeaflet" id="subpages_leaflet">
+            <property name="can-swipe-back" bind-source="AdwPreferencesWindow" 
bind-property="can-swipe-back" bind-flags="sync-create"/>
+            <property name="can-unfold">False</property>
+            <property name="width-request">360</property>
+            <signal name="notify::child-transition-running" 
handler="subpages_leaflet_child_transition_running_cb" swapped="yes"/>
+            <signal name="notify::visible-child" handler="subpages_leaflet_visible_child_cb" swapped="yes"/>
             <child>
-              <object class="AdwHeaderBar">
-                <property name="centering-policy">strict</property>
-                <property name="title-widget">
-                  <object class="GtkStack" id="title_stack">
+              <object class="GtkBox" id="preferences">
+                <property name="orientation">vertical</property>
+                <child>
+                  <object class="AdwHeaderBar">
+                    <property name="centering-policy">strict</property>
+                    <property name="title-widget">
+                      <object class="GtkStack" id="title_stack">
+                        <property name="transition-type">crossfade</property>
+                        <property name="hexpand">True</property>
+                        <signal name="notify::visible-child" handler="title_stack_notify_visible_child_cb" 
swapped="true"/>
+                        <signal name="notify::transition-running" 
handler="title_stack_notify_transition_running_cb" swapped="true"/>
+                        <child>
+                          <object class="GtkStackPage">
+                            <property name="name">pages</property>
+                            <property name="child">
+                              <object class="AdwViewSwitcherTitle" id="view_switcher_title">
+                                <property name="halign">center</property>
+                                <property name="stack">pages_stack</property>
+                                <property name="title" bind-source="AdwPreferencesWindow" 
bind-property="title" bind-flags="sync-create"/>
+                              </object>
+                            </property>
+                          </object>
+                        </child>
+                        <child>
+                          <object class="GtkStackPage">
+                            <property name="name">search</property>
+                            <property name="child">
+                              <object class="AdwClamp">
+                                <property name="tightening-threshold">300</property>
+                                <property name="maximum-size">400</property>
+                                <child>
+                                  <object class="GtkSearchEntry" id="search_entry">
+                                    <signal name="search-started" handler="search_started_cb" swapped="yes"/>
+                                    <signal name="search-changed" handler="search_changed_cb" swapped="yes"/>
+                                    <signal name="stop-search" handler="stop_search_cb" swapped="yes"/>
+                                  </object>
+                                </child>
+                              </object>
+                            </property>
+                          </object>
+                        </child>
+                      </object>
+                    </property>
+                    <child type="end">
+                      <object class="GtkToggleButton" id="search_button">
+                        <property name="valign">center</property>
+                        <property name="tooltip-text" translatable="yes">Search</property>
+                        <property name="icon_name">edit-find-symbolic</property>
+                        <signal name="notify::active" handler="search_button_notify_active_cb" 
swapped="yes"/>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+                <child>
+                  <object class="GtkStack" id="content_stack">
                     <property name="transition-type">crossfade</property>
-                    <property name="hexpand">True</property>
-                    <signal name="notify::visible-child" handler="title_stack_notify_visible_child_cb" 
swapped="true"/>
-                    <signal name="notify::transition-running" 
handler="title_stack_notify_transition_running_cb" swapped="true"/>
+                    <property name="vhomogeneous">False</property>
                     <child>
                       <object class="GtkStackPage">
                         <property name="name">pages</property>
                         <property name="child">
-                          <object class="AdwViewSwitcherTitle" id="view_switcher_title">
-                            <property name="halign">center</property>
-                            <property name="stack">pages_stack</property>
-                            <property name="title" bind-source="AdwPreferencesWindow" bind-property="title" 
bind-flags="sync-create"/>
+                          <object class="GtkBox">
+                            <property name="orientation">vertical</property>
+                            <child>
+                              <object class="AdwViewStack" id="pages_stack">
+                                <property name="vexpand">True</property>
+                                <signal name="notify::visible-child" handler="notify_visible_page_cb" 
swapped="yes"/>
+                                <signal name="notify::visible-child-name" 
handler="notify_visible_page_name_cb" swapped="yes"/>
+                              </object>
+                            </child>
+                            <child>
+                              <object class="AdwViewSwitcherBar" id="view_switcher_bar">
+                                <property name="stack">pages_stack</property>
+                                <property name="reveal" bind-source="view_switcher_title" 
bind-property="title-visible" bind-flags="sync-create"/>
+                              </object>
+                            </child>
                           </object>
                         </property>
                       </object>
@@ -46,103 +105,48 @@
                       <object class="GtkStackPage">
                         <property name="name">search</property>
                         <property name="child">
-                          <object class="AdwClamp">
-                            <property name="tightening-threshold">300</property>
-                            <property name="maximum-size">400</property>
+                          <object class="GtkStack" id="search_stack">
                             <child>
-                              <object class="GtkSearchEntry" id="search_entry">
-                                <signal name="search-started" handler="search_started_cb" swapped="yes"/>
-                                <signal name="search-changed" handler="search_changed_cb" swapped="yes"/>
-                                <signal name="stop-search" handler="stop_search_cb" swapped="yes"/>
-                              </object>
-                            </child>
-                          </object>
-                        </property>
-                      </object>
-                    </child>
-                  </object>
-                </property>
-                <child type="end">
-                  <object class="GtkToggleButton" id="search_button">
-                    <property name="valign">center</property>
-                    <property name="tooltip-text" translatable="yes">Search</property>
-                    <property name="icon_name">edit-find-symbolic</property>
-                    <signal name="notify::active" handler="search_button_notify_active_cb" swapped="yes"/>
-                  </object>
-                </child>
-              </object>
-            </child>
-            <child>
-              <object class="GtkStack" id="content_stack">
-                <property name="transition-type">crossfade</property>
-                <property name="vhomogeneous">False</property>
-                <child>
-                  <object class="GtkStackPage">
-                    <property name="name">pages</property>
-                    <property name="child">
-                      <object class="GtkBox">
-                        <property name="orientation">vertical</property>
-                        <child>
-                          <object class="AdwViewStack" id="pages_stack">
-                            <property name="vexpand">True</property>
-                            <signal name="notify::visible-child" handler="notify_visible_page_cb" 
swapped="yes"/>
-                            <signal name="notify::visible-child-name" handler="notify_visible_page_name_cb" 
swapped="yes"/>
-                          </object>
-                        </child>
-                        <child>
-                          <object class="AdwViewSwitcherBar" id="view_switcher_bar">
-                            <property name="stack">pages_stack</property>
-                            <property name="reveal" bind-source="view_switcher_title" 
bind-property="title-visible" bind-flags="sync-create"/>
-                          </object>
-                        </child>
-                      </object>
-                    </property>
-                  </object>
-                </child>
-                <child>
-                  <object class="GtkStackPage">
-                    <property name="name">search</property>
-                    <property name="child">
-                      <object class="GtkStack" id="search_stack">
-                        <child>
-                          <object class="GtkStackPage">
-                            <property name="name">results</property>
-                            <property name="child">
-                              <object class="AdwPreferencesPage">
-                                <child>
-                                  <object class="AdwPreferencesGroup">
+                              <object class="GtkStackPage">
+                                <property name="name">results</property>
+                                <property name="child">
+                                  <object class="AdwPreferencesPage">
                                     <child>
-                                      <object class="GtkListBox" id="search_results">
-                                        <property name="selection-mode">none</property>
-                                        <property name="valign">start</property>
-                                        <signal name="row-activated" handler="search_result_activated_cb" 
swapped="yes"/>
-                                        <signal name="map" handler="search_results_map" swapped="yes"/>
-                                        <signal name="unmap" handler="search_results_unmap" swapped="yes"/>
-                                        <style>
-                                          <class name="boxed-list"/>
-                                        </style>
+                                      <object class="AdwPreferencesGroup">
+                                        <child>
+                                          <object class="GtkListBox" id="search_results">
+                                            <property name="selection-mode">none</property>
+                                            <property name="valign">start</property>
+                                            <signal name="row-activated" 
handler="search_result_activated_cb" swapped="yes"/>
+                                            <signal name="map" handler="search_results_map" swapped="yes"/>
+                                            <signal name="unmap" handler="search_results_unmap" 
swapped="yes"/>
+                                            <style>
+                                              <class name="boxed-list"/>
+                                            </style>
+                                          </object>
+                                        </child>
                                       </object>
                                     </child>
                                   </object>
-                                </child>
+                                </property>
                               </object>
-                            </property>
-                          </object>
-                        </child>
-                        <child>
-                          <object class="GtkStackPage">
-                            <property name="name">no-results</property>
-                            <property name="child">
-                              <object class="AdwStatusPage">
-                                <property name="icon-name">edit-find-symbolic</property>
-                                <property name="title" translatable="yes">No Results Found</property>
-                                <property name="description" translatable="yes">Try a different 
search.</property>
+                            </child>
+                            <child>
+                              <object class="GtkStackPage">
+                                <property name="name">no-results</property>
+                                <property name="child">
+                                  <object class="AdwStatusPage">
+                                    <property name="icon-name">edit-find-symbolic</property>
+                                    <property name="title" translatable="yes">No Results Found</property>
+                                    <property name="description" translatable="yes">Try a different 
search.</property>
+                                  </object>
+                                </property>
                               </object>
-                            </property>
+                            </child>
                           </object>
-                        </child>
+                        </property>
                       </object>
-                    </property>
+                    </child>
                   </object>
                 </child>
               </object>
diff --git a/tests/test-preferences-window.c b/tests/test-preferences-window.c
index d6dabc4f..b6a44974 100644
--- a/tests/test-preferences-window.c
+++ b/tests/test-preferences-window.c
@@ -6,7 +6,6 @@
 
 #include <adwaita.h>
 
-
 static void
 test_adw_preferences_window_add_remove (void)
 {
@@ -24,6 +23,20 @@ test_adw_preferences_window_add_remove (void)
   g_assert_finalize_object (window);
 }
 
+static void
+test_adw_preferences_window_add_toast (void)
+{
+  AdwPreferencesWindow *window = ADW_PREFERENCES_WINDOW (adw_preferences_window_new ());
+  AdwToast *toast = adw_toast_new ("Test notification");
+
+  g_assert_nonnull (window);
+  g_assert_nonnull (toast);
+
+  adw_preferences_window_add_toast (window, g_object_ref (toast));
+
+  g_assert_finalize_object (window);
+  g_assert_finalize_object (toast);
+}
 
 int
 main (int   argc,
@@ -33,6 +46,7 @@ main (int   argc,
   adw_init ();
 
   g_test_add_func("/Adwaita/PreferencesWindow/add_remove", test_adw_preferences_window_add_remove);
+  g_test_add_func("/Adwaita/PreferencesWindow/add_toast", test_adw_preferences_window_add_toast);
 
   return g_test_run();
 }


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