[gnome-music/wip/jfelder/gtk4-v3: 181/208] albumsview: Make selection-mode somewhat work




commit 0c28bf68c73b2a1e5d82b0e246146788c0e7d9f7
Author: Marinus Schraal <mschraal gnome org>
Date:   Tue Feb 8 23:01:39 2022 +0100

    albumsview: Make selection-mode somewhat work
    
    Wrestle with gridview to get some semblance of a sane selection-mode
    like behaviour.

 data/ui/AlbumCoverListItem.ui  | 100 ++++++++++++++++-------------------------
 gnomemusic/corealbum.py        |   4 ++
 gnomemusic/views/albumsview.py |  82 ++++++++++++++++++++++++++-------
 3 files changed, 108 insertions(+), 78 deletions(-)
---
diff --git a/data/ui/AlbumCoverListItem.ui b/data/ui/AlbumCoverListItem.ui
index 1d827c5a5..00f951686 100644
--- a/data/ui/AlbumCoverListItem.ui
+++ b/data/ui/AlbumCoverListItem.ui
@@ -1,74 +1,52 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <requires lib="gtk+" version="4.0"/>
-  <template class="GtkListItem">
-    <property name="child">
-      <object class="GtkBox">
-        <property name="focusable">False</property>
-        <property name="has_tooltip">True</property>
-        <property name="valign">start</property>
-        <property name="orientation">vertical</property>
-        <!-- <signal name="query-tooltip" handler="_on_tooltip_query"/> -->
+  <object class="GtkBox" id="_album_cover">
+    <property name="focusable">False</property>
+    <property name="has_tooltip">True</property>
+    <property name="valign">start</property>
+    <property name="orientation">vertical</property>
+    <!-- <signal name="query-tooltip" handler="_on_tooltip_query"/> -->
+    <child>
+      <object class="GtkOverlay">
         <child>
-          <object class="GtkOverlay">
+          <object class="ArtStack" id="_art_stack">
             <property name="focusable">False</property>
-            <property name="margin-bottom">4</property>
-            <child>
-              <object class="ArtStack" id="_art_stack">
-                <binding name="coreobject">
-                  <lookup name="corealbum" type="CoreAlbum">
-                    <lookup name="item">GtkListItem</lookup>
-                  </lookup>
-                </binding>
-                <property name="focusable">False</property>
-                <property name="vexpand">True</property>
-                <property name="valign">end</property>
-                <property name="halign">center</property>
-              </object>
-            </child>
-            <!-- <child type="overlay"> -->
-            <!--   <object class="GtkCheckButton" id="_check"> -->
-            <!--     <property name="focusable">True</property> -->
-            <!--     <property name="receives_default">False</property> -->
-            <!--     <property name="halign">end</property> -->
-            <!--     <property name="valign">end</property> -->
-            <!--     <property name="draw_indicator">True</property> -->
-            <!--   </object> -->
-            <!-- </child> -->
+            <property name="vexpand">True</property>
+            <property name="valign">end</property>
+            <property name="halign">center</property>
           </object>
         </child>
-        <child>
-          <object class="GtkLabel">
-            <binding name="label">
-              <lookup name="title" type="CoreAlbum">
-                <lookup name="item">GtkListItem</lookup>
-              </lookup>
-            </binding>
-            <property name="focusable">False</property>
-            <property name="justify">center</property>
-            <property name="wrap">True</property>
-            <property name="ellipsize">middle</property>
-            <property name="max_width_chars">20</property>
-            <property name="lines">2</property>
-          </object>
-        </child>
-        <child>
-          <object class="GtkLabel" id="_artist_label">
-            <binding name="label">
-              <lookup name="artist" type="CoreAlbum">
-                <lookup name="item">GtkListItem</lookup>
-              </lookup>
-            </binding>
-            <property name="focusable">False</property>
-            <property name="ellipsize">middle</property>
-            <property name="max_width_chars">20</property>
+        <child type="overlay">
+          <object class="GtkCheckButton" id="_check">
+            <property name="visible">False</property>
+            <property name="halign">end</property>
+            <property name="valign">end</property>
             <style>
-              <class name="albumcover-artist-label"/>
-              <class name="dim-label"/>
+              <class name="selection-mode"/>
             </style>
           </object>
         </child>
       </object>
-    </property>
-  </template>
+    </child>
+    <child>
+      <object class="GtkLabel">
+        <property name="justify">center</property>
+        <property name="wrap">True</property>
+        <property name="ellipsize">middle</property>
+        <property name="max_width_chars">20</property>
+        <property name="lines">2</property>
+      </object>
+    </child>
+    <child>
+      <object class="GtkLabel" id="_artist_label">
+        <property name="ellipsize">middle</property>
+        <property name="max_width_chars">20</property>
+        <style>
+          <class name="albumcover-artist-label"/>
+          <class name="dim-label"/>
+        </style>
+      </object>
+    </child>
+  </object>
 </interface>
diff --git a/gnomemusic/corealbum.py b/gnomemusic/corealbum.py
index d628763ea..d2949d0df 100644
--- a/gnomemusic/corealbum.py
+++ b/gnomemusic/corealbum.py
@@ -161,3 +161,7 @@ class CoreAlbum(GObject.GObject):
 
         if self._thumbnail != "generic":
             self.props.media.set_thumbnail(self._thumbnail)
+
+    @GObject.Property()
+    def corealbum(self):
+        return self
diff --git a/gnomemusic/views/albumsview.py b/gnomemusic/views/albumsview.py
index d8ec64eeb..7128c07c6 100644
--- a/gnomemusic/views/albumsview.py
+++ b/gnomemusic/views/albumsview.py
@@ -56,7 +56,6 @@ class AlbumsView(Gtk.Stack):
     _album_scrolled_window = Gtk.Template.Child()
     _scrolled_window = Gtk.Template.Child()
     _gridview = Gtk.Template.Child()
-    # _flowbox_long_press = Gtk.Template.Child()
 
     def __init__(self, application):
         """Initialize AlbumsView
@@ -70,28 +69,32 @@ class AlbumsView(Gtk.Stack):
         self._application = application
         self._window = application.props.window
         self._headerbar = self._window._headerbar
-        self._adjustment_timeout_id = 0
-        self._widget_counter = 1
-        self._ctrl_hold = False
 
-        self._gridview.props.single_click_activate = True
+        list_item_factory = Gtk.SignalListItemFactory()
+        list_item_factory.connect("setup", self._setup_list_item)
+        list_item_factory.connect("bind", self._bind_list_item)
 
-        list_item_factory = Gtk.BuilderListItemFactory(
-            resource="/org/gnome/Music/ui/AlbumCoverListItem.ui")
         self._gridview.props.factory = list_item_factory
 
-        multi_select_model = Gtk.MultiSelection.new(
+        self._selection_model = Gtk.MultiSelection.new(
             self._application.props.coremodel.props.albums_sort)
-        self._gridview.props.model = multi_select_model
+        self._gridview.props.model = self._selection_model
 
         self._gridview.connect("activate", self._on_album_activated)
 
-        # self.bind_property(
-        #     "selection-mode", self._window, "selection-mode",
-        #     GObject.BindingFlags.DEFAULT)
+        self.bind_property(
+            "selection-mode", self._gridview, "single-click-activate",
+            GObject.BindingFlags.SYNC_CREATE |
+                GObject.BindingFlags.INVERT_BOOLEAN)
+        self.bind_property(
+            "selection-mode", self._gridview, "enable-rubberband",
+            GObject.BindingFlags.SYNC_CREATE)
+        self.bind_property(
+            "selection-mode", self._window, "selection-mode",
+            GObject.BindingFlags.DEFAULT)
 
-        # self._window.connect(
-        #     "notify::selection-mode", self._on_selection_mode_changed)
+        self._window.connect(
+            "notify::selection-mode", self._on_selection_mode_changed)
 
         self._album_widget = AlbumWidget(self._application)
         self._album_widget.bind_property(
@@ -112,7 +115,6 @@ class AlbumsView(Gtk.Stack):
         self.props.selection_mode = selection_mode
         if not self.props.selection_mode:
             self.deselect_all()
-            self._flowbox.props.selection_mode = Gtk.SelectionMode.NONE
 
     def _on_search_mode_changed(self, klass, param):
         if (not self.props.search_mode_active
@@ -157,11 +159,57 @@ class AlbumsView(Gtk.Stack):
                 else:
                     self._album_widget.deselect_all()
             else:
-                for child in self._flowbox.get_children():
-                    child.props.selected = selected
+                if selected:
+                    self._selection_model.select_all()
+                else:
+                    self._selection_model.unselect_all()
+                # for album in self._gridview.props.model:
+                #     album.props.selected = selected
 
     def select_all(self):
         self._toggle_all_selection(True)
 
     def deselect_all(self):
         self._toggle_all_selection(False)
+
+    def _setup_list_item(self, factory, listitem):
+        builder = Gtk.Builder.new_from_resource(
+            "/org/gnome/Music/ui/AlbumCoverListItem.ui")
+        listitem.set_child(builder.get_object("_album_cover"))
+
+        self.bind_property(
+            "selection-mode", listitem, "selectable",
+            GObject.BindingFlags.SYNC_CREATE)
+        self.bind_property(
+            "selection-mode", listitem, "activatable",
+            GObject.BindingFlags.SYNC_CREATE |
+                GObject.BindingFlags.INVERT_BOOLEAN)
+
+    def _bind_list_item(self, factory, listitem):
+        widget = listitem.get_child()
+
+        art_stack = widget.get_first_child().get_first_child()
+        check = art_stack.get_next_sibling()
+        album_label = widget.get_first_child().get_next_sibling()
+        artist_label = album_label.get_next_sibling()
+
+        listitem.props.item.bind_property(
+            "corealbum", art_stack, "coreobject",
+            GObject.BindingFlags.SYNC_CREATE)
+        listitem.props.item.bind_property(
+            "title", album_label, "label",
+            GObject.BindingFlags.SYNC_CREATE)
+        listitem.props.item.bind_property(
+            "artist", artist_label, "label",
+            GObject.BindingFlags.SYNC_CREATE)
+        listitem.bind_property(
+            "selected", listitem.props.item, "selected",
+            GObject.BindingFlags.SYNC_CREATE)
+
+        self.bind_property(
+            "selection-mode", check, "visible",
+            GObject.BindingFlags.SYNC_CREATE)
+        check.bind_property(
+            "active", listitem.props.item, "selected",
+            GObject.BindingFlags.SYNC_CREATE
+                | GObject.BindingFlags.BIDIRECTIONAL)


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