[gnome-music/wip/mschraal/albumview-covers-on-demand-3-34: 1/2] albumsview: Load covers on demand



commit ca900f30739ce043a415a65831303bac1e019fc7
Author: Marinus Schraal <mschraal gnome org>
Date:   Fri Oct 18 17:03:01 2019 +0200

    albumsview: Load covers on demand
    
    Loading all covers on startup is a very demanding task, especially on a
    fresh install. Previously Music was using a delayed loading hack, but it
    was still resource intensive and suboptimal.
    
    Load covers on demand instead.
    
    Closes #250

 gnomemusic/views/albumsview.py   | 56 +++++++++++++++++++++++++++++++++++++++-
 gnomemusic/views/searchview.py   |  1 +
 gnomemusic/widgets/albumcover.py | 23 ++++++++++-------
 3 files changed, 70 insertions(+), 10 deletions(-)
---
diff --git a/gnomemusic/views/albumsview.py b/gnomemusic/views/albumsview.py
index c0f70a91..900f32b2 100644
--- a/gnomemusic/views/albumsview.py
+++ b/gnomemusic/views/albumsview.py
@@ -22,8 +22,10 @@
 # code, but you are not obligated to do so.  If you do not wish to do so,
 # delete this exception statement from your version.
 
+import math
+
 from gettext import gettext as _
-from gi.repository import GObject, Gtk
+from gi.repository import GLib, GObject, Gtk
 
 from gnomemusic.widgets.headerbar import HeaderBar
 from gnomemusic.widgets.albumcover import AlbumCover
@@ -62,6 +64,8 @@ class AlbumsView(Gtk.Stack):
 
         self._window = application.props.window
         self._headerbar = self._window._headerbar
+        self._adjustment_timeout_id = None
+        self._viewport = self._scrolled_window.get_child()
 
         model = self._window._app.props.coremodel.props.albums_sort
         self._flowbox.bind_model(model, self._create_widget)
@@ -83,8 +87,58 @@ class AlbumsView(Gtk.Stack):
         self.connect(
             "notify::search-mode-active", self._on_search_mode_changed)
 
+        self._all_albums.props.vadjustment.connect(
+            "value-changed", self._on_vadjustment_changed)
+        self._all_albums.props.vadjustment.connect(
+            "changed", self._on_vadjustment_changed)
+
         self.show_all()
 
+    def _on_vadjustment_changed(self, adjustment):
+        if self._adjustment_timeout_id is not None:
+            GLib.source_remove(self._adjustment_timeout_id)
+            self._adjustment_timeout_id = None
+
+        self._adjustment_timeout_id = GLib.timeout_add(
+            200, self._retrieve_covers, adjustment.props.value,
+            priority=GLib.PRIORITY_LOW)
+
+    def _retrieve_covers(self, old_adjustment):
+        adjustment = self._all_albums.props.vadjustment.props.value
+
+        if old_adjustment != adjustment:
+            return GLib.SOURCE_CONTINUE
+
+        first_cover = self._flowbox.get_child_at_index(0)
+        cover_size, _ = first_cover.get_allocated_size()
+        viewport_size, _ = self._viewport.get_allocated_size()
+
+        h_space = self._flowbox.get_column_spacing()
+        v_space = self._flowbox.get_row_spacing()
+        nr_cols = (
+            (viewport_size.width + h_space) // (cover_size.width + h_space))
+
+        top_left_cover = self._flowbox.get_child_at_index(
+            nr_cols * (adjustment // (cover_size.height + v_space)))
+
+        covers_col = math.ceil(viewport_size.width / cover_size.width)
+        covers_row = math.ceil(viewport_size.height / cover_size.height)
+
+        children = self._flowbox.get_children()
+        retrieve_list = []
+        for i, albumcover in enumerate(children):
+            if top_left_cover == albumcover:
+                retrieve_covers = covers_row * covers_col
+                retrieve_list = children[i:i + retrieve_covers]
+                break
+
+        for albumcover in retrieve_list:
+            albumcover.retrieve()
+
+        self._adjustment_timeout_id = None
+
+        return GLib.SOURCE_REMOVE
+
     def _on_selection_mode_changed(self, widget, data=None):
         if not self.props.selection_mode:
             self.unselect_all()
diff --git a/gnomemusic/views/searchview.py b/gnomemusic/views/searchview.py
index 0cc31c56..40ed115b 100644
--- a/gnomemusic/views/searchview.py
+++ b/gnomemusic/views/searchview.py
@@ -160,6 +160,7 @@ class SearchView(Gtk.Stack):
 
     def _create_album_widget(self, corealbum):
         album_widget = AlbumCover(corealbum)
+        album_widget.retrieve()
 
         self.bind_property(
             "selection-mode", album_widget, "selection-mode",
diff --git a/gnomemusic/widgets/albumcover.py b/gnomemusic/widgets/albumcover.py
index ae6b82f8..7b9cfaa7 100644
--- a/gnomemusic/widgets/albumcover.py
+++ b/gnomemusic/widgets/albumcover.py
@@ -24,7 +24,7 @@
 
 import gi
 gi.require_version('Grl', '0.3')
-from gi.repository import Gdk, GLib, GObject, Gtk
+from gi.repository import Gdk, GObject, Gtk
 
 from gnomemusic import log
 from gnomemusic.albumartcache import Art
@@ -68,6 +68,7 @@ class AlbumCover(Gtk.FlowBoxChild):
         AlbumCover._nr_albums += 1
 
         self._corealbum = corealbum
+        self._retrieved = False
 
         self._tooltip = TwoLineTip()
 
@@ -96,16 +97,20 @@ class AlbumCover(Gtk.FlowBoxChild):
 
         self.show()
 
-        # FIXME: To work around slow updating of the albumsview,
-        # load album covers with a fixed delay. This results in a
-        # quick first show with a placeholder cover and then a
-        # reasonably responsive view while loading the actual
-        # covers.
         self._update_cover_id = self._cover_stack.connect(
             "updated", self._on_cover_stack_updated)
-        GLib.timeout_add(
-            50 * self._nr_albums, self._cover_stack.update, self._corealbum,
-            priority=GLib.PRIORITY_LOW)
+
+    def retrieve(self):
+        """Start retrieving the actual album cover
+
+        Cover retrieval is an expensive operation, so use this call to
+        start the retrieval process when needed.
+        """
+        if self._retrieved:
+            return
+
+        self._retrieved = True
+        self._cover_stack.update(self._corealbum)
 
     @GObject.Property(type=CoreAlbum, flags=GObject.ParamFlags.READABLE)
     def corealbum(self):


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