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.
This commit is contained in:
pascalyazid committed 2026-11-27 18:55:33 +01:00
1 parent 38c6ce5812
commit 2913fb020f
1 file changed
+24 -9
+24 -9
View File
@@ -13,6 +13,8 @@ function VinylRecord({ player, size }: { player: any; size: number }) {
$={(box) => { $={(box) => {
let angle = 0 let angle = 0
let timerId: number | null = null let timerId: number | null = null
let playing =
player.playback_status === AstalMpris.PlaybackStatus.PLAYING
const rotProvider = new Gtk.CssProvider() const rotProvider = new Gtk.CssProvider()
box 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 frame = new Gtk.Frame()
const provider = new Gtk.CssProvider() const provider = new Gtk.CssProvider()
provider.load_from_string(` provider.load_from_string(`
@@ -73,21 +80,23 @@ function VinylRecord({ player, size }: { player: any; size: number }) {
} }
} }
updateArt() updateArt()
if (player.playback_status === AstalMpris.PlaybackStatus.PLAYING) {
startSpin()
}
applyAngle() applyAngle()
const artConn = player.connect("notify::cover-art", updateArt) const artConn = player.connect("notify::cover-art", updateArt)
const statusConn = player.connect("notify::playback-status", () => { const statusConn = player.connect("notify::playback-status", () => {
if (player.playback_status === AstalMpris.PlaybackStatus.PLAYING) { playing =
startSpin() player.playback_status === AstalMpris.PlaybackStatus.PLAYING
} else { syncSpin()
stopSpin()
}
}) })
// 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(() => { onCleanup(() => {
stopSpin() stopSpin()
try { try {
@@ -96,6 +105,12 @@ function VinylRecord({ player, size }: { player: any; size: number }) {
try { try {
player.disconnect(statusConn) player.disconnect(statusConn)
} catch {} } catch {}
try {
box.disconnect(mapConn)
} catch {}
try {
box.disconnect(unmapConn)
} catch {}
}) })
}} }}
/> />