From 9a3d095081500dc6e7b3e9c955e1a82e8b897486 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Fri, 23 Sep 2022 09:16:54 -0400 Subject: [PATCH] emmental: Wire up the current track properties I attach these to the Now Playing card so the "add to favorites" button can be correctly marked as sensitive and enabled when a Track is selected for playback. Additionally, I look for a notification from the Track table to say it has been loaded. This lets me set the Player to load up the current_track if one is set. Signed-off-by: Anna Schumaker --- emmental/__init__.py | 26 +++++++++++++++++++++++++- tests/test_emmental.py | 3 +++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/emmental/__init__.py b/emmental/__init__.py index 8c9b31d..5c4d3a5 100644 --- a/emmental/__init__.py +++ b/emmental/__init__.py @@ -39,13 +39,20 @@ class Application(Adw.Application): self.add_main_option_entries([options.Version]) def __load_file(self, file: pathlib.Path) -> None: + self.__stop_current_track() self.player.stop() self.player.file = file self.player.play() def __load_path(self, src: GObject.GObject, path: pathlib.Path) -> None: + if (track := self.db.tracks.lookup(path=path)) is not None: + self.__load_track(track) self.__load_file(path) + def __load_track(self, track: GObject.GObject) -> None: + self.__load_file(track.path) + track.start() + def __on_seek(self, nowplay: nowplaying.Card, newpos: float) -> None: """Handle a seek event.""" self.player.seek(newpos) @@ -64,6 +71,16 @@ class Application(Adw.Application): mode = "track" if mode == "auto" else mode self.player.set_replaygain(enabled, mode) + def __stop_current_track(self) -> None: + if self.db.tracks.current_track is not None: + self.db.tracks.current_track.stop(self.player.playtime) + + def __tracks_table_loaded(self, track_table, param) -> None: + if track_table.current_track is not None: + self.player.file = track_table.current_track.path + self.player.pause() + track_table.current_track.start() + def build_header(self) -> header.Header: """Build a new header instance.""" hdr = header.Header(sql=self.db, title=VERSION_STRING) @@ -87,6 +104,10 @@ class Application(Adw.Application): for prop in ["title", "album", "artist", "album-artist", "playing", "position", "duration", "artwork", "have-track"]: self.player.bind_property(prop, playing, prop) + self.db.tracks.bind_property("have-current-track", + playing, "have-db-track") + self.db.tracks.bind_property("current-favorite", playing, "favorite", + GObject.BindingFlags.BIDIRECTIONAL) self.db.settings.bind_setting("now-playing.prefer-artist", playing, "prefer-artist") @@ -159,6 +180,7 @@ class Application(Adw.Application): gsetup.add_style() musicbrainzngs.set_useragent(f"emmental{gsetup.DEBUG_STR}", f"{MAJOR_VERSION}.{MINOR_VERSION}") + self.db.tracks.connect("notify::loaded", self.__tracks_table_loaded) self.db.load() self.win = self.build_window() @@ -173,7 +195,9 @@ class Application(Adw.Application): def do_open(self, files: list, n_files: int, hint: str) -> None: """Play an audio file passed from the command line.""" if n_files > 0: - self.__load_file(pathlib.Path(files[0].get_path())) + path = pathlib.Path(files[0].get_path()) + self.db.tracks.mark_path_active(path) + self.__load_path(None, path) self.activate() def do_shutdown(self) -> None: diff --git a/tests/test_emmental.py b/tests/test_emmental.py index dccf7eb..57c8221 100644 --- a/tests/test_emmental.py +++ b/tests/test_emmental.py @@ -125,6 +125,9 @@ class TestEmmental(unittest.TestCase): self.application.player.set_property(property, value) self.assertEqual(win.now_playing.get_property(property), value) + self.application.db.tracks.have_current_track = True + self.assertTrue(win.now_playing.have_db_track) + win.now_playing.emit("play") play_func.assert_called() win.now_playing.emit("pause")