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

159 lines
4.8 KiB
JavaScript
Raw Normal View History

import { timeout } from 'resource:///com/github/Aylur/ags/utils.js';
import { Box, EventBox, Overlay } from 'resource:///com/github/Aylur/ags/widget.js';
import Gtk from 'gi://Gtk';
const MAX_OFFSET = 200;
const OFFSCREEN = 500;
const ANIM_DURATION = 500;
const TRANSITION = `transition: margin ${ANIM_DURATION}ms ease,
opacity ${ANIM_DURATION}ms ease;`;
export default ({
properties,
connections,
props,
} = {}) => {
const widget = EventBox();
const gesture = Gtk.GestureDrag.new(widget);
// Have empty PlayerBox to define the size of the widget
const emptyPlayer = Box({ className: 'player' });
// Set this prop to differentiate it easily
emptyPlayer.empty = true;
const content = Overlay({
...props,
properties: [
...properties,
['dragging', false],
],
child: emptyPlayer,
connections: [
...connections,
2023-11-28 00:14:58 -05:00
[gesture, (overlay, realGesture) => {
if (realGesture) {
overlay.list().forEach((over) => {
over.visible = true;
});
}
else {
overlay.showTopOnly();
}
// Don't allow gesture when only one player
if (overlay.list().length <= 1) {
return;
}
overlay._dragging = true;
let offset = gesture.get_offset()[1];
const playerBox = overlay.list().at(-1);
2023-11-01 15:29:47 -04:00
// Slide right
if (offset >= 0) {
2023-11-06 18:37:23 -05:00
playerBox.setCss(`
margin-left: ${offset}px;
margin-right: -${offset}px;
${playerBox._bgStyle}
`);
}
2023-11-01 15:29:47 -04:00
// Slide left
else {
offset = Math.abs(offset);
2023-11-06 18:37:23 -05:00
playerBox.setCss(`
margin-left: -${offset}px;
margin-right: ${offset}px;
${playerBox._bgStyle}
`);
}
}, 'drag-update'],
[gesture, (overlay) => {
// Don't allow gesture when only one player
if (overlay.list().length <= 1) {
return;
}
overlay._dragging = false;
const offset = gesture.get_offset()[1];
const playerBox = overlay.list().at(-1);
// If crosses threshold after letting go, slide away
if (Math.abs(offset) > MAX_OFFSET) {
// Disable inputs during animation
widget.sensitive = false;
// Slide away right
if (offset >= 0) {
2023-11-06 18:37:23 -05:00
playerBox.setCss(`
${TRANSITION}
margin-left: ${OFFSCREEN}px;
margin-right: -${OFFSCREEN}px;
opacity: 0.7; ${playerBox._bgStyle}
`);
}
// Slide away left
else {
2023-11-06 18:37:23 -05:00
playerBox.setCss(`
${TRANSITION}
margin-left: -${OFFSCREEN}px;
margin-right: ${OFFSCREEN}px;
opacity: 0.7; ${playerBox._bgStyle}`);
}
timeout(ANIM_DURATION, () => {
// Put the player in the back after anim
overlay.reorder_overlay(playerBox, 0);
// Recenter player
2023-11-06 18:37:23 -05:00
playerBox.setCss(playerBox._bgStyle);
widget.sensitive = true;
overlay.showTopOnly();
});
}
else {
// Recenter with transition for animation
2023-11-06 18:37:23 -05:00
playerBox.setCss(`${TRANSITION} ${playerBox._bgStyle}`);
}
}, 'drag-end'],
],
});
widget.add(content);
// Overlay methods
content.list = () => content.get_children()
.filter((ch) => !ch.empty);
content.includesWidget = (playerW) => {
return content.list().find((w) => w === playerW);
};
content.showTopOnly = () => content.list().forEach((over) => {
over.visible = over === content.list().at(-1);
});
content.moveToTop = (player) => {
player.visible = true;
content.reorder_overlay(player, -1);
timeout(ANIM_DURATION, () => {
content.showTopOnly();
});
};
widget.getOverlay = () => content;
return widget;
};