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");
|
const mpris = await Service.import("mpris");
|
||||||
import { openMenu } from "../utils.js";
|
import { openMenu } from "../utils.js";
|
||||||
import options from "options";
|
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 { show_artist, truncation, truncation_size } = options.bar.media;
|
||||||
|
|
||||||
const Media = () => {
|
const Media = () => {
|
||||||
const activePlayer = Variable(mpris.players[0]);
|
const activePlayer = Variable(mpris.players[0]);
|
||||||
|
|
||||||
mpris.connect("changed", (value) => {
|
mpris.connect("changed", () => {
|
||||||
const statusOrder = {
|
const curPlayer = getCurrentPlayer(activePlayer.value);
|
||||||
Playing: 1,
|
activePlayer.value = curPlayer;
|
||||||
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];
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const getIconForPlayer = (playerName: string) => {
|
const getIconForPlayer = (playerName: string): string => {
|
||||||
const windowTitleMap = [
|
const windowTitleMap = [
|
||||||
["Mozilla Firefox", " "],
|
["Mozilla Firefox", " "],
|
||||||
["Microsoft Edge", " "],
|
["Microsoft Edge", " "],
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ const Bar = (getPlayerInfo: Function) => {
|
|||||||
const update = () => {
|
const update = () => {
|
||||||
const foundPlayer = getPlayerInfo(media);
|
const foundPlayer = getPlayerInfo(media);
|
||||||
if (foundPlayer !== undefined) {
|
if (foundPlayer !== undefined) {
|
||||||
const value = foundPlayer.position / foundPlayer.length;
|
const value = foundPlayer.length ? foundPlayer.position / foundPlayer.length : 0;
|
||||||
self.value = value > 0 ? value : 0;
|
self.value = value > 0 ? value : 0;
|
||||||
} else {
|
} else {
|
||||||
self.value = 0;
|
self.value = 0;
|
||||||
|
|||||||
@@ -18,17 +18,23 @@ const Media = () => {
|
|||||||
(p) => p["play-back-status"] === "Playing",
|
(p) => p["play-back-status"] === "Playing",
|
||||||
);
|
);
|
||||||
|
|
||||||
if (isPlaying) {
|
const playerStillExists = media.players.some(
|
||||||
curPlayer.value = media.players.sort(
|
(p) => curPlayer.value === p["bus-name"],
|
||||||
|
);
|
||||||
|
|
||||||
|
const nextPlayerUp = media.players.sort(
|
||||||
(a, b) =>
|
(a, b) =>
|
||||||
statusOrder[a["play-back-status"]] -
|
statusOrder[a["play-back-status"]] -
|
||||||
statusOrder[b["play-back-status"]],
|
statusOrder[b["play-back-status"]],
|
||||||
)[0].name;
|
)[0].bus_name;
|
||||||
|
|
||||||
|
if (isPlaying || !playerStillExists) {
|
||||||
|
curPlayer.value = nextPlayerUp;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const getPlayerInfo = (): MprisPlayer => {
|
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({
|
return Widget.Box({
|
||||||
|
|||||||
Reference in New Issue
Block a user