diff --git a/widget/Media.tsx b/widget/Media.tsx index d429e10..c02fb5d 100644 --- a/widget/Media.tsx +++ b/widget/Media.tsx @@ -13,6 +13,8 @@ function VinylRecord({ player, size }: { player: any; size: number }) { $={(box) => { let angle = 0 let timerId: number | null = null + let playing = + player.playback_status === AstalMpris.PlaybackStatus.PLAYING const rotProvider = new Gtk.CssProvider() box @@ -39,6 +41,11 @@ function VinylRecord({ player, size }: { player: any; size: number }) { } } + const syncSpin = () => { + if (playing && box.get_mapped()) startSpin() + else stopSpin() + } + const frame = new Gtk.Frame() const provider = new Gtk.CssProvider() provider.load_from_string(` @@ -73,21 +80,23 @@ function VinylRecord({ player, size }: { player: any; size: number }) { } } updateArt() - - if (player.playback_status === AstalMpris.PlaybackStatus.PLAYING) { - startSpin() - } applyAngle() const artConn = player.connect("notify::cover-art", updateArt) const statusConn = player.connect("notify::playback-status", () => { - if (player.playback_status === AstalMpris.PlaybackStatus.PLAYING) { - startSpin() - } else { - stopSpin() - } + playing = + player.playback_status === AstalMpris.PlaybackStatus.PLAYING + syncSpin() }) + // Keep the timer stopped while the record is hidden (other players, + // closed popover, or the whole bar) to avoid burning CPU. + const mapConn = box.connect("map", () => { + if (playing) startSpin() + }) + const unmapConn = box.connect("unmap", stopSpin) + syncSpin() + onCleanup(() => { stopSpin() try { @@ -96,6 +105,12 @@ function VinylRecord({ player, size }: { player: any; size: number }) { try { player.disconnect(statusConn) } catch {} + try { + box.disconnect(mapConn) + } catch {} + try { + box.disconnect(unmapConn) + } catch {} }) }} />