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 <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2022-09-23 09:16:54 -04:00
parent 1aec9df0a8
commit 9a3d095081
2 changed files with 28 additions and 1 deletions

View File

@ -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:

View File

@ -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")