[gnome-music/wip/mschraal/window-selection-mode: 4/7] Add selected-items-count property



commit 608eaeeaa5df0efb1bc88ddcd0a84df982c62e49
Author: Marinus Schraal <mschraal gnome org>
Date:   Sun Sep 2 09:06:11 2018 +0200

    Add selected-items-count property
    
    Add a selected-items-count property to window and the different views &
    widgets. Bind the properties to HeaderBar and SelectionToolbar as well, to
    automate selection-mode message passing.

 gnomemusic/views/albumsview.py    | 4 ++--
 gnomemusic/views/artistsview.py   | 4 +---
 gnomemusic/views/baseview.py      | 9 ++++++++-
 gnomemusic/views/searchview.py    | 3 ++-
 gnomemusic/views/songsview.py     | 3 ++-
 gnomemusic/widgets/albumwidget.py | 7 +++++--
 gnomemusic/window.py              | 7 +++++++
 7 files changed, 27 insertions(+), 10 deletions(-)
---
diff --git a/gnomemusic/views/albumsview.py b/gnomemusic/views/albumsview.py
index 4cbbb0df..a2decd60 100644
--- a/gnomemusic/views/albumsview.py
+++ b/gnomemusic/views/albumsview.py
@@ -157,7 +157,7 @@ class AlbumsView(BaseView):
                 and child.props.media in self.albums_selected):
             self.albums_selected.remove(child.props.media)
 
-        self._update_header_from_selection(len(self.albums_selected))
+        self.props.selected_items_count = len(self.albums_selected)
 
     @log
     def _get_selected_album_songs(self):
@@ -171,7 +171,7 @@ class AlbumsView(BaseView):
         if item:
             self.items_selected.append(item)
         if remaining == 0:
-            if self.albums_index < len(self.albums_selected):
+            if self.albums_index < self.props.selected_items_count:
                 self._get_selected_album_songs()
             else:
                 self.items_selected_callback(self.items_selected)
diff --git a/gnomemusic/views/artistsview.py b/gnomemusic/views/artistsview.py
index fc63108a..9f46c690 100644
--- a/gnomemusic/views/artistsview.py
+++ b/gnomemusic/views/artistsview.py
@@ -185,7 +185,7 @@ class ArtistsView(BaseView):
             if row.props.selected:
                 selected_artists += 1
 
-        self._update_header_from_selection(selected_artists)
+        self.props.selected_items_count = selected_artists
 
     @log
     def _on_selection_mode_changed(self, widget, data=None):
@@ -209,12 +209,10 @@ class ArtistsView(BaseView):
     @log
     def select_all(self):
         self._toggle_all_selection(True)
-        self._update_header_from_selection(len(self._sidebar))
 
     @log
     def unselect_all(self):
         self._toggle_all_selection(False)
-        self._update_header_from_selection(0)
 
     @log
     def get_selected_songs(self, callback):
diff --git a/gnomemusic/views/baseview.py b/gnomemusic/views/baseview.py
index c0a69c1d..ef546560 100644
--- a/gnomemusic/views/baseview.py
+++ b/gnomemusic/views/baseview.py
@@ -34,6 +34,8 @@ class BaseView(Gtk.Stack):
 
     _now_playing_icon_name = 'media-playback-start-symbolic'
     _error_icon_name = 'dialog-error-symbolic'
+
+    selected_items_count = GObject.Property(type=int, default=0, minimum=0)
     selection_mode = GObject.Property(type=bool, default=False)
 
     def __repr__(self):
@@ -99,6 +101,8 @@ class BaseView(Gtk.Stack):
 
         self._init = False
         grilo.connect('ready', self._on_grilo_ready)
+        self.connect(
+            'notify::selected-items-count', self._update_selected_items_count)
         self.connect('notify::selection-mode', self._on_selection_mode_changed)
         grilo.connect('changes-pending', self._on_changes_pending)
 
@@ -138,7 +142,10 @@ class BaseView(Gtk.Stack):
     def _update_header_from_selection(self, n_items):
         """Updates header during item selection."""
         self._selection_toolbar.props.items_selected = n_items
-        self._header_bar.props.items_selected = n_items
+
+    @log
+    def _update_selected_items_count(self, klass, value):
+        self._update_header_from_selection(self.props.selected_items_count)
 
     @log
     def _populate(self, data=None):
diff --git a/gnomemusic/views/searchview.py b/gnomemusic/views/searchview.py
index 06b21f2b..cc74567b 100644
--- a/gnomemusic/views/searchview.py
+++ b/gnomemusic/views/searchview.py
@@ -188,7 +188,8 @@ class SearchView(BaseView):
             iter_ = self.model.get_iter(path)
             self.model[iter_][6] = not self.model[iter_][6]
             selected_iters = self._get_selected_iters()
-            self._update_header_from_selection(len(selected_iters))
+
+            self.props.selected_items_count = len(selected_iters)
 
     @log
     def _get_selected_iters(self):
diff --git a/gnomemusic/views/songsview.py b/gnomemusic/views/songsview.py
index 16520105..22451bf0 100644
--- a/gnomemusic/views/songsview.py
+++ b/gnomemusic/views/songsview.py
@@ -225,7 +225,8 @@ class SongsView(BaseView):
                 *event.get_coords())
             iter_ = self.model.get_iter(path)
             self.model[iter_][6] = not self.model[iter_][6]
-            self._update_header_from_selection(len(self.get_selected_songs()))
+
+            self.props.selected_items_count = len(self.get_selected_songs())
 
     @log
     def _update_model(self, player, position):
diff --git a/gnomemusic/widgets/albumwidget.py b/gnomemusic/widgets/albumwidget.py
index a6d309c5..ebcf2fdf 100644
--- a/gnomemusic/widgets/albumwidget.py
+++ b/gnomemusic/widgets/albumwidget.py
@@ -54,6 +54,7 @@ class AlbumWidget(Gtk.EventBox):
     _running_info_label = Gtk.Template.Child()
     _title_label = Gtk.Template.Child()
 
+    selected_items_count = GObject.Property(type=int, default=0, minimum=0)
     selection_mode = GObject.Property(type=bool, default=False)
 
     _duration = 0
@@ -92,6 +93,9 @@ class AlbumWidget(Gtk.EventBox):
             GObject.BindingFlags.BIDIRECTIONAL |
             GObject.BindingFlags.SYNC_CREATE)
 
+        self.bind_property(
+            'selected-items-count', self._parent_view, 'selected-items-count')
+
         self.show_all()
 
     @log
@@ -179,8 +183,7 @@ class AlbumWidget(Gtk.EventBox):
     @log
     def _on_selection_changed(self, widget):
         n_items = len(self._disc_listbox.get_selected_items())
-        self._selection_toolbar.props.items_selected = n_items
-        self._header_bar.items_selected = n_items
+        self.props.selected_items_count = n_items
 
     @log
     def _create_disc_box(self, disc_nr, disc_songs):
diff --git a/gnomemusic/window.py b/gnomemusic/window.py
index ed2dd09d..eda1bfb8 100644
--- a/gnomemusic/window.py
+++ b/gnomemusic/window.py
@@ -59,6 +59,7 @@ playlists = Playlists.get_default()
 
 class Window(Gtk.ApplicationWindow):
 
+    selected_items_count = GObject.Property(type=int, default=0, minimum=0)
     selection_mode = GObject.Property(type=bool, default=False)
 
     def __repr__(self):
@@ -164,6 +165,8 @@ class Window(Gtk.ApplicationWindow):
             can_focus=False)
 
         self.connect('notify::selection-mode', self._on_selection_mode_changed)
+        self.bind_property(
+            'selected-items-count', self._headerbar, 'items-selected')
         self.bind_property(
             'selection-mode', self._headerbar, 'selection-mode',
             GObject.BindingFlags.BIDIRECTIONAL |
@@ -263,6 +266,10 @@ class Window(Gtk.ApplicationWindow):
         self.views[View.PLAYLIST] = PlaylistView(self, self.player)
         self.views[View.SEARCH] = SearchView(self, self.player)
 
+        for view in [View.ALBUM, View.ARTIST, View.SONG, View.SEARCH]:
+            self.views[view].bind_property(
+                'selected-items-count', self, 'selected-items-count')
+
         # empty view has already been created in self._setup_view starting at
         # View.ALBUM
         # empty view state is changed once album view is visible to prevent it


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