[gnome-music/wip/jfelder/gtk4-v3: 21/89] artistsview: Port sidebar to GtkListView




commit 2a4df6138ebe41f5d97c8c18b911033f2de43e84
Author: Jean Felder <jfelder src gnome org>
Date:   Wed Feb 2 16:48:07 2022 +0100

    artistsview: Port sidebar to GtkListView

 data/ui/ArtistsView.ui          |  6 ++--
 gnomemusic/views/artistsview.py | 63 +++++++++++++++++++++++++----------------
 2 files changed, 42 insertions(+), 27 deletions(-)
---
diff --git a/data/ui/ArtistsView.ui b/data/ui/ArtistsView.ui
index f56658cb7..6c45a11df 100644
--- a/data/ui/ArtistsView.ui
+++ b/data/ui/ArtistsView.ui
@@ -10,9 +10,9 @@
           <class name="sidebar"/>
         </style>
         <child>
-          <object class="GtkListBox" id="_sidebar">
-            <property name="selection_mode">single</property>
-            <signal name="row-activated" handler="_on_artist_activated" swapped="no"/>
+          <object class="GtkListView" id="_sidebar">
+            <property name="single-click-activate">True</property>
+            <signal name="activate" handler="_on_artist_activated" swapped="no"/>
           </object>
         </child>
       </object>
diff --git a/gnomemusic/views/artistsview.py b/gnomemusic/views/artistsview.py
index f3e1d0640..9388c68c5 100644
--- a/gnomemusic/views/artistsview.py
+++ b/gnomemusic/views/artistsview.py
@@ -73,11 +73,16 @@ class ArtistsView(Gtk.Paned):
         self._coremodel = application.props.coremodel
         self._model = self._coremodel.props.artists_sort
 
-        self._sidebar.bind_model(self._model, self._create_widget)
-
-        self._model.connect_after(
+        self._selection_model = Gtk.SingleSelection.new(self._model)
+        self._sidebar.props.model = self._selection_model
+        artist_item_factory = Gtk.SignalListItemFactory()
+        artist_item_factory.connect("setup", self._on_list_view_setup)
+        artist_item_factory.connect("bind", self._on_list_view_bind)
+        self._sidebar.props.factory = artist_item_factory
+
+        self._selection_model.connect_after(
             "items-changed", self._on_model_items_changed)
-        self._on_model_items_changed(self._model, 0, 0, 0)
+        self._on_model_items_changed(self._selection_model, 0, 0, 0)
 
         self._selection_mode = False
 
@@ -85,28 +90,38 @@ class ArtistsView(Gtk.Paned):
             "selection-mode", self, "selection-mode",
             GObject.BindingFlags.BIDIRECTIONAL)
 
-    def _create_widget(self, coreartist):
-        row = ArtistTile(coreartist)
-        row.props.text = coreartist.props.artist
+    def _on_list_view_setup(
+            self, factory: Gtk.SignalListItemFactory,
+            list_item: Gtk.ListItem) -> None:
+        list_item.props.child = ArtistTile()
+
+    def _on_list_view_bind(
+            self, factory: Gtk.SignalListItemFactory,
+            list_item: Gtk.ListItem) -> None:
+        coreartist = list_item.props.item
+        artist_tile = list_item.props.child
+
+        artist_tile.props.coreartist = coreartist
+        artist_tile.props.text = coreartist.props.artist
 
         GLib.timeout_add(
-            self._widget_counter * 300, row.retrieve,
+            self._widget_counter * 300, artist_tile.retrieve,
             priority=GLib.PRIORITY_LOW)
         self._widget_counter = self._widget_counter + 1
 
-        return row
-
-    def _on_model_items_changed(self, model, position, removed, added):
+    def _on_model_items_changed(
+            self, model: Gtk.SingleSelection, position: int, removed: int,
+            added: int) -> None:
         if model.get_n_items() == 0:
             self._untouched_list = True
             return
         elif self._untouched_list is True:
-            first_row = self._sidebar.get_row_at_index(0)
-            if first_row is None:
+            first_artist = model.get_item(0)
+            if first_artist is None:
                 return
 
-            self._sidebar.select_row(first_row)
-            self._on_artist_activated(self._sidebar, first_row, True)
+            model.props.selected = 0
+            self._on_artist_activated(self._sidebar, 0, True)
             return
 
         if removed == 0:
@@ -124,18 +139,19 @@ class ArtistsView(Gtk.Paned):
 
         self._loaded_artists.remove(removed_artist)
         if self._artist_view.get_visible_child_name() == removed_artist:
-            row_next = (self._sidebar.get_row_at_index(position)
-                        or self._sidebar.get_row_at_index(position - 1))
-            if row_next:
-                self._sidebar.select_row(row_next)
-                self._on_artist_activated(self._sidebar, row_next, True)
+            new_position = min(position, model.get_n_items() - 1)
+            if new_position >= 0:
+                model.props.selected = new_position
+                self._on_artist_activated(self._sidebar, new_position, True)
 
         removed_artist_page = self._artist_view.get_child_by_name(
             removed_artist)
         self._artist_view.remove(removed_artist_page)
 
     @Gtk.Template.Callback()
-    def _on_artist_activated(self, sidebar, row, data=None, untouched=False):
+    def _on_artist_activated(
+            self, sidebar: Gtk.ListView, position: int,
+            untouched: bool = False) -> None:
         """Initializes new artist album widgets"""
         # On application start the first row of ArtistView is activated
         # to show an intial artist. When this happens while any of the
@@ -150,11 +166,10 @@ class ArtistsView(Gtk.Paned):
         if untouched is False:
             self._untouched_list = False
 
-        selected_row = self._sidebar.get_selected_row()
-        self._selected_artist = selected_row.props.coreartist
+        coreartist = sidebar.get_model().get_item(position)
+        self._selected_artist = coreartist
 
         # Prepare a new artist_albums_widget here
-        coreartist = row.props.coreartist
         if coreartist.props.artist in self._loaded_artists:
             scroll_vadjustment = self._artist_container.props.vadjustment
             scroll_vadjustment.props.value = 0.


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