feat(ags player): make player into stack and add anim

This commit is contained in:
matt1432 2023-09-24 01:40:41 -04:00
parent 897fbc3afb
commit 26e04f9626

View file

@ -1,5 +1,6 @@
const { Mpris } = ags.Service; const { Mpris } = ags.Service;
const { Box, CenterBox, Label } = ags.Widget; const { Box, CenterBox, Label, Stack, EventBox } = ags.Widget;
const { Gtk } = imports.gi;
import * as mpris from './mpris.js'; import * as mpris from './mpris.js';
import { Separator } from '../misc/separator.js'; import { Separator } from '../misc/separator.js';
@ -82,28 +83,81 @@ const PlayerBox = player => mpris.CoverArt(player, {
], ],
}); });
// TODO: Make this a stack with animations and gestures export default () => {
export default () => Box({ let widget = EventBox();
vertical: true,
className: 'media',
properties: [['players', new Map()]],
connections: [
[Mpris, (box, busName) => {
if (!busName || box._players.has(busName))
return;
const player = Mpris.getPlayer(busName); let gesture = Gtk.GestureDrag.new(widget)
player.colors = ags.Variable();
box._players.set(busName, PlayerBox(player));
box.children = Array.from(box._players.values());
}, 'player-added'],
[Mpris, (box, busName) => { widget.child = Stack({
if (!busName || !box._players.has(busName)) className: 'media',
return; properties: [
['players', new Map()],
['previous', 'spotify'],
],
connections: [
[gesture, stack => {
const offset = gesture.get_offset()[1];
if (Math.abs(offset) > 200) {
let players = [];
stack._players.forEach((_, busName) => players.push(Mpris.getPlayer(busName).name));
let current = players.findIndex(player => player === stack.shown);
box._players.delete(busName); if (offset > 0) {
box.children = Array.from(box._players.values()); stack.transition = 'slide_right';
}, 'player-closed'], if (players[++current])
], stack.shown = `${players[current]}`;
}); else
stack.shown = `${players[0]}`;
}
else {
stack.transition = 'slide_left';
if (players[--current])
stack.shown = `${players[current]}`;
else
stack.shown = `${players.at(-1)}`;
}
stack._previous = stack.shown;
}
}, 'drag-end'],
[Mpris, (stack, busName) => {
if (!busName || stack._players.has(busName))
return;
const player = Mpris.getPlayer(busName);
player.colors = ags.Variable();
stack._players.set(busName, PlayerBox(player));
let result = [];
stack._players.forEach((widget, busName) => {
result.push([`${Mpris.getPlayer(busName).name}`, widget]);
});
stack.items = result;
if (result.find(p => p[0] === stack._previous))
stack.shown = stack._previous;
else if (stack._players.has('org.mpris.MediaPlayer2.spotify'))
stack.shown = `spotify`;
}, 'player-added'],
[Mpris, (stack, busName) => {
if (!busName || !stack._players.has(busName))
return;
stack._players.delete(busName);
let result = [];
stack._players.forEach((widget, busName) => {
result.push([`${Mpris.getPlayer(busName).name}`, widget]);
});
stack.items = result;
if (result.find(p => p[0] === stack._previous))
stack.shown = stack._previous;
else if (stack._players.has('org.mpris.MediaPlayer2.spotify'))
stack.shown = `spotify`;
}, 'player-closed'],
],
});
return widget;
};