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

430 lines
12 KiB
JavaScript
Raw Normal View History

import Mpris from 'resource:///com/github/Aylur/ags/service/mpris.js';
import { Button, Icon, Label, Stack, Slider, CenterBox, Box } from 'resource:///com/github/Aylur/ags/widget.js';
import { execAsync, lookUpIcon, readFileAsync } from 'resource:///com/github/Aylur/ags/utils.js';
import Separator from '../misc/separator.js';
2023-11-16 15:02:00 -05:00
import EventBox from '../misc/cursorbox.js';
2023-12-18 18:00:30 -05:00
/**
* @typedef {import('types/service/mpris').MprisPlayer} Player
* @typedef {import('types/variable').Variable} Variable
* @typedef {import('types/widgets/overlay').default} Overlay
*/
const ICON_SIZE = 32;
const icons = {
mpris: {
fallback: 'audio-x-generic-symbolic',
shuffle: {
enabled: '󰒝',
disabled: '󰒞',
},
loop: {
none: '󰑗',
track: '󰑘',
playlist: '󰑖',
},
playing: ' ',
paused: ' ',
stopped: ' ',
prev: '󰒮',
next: '󰒭',
},
};
2023-12-18 18:00:30 -05:00
/**
* @param {Player} player
* @param {Variable} colors
* @param {import('types/widgets/centerbox').CenterBoxProps=} props
*/
export const CoverArt = (player, colors, props) => CenterBox({
...props,
2023-12-18 18:00:30 -05:00
// @ts-expect-error
vertical: true,
2023-12-18 18:00:30 -05:00
attribute: {
bgStyle: '',
player,
},
setup: (self) => {
// Give temp cover art
2023-12-18 18:00:30 -05:00
readFileAsync(player.cover_path).catch(() => {
if (!colors.value && !player.track_cover_url) {
colors.value = {
imageAccent: '#6b4fa2',
buttonAccent: '#ecdcff',
buttonText: '#25005a',
hoverAccent: '#d4baff',
};
2023-12-18 18:00:30 -05:00
self.attribute.bgStyle = `
background: radial-gradient(circle,
rgba(0, 0, 0, 0.4) 30%,
2023-12-18 18:00:30 -05:00
${colors.value.imageAccent}),
rgb(0, 0, 0);
background-size: cover;
background-position: center;
`;
2023-12-18 18:00:30 -05:00
self.setCss(self.attribute.bgStyle);
}
});
self.hook(player, () => {
2023-12-18 18:00:30 -05:00
execAsync(['bash', '-c', `[[ -f "${player.cover_path}" ]] &&
coloryou "${player.cover_path}" | grep -v Warning`])
.then((out) => {
if (!Mpris.players.find((p) => player === p)) {
return;
}
2023-12-18 18:00:30 -05:00
colors.value = JSON.parse(out);
2023-12-18 18:00:30 -05:00
self.attribute.bgStyle = `
background: radial-gradient(circle,
rgba(0, 0, 0, 0.4) 30%,
${colors.value.imageAccent}),
url("${player.cover_path}");
background-size: cover;
background-position: center;
`;
2023-12-18 18:00:30 -05:00
// @ts-expect-error
if (!self?.get_parent()?.attribute.dragging) {
self.setCss(self.attribute.bgStyle);
}
}).catch((err) => {
if (err !== '') {
print(err);
}
});
});
},
});
2023-12-18 18:00:30 -05:00
/** @param {Player} player */
export const TitleLabel = (player) => Label({
xalign: 0,
2023-12-18 18:00:30 -05:00
max_width_chars: 40,
truncate: 'end',
justification: 'left',
2023-12-18 18:00:30 -05:00
class_name: 'title',
label: player.bind('track_title'),
});
2023-12-18 18:00:30 -05:00
/** @param {Player} player */
export const ArtistLabel = (player) => Label({
xalign: 0,
2023-12-18 18:00:30 -05:00
max_width_chars: 40,
truncate: 'end',
justification: 'left',
2023-12-18 18:00:30 -05:00
class_name: 'artist',
label: player.bind('track_artists')
.transform((a) => a.join(', ') || ''),
});
2023-12-18 18:00:30 -05:00
/**
* @param {Player} player
* @param {Overlay} overlay
*/
export const PlayerIcon = (player, overlay) => {
/**
* @param {Player} p
* @param {Overlay=} widget
* @param {Overlay=} over
*/
const playerIcon = (p, widget, over) => EventBox({
2023-12-18 18:00:30 -05:00
tooltip_text: p.identity || '',
onPrimaryClickRelease: () => {
2023-12-18 18:00:30 -05:00
widget?.attribute.moveToTop(over);
},
child: Icon({
2023-12-18 18:00:30 -05:00
class_name: widget ? 'position-indicator' : 'player-icon',
size: widget ? 0 : ICON_SIZE,
setup: (self) => {
self.hook(p, () => {
self.icon = lookUpIcon(p.entry) ?
p.entry :
icons.mpris.fallback;
});
},
}),
});
2023-12-18 18:00:30 -05:00
return Box().hook(Mpris, (self) => {
const thisIndex = overlay.attribute.list()
.indexOf(self?.get_parent()?.get_parent());
2023-12-18 18:00:30 -05:00
self.children = Array.from(overlay.attribute.list())
.map((over, i) => {
self.children.push(Separator(2));
2023-12-18 18:00:30 -05:00
return i === thisIndex ?
playerIcon(player) :
playerIcon(over.attribute.player, overlay, over);
})
.reverse();
});
};
2023-12-18 18:00:30 -05:00
/**
* @param {Player} player
* @param {Variable} colors
*/
export const PositionSlider = (player, colors) => Slider({
class_name: 'position-slider',
2023-11-16 15:02:00 -05:00
cursor: 'pointer',
vpack: 'center',
hexpand: true,
2023-12-18 18:00:30 -05:00
draw_value: false,
2023-12-18 18:00:30 -05:00
on_change: ({ value }) => {
2023-11-16 15:02:00 -05:00
player.position = player.length * value;
},
setup: (self) => {
const update = () => {
if (!self.dragging) {
self.visible = player.length > 0;
if (player.length > 0) {
self.value = player.position / player.length;
}
}
};
self
.poll(1000, () => update())
.hook(player, () => update(), 'position')
2023-12-18 18:00:30 -05:00
.hook(colors, () => {
if (colors.value) {
const c = colors.value;
self.setCss(`
highlight { background-color: ${c.buttonAccent}; }
slider { background-color: ${c.buttonAccent}; }
slider:hover { background-color: ${c.hoverAccent}; }
trough { background-color: ${c.buttonText}; }
`);
}
})
2023-12-18 18:00:30 -05:00
.on('button-press-event', () => {
self.cursor = 'grabbing';
})
2023-12-18 18:00:30 -05:00
.on('button-release-event', () => {
self.cursor = 'pointer';
});
},
});
2023-12-18 18:00:30 -05:00
const PlayerButton = ({ player, colors, items, onClick, prop }) => EventBox({
2023-11-16 15:02:00 -05:00
child: Button({
2023-12-18 18:00:30 -05:00
attribute: { hovered: false },
2023-11-16 15:02:00 -05:00
child: Stack({ items }),
2023-12-18 18:00:30 -05:00
on_primary_click_release: () => player[onClick](),
2023-12-18 18:00:30 -05:00
on_hover: (self) => {
self.attribute.hovered = true;
2023-12-18 18:00:30 -05:00
if (prop === 'playBackStatus' && colors.value) {
const c = colors.value;
2023-12-18 18:00:30 -05:00
Array.from(items).forEach((item) => {
2023-11-16 15:02:00 -05:00
item[1].setCss(`
background-color: ${c.hoverAccent};
color: ${c.buttonText};
min-height: 40px;
min-width: 36px;
margin-bottom: 1px;
margin-right: 1px;
`);
2023-11-16 15:02:00 -05:00
});
}
},
2023-12-18 18:00:30 -05:00
on_hover_lost: (self) => {
self.attribute.hovered = false;
if (prop === 'playBackStatus' && colors.value) {
const c = colors.value;
2023-12-18 18:00:30 -05:00
Array.from(items).forEach((item) => {
2023-11-16 15:02:00 -05:00
item[1].setCss(`
background-color: ${c.buttonAccent};
color: ${c.buttonText};
min-height: 42px;
min-width: 38px;
`);
2023-11-16 15:02:00 -05:00
});
}
},
setup: (self) => {
self
.hook(player, () => {
2023-12-18 18:00:30 -05:00
// @ts-expect-error
self.child.shown = `${player[prop]}`;
})
2023-12-18 18:00:30 -05:00
.hook(colors, () => {
if (!Mpris.players.find((p) => player === p)) {
return;
}
2023-12-18 18:00:30 -05:00
if (colors.value) {
const c = colors.value;
if (prop === 'playBackStatus') {
2023-12-18 18:00:30 -05:00
if (self.attribute.hovered) {
Array.from(items).forEach((item) => {
item[1].setCss(`
background-color: ${c.hoverAccent};
color: ${c.buttonText};
min-height: 40px;
min-width: 36px;
margin-bottom: 1px;
margin-right: 1px;
`);
});
}
else {
2023-12-18 18:00:30 -05:00
Array.from(items).forEach((item) => {
item[1].setCss(`
background-color: ${c.buttonAccent};
color: ${c.buttonText};
min-height: 42px;
min-width: 38px;
`);
});
}
2023-11-16 15:02:00 -05:00
}
else {
self.setCss(`
* { color: ${c.buttonAccent}; }
*:hover { color: ${c.hoverAccent}; }
`);
2023-11-16 15:02:00 -05:00
}
}
});
},
2023-11-16 15:02:00 -05:00
}),
});
2023-12-18 18:00:30 -05:00
/**
* @param {Player} player
* @param {Variable} colors
*/
export const ShuffleButton = (player, colors) => PlayerButton({
player,
2023-12-18 18:00:30 -05:00
colors,
items: [
['true', Label({
2023-12-18 18:00:30 -05:00
class_name: 'shuffle enabled',
label: icons.mpris.shuffle.enabled,
})],
['false', Label({
2023-12-18 18:00:30 -05:00
class_name: 'shuffle disabled',
label: icons.mpris.shuffle.disabled,
})],
],
onClick: 'shuffle',
prop: 'shuffleStatus',
});
2023-12-18 18:00:30 -05:00
/**
* @param {Player} player
* @param {Variable} colors
*/
export const LoopButton = (player, colors) => PlayerButton({
player,
2023-12-18 18:00:30 -05:00
colors,
items: [
['None', Label({
2023-12-18 18:00:30 -05:00
class_name: 'loop none',
label: icons.mpris.loop.none,
})],
['Track', Label({
2023-12-18 18:00:30 -05:00
class_name: 'loop track',
label: icons.mpris.loop.track,
})],
['Playlist', Label({
2023-12-18 18:00:30 -05:00
class_name: 'loop playlist',
label: icons.mpris.loop.playlist,
})],
],
onClick: 'loop',
prop: 'loopStatus',
});
2023-12-18 18:00:30 -05:00
/**
* @param {Player} player
* @param {Variable} colors
*/
export const PlayPauseButton = (player, colors) => PlayerButton({
player,
2023-12-18 18:00:30 -05:00
colors,
items: [
['Playing', Label({
2023-12-18 18:00:30 -05:00
class_name: 'pausebutton playing',
label: icons.mpris.playing,
})],
['Paused', Label({
2023-12-18 18:00:30 -05:00
class_name: 'pausebutton paused',
label: icons.mpris.paused,
})],
['Stopped', Label({
2023-12-18 18:00:30 -05:00
class_name: 'pausebutton stopped paused',
label: icons.mpris.stopped,
})],
],
onClick: 'playPause',
prop: 'playBackStatus',
});
2023-12-18 18:00:30 -05:00
/**
* @param {Player} player
* @param {Variable} colors
*/
export const PreviousButton = (player, colors) => PlayerButton({
player,
2023-12-18 18:00:30 -05:00
colors,
items: [
['true', Label({
2023-12-18 18:00:30 -05:00
class_name: 'previous',
label: icons.mpris.prev,
})],
['false', Label({
2023-12-18 18:00:30 -05:00
class_name: 'previous',
label: icons.mpris.prev,
})],
],
onClick: 'previous',
prop: 'canGoPrev',
});
2023-12-18 18:00:30 -05:00
/**
* @param {Player} player
* @param {Variable} colors
*/
export const NextButton = (player, colors) => PlayerButton({
player,
2023-12-18 18:00:30 -05:00
colors,
items: [
['true', Label({
2023-12-18 18:00:30 -05:00
class_name: 'next',
label: icons.mpris.next,
})],
['false', Label({
2023-12-18 18:00:30 -05:00
class_name: 'next',
label: icons.mpris.next,
})],
],
onClick: 'next',
prop: 'canGoNext',
});