2023-11-21 01:29:46 -05:00
|
|
|
import Mpris from 'resource:///com/github/Aylur/ags/service/mpris.js';
|
2023-10-31 08:32:40 -04:00
|
|
|
import Variable from 'resource:///com/github/Aylur/ags/variable.js';
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-10-31 08:32:40 -04:00
|
|
|
import { Box, CenterBox } from 'resource:///com/github/Aylur/ags/widget.js';
|
2023-09-15 23:22:16 -04:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
import * as mpris from './mpris.js';
|
2023-09-26 10:43:37 -04:00
|
|
|
import PlayerGesture from './gesture.js';
|
2023-11-21 01:29:46 -05:00
|
|
|
import Separator from '../misc/separator.js';
|
2023-10-17 13:47:02 -04:00
|
|
|
|
|
|
|
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-09-17 17:27:23 -04:00
|
|
|
|
2023-10-02 12:06:35 -04:00
|
|
|
|
2023-12-18 18:00:30 -05:00
|
|
|
/**
|
|
|
|
* @param {Player} player
|
|
|
|
* @param {Overlay} overlay
|
|
|
|
*/
|
2023-11-28 12:24:58 -05:00
|
|
|
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',
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
children: [
|
2023-11-28 12:24:58 -05:00
|
|
|
mpris.PlayerIcon(player, overlay),
|
2023-10-20 23:11:21 -04:00
|
|
|
],
|
2023-09-15 23:22:16 -04:00
|
|
|
});
|
|
|
|
|
2023-12-18 18:00:30 -05:00
|
|
|
/**
|
|
|
|
* @param {Player} player
|
|
|
|
* @param {Variable} colors
|
|
|
|
*/
|
|
|
|
const Center = (player, colors) => Box({
|
|
|
|
class_name: 'center',
|
2023-10-20 23:11:21 -04:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
children: [
|
2023-10-20 23:11:21 -04:00
|
|
|
CenterBox({
|
2023-12-18 18:00:30 -05:00
|
|
|
// @ts-expect-error
|
2023-10-20 23:11:21 -04:00
|
|
|
vertical: true,
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
children: [
|
|
|
|
Box({
|
2023-12-18 18:00:30 -05:00
|
|
|
class_name: 'metadata',
|
2023-10-20 23:11:21 -04:00
|
|
|
vertical: true,
|
2023-11-06 18:37:23 -05:00
|
|
|
hpack: 'start',
|
|
|
|
vpack: 'center',
|
2023-10-20 23:11:21 -04:00
|
|
|
hexpand: true,
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
children: [
|
|
|
|
mpris.TitleLabel(player),
|
|
|
|
mpris.ArtistLabel(player),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
],
|
2023-09-15 23:22:16 -04:00
|
|
|
}),
|
2023-09-17 17:27:23 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
CenterBox({
|
2023-12-18 18:00:30 -05:00
|
|
|
// @ts-expect-error
|
2023-10-20 23:11:21 -04:00
|
|
|
vertical: true,
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
children: [
|
|
|
|
null,
|
2023-12-18 18:00:30 -05:00
|
|
|
mpris.PlayPauseButton(player, colors),
|
2023-10-20 23:11:21 -04:00
|
|
|
null,
|
|
|
|
],
|
|
|
|
}),
|
2023-09-17 17:27:23 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
],
|
2023-09-15 23:22:16 -04:00
|
|
|
});
|
|
|
|
|
2023-12-18 18:00:30 -05:00
|
|
|
/**
|
|
|
|
* @param {Player} player
|
|
|
|
* @param {Variable} colors
|
|
|
|
*/
|
|
|
|
const Bottom = (player, colors) => Box({
|
|
|
|
class_name: 'bottom',
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
children: [
|
2023-12-18 18:00:30 -05:00
|
|
|
mpris.PreviousButton(player, colors),
|
2023-11-21 01:29:46 -05:00
|
|
|
Separator(SPACING),
|
2023-10-17 13:47:02 -04:00
|
|
|
|
2023-12-18 18:00:30 -05:00
|
|
|
mpris.PositionSlider(player, colors),
|
2023-11-21 01:29:46 -05:00
|
|
|
Separator(SPACING),
|
2023-10-17 13:47:02 -04:00
|
|
|
|
2023-12-18 18:00:30 -05:00
|
|
|
mpris.NextButton(player, colors),
|
2023-11-21 01:29:46 -05:00
|
|
|
Separator(SPACING),
|
2023-10-17 13:47:02 -04:00
|
|
|
|
2023-12-18 18:00:30 -05:00
|
|
|
mpris.ShuffleButton(player, colors),
|
2023-11-21 01:29:46 -05:00
|
|
|
Separator(SPACING),
|
2023-09-17 17:27:23 -04:00
|
|
|
|
2023-12-18 18:00:30 -05:00
|
|
|
mpris.LoopButton(player, colors),
|
2023-10-20 23:11:21 -04:00
|
|
|
],
|
2023-09-17 17:27:23 -04:00
|
|
|
});
|
|
|
|
|
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}`,
|
2023-11-28 00:09:52 -05:00
|
|
|
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),
|
2023-11-28 00:09:52 -05:00
|
|
|
});
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-11-28 00:09:52 -05:00
|
|
|
widget.visible = false;
|
|
|
|
|
|
|
|
return widget;
|
|
|
|
};
|
2023-09-15 23:22:16 -04:00
|
|
|
|
2023-11-28 12:24:58 -05:00
|
|
|
export default () => {
|
|
|
|
const content = PlayerGesture({
|
2023-12-18 18:00:30 -05:00
|
|
|
attribute: {
|
|
|
|
players: new Map(),
|
|
|
|
setup: false,
|
|
|
|
},
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-12-17 00:01:58 -05:00
|
|
|
setup: (self) => {
|
|
|
|
self
|
2023-12-18 18:00:30 -05:00
|
|
|
.hook(Mpris, (overlay, bus_name) => {
|
|
|
|
const players = overlay.attribute.players;
|
|
|
|
|
|
|
|
if (players.has(bus_name)) {
|
2023-12-17 00:01:58 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-18 18:00:30 -05:00
|
|
|
// Sometimes the signal doesn't give the bus_name
|
|
|
|
if (!bus_name) {
|
2023-12-17 00:01:58 -05:00
|
|
|
const player = Mpris.players.find((p) => {
|
2023-12-18 18:00:30 -05:00
|
|
|
return !players.has(p.bus_name);
|
2023-12-17 00:01:58 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
if (player) {
|
2023-12-18 18:00:30 -05:00
|
|
|
bus_name = player.bus_name;
|
2023-12-17 00:01:58 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the one on top so we can move it up later
|
2023-12-18 18:00:30 -05:00
|
|
|
const previousFirst = overlay.attribute.list().at(-1);
|
2023-12-17 00:01:58 -05:00
|
|
|
|
|
|
|
// Make the new player
|
2023-12-18 18:00:30 -05:00
|
|
|
const player = Mpris.getPlayer(bus_name);
|
|
|
|
const Colors = Variable(null);
|
2023-12-17 00:01:58 -05:00
|
|
|
|
2023-12-18 18:00:30 -05:00
|
|
|
if (!player) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
players.set(
|
|
|
|
bus_name,
|
|
|
|
// @ts-expect-error
|
|
|
|
PlayerBox(player, Colors, content.child),
|
2023-12-17 00:01:58 -05:00
|
|
|
);
|
2023-12-18 18:00:30 -05:00
|
|
|
overlay.overlays = Array.from(players.values())
|
2023-12-17 00:01:58 -05:00
|
|
|
.map((widget) => widget);
|
|
|
|
|
2023-12-18 18:00:30 -05:00
|
|
|
const includes = overlay.attribute
|
|
|
|
.includesWidget(previousFirst);
|
|
|
|
|
2023-12-17 00:01:58 -05:00
|
|
|
// Select favorite player at startup
|
2023-12-18 18:00:30 -05:00
|
|
|
if (!overlay.attribute.setup && players.has(FAVE_PLAYER)) {
|
|
|
|
overlay.attribute.moveToTop(players.get(FAVE_PLAYER));
|
|
|
|
overlay.attribute.setup = true;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-12-17 00:01:58 -05:00
|
|
|
|
|
|
|
// Move previousFirst on top again
|
2023-12-18 18:00:30 -05:00
|
|
|
else if (includes) {
|
|
|
|
overlay.attribute.moveToTop(previousFirst);
|
2023-12-17 00:01:58 -05:00
|
|
|
}
|
|
|
|
}, 'player-added')
|
|
|
|
|
2023-12-18 18:00:30 -05:00
|
|
|
.hook(Mpris, (overlay, bus_name) => {
|
|
|
|
const players = overlay.attribute.players;
|
|
|
|
|
|
|
|
if (!bus_name || !players.has(bus_name)) {
|
2023-10-20 23:11:21 -04:00
|
|
|
return;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-12-17 00:01:58 -05:00
|
|
|
|
|
|
|
// Get the one on top so we can move it up later
|
2023-12-18 18:00:30 -05:00
|
|
|
const previousFirst = overlay.attribute.list().at(-1);
|
2023-12-17 00:01:58 -05:00
|
|
|
|
|
|
|
// Remake overlays without deleted one
|
2023-12-18 18:00:30 -05:00
|
|
|
players.delete(bus_name);
|
|
|
|
overlay.overlays = Array.from(players.values())
|
2023-12-17 00:01:58 -05:00
|
|
|
.map((widget) => widget);
|
|
|
|
|
|
|
|
// Move previousFirst on top again
|
2023-12-18 18:00:30 -05:00
|
|
|
const includes = overlay.attribute
|
|
|
|
.includesWidget(previousFirst);
|
|
|
|
|
|
|
|
if (includes) {
|
|
|
|
overlay.attribute.moveToTop(previousFirst);
|
2023-12-17 00:01:58 -05:00
|
|
|
}
|
|
|
|
}, 'player-closed');
|
|
|
|
},
|
2023-11-28 12:24:58 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
return Box({
|
2023-12-18 18:00:30 -05:00
|
|
|
class_name: 'media',
|
2023-11-28 12:24:58 -05:00
|
|
|
child: content,
|
|
|
|
});
|
|
|
|
};
|