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) => {
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 {}
})
}}
/>