[gnome-music/wip/jfelder/gtk4-v3: 76/259] songwidgetmenu: Port to gmenu




commit 0f66920beb60978a3fb89fb05f08ac061f04322c
Author: Jean Felder <jfelder src gnome org>
Date:   Mon Jan 10 16:03:35 2022 +0100

    songwidgetmenu: Port to gmenu

 data/ui/SongWidgetMenu.ui            | 56 ++++++++++--------------------------
 gnomemusic/widgets/songwidgetmenu.py | 41 +++++++++++++++-----------
 2 files changed, 39 insertions(+), 58 deletions(-)
---
diff --git a/data/ui/SongWidgetMenu.ui b/data/ui/SongWidgetMenu.ui
index c8ac0cd4b..17c7a34a1 100644
--- a/data/ui/SongWidgetMenu.ui
+++ b/data/ui/SongWidgetMenu.ui
@@ -4,46 +4,20 @@
     <property name="autohide">True</property>
     <property name="position">bottom</property>
     <property name="visible">False</property>
-    <child>
-      <object class="GtkBox">
-        <property name="can-focus">False</property>
-        <property name="margin-bottom">6</property>
-        <property name="margin-end">6</property>
-        <property name="margin-start">6</property>
-        <property name="margin-top">6</property>
-        <property name="orientation">vertical</property>
-        <property name="visible">True</property>
-        <child>
-          <object class="GtkModelButton">
-            <property name="visible">True</property>
-            <property name="text" translatable="yes">Play</property>
-            <signal name="clicked" handler="_on_play_clicked" swapped="no"/>
-            <style>
-              <class name="flat"/>
-            </style>
-          </object>
-        </child>
-        <child>
-          <object class="GtkModelButton">
-            <property name="visible">True</property>
-            <property name="text" translatable="yes">Add to Playlist…</property>
-            <signal name="clicked" handler="_on_add_to_playlist_clicked" swapped="no"/>
-            <style>
-              <class name="flat"/>
-            </style>
-          </object>
-        </child>
-        <child>
-          <object class="GtkModelButton" id="_remove_from_playlist_button">
-            <property name="visible">True</property>
-            <property name="text" translatable="yes">Remove From Playlist</property>
-            <signal name="clicked" handler="_on_remove_from_playlist_clicked" swapped="no"/>
-            <style>
-              <class name="flat"/>
-            </style>
-          </object>
-        </child>
-      </object>
-    </child>
+    <property name="menu-model">song-menu</property>
   </template>
+  <menu id="song-menu">
+    <item>
+      <attribute name="label" translatable="yes">_Play</attribute>
+      <attribute name="action">songwidget.play</attribute>
+    </item>
+    <item>
+      <attribute name="label" translatable="yes">_Add to Playlist…</attribute>
+      <attribute name="action">songwidget.add_playlist</attribute>
+    </item>
+    <item>
+      <attribute name="label" translatable="yes">_Remove from Playlist</attribute>
+      <attribute name="action">songwidget.remove_playlist</attribute>
+    </item>
+  </menu>
 </interface>
diff --git a/gnomemusic/widgets/songwidgetmenu.py b/gnomemusic/widgets/songwidgetmenu.py
index 9c4e75b5e..0384b09a4 100644
--- a/gnomemusic/widgets/songwidgetmenu.py
+++ b/gnomemusic/widgets/songwidgetmenu.py
@@ -23,10 +23,10 @@
 # delete this exception statement from your version.
 
 from __future__ import annotations
-from typing import Optional, Union
+from typing import Any, Optional, Union
 import typing
 
-from gi.repository import Gtk
+from gi.repository import Gio, Gtk
 
 from gnomemusic.grilowrappers.grltrackerplaylists import Playlist
 from gnomemusic.widgets.notificationspopup import PlaylistNotification
@@ -43,8 +43,6 @@ class SongWidgetMenu(Gtk.PopoverMenu):
 
     __gtype_name__ = "SongWidgetMenu"
 
-    _remove_from_playlist_button = Gtk.Template.Child()
-
     def __init__(
             self, application: Application, song_widget: SongWidget,
             coreobject: Union[CoreAlbum, CoreSong, Playlist]) -> None:
@@ -68,15 +66,26 @@ class SongWidgetMenu(Gtk.PopoverMenu):
 
         self._playlist_dialog: Optional[PlaylistDialog] = None
 
-        if isinstance(self._coreobject, Playlist):
-            self._remove_from_playlist_button.props.visible = True
-            self._remove_from_playlist_button.props.sensitive = (
-                not self._coreobject.props.is_smart)
-        else:
-            self._remove_from_playlist_button.props.visible = False
-
-    @Gtk.Template.Callback()
-    def _on_play_clicked(self, widget: Gtk.Button) -> None:
+        action_group = Gio.SimpleActionGroup()
+        action_entries = [
+            ("play", self._play_song),
+            ("add_playlist", self._add_to_playlist)
+        ]
+        if (isinstance(self._coreobject, Playlist)
+                and not self._coreobject.props.is_smart):
+            action_entries.append(
+                ("remove_playlist", self._remove_from_playlist))
+        elif not isinstance(self._coreobject, Playlist):
+            self.props.menu_model.remove(2)
+
+        for name, callback in action_entries:
+            action = Gio.SimpleAction.new(name, None)
+            action.connect("activate", callback)
+            action_group.add_action(action)
+
+        self.insert_action_group("songwidget", action_group)
+
+    def _play_song(self, action: Gio.Simple, param: Any) -> None:
         self.popdown()
         signal_id = 0
 
@@ -88,8 +97,7 @@ class SongWidgetMenu(Gtk.PopoverMenu):
             "playlist-loaded", _on_playlist_loaded)
         self._coremodel.props.active_core_object = self._coreobject
 
-    @Gtk.Template.Callback()
-    def _on_add_to_playlist_clicked(self, widget: Gtk.Button) -> None:
+    def _add_to_playlist(self, action: Gio.Simple, param: Any) -> None:
 
         def on_response(dialog: PlaylistDialog, response_id: int) -> None:
             if not self._playlist_dialog:
@@ -108,8 +116,7 @@ class SongWidgetMenu(Gtk.PopoverMenu):
         self._playlist_dialog.connect("response", on_response)
         self._playlist_dialog.present()
 
-    @Gtk.Template.Callback()
-    def _on_remove_from_playlist_clicked(self, widget: Gtk.Button) -> None:
+    def _remove_from_playlist(self, action: Gio.Simple, param: Any) -> None:
         self.popdown()
         position = self._song_widget.get_index()
         notification = PlaylistNotification(  # noqa: F841


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