[gnome-music/wip/jfelder/new-album-design-v3: 11/11] albumwidget: Add an album menu




commit 27acdf28944759079729121e40839d1111b621ac
Author: Jean Felder <jfelder src gnome org>
Date:   Thu Jul 29 19:27:30 2021 +0200

    albumwidget: Add an album menu

 data/ui/AlbumWidget.ui            | 71 ++++++++++++++++++++++++++++++++-------
 gnomemusic/widgets/albumwidget.py | 51 +++++++++++++++++++++++++++-
 2 files changed, 108 insertions(+), 14 deletions(-)
---
diff --git a/data/ui/AlbumWidget.ui b/data/ui/AlbumWidget.ui
index 0886cd53f..e7d947efe 100644
--- a/data/ui/AlbumWidget.ui
+++ b/data/ui/AlbumWidget.ui
@@ -99,20 +99,44 @@
                       </object>
                     </child>
                     <child>
-                      <object class="GtkButton" id="_play_button">
+                      <object class="GtkBox">
+                        <property name="orientation">horizontal</property>
                         <property name="visible">True</property>
-                        <property name="can-focus">True</property>
-                        <property name="receives-default">True</property>
-                        <property name="image">_play_image</property>
-                        <property name="always_show_image">True</property>
-                        <property name="tooltip-text" translatable="yes">Play</property>
-                        <property name="halign">start</property>
-                        <property name="valign">center</property>
-                        <property name="margin-top">12</property>
-                        <signal name="clicked" handler="_on_play_button_clicked" swapped="no"/>
-                        <style>
-                          <class name="circular"/>
-                        </style>
+                        <property name="spacing">12</property>
+                        <property name="margin-top">6</property>
+                        <child>
+                          <object class="GtkButton" id="_play_button">
+                            <property name="visible">True</property>
+                            <property name="can-focus">True</property>
+                            <property name="receives-default">True</property>
+                            <property name="image">_play_image</property>
+                            <property name="always_show_image">True</property>
+                            <property name="tooltip-text" translatable="yes">Play</property>
+                            <property name="valign">center</property>
+                            <signal name="clicked" handler="_on_play_button_clicked" swapped="no"/>
+                            <style>
+                              <class name="circular"/>
+                            </style>
+                          </object>
+                        </child>
+                        <child>
+                          <object class="GtkMenuButton" id="_menu_button">
+                            <property name="visible">True</property>
+                            <property name="can_focus">True</property>
+                            <property name="receives_default">True</property>
+                            <property name="halign">start</property>
+                            <property name="valign">center</property>
+                            <property name="focus_on_click">False</property>
+                            <property name="menu-model">album_menu</property>
+                            <property name="direction">none</property>
+                            <property name="use_popover">True</property>
+                            <property name="image">_view_more_image</property>
+                            <style>
+                              <class name="image-button"/>
+                              <class name="circular"/>
+                            </style>
+                          </object>
+                        </child>
                       </object>
                     </child>
                   </object>
@@ -132,6 +156,13 @@
       </object>
     </child>
   </template>
+  <object class="GtkImage" id="_view_more_image">
+    <property name="visible">True</property>
+    <property name="can_focus">False</property>
+    <property name="margin-top">1</property>
+    <property name="icon_name">view-more-symbolic</property>
+    <property name="icon_size">1</property>
+  </object>
   <object class="GtkImage" id="_play_image">
     <property name="visible">True</property>
     <property name="can_focus">False</property>
@@ -139,4 +170,18 @@
     <property name="icon_name">media-playback-start-symbolic</property>
     <property name="icon_size">1</property>
   </object>
+  <menu id="album_menu">
+    <item>
+      <attribute name="label" translatable="yes">_Play</attribute>
+      <attribute name="action">album.play</attribute>
+    </item>
+    <item>
+      <attribute name="label" translatable="yes">Add to _Favorite Songs</attribute>
+      <attribute name="action">album.add_favorites</attribute>
+    </item>
+    <item>
+      <attribute name="label" translatable="yes">_Add to Playlist…</attribute>
+      <attribute name="action">album.add_playlist</attribute>
+    </item>
+  </menu>
 </interface>
diff --git a/gnomemusic/widgets/albumwidget.py b/gnomemusic/widgets/albumwidget.py
index cd5962cf3..df90a8d62 100644
--- a/gnomemusic/widgets/albumwidget.py
+++ b/gnomemusic/widgets/albumwidget.py
@@ -27,12 +27,13 @@ from gettext import ngettext
 from typing import Optional
 import typing
 
-from gi.repository import Gfm, GObject, Gtk
+from gi.repository import Gfm, Gio, GLib, GObject, Gtk
 
 from gnomemusic.corealbum import CoreAlbum
 from gnomemusic.utils import ArtSize
 from gnomemusic.widgets.disclistboxwidget import DiscBox
 from gnomemusic.widgets.disclistboxwidget import DiscListBox  # noqa: F401
+from gnomemusic.widgets.playlistdialog import PlaylistDialog
 if typing.TYPE_CHECKING:
     from gnomemusic.application import Application
     from gnomemusic.coredisc import CoreDisc
@@ -54,6 +55,7 @@ class AlbumWidget(Gtk.ScrolledWindow):
     _composer_label = Gtk.Template.Child()
     _art_stack = Gtk.Template.Child()
     _disc_list_box = Gtk.Template.Child()
+    _menu_button = Gtk.Template.Child()
     _play_button = Gtk.Template.Child()
     _released_label = Gtk.Template.Child()
     _title_label = Gtk.Template.Child()
@@ -84,6 +86,19 @@ class AlbumWidget(Gtk.ScrolledWindow):
 
         self.connect("notify::selection-mode", self._on_selection_mode_changed)
 
+        action_group = Gio.SimpleActionGroup()
+        actions = (
+            ("play", self._on_play_action),
+            ("add_favorites", self._on_add_favorites_action),
+            ("add_playlist", self._on_add_playlist_action)
+        )
+        for (name, callback) in actions:
+            action = Gio.SimpleAction.new(name, None)
+            action.connect("activate", callback)
+            action_group.add_action(action)
+
+        self.insert_action_group("album", action_group)
+
     def update(self, corealbum: CoreAlbum) -> None:
         """Update the album widget.
 
@@ -145,6 +160,7 @@ class AlbumWidget(Gtk.ScrolledWindow):
 
         empty_album = (n_items == 0)
         self._play_button.props.sensitive = not empty_album
+        self._menu_button.props.sensitive = not empty_album
 
     def _set_composer_label(self, corealbum: CoreAlbum) -> None:
         composer = corealbum.props.composer
@@ -210,6 +226,39 @@ class AlbumWidget(Gtk.ScrolledWindow):
         """
         return self._corealbum
 
+    def _on_add_favorites_action(
+            self, action: Gio.SimpleAction,
+            data: Optional[GLib.Variant]) -> None:
+        if self._corealbum:
+            for coredisc in self._corealbum.props.model:
+                for coresong in coredisc.props.model:
+                    if not coresong.props.favorite:
+                        coresong.props.favorite = True
+
+    def _on_add_playlist_action(
+            self, action: Gio.SimpleAction,
+            data: Optional[GLib.Variant]) -> None:
+        if not self._corealbum:
+            return
+
+        playlist_dialog = PlaylistDialog(self._application)
+        active_window = self._application.props.active_window
+        playlist_dialog.props.transient_for = active_window
+        if playlist_dialog.run() == Gtk.ResponseType.ACCEPT:
+            playlist = playlist_dialog.props.selected_playlist
+            coresongs = [
+                song
+                for disc in self._corealbum.props.model
+                for song in disc.props.model]
+            playlist.add_songs(coresongs)
+
+        playlist_dialog.destroy()
+
+    def _on_play_action(
+            self, action: Gio.SimpleAction,
+            data: Optional[GLib.Variant]) -> None:
+        self._play()
+
     @Gtk.Template.Callback()
     def _on_play_button_clicked(self, button: Gtk.Button) -> None:
         self._play()


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