[gnome-music/wip/jfelder/repeat-mode-v44: 1/2] playertoolbar: Manually build the repeat menu




commit d81bd8e3b26ec82db18c4bfe46e593ca01321919
Author: Jean Felder <jfelder src gnome org>
Date:   Sat Jul 31 15:33:14 2021 +0200

    playertoolbar: Manually build the repeat menu
    
    Like the previous commit, this helps to centralize the repeat logic in
    one place.
    The RepeatMode enum order is changed to keep the same repeat menu
    order.
    
    This will also make it possible to move the repeat action from Window
    to PlayerToolbar in the next commit.
    
    Related: https://gitlab.gnome.org/GNOME/gnome-music/-/issues/225

 data/ui/PlayerToolbar.ui            | 25 +------------------------
 gnomemusic/player.py                | 18 ++++++++++++------
 gnomemusic/widgets/playertoolbar.py | 13 ++++++++++++-
 po/POTFILES.in                      |  1 +
 4 files changed, 26 insertions(+), 31 deletions(-)
---
diff --git a/data/ui/PlayerToolbar.ui b/data/ui/PlayerToolbar.ui
index 23493a2b5..a39404af4 100644
--- a/data/ui/PlayerToolbar.ui
+++ b/data/ui/PlayerToolbar.ui
@@ -1,28 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <requires lib="gtk+" version="3.12"/>
-  <menu id="repeatMenu">
-    <item>
-      <attribute name="label" translatable="yes">Shuffle</attribute>
-      <attribute name="action">win.repeat</attribute>
-      <attribute name="target">shuffle</attribute>
-    </item>
-    <item>
-      <attribute name="label" translatable="yes">Repeat All</attribute>
-      <attribute name="action">win.repeat</attribute>
-      <attribute name="target">all</attribute>
-    </item>
-    <item>
-      <attribute name="label" translatable="yes">Repeat Song</attribute>
-      <attribute name="action">win.repeat</attribute>
-      <attribute name="target">song</attribute>
-    </item>
-    <item>
-      <attribute name="label" translatable="yes" comments="Causes tracks to play in random 
order">Shuffle/Repeat Off</attribute>
-      <attribute name="action">win.repeat</attribute>
-      <attribute name="target">none</attribute>
-    </item>
-  </menu>
   <object class="GtkImage" id="next_image">
     <property name="visible">True</property>
     <property name="can_focus">False</property>
@@ -232,12 +210,11 @@
         <property name="halign">end</property>
         <property name="valign">center</property>
         <child>
-          <object class="GtkMenuButton" id="menuButton">
+          <object class="GtkMenuButton" id="_repeat_menu_button">
             <property name="visible">True</property>
             <property name="can_focus">True</property>
             <property name="receives_default">True</property>
             <property name="use_popover">True</property>
-            <property name="menu_model">repeatMenu</property>
             <child>
               <object class="GtkBox" id="replayBox">
                 <property name="visible">True</property>
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index 2894fc275..b717786bd 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -23,6 +23,7 @@
 # delete this exception statement from your version.
 
 from enum import Enum, IntEnum
+from gettext import gettext as _
 from random import randint, randrange
 import time
 import typing
@@ -39,20 +40,25 @@ import gnomemusic.utils as utils
 
 class RepeatMode(Enum):
     """Enum for player repeat mode"""
-    NONE = 0, "media-playlist-consecutive-symbolic"
-    SONG = 1, "media-playlist-repeat-song-symbolic"
-    ALL = 2, "media-playlist-repeat-symbolic"
-    SHUFFLE = 3, "media-playlist-shuffle-symbolic"
 
-    # The type_checking is necessary to avoid false positives
+    # Translators: "shuffle" causes tracks to play in random order.
+    SHUFFLE = 0, "media-playlist-shuffle-symbolic", _("Shuffle")
+    SONG = 1, "media-playlist-repeat-song-symbolic", _("Repeat Song")
+    ALL = 2, "media-playlist-repeat-symbolic", _("Repeat All")
+    NONE = 3, "media-playlist-consecutive-symbolic", _("Shuffle/Repeat Off")
+
+    # The type checking is necessary to avoid false positives
     # See: https://github.com/python/mypy/issues/1021
     if typing.TYPE_CHECKING:
         icon: str
+        label: str
 
-    def __new__(cls, value: int, icon: str = "") -> "RepeatMode":
+    def __new__(
+            cls, value: int, icon: str = "", label: str = "") -> "RepeatMode":
         obj = object.__new__(cls)
         obj._value_ = value
         obj.icon = icon
+        obj.label = label
         return obj
 
 
diff --git a/gnomemusic/widgets/playertoolbar.py b/gnomemusic/widgets/playertoolbar.py
index 2af6f81bb..e78e7b175 100644
--- a/gnomemusic/widgets/playertoolbar.py
+++ b/gnomemusic/widgets/playertoolbar.py
@@ -23,7 +23,7 @@
 # delete this exception statement from your version.
 
 from gettext import gettext as _
-from gi.repository import GObject, Gtk
+from gi.repository import Gio, GLib, GObject, Gtk
 
 from gnomemusic.gstplayer import Playback
 from gnomemusic.utils import ArtSize
@@ -53,6 +53,7 @@ class PlayerToolbar(Gtk.ActionBar):
     _prev_button = Gtk.Template.Child()
     _progress_scale = Gtk.Template.Child()
     _progress_time_label = Gtk.Template.Child()
+    _repeat_menu_button = Gtk.Template.Child()
     _repeat_image = Gtk.Template.Child()
     _song_info_box = Gtk.Template.Child()
     _title_label = Gtk.Template.Child()
@@ -73,6 +74,16 @@ class PlayerToolbar(Gtk.ActionBar):
         main_container.child_set_property(
             self._buttons_and_scale, "expand", True)
 
+        repeat_menu = Gio.Menu.new()
+        for mode in RepeatMode:
+            item = Gio.MenuItem.new()
+            item.set_label(mode.label)
+            item.set_action_and_target_value(
+                "win.repeat", GLib.Variant("s", mode.name.lower()))
+            repeat_menu.append_item(item)
+
+        self._repeat_menu_button.props.menu_model = repeat_menu
+
     # FIXME: This is a workaround for not being able to pass the player
     # object via init when using Gtk.Builder.
     @GObject.Property(type=Player, default=None)
diff --git a/po/POTFILES.in b/po/POTFILES.in
index c88c5b2cc..a880ab386 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -25,6 +25,7 @@ gnomemusic/grilowrappers/grltrackerplaylists.py
 gnomemusic/gstplayer.py
 gnomemusic/inhibitsuspend.py
 gnomemusic/mpris.py
+gnomemusic/player.py
 gnomemusic/utils.py
 gnomemusic/views/albumsview.py
 gnomemusic/views/artistsview.py


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