[gnome-music/wip/jfelder/empty-playlist-v2: 85/85] playlistswidget: Display a special view for an empty playlist




commit 976d162c5678e02c30bad0bd514b609d30c84e12
Author: Jean Felder <jfelder src gnome org>
Date:   Wed Nov 11 21:18:50 2020 +0100

    playlistswidget: Display a special view for an empty playlist
    
    When a playlist does not contain any song, the view seems broken: the
    big empty space makes the feature appear ambiguous and the line at the
    top also looks out of place (see #424).
    
    This issue is fixed by replacing the default state by a new empty view
    when a playlist is empty. This view contains the icon introduced in
    the previous commit.
    
    closes: #424

 data/ui/PlaylistsWidget.ui            | 10 ++++++-
 gnomemusic/widgets/playlistswidget.py | 52 ++++++++++++++++++++++++++++++++++-
 po/POTFILES.in                        |  1 +
 3 files changed, 61 insertions(+), 2 deletions(-)
---
diff --git a/data/ui/PlaylistsWidget.ui b/data/ui/PlaylistsWidget.ui
index 7712c6b6f..e0ce52db7 100644
--- a/data/ui/PlaylistsWidget.ui
+++ b/data/ui/PlaylistsWidget.ui
@@ -9,7 +9,7 @@
       </object>
     </child>
     <child>
-      <object class="GtkScrolledWindow" id="playlist-container">
+      <object class="GtkScrolledWindow" id="_playlist_container">
         <property name="vexpand">True</property>
         <property name="visible">True</property>
         <child>
@@ -28,6 +28,14 @@
         </child>
       </object>
     </child>
+    <child>
+      <object class="HdyStatusPage" id="_empty_page">
+        <property name="hexpand">True</property>
+        <!-- <property name="valign">center</property> -->
+        <property name="vexpand">True</property>
+        <property name="visible">False</property>
+      </object>
+    </child>
   </template>
   <object class="GtkGestureMultiPress" id="_songs_list_ctrlr">
     <property name="widget">_songs_list</property>
diff --git a/gnomemusic/widgets/playlistswidget.py b/gnomemusic/widgets/playlistswidget.py
index 30bceafc9..2e021955d 100644
--- a/gnomemusic/widgets/playlistswidget.py
+++ b/gnomemusic/widgets/playlistswidget.py
@@ -23,9 +23,12 @@
 # delete this exception statement from your version.
 
 from __future__ import annotations
+
+from gettext import gettext as _
+from typing import Optional
 import typing
 
-from gi.repository import Gdk, GObject, Gtk
+from gi.repository import Gdk, GLib, GObject, Gtk
 
 from gnomemusic.widgets.notificationspopup import PlaylistNotification
 from gnomemusic.widgets.playlistcontextmenu import PlaylistContextMenu
@@ -45,7 +48,9 @@ class PlaylistsWidget(Gtk.Box):
 
     __gtype_name__ = "PlaylistsWidget"
 
+    _empty_page = Gtk.Template.Child()
     _pl_ctrls = Gtk.Template.Child()
+    _playlist_container = Gtk.Template.Child()
     _songs_list = Gtk.Template.Child()
     _songs_list_ctrlr = Gtk.Template.Child()
 
@@ -70,6 +75,10 @@ class PlaylistsWidget(Gtk.Box):
 
         self._pl_ctrls.props.application = application
 
+        self._previous_playlist: Optional[Playlist] = None
+        self._count_id = 0
+        self._count_timeout = 0
+
         self._song_popover = PlaylistContextMenu(application, self._songs_list)
 
         play_song = self._window.lookup_action("play_song")
@@ -94,6 +103,22 @@ class PlaylistsWidget(Gtk.Box):
 
         self._songs_list.bind_model(
             playlist.props.model, self._create_song_widget, playlist)
+
+        if self._count_id > 0:
+            self._previous_playlist.disconnect(self._count_id)
+            self._count_id = 0
+
+        self._previous_playlist = playlist
+        self._count_id = playlist.connect(
+            "notify::count", self._on_count_changed)
+        if playlist.props.count == 0:
+            self._pl_ctrls.props.visible = False
+            self._playlist_container.props.visible = False
+            self._count_timeout = GLib.timeout_add(
+                500, self._on_count_changed, playlist)
+        else:
+            self._on_count_changed(playlist)
+
         if playlist.props.is_smart:
             playlist.update()
 
@@ -101,6 +126,31 @@ class PlaylistsWidget(Gtk.Box):
 
         self._remove_song_action.set_enabled(not playlist.props.is_smart)
 
+    def _on_count_changed(
+            self, playlist: Playlist,
+            value: GObject.GParamSpec = None) -> None:
+        if self._count_timeout > 0:
+            GLib.source_remove(self._count_timeout)
+            self._count_timeout = 0
+
+        if playlist.props.count == 0:
+            self._pl_ctrls.props.visible = False
+            self._playlist_container.props.visible = False
+            self._empty_page.props.visible = True
+            self._empty_page.props.icon_name = playlist.props.icon_name
+
+            if playlist.props.is_smart:
+                empty_label = _("{} Will Appear Here".format(
+                    playlist.props.title))
+            else:
+                empty_label = _("{} Is Empty".format(playlist.props.title))
+
+            self._empty_page.props.title = empty_label
+        else:
+            self._empty_page.props.visible = False
+            self._pl_ctrls.props.visible = True
+            self._playlist_container.props.visible = True
+
     def _create_song_widget(
             self, coresong: CoreSong, playlist: Playlist) -> Gtk.ListBoxRow:
         can_dnd = not playlist.props.is_smart
diff --git a/po/POTFILES.in b/po/POTFILES.in
index c88c5b2cc..e947a235a 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -40,6 +40,7 @@ gnomemusic/widgets/lastfmdialog.py
 gnomemusic/widgets/notificationspopup.py
 gnomemusic/widgets/playertoolbar.py
 gnomemusic/widgets/playlistcontrols.py
+gnomemusic/widgets/playlistswidget.py
 gnomemusic/widgets/playlistdialog.py
 gnomemusic/widgets/starhandlerwidget.py
 gnomemusic/window.py


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