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-09-17 17:27:23 -04:00
|
|
|
|
2023-10-02 12:06:35 -04:00
|
|
|
|
2023-11-28 12:24:58 -05:00
|
|
|
const Top = (player, overlay) => Box({
|
2023-10-20 23:11:21 -04:00
|
|
|
className: '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-11-21 01:29:46 -05:00
|
|
|
const Center = (player) => Box({
|
2023-10-20 23:11:21 -04:00
|
|
|
className: 'center',
|
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
children: [
|
2023-10-20 23:11:21 -04:00
|
|
|
CenterBox({
|
|
|
|
vertical: true,
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
children: [
|
|
|
|
Box({
|
|
|
|
className: 'metadata',
|
|
|
|
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({
|
|
|
|
vertical: true,
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
children: [
|
|
|
|
null,
|
|
|
|
mpris.PlayPauseButton(player),
|
|
|
|
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-11-21 01:29:46 -05:00
|
|
|
const SPACING = 8;
|
|
|
|
|
|
|
|
const Bottom = (player) => Box({
|
2023-10-20 23:11:21 -04:00
|
|
|
className: 'bottom',
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
children: [
|
|
|
|
mpris.PreviousButton(player, {
|
2023-11-06 18:37:23 -05:00
|
|
|
vpack: 'end',
|
|
|
|
hpack: 'start',
|
2023-10-20 23:11:21 -04:00
|
|
|
}),
|
2023-11-21 01:29:46 -05:00
|
|
|
Separator(SPACING),
|
2023-10-17 13:47:02 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
mpris.PositionSlider(player),
|
2023-11-21 01:29:46 -05:00
|
|
|
Separator(SPACING),
|
2023-10-17 13:47:02 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
mpris.NextButton(player),
|
2023-11-21 01:29:46 -05:00
|
|
|
Separator(SPACING),
|
2023-10-17 13:47:02 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
mpris.ShuffleButton(player),
|
2023-11-21 01:29:46 -05:00
|
|
|
Separator(SPACING),
|
2023-09-17 17:27:23 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
mpris.LoopButton(player),
|
|
|
|
],
|
2023-09-17 17:27:23 -04:00
|
|
|
});
|
|
|
|
|
2023-11-28 12:24:58 -05:00
|
|
|
const PlayerBox = (player, overlay) => {
|
2023-11-28 00:09:52 -05:00
|
|
|
const widget = mpris.CoverArt(player, {
|
|
|
|
className: `player ${player.name}`,
|
|
|
|
hexpand: true,
|
|
|
|
|
|
|
|
children: [
|
2023-11-28 12:24:58 -05:00
|
|
|
Top(player, overlay),
|
2023-11-28 00:09:52 -05:00
|
|
|
Center(player),
|
|
|
|
Bottom(player),
|
|
|
|
],
|
|
|
|
});
|
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-10-20 23:11:21 -04:00
|
|
|
properties: [
|
|
|
|
['players', new Map()],
|
|
|
|
['setup', false],
|
|
|
|
],
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
connections: [
|
|
|
|
[Mpris, (overlay, busName) => {
|
2023-11-21 01:29:46 -05:00
|
|
|
if (overlay._players.has(busName)) {
|
2023-10-20 23:11:21 -04:00
|
|
|
return;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-10-20 23:11:21 -04:00
|
|
|
|
2023-11-01 13:13:30 -04:00
|
|
|
// Sometimes the signal doesn't give the busName
|
2023-10-20 23:11:21 -04:00
|
|
|
if (!busName) {
|
2023-11-21 01:29:46 -05:00
|
|
|
const player = Mpris.players.find((p) => {
|
|
|
|
return !overlay._players.has(p.busName);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (player) {
|
2023-10-20 23:11:21 -04:00
|
|
|
busName = player.busName;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
|
|
|
else {
|
2023-10-20 23:11:21 -04:00
|
|
|
return;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-10-20 23:11:21 -04:00
|
|
|
}
|
|
|
|
|
2023-11-28 12:24:58 -05:00
|
|
|
// Get the one on top so we can move it up later
|
|
|
|
const previousFirst = overlay.list().at(-1);
|
2023-11-01 13:13:30 -04:00
|
|
|
|
2023-11-28 00:09:52 -05:00
|
|
|
// Make the new player
|
2023-10-20 23:11:21 -04:00
|
|
|
const player = Mpris.getPlayer(busName);
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
player.colors = Variable();
|
2023-11-28 12:24:58 -05:00
|
|
|
overlay._players.set(
|
|
|
|
busName,
|
|
|
|
PlayerBox(player, content.getOverlay()),
|
|
|
|
);
|
2023-11-28 00:09:52 -05:00
|
|
|
overlay.overlays = Array.from(overlay._players.values())
|
|
|
|
.map((widget) => widget);
|
2023-10-20 23:11:21 -04:00
|
|
|
|
|
|
|
// Select favorite player at startup
|
2023-11-01 23:11:39 -04:00
|
|
|
if (!overlay._setup && overlay._players.has(FAVE_PLAYER)) {
|
2023-11-28 12:24:58 -05:00
|
|
|
overlay.moveToTop(overlay._players.get(FAVE_PLAYER));
|
2023-10-20 23:11:21 -04:00
|
|
|
overlay._setup = true;
|
|
|
|
}
|
2023-11-28 00:09:52 -05:00
|
|
|
|
|
|
|
// Move previousFirst on top again
|
2023-11-28 12:24:58 -05:00
|
|
|
else if (overlay.includesWidget(previousFirst)) {
|
|
|
|
overlay.moveToTop(previousFirst);
|
2023-11-01 13:13:30 -04:00
|
|
|
}
|
2023-10-20 23:11:21 -04:00
|
|
|
}, 'player-added'],
|
|
|
|
|
|
|
|
|
|
|
|
[Mpris, (overlay, busName) => {
|
2023-11-21 01:29:46 -05:00
|
|
|
if (!busName || !overlay._players.has(busName)) {
|
2023-10-20 23:11:21 -04:00
|
|
|
return;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-10-20 23:11:21 -04:00
|
|
|
|
2023-11-28 12:24:58 -05:00
|
|
|
// Get the one on top so we can move it up later
|
|
|
|
const previousFirst = overlay.list().at(-1);
|
2023-11-01 13:13:30 -04:00
|
|
|
|
2023-11-28 00:09:52 -05:00
|
|
|
// Remake overlays without deleted one
|
2023-10-20 23:11:21 -04:00
|
|
|
overlay._players.delete(busName);
|
2023-11-28 00:09:52 -05:00
|
|
|
overlay.overlays = Array.from(overlay._players.values())
|
|
|
|
.map((widget) => widget);
|
2023-10-20 23:11:21 -04:00
|
|
|
|
2023-11-28 00:09:52 -05:00
|
|
|
// Move previousFirst on top again
|
2023-11-28 12:24:58 -05:00
|
|
|
if (overlay.includesWidget(previousFirst)) {
|
|
|
|
overlay.moveToTop(previousFirst);
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-10-20 23:11:21 -04:00
|
|
|
}, 'player-closed'],
|
|
|
|
],
|
2023-11-28 12:24:58 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
return Box({
|
|
|
|
className: 'media',
|
|
|
|
child: content,
|
|
|
|
});
|
|
|
|
};
|