feat: persist play/pause state — auto-resume playback or pause-on-load on restart

This commit is contained in:
Maksim Totmin 2026-06-01 01:25:34 +07:00
parent 06f6529cbe
commit ec797b8074
2 changed files with 14 additions and 5 deletions

View File

@ -179,6 +179,8 @@ class ConfigLoader:
self.config.set('Playback', 'last_position_ms', '0') self.config.set('Playback', 'last_position_ms', '0')
if not self.config.has_option('Playback', 'last_volume'): if not self.config.has_option('Playback', 'last_volume'):
self.config.set('Playback', 'last_volume', '80') self.config.set('Playback', 'last_volume', '80')
if not self.config.has_option('Playback', 'last_playing'):
self.config.set('Playback', 'last_playing', 'false')
def get(self, section: str, option: str, fallback: Any = None) -> Any: def get(self, section: str, option: str, fallback: Any = None) -> Any:
""" """

View File

@ -297,6 +297,7 @@ class MainWindow(QMainWindow):
self._saved_volume: int = 80 self._saved_volume: int = 80
self._saved_track_path: str = "" self._saved_track_path: str = ""
self._saved_position_ms: int = 0 self._saved_position_ms: int = 0
self._saved_playing: bool = False
self._pending_seek_ms: int = 0 self._pending_seek_ms: int = 0
self._setup_ui() self._setup_ui()
@ -353,6 +354,7 @@ class MainWindow(QMainWindow):
self._saved_volume = config.get_int("Playback", "last_volume", fallback=80) self._saved_volume = config.get_int("Playback", "last_volume", fallback=80)
self._saved_track_path = config.get("Playback", "last_track_path", fallback="") self._saved_track_path = config.get("Playback", "last_track_path", fallback="")
self._saved_position_ms = config.get_int("Playback", "last_position_ms", fallback=0) self._saved_position_ms = config.get_int("Playback", "last_position_ms", fallback=0)
self._saved_playing = config.get_boolean("Playback", "last_playing", fallback=False)
def _save_settings(self): def _save_settings(self):
config = self.config_loader config = self.config_loader
@ -377,6 +379,7 @@ class MainWindow(QMainWindow):
if duration > 0 and position > duration - 2000: if duration > 0 and position > duration - 2000:
position = 0 position = 0
config.set("Playback", "last_position_ms", str(position)) config.set("Playback", "last_position_ms", str(position))
config.set("Playback", "last_playing", str(self._is_playing).lower())
else: else:
config.set("Playback", "last_track_path", "") config.set("Playback", "last_track_path", "")
config.set("Playback", "last_position_ms", "0") config.set("Playback", "last_position_ms", "0")
@ -832,7 +835,7 @@ class MainWindow(QMainWindow):
self.player.durationChanged.connect(self._on_media_duration_changed) self.player.durationChanged.connect(self._on_media_duration_changed)
self.player.mediaStatusChanged.connect(self._on_media_status_changed) self.player.mediaStatusChanged.connect(self._on_media_status_changed)
def _play_track_from_index(self, index: int, start_position_ms: int = 0): def _play_track_from_index(self, index: int, start_position_ms: int = 0, auto_play: bool = True):
"""Start playing a track from the library table by row index.""" """Start playing a track from the library table by row index."""
if index < 0 or index >= self.library_table.rowCount(): if index < 0 or index >= self.library_table.rowCount():
return return
@ -845,9 +848,13 @@ class MainWindow(QMainWindow):
self.current_playback_index = index self.current_playback_index = index
self.player.setSource(QUrl.fromLocalFile(track_data["path"])) self.player.setSource(QUrl.fromLocalFile(track_data["path"]))
self.player.play() if auto_play:
self._is_playing = True self.player.play()
self.play_btn.setText("\u23F8") self._is_playing = True
self.play_btn.setText("\u23F8")
else:
self._is_playing = False
self.play_btn.setText("\u25B6")
self.now_playing_label.setText(f"{track_data['artist']} \u2014 {track_data['title']}") self.now_playing_label.setText(f"{track_data['artist']} \u2014 {track_data['title']}")
self.now_playing_label.setStyleSheet( self.now_playing_label.setStyleSheet(
@ -1079,7 +1086,7 @@ class MainWindow(QMainWindow):
continue continue
_is_ready, track_data = data_item.data(Qt.ItemDataRole.UserRole) _is_ready, track_data = data_item.data(Qt.ItemDataRole.UserRole)
if track_data and track_data.get("path", "") == self._saved_track_path: if track_data and track_data.get("path", "") == self._saved_track_path:
self._play_track_from_index(row, start_position_ms=self._saved_position_ms) self._play_track_from_index(row, start_position_ms=self._saved_position_ms, auto_play=self._saved_playing)
return return
def _extract_ready_track(self, fpath: str, fname: str, handler) -> Dict[str, Any]: def _extract_ready_track(self, fpath: str, fname: str, handler) -> Dict[str, Any]: