Skip to content

Commit

Permalink
Added preference "queue/remove_item_after_played" to keep playing tra… (
Browse files Browse the repository at this point in the history
#808)

* Added preference "queue/remove_item_after_played" to keep playing track in queue (Fixes: #662)
  • Loading branch information
luzip665 committed Dec 11, 2022
1 parent c5827e6 commit 3d57245
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 29 deletions.
37 changes: 26 additions & 11 deletions data/ui/preferences/playback.ui
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,21 @@
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="queue/remove_item_after_played">
<property name="label" translatable="yes">Remove after playback</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_underline">True</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">8</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="queue/disable_new_track_when_playing">
<property name="label" translatable="yes">Disallow playing new tracks when another track is playing</property>
Expand All @@ -228,7 +243,7 @@
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">8</property>
<property name="top_attach">9</property>
<property name="width">2</property>
</packing>
</child>
Expand All @@ -242,7 +257,7 @@
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">10</property>
<property name="top_attach">11</property>
<property name="width">2</property>
</packing>
</child>
Expand All @@ -258,7 +273,7 @@
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">11</property>
<property name="top_attach">12</property>
</packing>
</child>
<child>
Expand All @@ -274,7 +289,7 @@
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">11</property>
<property name="top_attach">12</property>
</packing>
</child>
<child>
Expand All @@ -289,7 +304,7 @@
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">15</property>
<property name="top_attach">16</property>
</packing>
</child>
<child>
Expand All @@ -304,7 +319,7 @@
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">15</property>
<property name="top_attach">16</property>
</packing>
</child>
<child>
Expand All @@ -319,7 +334,7 @@
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">14</property>
<property name="top_attach">15</property>
<property name="width">2</property>
</packing>
</child>
Expand All @@ -335,7 +350,7 @@
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">13</property>
<property name="top_attach">15</property>
</packing>
</child>
<child>
Expand All @@ -350,7 +365,7 @@
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">13</property>
<property name="top_attach">14</property>
</packing>
</child>
<child>
Expand All @@ -364,7 +379,7 @@
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">12</property>
<property name="top_attach">13</property>
<property name="width">2</property>
</packing>
</child>
Expand Down Expand Up @@ -417,7 +432,7 @@
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">9</property>
<property name="top_attach">10</property>
<property name="width">2</property>
</packing>
</child>
Expand Down
55 changes: 37 additions & 18 deletions xl/player/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,23 @@ def __init__(self, player, name, location=None):
event.add_callback(self._on_option_set, '%s_option_set' % name)

self.__opt_remove_item_when_played = '%s/remove_item_when_played' % name
self.__opt_remove_item_after_played = '%s/remove_item_after_played' % name
self.__opt_disable_new_track_when_playing = (
'%s/disable_new_track_when_playing' % name
)
self.__opt_enqueue_begins_playback = '%s/enqueue_begins_playback' % name

self._on_option_set(None, settings, self.__opt_remove_item_when_played)
self._on_option_set(None, settings, self.__opt_remove_item_after_played)
self._on_option_set(None, settings, self.__opt_disable_new_track_when_playing)

def _on_option_set(self, evtype, settings, option):
if option == self.__opt_remove_item_when_played:
self.__remove_item_on_playback = settings.get_option(option, True)
if len(self):
self.__queue_has_tracks = True
if option == self.__opt_remove_item_after_played:
self.__remove_item_after_playback = settings.get_option(option, True)
elif option == self.__opt_disable_new_track_when_playing:
self.__disable_new_track_when_playing = settings.get_option(option, False)

Expand Down Expand Up @@ -127,7 +131,11 @@ def get_next(self):
"""
if self.__queue_has_tracks and len(self):
if self.__remove_item_on_playback:
return self[0]
track = self._calculate_next_track()
if track is None:
# pop the last track
self.pop(0)
return track
else:
return playlist.Playlist.get_next(self)
elif self.current_playlist is not self:
Expand All @@ -151,13 +159,19 @@ def next(self, autoplay=True, track=None):
"""
if track is None:
if self.__queue_has_tracks:
if not self.__remove_item_on_playback:
track = super().next()
else:
try:
if self.__remove_item_on_playback:
if self.__remove_item_after_playback:
track = self._calculate_next_track()
try:
self.pop(self.current_position)
except IndexError:
pass
self.current_position = 0
else:
track = self.pop(0)
except IndexError:
pass
self.current_position = -1
else:
track = super().next()

# reached the end of the internal queue, don't repeat
if track is None:
Expand Down Expand Up @@ -215,16 +229,6 @@ def get_current(self):
current = self.current_playlist.get_current()
return current

def get_current_position(self):
if self.__remove_item_on_playback:
return 0
else:
return playlist.Playlist.get_current_position(self)

def set_current_position(self, position):
if not self.__remove_item_on_playback:
return playlist.Playlist.set_current_position(self, position)

def is_play_enabled(self):
''':returns: True when calling play() will have no effect'''
return not (self.player.is_playing() and self.__disable_new_track_when_playing)
Expand All @@ -244,7 +248,7 @@ def play(self, track=None):
track = self.current
if track:
self.player.play(track)
if self.__remove_item_on_playback:
if self.__remove_item_on_playback and not self.__remove_item_after_playback:
try:
del self[self.index(track)]
except ValueError:
Expand Down Expand Up @@ -347,3 +351,18 @@ def _do_restore_player_state(self, state):
self.player.play(
self.current_playlist.get_current(), start_at=start_at, paused=paused
)

def _calculate_next_track(self):
player_track = self.player.current
try:
real_position = self.index(player_track)
except:
real_position = -1

if real_position == 0:
if len(self) > 1:
return self[1]
elif len(self) > 0:
return self[0]

return None
10 changes: 10 additions & 0 deletions xlgui/preferences/playback.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ class RemoveQueuedItemWhenPlayed(widgets.CheckPreference):
name = 'queue/remove_item_when_played'


class RemoveQueuedItemAfterPlayed(widgets.CheckPreference, widgets.CheckConditional):
default = False
name = 'queue/remove_item_after_played'
condition_preference_name = 'queue/remove_item_when_played'

def __init__(self, preferences, widget):
widgets.CheckPreference.__init__(self, preferences, widget)
widgets.CheckConditional.__init__(self)


class DisableNewTrackWhenPlaying(widgets.CheckPreference):
default = False
name = 'queue/disable_new_track_when_playing'
Expand Down

0 comments on commit 3d57245

Please sign in to comment.