2023-09-11 13:58:24 -04:00
|
|
|
const { Window, Box, EventBox, Button } = ags.Widget;
|
|
|
|
const { Gtk, Gdk } = imports.gi;
|
|
|
|
const display = Gdk.Display.get_default();
|
|
|
|
|
|
|
|
export const Draggable = ({
|
|
|
|
maxOffset = 150,
|
|
|
|
startMargin = 0,
|
2023-09-13 21:59:49 -04:00
|
|
|
endMargin = 300,
|
2023-09-11 13:58:24 -04:00
|
|
|
command = () => {},
|
2023-09-13 19:30:59 -04:00
|
|
|
onHover = w => {},
|
|
|
|
onHoverLost = w => {},
|
2023-09-11 13:58:24 -04:00
|
|
|
child = '',
|
|
|
|
children = [],
|
|
|
|
...params
|
|
|
|
}) => {
|
|
|
|
let w = EventBox({
|
|
|
|
...params,
|
|
|
|
onHover: box => {
|
|
|
|
box.window.set_cursor(Gdk.Cursor.new_from_name(display, 'grab'));
|
2023-09-13 19:30:59 -04:00
|
|
|
onHover(box);
|
2023-09-11 13:58:24 -04:00
|
|
|
},
|
|
|
|
onHoverLost: box => {
|
|
|
|
box.window.set_cursor(null);
|
2023-09-13 19:30:59 -04:00
|
|
|
onHoverLost(box);
|
2023-09-11 13:58:24 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
let gesture = Gtk.GestureDrag.new(w);
|
|
|
|
|
2023-09-14 16:42:39 -04:00
|
|
|
let leftAnim = 'transition: margin 0.5s ease, opacity 0.5s ease; ' +
|
|
|
|
'margin-left: -' + Number(maxOffset + endMargin) + 'px; ' +
|
|
|
|
'margin-right: ' + Number(maxOffset + endMargin) + 'px; ' +
|
|
|
|
'margin-bottom: -70px; margin-top: -70px; opacity: 0;';
|
|
|
|
|
|
|
|
let rightAnim = 'transition: margin 0.5s ease, opacity 0.5s ease; ' +
|
|
|
|
'margin-left: ' + Number(maxOffset + endMargin) + 'px; ' +
|
|
|
|
'margin-right: -' + Number(maxOffset + endMargin) + 'px; ' +
|
|
|
|
'margin-bottom: -70px; margin-top: -70px; opacity: 0;';
|
|
|
|
|
2023-09-11 13:58:24 -04:00
|
|
|
w.child = Box({
|
2023-09-14 16:42:39 -04:00
|
|
|
properties: [
|
|
|
|
['leftAnim', leftAnim],
|
|
|
|
['rightAnim', rightAnim],
|
|
|
|
],
|
2023-09-11 13:58:24 -04:00
|
|
|
children: [
|
|
|
|
...children,
|
|
|
|
child,
|
|
|
|
],
|
2023-09-14 16:42:39 -04:00
|
|
|
style: leftAnim,
|
2023-09-11 13:58:24 -04:00
|
|
|
connections: [
|
|
|
|
|
|
|
|
[gesture, box => {
|
|
|
|
var offset = gesture.get_offset()[1];
|
|
|
|
|
|
|
|
if (offset >= 0) {
|
2023-09-14 16:42:39 -04:00
|
|
|
box.setStyle('margin-left: ' + Number(offset + startMargin) + 'px; ' +
|
2023-09-11 13:58:24 -04:00
|
|
|
'margin-right: -' + Number(offset + startMargin) + 'px;');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
offset = Math.abs(offset);
|
2023-09-14 16:42:39 -04:00
|
|
|
box.setStyle('margin-right: ' + Number(offset + startMargin) + 'px; ' +
|
2023-09-11 13:58:24 -04:00
|
|
|
'margin-left: -' + Number(offset + startMargin) + 'px;');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (w.window)
|
|
|
|
w.window.set_cursor(Gdk.Cursor.new_from_name(display, 'grabbing'));
|
|
|
|
}, 'drag-update'],
|
|
|
|
|
|
|
|
[gesture, box => {
|
|
|
|
const offset = gesture.get_offset()[1];
|
|
|
|
|
|
|
|
if (Math.abs(offset) > maxOffset) {
|
2023-09-13 21:59:49 -04:00
|
|
|
if (offset > 0) {
|
2023-09-14 16:42:39 -04:00
|
|
|
box.setStyle(rightAnim);
|
2023-09-13 21:59:49 -04:00
|
|
|
}
|
|
|
|
else {
|
2023-09-14 16:42:39 -04:00
|
|
|
box.setStyle(leftAnim);
|
2023-09-13 21:59:49 -04:00
|
|
|
}
|
|
|
|
setTimeout(command, 500);
|
2023-09-11 13:58:24 -04:00
|
|
|
}
|
|
|
|
else {
|
2023-09-14 16:42:39 -04:00
|
|
|
box.setStyle('transition: margin 0.5s ease, opacity 0.5s ease; ' +
|
|
|
|
'margin-left: ' + startMargin + 'px; ' +
|
|
|
|
'margin-right: ' + startMargin + 'px; ' +
|
|
|
|
'margin-bottom: unset; margin-top: unset; opacity: 1;');
|
2023-09-11 13:58:24 -04:00
|
|
|
if (w.window)
|
|
|
|
w.window.set_cursor(Gdk.Cursor.new_from_name(display, 'grab'));
|
|
|
|
}
|
|
|
|
}, 'drag-end'],
|
2023-09-11 19:28:22 -04:00
|
|
|
|
2023-09-11 13:58:24 -04:00
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
return w;
|
|
|
|
};
|