nixos-configs/devices/wim/config/ags/js/media-player/player.js

231 lines
6.5 KiB
JavaScript
Raw Normal View History

import Mpris from 'resource:///com/github/Aylur/ags/service/mpris.js';
import Variable from 'resource:///com/github/Aylur/ags/variable.js';
import { Box, CenterBox } from 'resource:///com/github/Aylur/ags/widget.js';
import * as mpris from './mpris.js';
import PlayerGesture from './gesture.js';
import Separator from '../misc/separator.js';
const FAVE_PLAYER = 'org.mpris.MediaPlayer2.spotify';
2023-12-18 18:00:30 -05:00
const SPACING = 8;
/**
* @typedef {import('types/service/mpris').MprisPlayer} Player
* @typedef {import('types/widgets/overlay').default} Overlay
* @typedef {import('types/variable').Variable} Variable
*/
2023-12-18 18:00:30 -05:00
/**
* @param {Player} player
* @param {Overlay} overlay
*/
const Top = (player, overlay) => Box({
2023-12-18 18:00:30 -05:00
class_name: 'top',
2023-11-06 18:37:23 -05:00
hpack: 'start',
vpack: 'start',
children: [
mpris.PlayerIcon(player, overlay),
],
});
2023-12-18 18:00:30 -05:00
/**
* @param {Player} player
* @param {Variable} colors
*/
const Center = (player, colors) => Box({
class_name: 'center',
children: [
CenterBox({
2023-12-18 18:00:30 -05:00
// @ts-expect-error
vertical: true,
children: [
Box({
2023-12-18 18:00:30 -05:00
class_name: 'metadata',
vertical: true,
2023-11-06 18:37:23 -05:00
hpack: 'start',
vpack: 'center',
hexpand: true,
children: [
mpris.TitleLabel(player),
mpris.ArtistLabel(player),
],
}),
null,
null,
],
}),
CenterBox({
2023-12-18 18:00:30 -05:00
// @ts-expect-error
vertical: true,
children: [
null,
2023-12-18 18:00:30 -05:00
mpris.PlayPauseButton(player, colors),
null,
],
}),
],
});
2023-12-18 18:00:30 -05:00
/**
* @param {Player} player
* @param {Variable} colors
*/
const Bottom = (player, colors) => Box({
class_name: 'bottom',
children: [
2023-12-18 18:00:30 -05:00
mpris.PreviousButton(player, colors),
Separator(SPACING),
2023-12-18 18:00:30 -05:00
mpris.PositionSlider(player, colors),
Separator(SPACING),
2023-12-18 18:00:30 -05:00
mpris.NextButton(player, colors),
Separator(SPACING),
2023-12-18 18:00:30 -05:00
mpris.ShuffleButton(player, colors),
Separator(SPACING),
2023-12-18 18:00:30 -05:00
mpris.LoopButton(player, colors),
],
});
2023-12-18 18:00:30 -05:00
/**
* @param {Player} player
* @param {Variable} colors
* @param {Overlay} overlay
*/
const PlayerBox = (player, colors, overlay) => {
const widget = mpris.CoverArt(player, colors, {
class_name: `player ${player.name}`,
hexpand: true,
2023-12-18 18:00:30 -05:00
start_widget: Top(player, overlay),
center_widget: Center(player, colors),
end_widget: Bottom(player, colors),
});
widget.visible = false;
return widget;
};
export default () => {
const content = PlayerGesture({
2023-12-18 18:00:30 -05:00
attribute: {
players: new Map(),
setup: false,
},
setup: /** @param {any} self */ (self) => {
self
.hook(Mpris,
/**
* @param {Overlay} overlay
* @param {string} bus_name
*/
(overlay, bus_name) => {
const players = overlay.attribute.players;
if (players.has(bus_name)) {
return;
}
2023-12-18 18:00:30 -05:00
// Sometimes the signal doesn't give the bus_name
if (!bus_name) {
const player = Mpris.players.find((p) => {
return !players.has(p.bus_name);
});
if (player) {
bus_name = player.bus_name;
}
else {
return;
}
}
// Get the one on top so we can move it up later
const previousFirst = overlay.attribute.list().at(-1);
// Make the new player
const player = Mpris.getPlayer(bus_name);
const Colors = Variable(null);
if (!player) {
return;
}
players.set(
bus_name,
// @ts-expect-error
PlayerBox(player, Colors, content.child),
);
overlay.overlays = Array.from(players.values())
.map((widget) => widget);
const includes = overlay.attribute
.includesWidget(previousFirst);
// Select favorite player at startup
const attrs = overlay.attribute;
if (!attrs.setup && players.has(FAVE_PLAYER)) {
attrs.moveToTop(players.get(FAVE_PLAYER));
attrs.setup = true;
}
// Move previousFirst on top again
else if (includes) {
attrs.moveToTop(previousFirst);
}
},
'player-added')
.hook(Mpris,
/**
* @param {Overlay} overlay
* @param {string} bus_name
*/
(overlay, bus_name) => {
const players = overlay.attribute.players;
if (!bus_name || !players.has(bus_name)) {
return;
}
// Get the one on top so we can move it up later
const previousFirst = overlay.attribute.list().at(-1);
// Remake overlays without deleted one
players.delete(bus_name);
overlay.overlays = Array.from(players.values())
.map((widget) => widget);
// Move previousFirst on top again
const includes = overlay.attribute
.includesWidget(previousFirst);
if (includes) {
overlay.attribute.moveToTop(previousFirst);
}
},
'player-closed');
},
});
return Box({
2023-12-18 18:00:30 -05:00
class_name: 'media',
child: content,
});
};