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




commit a7005b095eaa6c00605252c8d9da353f2cb45d4b
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 | 19 ++++++++++++++++++-
 gnomemusic/window.py                |  1 -
 3 files changed, 18 insertions(+), 8 deletions(-)
---
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index b717786bd..7e6ddf289 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..94f61895a 100644
--- a/gnomemusic/widgets/playertoolbar.py
+++ b/gnomemusic/widgets/playertoolbar.py
@@ -79,10 +79,12 @@ 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: Gio.SimpleAction = Gio.SimpleAction.new_stateful(
+            "repeat", GLib.VariantType.new("s"), GLib.Variant("s", ""))
 
     # FIXME: This is a workaround for not being able to pass the player
     # object via init when using Gtk.Builder.
@@ -114,8 +116,23 @@ 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.set_state(
+            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:
+        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]