2023-10-31 08:32:40 -04:00
|
|
|
import { timeout } from 'resource:///com/github/Aylur/ags/utils.js';
|
|
|
|
import { Box, EventBox, Overlay } from 'resource:///com/github/Aylur/ags/widget.js';
|
2023-10-02 12:06:35 -04:00
|
|
|
|
2023-12-18 23:20:32 -05:00
|
|
|
const { Gtk } = imports.gi;
|
2023-09-26 10:43:37 -04:00
|
|
|
|
|
|
|
const MAX_OFFSET = 200;
|
|
|
|
const OFFSCREEN = 500;
|
2023-11-21 01:29:46 -05:00
|
|
|
const ANIM_DURATION = 500;
|
|
|
|
const TRANSITION = `transition: margin ${ANIM_DURATION}ms ease,
|
2023-11-28 12:24:58 -05:00
|
|
|
opacity ${ANIM_DURATION}ms ease;`;
|
2023-09-26 10:43:37 -04:00
|
|
|
|
2023-12-19 12:28:29 -05:00
|
|
|
/**
|
|
|
|
* @typedef {import('types/widgets/overlay').OverlayProps} OverlayProps
|
|
|
|
* @typedef {import('types/widgets/overlay').default} Overlay
|
2023-12-20 17:14:07 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/** @param {OverlayProps & {
|
2023-12-19 12:28:29 -05:00
|
|
|
* setup?: function(Overlay):void
|
|
|
|
* }} o
|
|
|
|
*/
|
2023-10-20 23:11:21 -04:00
|
|
|
export default ({
|
2023-12-18 18:00:30 -05:00
|
|
|
attribute = {},
|
2023-12-19 12:28:29 -05:00
|
|
|
setup = () => {/**/},
|
2023-12-18 18:00:30 -05:00
|
|
|
...props
|
|
|
|
}) => {
|
2023-11-06 21:36:12 -05:00
|
|
|
const widget = EventBox();
|
2023-10-20 23:11:21 -04:00
|
|
|
const gesture = Gtk.GestureDrag.new(widget);
|
2023-09-26 10:43:37 -04:00
|
|
|
|
2023-11-28 00:09:52 -05:00
|
|
|
// Have empty PlayerBox to define the size of the widget
|
2023-12-18 18:00:30 -05:00
|
|
|
const emptyPlayer = Box({
|
|
|
|
class_name: 'player',
|
|
|
|
attribute: { empty: true },
|
|
|
|
});
|
2023-11-28 00:09:52 -05:00
|
|
|
|
2023-11-28 12:24:58 -05:00
|
|
|
const content = Overlay({
|
2023-10-20 23:11:21 -04:00
|
|
|
...props,
|
2023-12-18 18:00:30 -05:00
|
|
|
attribute: {
|
|
|
|
...attribute,
|
|
|
|
dragging: false,
|
|
|
|
|
|
|
|
list: () => content.get_children()
|
|
|
|
// @ts-expect-error
|
|
|
|
.filter((ch) => !ch.attribute?.empty),
|
|
|
|
|
2023-12-19 12:28:29 -05:00
|
|
|
/** @param {Overlay} playerW */
|
2023-12-18 18:00:30 -05:00
|
|
|
includesWidget: (playerW) => {
|
|
|
|
return Array.from(content.attribute.list())
|
|
|
|
.find((w) => w === playerW);
|
|
|
|
},
|
|
|
|
|
|
|
|
showTopOnly: () => Array.from(content.attribute.list())
|
|
|
|
.forEach((over) => {
|
|
|
|
over.visible = over === content.attribute.list().at(-1);
|
|
|
|
}),
|
|
|
|
|
2023-12-19 12:28:29 -05:00
|
|
|
/** @param {import('types/widgets/centerbox').default} player */
|
2023-12-18 18:00:30 -05:00
|
|
|
moveToTop: (player) => {
|
|
|
|
player.visible = true;
|
|
|
|
content.reorder_overlay(player, -1);
|
|
|
|
timeout(ANIM_DURATION, () => {
|
|
|
|
content.attribute.showTopOnly();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
2023-11-01 14:33:06 -04:00
|
|
|
|
2023-11-28 00:09:52 -05:00
|
|
|
child: emptyPlayer,
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-12-17 00:01:58 -05:00
|
|
|
setup: (self) => {
|
|
|
|
setup(self);
|
|
|
|
|
|
|
|
self
|
2023-12-23 01:14:21 -05:00
|
|
|
.hook(gesture, (_, realGesture) => {
|
|
|
|
if (realGesture) {
|
|
|
|
Array.from(self.attribute.list())
|
|
|
|
.forEach((over) => {
|
|
|
|
over.visible = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.attribute.showTopOnly();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow gesture when only one player
|
|
|
|
if (self.attribute.list().length <= 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.attribute.dragging = true;
|
|
|
|
let offset = gesture.get_offset()[1];
|
|
|
|
const playerBox = self.attribute.list().at(-1);
|
|
|
|
|
|
|
|
if (!offset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slide right
|
|
|
|
if (offset >= 0) {
|
|
|
|
playerBox.setCss(`
|
2023-12-17 00:01:58 -05:00
|
|
|
margin-left: ${offset}px;
|
|
|
|
margin-right: -${offset}px;
|
2023-12-18 18:00:30 -05:00
|
|
|
${playerBox.attribute.bgStyle}
|
2023-11-01 13:12:43 -04:00
|
|
|
`);
|
2023-12-23 01:14:21 -05:00
|
|
|
}
|
2023-11-01 14:33:06 -04:00
|
|
|
|
2023-12-23 01:14:21 -05:00
|
|
|
// Slide left
|
|
|
|
else {
|
|
|
|
offset = Math.abs(offset);
|
|
|
|
playerBox.setCss(`
|
2023-12-17 00:01:58 -05:00
|
|
|
margin-left: -${offset}px;
|
|
|
|
margin-right: ${offset}px;
|
2023-12-18 18:00:30 -05:00
|
|
|
${playerBox.attribute.bgStyle}
|
2023-12-17 00:01:58 -05:00
|
|
|
`);
|
2023-12-23 01:14:21 -05:00
|
|
|
}
|
|
|
|
}, 'drag-update')
|
2023-11-01 14:33:06 -04:00
|
|
|
|
2023-11-28 00:09:52 -05:00
|
|
|
|
2023-12-23 01:14:21 -05:00
|
|
|
.hook(gesture, () => {
|
|
|
|
// Don't allow gesture when only one player
|
|
|
|
if (self.attribute.list().length <= 1) {
|
|
|
|
return;
|
|
|
|
}
|
2023-12-17 00:01:58 -05:00
|
|
|
|
2023-12-23 01:14:21 -05:00
|
|
|
self.attribute.dragging = false;
|
|
|
|
const offset = gesture.get_offset()[1];
|
2023-12-17 00:01:58 -05:00
|
|
|
|
2023-12-23 01:14:21 -05:00
|
|
|
const playerBox = self.attribute.list().at(-1);
|
2023-12-17 00:01:58 -05:00
|
|
|
|
2023-12-23 01:14:21 -05:00
|
|
|
// If crosses threshold after letting go, slide away
|
|
|
|
if (offset && Math.abs(offset) > MAX_OFFSET) {
|
|
|
|
// Disable inputs during animation
|
|
|
|
widget.sensitive = false;
|
2023-12-19 12:28:29 -05:00
|
|
|
|
2023-12-23 01:14:21 -05:00
|
|
|
// Slide away right
|
|
|
|
if (offset >= 0) {
|
|
|
|
playerBox.setCss(`
|
2023-12-17 00:01:58 -05:00
|
|
|
${TRANSITION}
|
|
|
|
margin-left: ${OFFSCREEN}px;
|
|
|
|
margin-right: -${OFFSCREEN}px;
|
2023-12-18 18:00:30 -05:00
|
|
|
opacity: 0.7; ${playerBox.attribute.bgStyle}
|
2023-12-17 00:01:58 -05:00
|
|
|
`);
|
2023-12-23 01:14:21 -05:00
|
|
|
}
|
2023-12-17 00:01:58 -05:00
|
|
|
|
2023-12-23 01:14:21 -05:00
|
|
|
// Slide away left
|
|
|
|
else {
|
|
|
|
playerBox.setCss(`
|
2023-12-17 00:01:58 -05:00
|
|
|
${TRANSITION}
|
|
|
|
margin-left: -${OFFSCREEN}px;
|
|
|
|
margin-right: ${OFFSCREEN}px;
|
2023-12-18 18:00:30 -05:00
|
|
|
opacity: 0.7; ${playerBox.attribute.bgStyle}
|
2023-12-17 00:01:58 -05:00
|
|
|
`);
|
2023-12-23 01:14:21 -05:00
|
|
|
}
|
2023-12-17 00:01:58 -05:00
|
|
|
|
2023-12-23 01:14:21 -05:00
|
|
|
timeout(ANIM_DURATION, () => {
|
|
|
|
// Put the player in the back after anim
|
|
|
|
self.reorder_overlay(playerBox, 0);
|
|
|
|
// Recenter player
|
|
|
|
playerBox.setCss(playerBox.attribute.bgStyle);
|
2023-12-17 00:01:58 -05:00
|
|
|
|
2023-12-23 01:14:21 -05:00
|
|
|
widget.sensitive = true;
|
2023-12-17 00:01:58 -05:00
|
|
|
|
2023-12-23 01:14:21 -05:00
|
|
|
self.attribute.showTopOnly();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Recenter with transition for animation
|
|
|
|
playerBox.setCss(`${TRANSITION}
|
2023-12-18 18:00:30 -05:00
|
|
|
${playerBox.attribute.bgStyle}`);
|
2023-12-23 01:14:21 -05:00
|
|
|
}
|
|
|
|
}, 'drag-end');
|
2023-12-17 00:01:58 -05:00
|
|
|
},
|
2023-11-28 12:24:58 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
widget.add(content);
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
return widget;
|
2023-09-26 10:43:37 -04:00
|
|
|
};
|