2023-09-09 00:55:42 -04:00
|
|
|
const { Window, Box, EventBox, Button } = ags.Widget;
|
2023-09-08 22:52:29 -04:00
|
|
|
const { Gtk, Gdk } = imports.gi;
|
2023-09-08 23:16:46 -04:00
|
|
|
const display = Gdk.Display.get_default();
|
2023-09-08 22:52:29 -04:00
|
|
|
|
2023-09-09 00:55:42 -04:00
|
|
|
const Draggable = ({ maxOffset = 150, style, connections = [], ...props }) => {
|
|
|
|
let w = EventBox({
|
|
|
|
onHover: box => {
|
|
|
|
box.window.set_cursor(Gdk.Cursor.new_from_name(display, 'grab'));
|
|
|
|
},
|
|
|
|
onHoverLost: box => {
|
|
|
|
box.window.set_cursor(null);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
let gesture = Gtk.GestureDrag.new(w);
|
|
|
|
|
|
|
|
w.child = Box({
|
|
|
|
...props,
|
|
|
|
connections: [
|
|
|
|
|
|
|
|
[gesture, box => {
|
|
|
|
const offset = gesture.get_offset()[1];
|
|
|
|
|
|
|
|
box.setStyle('margin-left: ' + offset + 'px; ' + style);
|
|
|
|
w.window.set_cursor(Gdk.Cursor.new_from_name(display, 'grabbing'));
|
|
|
|
}, 'drag-update'],
|
|
|
|
|
|
|
|
[gesture, box => {
|
|
|
|
const offset = gesture.get_offset()[1];
|
|
|
|
|
|
|
|
if (offset > maxOffset) {
|
|
|
|
w.destroy();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
box.setStyle('transition: margin 0.5s ease; ' + style);
|
|
|
|
w.window.set_cursor(Gdk.Cursor.new_from_name(display, 'grab'));
|
|
|
|
}
|
|
|
|
}, 'drag-end'],
|
|
|
|
|
|
|
|
...connections,
|
|
|
|
],
|
|
|
|
});
|
2023-09-08 22:52:29 -04:00
|
|
|
|
2023-09-09 00:55:42 -04:00
|
|
|
return w;
|
|
|
|
};
|
2023-09-08 22:52:29 -04:00
|
|
|
|
|
|
|
export const DragTest = Window({
|
|
|
|
name: 'drag-test',
|
|
|
|
layer: 'overlay',
|
|
|
|
anchor: 'top right',
|
|
|
|
child: Box({
|
|
|
|
style: 'background: white; min-width: 200px; min-height: 200px;',
|
|
|
|
children: [
|
2023-09-09 00:55:42 -04:00
|
|
|
Draggable({
|
|
|
|
maxOffset: 120,
|
|
|
|
className: 'test',
|
|
|
|
style: 'background: black; min-width: 40px; min-height: 20px',
|
|
|
|
child: Button({
|
|
|
|
style: 'background: red; min-width: 10px; min-height: 10px',
|
|
|
|
onClicked: 'echo hi',
|
|
|
|
}),
|
2023-09-08 22:52:29 -04:00
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
});
|