diff --git a/lib/shared/media.ts b/lib/shared/media.ts new file mode 100644 index 0000000..733fdcf --- /dev/null +++ b/lib/shared/media.ts @@ -0,0 +1,34 @@ +import { MprisPlayer } from "types/service/mpris"; +const mpris = await Service.import("mpris"); + +export const getCurrentPlayer = (activePlayer: MprisPlayer = mpris.players[0]): MprisPlayer => { + const statusOrder = { + Playing: 1, + Paused: 2, + Stopped: 3, + }; + + if (mpris.players.length === 0) { + return mpris.players[0]; + } + + const isPlaying = mpris.players.some( + (p) => p["play-back-status"] === "Playing", + ); + + const playerStillExists = mpris.players.some( + (p) => activePlayer["bus-name"] === p["bus-name"], + ); + + const nextPlayerUp = mpris.players.sort( + (a, b) => + statusOrder[a["play-back-status"]] - + statusOrder[b["play-back-status"]], + )[0]; + + if (isPlaying || !playerStillExists) { + return nextPlayerUp; + } + + return activePlayer; +} diff --git a/modules/bar/media/index.ts b/modules/bar/media/index.ts index 3d6cdbf..83227a3 100644 --- a/modules/bar/media/index.ts +++ b/modules/bar/media/index.ts @@ -2,38 +2,20 @@ import Gdk from 'gi://Gdk?version=3.0'; const mpris = await Service.import("mpris"); import { openMenu } from "../utils.js"; import options from "options"; +import { Mpris } from 'types/service/mpris.js'; +import { getCurrentPlayer } from 'lib/shared/media.js'; const { show_artist, truncation, truncation_size } = options.bar.media; const Media = () => { const activePlayer = Variable(mpris.players[0]); - mpris.connect("changed", (value) => { - const statusOrder = { - Playing: 1, - Paused: 2, - Stopped: 3, - }; - - if (value.players.length === 0) { - activePlayer.value = mpris.players[0]; - return; - } - - const isPlaying = value.players.find( - (p) => p["play-back-status"] === "Playing", - ); - - if (isPlaying) { - activePlayer.value = value.players.sort( - (a, b) => - statusOrder[a["play-back-status"]] - - statusOrder[b["play-back-status"]], - )[0]; - } + mpris.connect("changed", () => { + const curPlayer = getCurrentPlayer(activePlayer.value); + activePlayer.value = curPlayer; }); - const getIconForPlayer = (playerName: string) => { + const getIconForPlayer = (playerName: string): string => { const windowTitleMap = [ ["Mozilla Firefox", "󰈹 "], ["Microsoft Edge", "󰇩 "], @@ -63,7 +45,7 @@ const Media = () => { songIcon.value = getIconForPlayer(identity); return track_title.length === 0 ? `No media playing...` - : truncation.value + : truncation.value ? `${track_title + trackArtist}`.substring(0, truncation_size.value) : `${track_title + trackArtist}`; } else { @@ -104,4 +86,4 @@ const Media = () => { }; }; -export { Media }; \ No newline at end of file +export { Media }; diff --git a/modules/menus/media/components/bar.ts b/modules/menus/media/components/bar.ts index 75914ad..c6dcd37 100644 --- a/modules/menus/media/components/bar.ts +++ b/modules/menus/media/components/bar.ts @@ -23,7 +23,7 @@ const Bar = (getPlayerInfo: Function) => { const update = () => { const foundPlayer = getPlayerInfo(media); if (foundPlayer !== undefined) { - const value = foundPlayer.position / foundPlayer.length; + const value = foundPlayer.length ? foundPlayer.position / foundPlayer.length : 0; self.value = value > 0 ? value : 0; } else { self.value = 0; diff --git a/modules/menus/media/media.ts b/modules/menus/media/media.ts index 959157d..8d4d35e 100644 --- a/modules/menus/media/media.ts +++ b/modules/menus/media/media.ts @@ -18,17 +18,23 @@ const Media = () => { (p) => p["play-back-status"] === "Playing", ); - if (isPlaying) { - curPlayer.value = media.players.sort( - (a, b) => - statusOrder[a["play-back-status"]] - - statusOrder[b["play-back-status"]], - )[0].name; + const playerStillExists = media.players.some( + (p) => curPlayer.value === p["bus-name"], + ); + + const nextPlayerUp = media.players.sort( + (a, b) => + statusOrder[a["play-back-status"]] - + statusOrder[b["play-back-status"]], + )[0].bus_name; + + if (isPlaying || !playerStillExists) { + curPlayer.value = nextPlayerUp; } }); const getPlayerInfo = (): MprisPlayer => { - return media.players.find((p) => p.name === curPlayer.value) || media.players[0]; + return media.players.find((p) => p.bus_name === curPlayer.value) || media.players[0]; }; return Widget.Box({