From 2913fb020fec90609a7049d8e66fc4acd8573ba7 Mon Sep 17 00:00:00 2001 From: pascalyazid Date: Fri, 27 Nov 2026 18:55:33 +0100 Subject: [PATCH] perf(media): stop vinyl animation when hidden Only run the rotation timer while the record is mapped and playing, and clean up the map and unmap handlers. --- widget/Media.tsx | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) 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 {} }) }} />