[gnome-music/wip/mschraal/searchview-selection-fixes-3-34: 3/3] window: Move selection handling to views



commit 4cd99f3143bd54d238a97efc8ac5a2215089f241
Author: Marinus Schraal <mschraal gnome org>
Date:   Tue Feb 11 15:41:58 2020 +0100

    window: Move selection handling to views
    
    The select all/none actions of views with children (AlbumsView &
    SearchView) was previously handled in Window. As SearchView became more
    complicated this is no longer feasible.
    
    Instead let AlbumsView and SearchView pass the given actions to their
    active child if active.
    
    Closes: #364

 gnomemusic/views/albumsview.py | 14 ++++++----
 gnomemusic/views/searchview.py | 61 ++++++++++++++++++++++++++++++++----------
 gnomemusic/window.py           | 14 +++-------
 3 files changed, 59 insertions(+), 30 deletions(-)
---
diff --git a/gnomemusic/views/albumsview.py b/gnomemusic/views/albumsview.py
index ce1dc37d..3a8148b9 100644
--- a/gnomemusic/views/albumsview.py
+++ b/gnomemusic/views/albumsview.py
@@ -199,13 +199,17 @@ class AlbumsView(Gtk.Stack):
         self._headerbar.props.subtitle = corealbum.props.artist
 
     def _toggle_all_selection(self, selected):
-        """
-        Selects or unselects all items without sending the notify::active
-        signal for performance purposes.
+        """Selects or deselects all items.
         """
         with self._window._app.props.coreselection.freeze_notify():
-            for child in self._flowbox.get_children():
-                child.props.selected = selected
+            if self.get_visible_child() == self._album_widget:
+                if selected is True:
+                    self._album_widget.select_all()
+                else:
+                    self._album_widget.select_none()
+            else:
+                for child in self._flowbox.get_children():
+                    child.props.selected = selected
 
     def select_all(self):
         self._toggle_all_selection(True)
diff --git a/gnomemusic/views/searchview.py b/gnomemusic/views/searchview.py
index 0e12f410..1e7f823b 100644
--- a/gnomemusic/views/searchview.py
+++ b/gnomemusic/views/searchview.py
@@ -22,7 +22,9 @@
 # code, but you are not obligated to do so.  If you do not wish to do so,
 # delete this exception statement from your version.
 
+from enum import IntEnum
 from gettext import gettext as _
+
 from gi.repository import Gdk, GObject, Gtk
 
 from gnomemusic import log
@@ -45,9 +47,19 @@ class SearchView(Gtk.Stack):
 
     __gtype_name__ = "SearchView"
 
+    class State(IntEnum):
+        """The different states of SearchView
+        """
+        MAIN = 0
+        ALL_ALBUMS = 1
+        ALL_ARTISTS = 2
+        ALBUM = 3
+        ARTIST = 4
+
     search_state = GObject.Property(type=int, default=Search.State.NONE)
     selected_items_count = GObject.Property(type=int, default=0, minimum=0)
     selection_mode = GObject.Property(type=bool, default=False)
+    state = GObject.Property(type=int, default=State.MAIN)
 
     _album_header = Gtk.Template.Child()
     _album_flowbox = Gtk.Template.Child()
@@ -324,6 +336,7 @@ class SearchView(Gtk.Stack):
         # Update and display the album widget if not in selection mode
         self._album_widget.update(corealbum)
 
+        self.props.state = SearchView.State.ALBUM
         self._headerbar.props.state = HeaderBar.State.SEARCH
         self._headerbar.props.title = corealbum.props.title
         self._headerbar.props.subtitle = corealbum.props.artist
@@ -352,6 +365,7 @@ class SearchView(Gtk.Stack):
             "selection-mode", artist_albums_widget, "selection-mode",
             GObject.BindingFlags.BIDIRECTIONAL)
 
+        self.props.state = SearchView.State.ARTIST
         self._headerbar.props.state = HeaderBar.State.SEARCH
         self._headerbar.props.title = coreartist.props.artist
         self._headerbar.props.subtitle = None
@@ -361,6 +375,7 @@ class SearchView(Gtk.Stack):
 
     @Gtk.Template.Callback()
     def _on_all_artists_clicked(self, widget, event, user_data=None):
+        self.props.state = SearchView.State.ALL_ARTISTS
         self._headerbar.props.state = HeaderBar.State.SEARCH
         self._headerbar.props.title = _("Artists Results")
         self._headerbar.props.subtitle = None
@@ -375,6 +390,7 @@ class SearchView(Gtk.Stack):
 
     @Gtk.Template.Callback()
     def _on_all_albums_clicked(self, widget, event, user_data=None):
+        self.props.state = SearchView.State.ALL_ALBUMS
         self._headerbar.props.state = HeaderBar.State.SEARCH
         self._headerbar.props.title = _("Albums Results")
         self._headerbar.props.subtitle = None
@@ -388,20 +404,36 @@ class SearchView(Gtk.Stack):
         self.props.search_mode_active = False
 
     def _select_all(self, value):
-        with self._model.freeze_notify():
-            def song_select(child):
-                song_widget = child.get_child()
-                song_widget.props.selected = value
-
-            def album_select(child):
-                child.props.selected = value
-
-            def artist_select(child):
-                child.props.selected = value
-
-            self._songs_listbox.foreach(song_select)
-            self._album_flowbox.foreach(album_select)
-            self._artist_flowbox.foreach(artist_select)
+        def child_select(child):
+            child.props.selected = value
+
+        if self.props.state == SearchView.State.MAIN:
+            with self._model.freeze_notify():
+                def song_select(child):
+                    song_widget = child.get_child()
+                    song_widget.props.coresong.props.selected = value
+
+                self._songs_listbox.foreach(song_select)
+                self._album_flowbox.foreach(child_select)
+                self._artist_flowbox.foreach(child_select)
+        elif self.props.state == SearchView.State.ALL_ALBUMS:
+            with self._model.freeze_notify():
+                self._album_all_flowbox.foreach(child_select)
+        elif self.props.state == SearchView.State.ALL_ARTISTS:
+            with self._model.freeze_notify():
+                self._artist_all_flowbox.foreach(child_select)
+        elif self.props.state == SearchView.State.ALBUM:
+            view = self.get_visible_child()
+            if value is True:
+                view.select_all()
+            else:
+                view.select_none()
+        elif self.props.state == SearchView.State.ARTIST:
+            view = self.get_visible_child().get_child().get_child()
+            if value is True:
+                view.select_all()
+            else:
+                view.select_none()
 
     def select_all(self):
         self._select_all(True)
@@ -420,6 +452,7 @@ class SearchView(Gtk.Stack):
 
         self.set_visible_child(self._search_results)
         self.props.search_mode_active = True
+        self.props.state = SearchView.State.MAIN
         self._headerbar.props.state = HeaderBar.State.MAIN
 
     @log
diff --git a/gnomemusic/window.py b/gnomemusic/window.py
index b6c7525f..87c17257 100644
--- a/gnomemusic/window.py
+++ b/gnomemusic/window.py
@@ -305,23 +305,15 @@ class Window(Gtk.ApplicationWindow):
     def _select_all(self, action=None, param=None):
         if not self.props.selection_mode:
             return
-        if self._headerbar.props.state == HeaderBar.State.MAIN:
-            view = self.props.active_view
-        else:
-            view = self.props.active_view.get_visible_child()
 
-        view.select_all()
+        self.props.active_view.select_all()
 
     @log
     def _select_none(self, action=None, param=None):
         if not self.props.selection_mode:
             return
-        if self._headerbar.props.state == HeaderBar.State.MAIN:
-            view = self.props.active_view
-            view.unselect_all()
-        else:
-            view = self.props.active_view.get_visible_child()
-            view.select_none()
+
+        self.props.active_view.unselect_all()
 
     @log
     def _on_key_press(self, widget, event):


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