Made the media player more responsive and accurate. (#95)
This commit is contained in:
34
lib/shared/media.ts
Normal file
34
lib/shared/media.ts
Normal file
@@ -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;
|
||||
}
|
||||
@@ -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", " "],
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user