[gnome-music/wip/jfelder/repeat-mode-v44: 3/3] playertoolbar: Handle the repeat action




commit 7abcfa15b389043452063212650686c70ecbe1cb
Author: Jean Felder <jfelder src gnome org>
Date:   Sat Jul 31 16:26:51 2021 +0200

    playertoolbar: Handle the repeat action
    
    This allows to remove the "repeat" logic from Window.
    
    An "activate" signal handler is also added to this action. This
    handler allows to directly update the "repeat-mode" property in
    PlayerToolbar insted of listening to the settings changes in Player.
    
    Closes: https://gitlab.gnome.org/GNOME/gnome-music/-/issues/225

 gnomemusic/player.py                |  6 ------
 gnomemusic/widgets/playertoolbar.py | 24 +++++++++++++++++++++++-
 gnomemusic/window.py                |  1 -
 3 files changed, 23 insertions(+), 8 deletions(-)
---
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index ddcc00286..3433e41b7 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -381,9 +381,6 @@ class Player(GObject.GObject):
             "items-changed", self._on_playlist_model_items_changed)
 
         self._settings = application.props.settings
-        self._settings.connect(
-            'changed::repeat', self._on_repeat_setting_changed)
-
         self._repeat = RepeatMode(self._settings.get_enum("repeat"))
         self.bind_property(
             'repeat-mode', self._playlist, 'repeat-mode',
@@ -596,9 +593,6 @@ class Player(GObject.GObject):
                 current_song.bump_play_count()
                 current_song.set_last_played()
 
-    def _on_repeat_setting_changed(self, settings, value):
-        self.props.repeat_mode = RepeatMode(settings.get_enum("repeat"))
-
     @GObject.Property(type=object)
     def repeat_mode(self) -> RepeatMode:
         """Gets current repeat mode.
diff --git a/gnomemusic/widgets/playertoolbar.py b/gnomemusic/widgets/playertoolbar.py
index e78e7b175..d125650b6 100644
--- a/gnomemusic/widgets/playertoolbar.py
+++ b/gnomemusic/widgets/playertoolbar.py
@@ -23,6 +23,8 @@
 # delete this exception statement from your version.
 
 from gettext import gettext as _
+from typing import Optional
+
 from gi.repository import Gio, GLib, GObject, Gtk
 
 from gnomemusic.gstplayer import Playback
@@ -79,10 +81,11 @@ class PlayerToolbar(Gtk.ActionBar):
             item = Gio.MenuItem.new()
             item.set_label(mode.label)
             item.set_action_and_target_value(
-                "win.repeat", GLib.Variant("s", mode.name.lower()))
+                "playertoolbar.repeat", GLib.Variant("s", str(mode.value)))
             repeat_menu.append_item(item)
 
         self._repeat_menu_button.props.menu_model = repeat_menu
+        self._repeat_action: Optional[Gio.SimpleAction] = None
 
     # FIXME: This is a workaround for not being able to pass the player
     # object via init when using Gtk.Builder.
@@ -114,8 +117,27 @@ class PlayerToolbar(Gtk.ActionBar):
             'notify::repeat-mode', self._on_repeat_mode_changed)
         self._player.connect('notify::state', self._sync_playing)
 
+        repeat_mode = self._player.props.repeat_mode
+        self._repeat_action = Gio.SimpleAction.new_stateful(
+            "repeat", GLib.VariantType.new("s"),
+            GLib.Variant("s", str(repeat_mode.value)))
+        self._repeat_action.connect("activate", self._repeat_menu_changed)
+        action_group = Gio.SimpleActionGroup()
+        action_group.add_action(self._repeat_action)
+        self.insert_action_group("playertoolbar", action_group)
+
         self._sync_repeat_image()
 
+    def _repeat_menu_changed(
+            self, action: Gio.SimpleAction, new_state: GLib.Variant) -> None:
+        if not self._repeat_action:
+            return
+
+        self._repeat_action.set_state(new_state)
+        new_mode = new_state.get_string()
+        self._player.props.repeat_mode = RepeatMode(int(new_mode))
+        self._repeat_menu_button.props.active = False
+
     @Gtk.Template.Callback()
     def _on_progress_value_changed(self, progress_scale):
         seconds = int(progress_scale.get_value() / 60)
diff --git a/gnomemusic/window.py b/gnomemusic/window.py
index 91f28c1c3..efe8c0973 100644
--- a/gnomemusic/window.py
+++ b/gnomemusic/window.py
@@ -77,7 +77,6 @@ class Window(Handy.ApplicationWindow):
             "selected-songs-count", self, "selected-songs-count")
 
         self._settings = app.props.settings
-        self.add_action(self._settings.create_action('repeat'))
         select_all = Gio.SimpleAction.new('select_all', None)
         select_all.connect('activate', self._select_all)
         self.add_action(select_all)


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