2023-10-02 12:06:35 -04:00
|
|
|
import { Widget } from '../../imports.js';
|
|
|
|
const { Box, EventBox } = Widget;
|
|
|
|
|
|
|
|
import Gtk from 'gi://Gtk';
|
|
|
|
import Gdk from 'gi://Gdk';
|
2023-09-11 13:58:24 -04:00
|
|
|
const display = Gdk.Display.get_default();
|
|
|
|
|
2023-10-02 12:06:35 -04:00
|
|
|
|
2023-10-16 18:37:22 -04:00
|
|
|
export default ({
|
2023-09-11 13:58:24 -04:00
|
|
|
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 = [],
|
2023-09-15 15:49:43 -04:00
|
|
|
properties = [[]],
|
2023-10-17 13:47:02 -04:00
|
|
|
...props
|
2023-09-11 13:58:24 -04:00
|
|
|
}) => {
|
2023-10-17 13:47:02 -04:00
|
|
|
let widget = EventBox({
|
|
|
|
...props,
|
2023-09-15 15:49:43 -04:00
|
|
|
properties: [
|
|
|
|
['dragging', false],
|
|
|
|
...properties,
|
|
|
|
],
|
2023-10-17 13:47:02 -04:00
|
|
|
onHover: self => {
|
|
|
|
self.window.set_cursor(Gdk.Cursor.new_from_name(display, 'grab'));
|
|
|
|
onHover(self);
|
2023-09-11 13:58:24 -04:00
|
|
|
},
|
2023-10-17 13:47:02 -04:00
|
|
|
onHoverLost: self => {
|
|
|
|
self.window.set_cursor(null);
|
|
|
|
onHoverLost(self);
|
2023-09-11 13:58:24 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-10-17 13:47:02 -04:00
|
|
|
let gesture = Gtk.GestureDrag.new(widget);
|
2023-09-11 13:58:24 -04:00
|
|
|
|
2023-09-21 00:27:58 -04:00
|
|
|
let leftAnim1 = `transition: margin 0.5s ease, opacity 0.5s ease;
|
|
|
|
margin-left: -${Number(maxOffset + endMargin)}px;
|
|
|
|
margin-right: ${Number(maxOffset + endMargin)}px;
|
|
|
|
opacity: 0;`;
|
2023-09-14 16:42:39 -04:00
|
|
|
|
2023-09-21 00:27:58 -04:00
|
|
|
let leftAnim2 = `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 rightAnim1 = `transition: margin 0.5s ease, opacity 0.5s ease;
|
|
|
|
margin-left: ${Number(maxOffset + endMargin)}px;
|
|
|
|
margin-right: -${Number(maxOffset + endMargin)}px;
|
|
|
|
opacity: 0;`;
|
|
|
|
|
|
|
|
let rightAnim2 = `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-14 16:42:39 -04:00
|
|
|
|
2023-10-17 13:47:02 -04:00
|
|
|
widget.child = Box({
|
2023-09-14 16:42:39 -04:00
|
|
|
properties: [
|
2023-10-17 13:47:02 -04:00
|
|
|
['leftAnim1', leftAnim1],
|
|
|
|
['leftAnim2', leftAnim2],
|
2023-09-21 00:27:58 -04:00
|
|
|
['rightAnim1', rightAnim1],
|
|
|
|
['rightAnim2', rightAnim2],
|
|
|
|
['ready', false]
|
2023-09-14 16:42:39 -04:00
|
|
|
],
|
2023-09-11 13:58:24 -04:00
|
|
|
children: [
|
|
|
|
...children,
|
|
|
|
child,
|
|
|
|
],
|
2023-09-21 00:27:58 -04:00
|
|
|
style: leftAnim2,
|
2023-09-11 13:58:24 -04:00
|
|
|
connections: [
|
|
|
|
|
2023-10-17 13:47:02 -04:00
|
|
|
[gesture, self => {
|
2023-09-11 13:58:24 -04:00
|
|
|
var offset = gesture.get_offset()[1];
|
|
|
|
|
|
|
|
if (offset >= 0) {
|
2023-10-17 13:47:02 -04:00
|
|
|
self.setStyle(`margin-left: ${Number(offset + startMargin)}px;
|
|
|
|
margin-right: -${Number(offset + startMargin)}px;`);
|
2023-09-11 13:58:24 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
offset = Math.abs(offset);
|
2023-10-17 13:47:02 -04:00
|
|
|
self.setStyle(`margin-right: ${Number(offset + startMargin)}px;
|
|
|
|
margin-left: -${Number(offset + startMargin)}px;`);
|
2023-09-11 13:58:24 -04:00
|
|
|
}
|
2023-09-15 15:49:43 -04:00
|
|
|
|
2023-10-17 13:47:02 -04:00
|
|
|
self.get_parent()._dragging = Math.abs(offset) > 10;
|
2023-09-15 15:49:43 -04:00
|
|
|
|
2023-10-17 13:47:02 -04:00
|
|
|
if (widget.window)
|
|
|
|
widget.window.set_cursor(Gdk.Cursor.new_from_name(display, 'grabbing'));
|
2023-09-11 13:58:24 -04:00
|
|
|
}, 'drag-update'],
|
|
|
|
|
2023-10-17 13:47:02 -04:00
|
|
|
[gesture, self => {
|
|
|
|
if (!self._ready) {
|
|
|
|
self.setStyle(`transition: margin 0.5s ease, opacity 0.5s ease;
|
|
|
|
margin-left: -${Number(maxOffset + endMargin)}px;
|
|
|
|
margin-right: ${Number(maxOffset + endMargin)}px;
|
|
|
|
margin-bottom: 0px; margin-top: 0px; opacity: 0;`);
|
2023-09-21 00:27:58 -04:00
|
|
|
|
|
|
|
setTimeout(() => {
|
2023-10-17 13:47:02 -04:00
|
|
|
self.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-21 00:27:58 -04:00
|
|
|
}, 500);
|
2023-10-17 13:47:02 -04:00
|
|
|
setTimeout(() => self._ready = true, 1000);
|
2023-09-21 00:27:58 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-11 13:58:24 -04:00
|
|
|
const offset = gesture.get_offset()[1];
|
|
|
|
|
|
|
|
if (Math.abs(offset) > maxOffset) {
|
2023-09-13 21:59:49 -04:00
|
|
|
if (offset > 0) {
|
2023-10-17 13:47:02 -04:00
|
|
|
self.setStyle(rightAnim1);
|
|
|
|
setTimeout(() => self.setStyle(rightAnim2), 500);
|
2023-09-13 21:59:49 -04:00
|
|
|
}
|
|
|
|
else {
|
2023-10-17 13:47:02 -04:00
|
|
|
self.setStyle(leftAnim1);
|
|
|
|
setTimeout(() => self.setStyle(leftAnim2), 500);
|
2023-09-13 21:59:49 -04:00
|
|
|
}
|
2023-09-21 00:27:58 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
command();
|
2023-10-17 13:47:02 -04:00
|
|
|
self.destroy();
|
2023-09-21 00:27:58 -04:00
|
|
|
}, 1000);
|
2023-09-11 13:58:24 -04:00
|
|
|
}
|
|
|
|
else {
|
2023-10-17 13:47:02 -04:00
|
|
|
self.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;`);
|
|
|
|
if (widget.window)
|
|
|
|
widget.window.set_cursor(Gdk.Cursor.new_from_name(display, 'grab'));
|
|
|
|
|
|
|
|
self.get_parent()._dragging = false;
|
2023-09-11 13:58:24 -04:00
|
|
|
}
|
|
|
|
}, 'drag-end'],
|
2023-09-11 19:28:22 -04:00
|
|
|
|
2023-09-11 13:58:24 -04:00
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2023-10-17 13:47:02 -04:00
|
|
|
return widget;
|
2023-09-11 13:58:24 -04:00
|
|
|
};
|