[gnome-music/wip/jfelder/gtk4-v3: 62/127] scope




commit 03e801fa4c295bef7b0273bce31072282edc493f
Author: Jean Felder <jfelder src gnome org>
Date:   Sun Jan 9 17:43:34 2022 +0100

    scope

 data/ui/SongListItem.ui       |  4 ++--
 gnomemusic/songlistscope.py   | 51 +++++++++++++++++++++++++++++++++++++++++++
 gnomemusic/widgets/discbox.py |  2 ++
 3 files changed, 55 insertions(+), 2 deletions(-)
---
diff --git a/data/ui/SongListItem.ui b/data/ui/SongListItem.ui
index 7477a404d..36e286c97 100644
--- a/data/ui/SongListItem.ui
+++ b/data/ui/SongListItem.ui
@@ -39,9 +39,9 @@
             <property name="hexpand">True</property>
             <property name="single_line_mode">True</property>
             <binding name="label">
-              <lookup name="duration" type="CoreSong">
+              <closure type="gchararray" function="song_duration">
                 <lookup name="item">GtkListItem</lookup>
-              </lookup>
+              </closure>
             </binding>
             <attributes>
               <attribute name="font-features" value="tnum=1"/>
diff --git a/gnomemusic/songlistscope.py b/gnomemusic/songlistscope.py
new file mode 100644
index 000000000..926c01d05
--- /dev/null
+++ b/gnomemusic/songlistscope.py
@@ -0,0 +1,51 @@
+# Copyright 2022 The GNOME Music developers
+#
+# GNOME Music is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# GNOME Music is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with GNOME Music; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# The GNOME Music authors hereby grant permission for non-GPL compatible
+# GStreamer plugins to be used and distributed together with GStreamer
+# and GNOME Music.  This permission is above and beyond the permissions
+# granted by the GPL license by which GNOME Music is covered.  If you
+# modify this code, you may extend this exception to your version of the
+# 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 __future__ import annotations
+from typing import Callable, Optional
+import typing
+
+from gi.repository import GObject, Gtk
+
+import gnomemusic.utils as utils
+if typing.TYPE_CHECKING:
+    from gnomemusic.coresong import CoreSong
+    ClosureType = Callable[[Gtk.ListItem, CoreSong], str]
+
+
+class SongListScope(GObject.GObject, Gtk.BuilderScope):
+
+    __gtype_name__ = "SongListScope"
+
+    def __init__(self) -> None:
+        super().__init__()
+
+    def do_create_closure(
+            self, builder: Gtk.Builder, func_name: str,
+            flags: Gtk.BuilderClosureFlags,
+            obj: Optional[GObject.GObject]) -> ClosureType:
+        return getattr(self, func_name)
+
+    def song_duration(self, listitem: Gtk.ListItem, coresong: CoreSong) -> str:
+        return utils.seconds_to_string(coresong.props.duration)
diff --git a/gnomemusic/widgets/discbox.py b/gnomemusic/widgets/discbox.py
index ff56180a5..669ae6c20 100644
--- a/gnomemusic/widgets/discbox.py
+++ b/gnomemusic/widgets/discbox.py
@@ -29,6 +29,7 @@ import typing
 from gi.repository import Gdk, Gio, GObject, Gtk
 
 from gnomemusic.coresong import CoreSong
+from gnomemusic.songlistscope import SongListScope
 from gnomemusic.widgets.songwidget import SongWidget
 from gnomemusic.widgets.songwidgetmenu import SongWidgetMenu
 if typing.TYPE_CHECKING:
@@ -83,6 +84,7 @@ class DiscBox(Gtk.ListBoxRow):
         self._list_view.props.model = multi_selection_model
 
         list_item_factory = Gtk.BuilderListItemFactory(
+            scope=SongListScope(),
             resource="/org/gnome/Music/ui/SongListItem.ui")
         self._list_view.props.factory = list_item_factory
 


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