[gnome-games] games-page: Add support for selection-mode



commit 292ee9e1274e07a27f80326908e804de3497e19a
Author: Neville <nevilleantony98 gmail com>
Date:   Fri Jun 5 19:55:23 2020 +0530

    games-page: Add support for selection-mode
    
    It now stores the selected games in a GenericSet. Whenever a game's
    checked state changes it adds/removes the corresponding GameIconView
    into the selected_games list.
    
    When in selection-mode activating the GameIconView will toggle the
    game's checked state.
    
    All or none games can be selected using the select_all/none ()
    When in PlatformsPage all games of the currently viewed platform
    can be selected by calling select_all () in PlatformsPage.
    
    select_none () will remove every game in selected_games regardless
    of the currenlty selected platform.
    
    This will be used in the upcoming commits to implement selection-mode
    in CollectionView.

 data/ui/platforms-page.ui  |  4 +++-
 src/ui/games-page.vala     | 36 +++++++++++++++++++++++++++++++++++-
 src/ui/platforms-page.vala | 18 +++++++++++++++++-
 3 files changed, 55 insertions(+), 3 deletions(-)
---
diff --git a/data/ui/platforms-page.ui b/data/ui/platforms-page.ui
index 21c5b828..89f9ae22 100644
--- a/data/ui/platforms-page.ui
+++ b/data/ui/platforms-page.ui
@@ -6,12 +6,13 @@
     <property name="expand">True</property>
     <signal name="map" after="yes" handler="on_map"/>
     <signal name="unmap" after="no" handler="on_unmap"/>
-    <signal name="notify::is-folded" handler="update_selection_mode"/>
+    <signal name="notify::is-folded" handler="on_folded_changed"/>
     <child>
       <object class="HdyLeaflet" id="leaflet">
         <property name="visible">True</property>
         <property name="can-swipe-back">True</property>
         <property name="mode-transition-duration">200</property>
+        <property name="can-swipe-back" bind-source="GamesPlatformsPage" bind-property="is-selection-mode" 
bind-flags="invert-boolean"/>
         <signal name="notify::folded" handler="on_leaflet_folded_changed"/>
         <signal name="notify::visible-child" handler="on_visible_child_changed"/>
         <child>
@@ -54,6 +55,7 @@
           <object class="GamesGamesPage" id="games_page">
             <property name="visible">True</property>
             <property name="expand">True</property>
+            <property name="is-selection-mode" bind-source="GamesPlatformsPage" 
bind-property="is-selection-mode"/>
             <signal name="game-activated" handler="on_game_activated"/>
           </object>
         </child>
diff --git a/src/ui/games-page.vala b/src/ui/games-page.vala
index 974b3201..97e00997 100644
--- a/src/ui/games-page.vala
+++ b/src/ui/games-page.vala
@@ -5,6 +5,7 @@ private class Games.GamesPage : Gtk.Bin {
        public signal void game_activated (Game game);
 
        private string[] filtering_terms;
+       public bool is_selection_mode { get; set; }
 
        public delegate bool GameFilter (Game game);
        private unowned GameFilter? game_filter;
@@ -21,6 +22,7 @@ private class Games.GamesPage : Gtk.Bin {
                }
        }
 
+       private GenericSet<GameIconView> selected_games;
        private Binding window_active_binding;
        private bool _is_active;
        public bool is_active {
@@ -51,6 +53,8 @@ private class Games.GamesPage : Gtk.Bin {
        construct {
                flow_box.max_children_per_line = uint.MAX;
                flow_box.set_filter_func (filter_box);
+
+               selected_games = new GenericSet<GameIconView> (GameIconView.hash, GameIconView.equal);
        }
 
        [GtkCallback]
@@ -129,6 +133,21 @@ private class Games.GamesPage : Gtk.Bin {
                flow_box.unselect_all ();
        }
 
+       public void select_none () {
+               foreach (var game_icon_view in selected_games.get_values ())
+                       game_icon_view.checked = false;
+       }
+
+       public void select_all () {
+               foreach (var child in flow_box.get_children ()) {
+                       var game_icon_view = child as GameIconView;
+                       if (game_filter == null)
+                               game_icon_view.checked = filtering_terms == null || filter_game 
(game_icon_view.game);
+                       else if (filter_game (game_icon_view.game))
+                               game_icon_view.checked = true;
+               }
+       }
+
        [GtkCallback]
        private bool on_gamepad_browse (Gtk.DirectionType direction) {
                if (!has_game_selected ())
@@ -160,11 +179,26 @@ private class Games.GamesPage : Gtk.Bin {
        private void on_child_activated (Gtk.FlowBoxChild child) {
                var game_view = child as GameIconView;
 
+               if (is_selection_mode) {
+                       game_view.checked = !game_view.checked;
+                       return;
+               }
+
                game_activated (game_view.game);
        }
 
        private Gtk.Widget add_game (Object item) {
-               return new GameIconView (item as Game);
+               var game_icon = new GameIconView (item as Game);
+               bind_property ("is-selection-mode", game_icon, "is-selection-mode", BindingFlags.DEFAULT);
+
+               game_icon.notify["checked"].connect (() => {
+                       if (game_icon.checked)
+                               selected_games.add (game_icon);
+                       else
+                               selected_games.remove (game_icon);
+               });
+
+               return game_icon;
        }
 
        public void invalidate_filter () {
diff --git a/src/ui/platforms-page.vala b/src/ui/platforms-page.vala
index d38ffecd..3cb89f3e 100644
--- a/src/ui/platforms-page.vala
+++ b/src/ui/platforms-page.vala
@@ -48,6 +48,7 @@ private class Games.PlatformsPage : Gtk.Bin {
        public bool is_folded { get; set; }
        public bool is_subview_open { get; set; }
        public string subview_title { get; set; }
+       public bool is_selection_mode { get; set; }
 
        construct {
                games_page.set_game_filter (filter_game);
@@ -144,6 +145,14 @@ private class Games.PlatformsPage : Gtk.Bin {
                return gamepad_browse.gamepad_absolute_axis_event (event);
        }
 
+       public void select_all () {
+               games_page.select_all ();
+       }
+
+       public void select_none () {
+               games_page.select_none ();
+       }
+
        [GtkCallback]
        private bool on_gamepad_browse (Gtk.DirectionType direction) {
                if (list_box.get_selected_rows ().length () == 0) {
@@ -254,7 +263,6 @@ private class Games.PlatformsPage : Gtk.Bin {
                return item;
        }
 
-       [GtkCallback]
        private void update_selection_mode () {
                if (!is_folded || has_used_gamepad)
                        list_box.selection_mode = Gtk.SelectionMode.SINGLE;
@@ -263,6 +271,14 @@ private class Games.PlatformsPage : Gtk.Bin {
                select_current_row ();
        }
 
+       [GtkCallback]
+       private void on_folded_changed () {
+               update_selection_mode ();
+
+               if (is_selection_mode && is_folded)
+                       leaflet.navigate (Hdy.NavigationDirection.FORWARD);
+       }
+
        [GtkCallback]
        private void on_leaflet_folded_changed () {
                is_folded = leaflet.folded;


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