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




commit 364cb99043d1fc72eb38b86a907f7297533f13c2
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 | 48 +++++++++++++++++++++++++-
 2 files changed, 105 insertions(+), 14 deletions(-)
---
diff --git a/data/ui/AlbumWidget.ui b/data/ui/AlbumWidget.ui
index a71043bf5..52a5ec8e9 100644
--- a/data/ui/AlbumWidget.ui
+++ b/data/ui/AlbumWidget.ui
@@ -101,20 +101,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">albumMenu</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>
@@ -134,6 +158,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>
@@ -141,4 +172,18 @@
     <property name="icon_name">media-playback-start-symbolic</property>
     <property name="icon_size">1</property>
   </object>
+  <menu id="albumMenu">
+    <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 31964e56c..1a88c47c0 100644
--- a/gnomemusic/widgets/albumwidget.py
+++ b/gnomemusic/widgets/albumwidget.py
@@ -26,12 +26,13 @@ from __future__ import annotations
 from typing import Any, Optional
 import typing
 
-from gi.repository import Gfm, GLib, 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
@@ -53,6 +54,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()
@@ -81,6 +83,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.
 
@@ -140,6 +155,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
@@ -194,6 +210,36 @@ class AlbumWidget(Gtk.ScrolledWindow):
         """
         return self._corealbum
 
+    def _on_add_favorites_action(
+            self, action: Gio.SimpleAction,
+            data: Optional[GLib.Variant]) -> None:
+        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:
+        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 = []
+            for disc in self._corealbum.props.model:
+                for song in disc.props.model:
+                    coresongs.append(song)
+
+            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]