[gnome-games/wip/exalm/libhandy: 7/8] preferences-window: Implement back swipe for subpages



commit 951f6934ac217bcae88d04efa67f3cf78df7b720
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Wed Feb 26 00:26:09 2020 +0500

    preferences-window: Implement back swipe for subpages
    
    Now that libhandy has HdyDeck, it's possible to have swipes there as well.

 data/ui/preferences-window.ui  | 15 +++++++++------
 src/ui/preferences-page.vala   |  2 +-
 src/ui/preferences-window.vala | 39 +++++++++++++++++++++------------------
 3 files changed, 31 insertions(+), 25 deletions(-)
---
diff --git a/data/ui/preferences-window.ui b/data/ui/preferences-window.ui
index 92f4ec42..2a6ca3d1 100644
--- a/data/ui/preferences-window.ui
+++ b/data/ui/preferences-window.ui
@@ -10,10 +10,8 @@
       <object class="HdyTitleBar" id="titlebar">
         <property name="visible">True</property>
         <child>
-          <object class="GtkStack" id="titlebar_stack">
+          <object class="HdyDeck" id="titlebar_deck">
             <property name="visible">True</property>
-            <property name="transition-type">slide-left-right</property>
-            <property name="transition-duration">250</property>
             <child>
               <object class="HdyLeaflet" id="titlebar_leaflet">
                 <property name="visible">True</property>
@@ -106,10 +104,9 @@
       </object>
     </child>
     <child>
-      <object class="GtkStack" id="main_stack">
+      <object class="HdyDeck" id="content_deck">
         <property name="visible">True</property>
-        <property name="transition-type">slide-left-right</property>
-        <property name="transition-duration">250</property>
+        <property name="can-swipe-back" bind-source="titlebar" bind-property="selection-mode" 
bind-flags="sync-create|invert-boolean"/>
         <signal name="notify::transition-running" handler="subpage_transition_finished"/>
         <child>
           <object class="HdyLeaflet" id="content_leaflet">
@@ -197,6 +194,12 @@
       <headerbar name="right_header_bar"/>
     </headerbars>
   </object>
+  <object class="HdySwipeGroup">
+    <swipeables>
+      <swipeable name="titlebar_deck"/>
+      <swipeable name="content_deck"/>
+    </swipeables>
+  </object>
   <object class="HdySwipeGroup">
     <swipeables>
       <swipeable name="titlebar_leaflet"/>
diff --git a/src/ui/preferences-page.vala b/src/ui/preferences-page.vala
index f34449bf..c96e242a 100644
--- a/src/ui/preferences-page.vala
+++ b/src/ui/preferences-page.vala
@@ -2,7 +2,7 @@
 
 [GtkTemplate (ui = "/org/gnome/Games/ui/preferences-page.ui")]
 private class Games.PreferencesPage : Gtk.Bin, Gtk.Buildable {
-       public PreferencesSubpage subpage { get; protected set; }
+       public PreferencesSubpage subpage { get; set; }
        public string title { get; construct set; }
 
        [GtkChild]
diff --git a/src/ui/preferences-window.vala b/src/ui/preferences-window.vala
index 5b0bb998..41edf6f9 100644
--- a/src/ui/preferences-window.vala
+++ b/src/ui/preferences-window.vala
@@ -5,15 +5,13 @@ private class Games.PreferencesWindow : Gtk.Window {
        [GtkChild]
        private Hdy.TitleBar titlebar;
        [GtkChild]
-       private Gtk.Stack titlebar_stack;
-       [GtkChild]
        private Hdy.Leaflet titlebar_leaflet;
        [GtkChild]
        private Gtk.Box titlebar_subpage_box;
        [GtkChild]
        private Gtk.HeaderBar right_header_bar;
        [GtkChild]
-       private Gtk.Stack main_stack;
+       private Hdy.Deck content_deck;
        [GtkChild]
        private Hdy.Leaflet content_leaflet;
        [GtkChild]
@@ -37,9 +35,7 @@ private class Games.PreferencesWindow : Gtk.Window {
                                return;
 
                        if (subpage != null) {
-                               previous_subpage = subpage;
-                               main_stack.visible_child = content_leaflet;
-                               titlebar_stack.visible_child = titlebar_leaflet;
+                               content_deck.navigate (Hdy.NavigationDirection.BACK);
                                selection_mode_binding.unbind ();
                        }
 
@@ -47,15 +43,13 @@ private class Games.PreferencesWindow : Gtk.Window {
                                var header_bar = value.header_bar;
 
                                content_subpage_box.add (value);
-                               main_stack.visible_child = content_subpage_box;
+                               titlebar_subpage_box.add (header_bar);
 
                                selection_mode_binding = value.bind_property ("request-selection-mode",
                                                                              titlebar, "selection-mode",
                                                                              BindingFlags.SYNC_CREATE);
 
-                               titlebar_subpage_box.add (header_bar);
-                               titlebar_stack.visible_child = titlebar_subpage_box;
-
+                               content_deck.navigate (Hdy.NavigationDirection.FORWARD);
                                content_leaflet.navigate (Hdy.NavigationDirection.FORWARD);
                        }
 
@@ -63,9 +57,6 @@ private class Games.PreferencesWindow : Gtk.Window {
                }
        }
 
-       // The previous subpage instance must be kept around during the transition
-       private PreferencesSubpage previous_subpage;
-
        private Binding subpage_binding;
        private Binding selection_mode_binding;
 
@@ -82,6 +73,12 @@ private class Games.PreferencesWindow : Gtk.Window {
 
        private void update_ui () {
                var page = stack.visible_child as PreferencesPage;
+
+               if (subpage_binding != null) {
+                       subpage_binding.unbind ();
+                       subpage_binding = null;
+               }
+
                if (page == null) {
                        right_header_bar.title = "";
                        subpage = null;
@@ -92,17 +89,22 @@ private class Games.PreferencesWindow : Gtk.Window {
                right_header_bar.title = page.title;
 
                subpage_binding = page.bind_property ("subpage", this, "subpage",
-                                                     BindingFlags.SYNC_CREATE);
+                                                     BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL);
        }
 
        [GtkCallback]
        public void subpage_transition_finished (Object object, ParamSpec param) {
-               if (main_stack.transition_running || previous_subpage == null)
+               if (content_deck.transition_running ||
+                   content_deck.visible_child != content_leaflet)
                        return;
 
-               content_subpage_box.remove (previous_subpage);
-               titlebar_subpage_box.remove (previous_subpage.header_bar);
-               previous_subpage = null;
+               foreach (var child in content_subpage_box.get_children ())
+                       content_subpage_box.remove (child);
+
+               foreach (var child in titlebar_subpage_box.get_children ())
+                       titlebar_subpage_box.remove (child);
+
+               subpage = null;
        }
 
        [GtkCallback]
@@ -126,6 +128,7 @@ private class Games.PreferencesWindow : Gtk.Window {
                        stack.transition_type = Gtk.StackTransitionType.CROSSFADE;
        }
 
+       [GtkCallback]
        private void update_header_group () {
                var folded = content_leaflet.folded;
                var visible_header_bar = titlebar_leaflet.visible_child as Gtk.HeaderBar;


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