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-21 01:29:46 -05:00
|
|
|
const Top = (player) => 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: [
|
|
|
|
mpris.PlayerIcon(player, {
|
|
|
|
symbolic: false,
|
|
|
|
}),
|
|
|
|
],
|
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-21 01:29:46 -05:00
|
|
|
const PlayerBox = (player) => mpris.CoverArt(player, {
|
2023-10-20 23:11:21 -04:00
|
|
|
className: `player ${player.name}`,
|
|
|
|
hexpand: true,
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
children: [
|
|
|
|
Top(player),
|
|
|
|
Center(player),
|
|
|
|
Bottom(player),
|
|
|
|
],
|
2023-09-15 23:22:16 -04:00
|
|
|
});
|
|
|
|
|
2023-09-26 10:47:36 -04:00
|
|
|
export default () => Box({
|
2023-10-20 23:11:21 -04:00
|
|
|
className: 'media',
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
child: PlayerGesture({
|
|
|
|
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-01 13:13:30 -04:00
|
|
|
// Get the one on top so it stays there
|
2023-11-21 01:29:46 -05:00
|
|
|
let previousFirst = overlay.get_children().at(-1);
|
|
|
|
|
2023-11-01 13:13:30 -04:00
|
|
|
for (const [key, value] of overlay._players.entries()) {
|
|
|
|
if (value === previousFirst) {
|
|
|
|
previousFirst = key;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10-31 12:27:59 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
overlay._players.set(busName, PlayerBox(player));
|
|
|
|
|
|
|
|
const result = [];
|
2023-11-21 01:29:46 -05:00
|
|
|
|
|
|
|
overlay._players.forEach((widget) => {
|
2023-10-20 23:11:21 -04:00
|
|
|
result.push(widget);
|
|
|
|
});
|
|
|
|
|
|
|
|
overlay.overlays = result;
|
|
|
|
|
|
|
|
// Select favorite player at startup
|
2023-11-01 23:11:39 -04:00
|
|
|
if (!overlay._setup && overlay._players.has(FAVE_PLAYER)) {
|
2023-11-21 01:29:46 -05:00
|
|
|
overlay.reorder_overlay(
|
|
|
|
overlay._players.get(FAVE_PLAYER),
|
|
|
|
-1,
|
|
|
|
);
|
2023-10-20 23:11:21 -04:00
|
|
|
overlay._setup = true;
|
|
|
|
}
|
2023-11-01 23:11:39 -04:00
|
|
|
else if (overlay._players.get(previousFirst)) {
|
2023-11-21 01:29:46 -05:00
|
|
|
overlay.reorder_overlay(
|
|
|
|
overlay._players.get(previousFirst),
|
|
|
|
-1,
|
|
|
|
);
|
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-01 13:13:30 -04:00
|
|
|
// Get the one on top so it stays there
|
2023-11-21 01:29:46 -05:00
|
|
|
let previousFirst = overlay.get_children().at(-1);
|
|
|
|
|
2023-11-01 13:13:30 -04:00
|
|
|
for (const [key, value] of overlay._players.entries()) {
|
|
|
|
if (value === previousFirst) {
|
|
|
|
previousFirst = key;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
overlay._players.delete(busName);
|
|
|
|
|
|
|
|
const result = [];
|
2023-11-21 01:29:46 -05:00
|
|
|
|
|
|
|
overlay._players.forEach((widget) => {
|
2023-10-20 23:11:21 -04:00
|
|
|
result.push(widget);
|
|
|
|
});
|
|
|
|
|
|
|
|
overlay.overlays = result;
|
2023-11-01 13:13:30 -04:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
if (overlay._players.has(previousFirst)) {
|
|
|
|
overlay.reorder_overlay(
|
|
|
|
overlay._players.get(previousFirst),
|
|
|
|
-1,
|
|
|
|
);
|
|
|
|
}
|
2023-10-20 23:11:21 -04:00
|
|
|
}, 'player-closed'],
|
|
|
|
],
|
|
|
|
}),
|
2023-09-26 10:47:36 -04:00
|
|
|
});
|