feat(ags): abstract drag code

This commit is contained in:
matt1432 2023-09-09 00:55:42 -04:00
parent adf818fd44
commit 84aa1a40a6

View file

@ -1,27 +1,48 @@
const { Window, Box, EventBox } = ags.Widget; const { Window, Box, EventBox, Button } = ags.Widget;
const { Gtk, Gdk } = imports.gi; const { Gtk, Gdk } = imports.gi;
const display = Gdk.Display.get_default(); const display = Gdk.Display.get_default();
var Gesture; const Draggable = ({ maxOffset = 150, style, connections = [], ...props }) => {
var shouldDelete = false; let w = EventBox({
const DraggableCtor = props => EventBox({
onHover: box => { onHover: box => {
box.window.set_cursor(Gdk.Cursor.new_from_name(display, 'grab')); box.window.set_cursor(Gdk.Cursor.new_from_name(display, 'grab'));
}, },
onHoverLost: box => { onHoverLost: box => {
box.window.set_cursor(null); box.window.set_cursor(null);
}, },
setup: widget => {
Gesture = Gtk.GestureDrag.new(widget);
},
child: Box({
style: 'background: black; min-width: 40px; min-height: 20px',
}),
...props,
}); });
const Draggable = DraggableCtor(); 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,
],
});
return w;
};
export const DragTest = Window({ export const DragTest = Window({
name: 'drag-test', name: 'drag-test',
@ -30,33 +51,14 @@ export const DragTest = Window({
child: Box({ child: Box({
style: 'background: white; min-width: 200px; min-height: 200px;', style: 'background: white; min-width: 200px; min-height: 200px;',
children: [ children: [
Draggable, Draggable({
Box({ maxOffset: 120,
connections: [ className: 'test',
[Gesture, event => { style: 'background: black; min-width: 40px; min-height: 20px',
const offset = Gesture.get_offset()[1]; child: Button({
Draggable.child.setStyle('background: black; min-width: 40px; min-height: 20px; margin-left: ' + offset + 'px;'); style: 'background: red; min-width: 10px; min-height: 10px',
Draggable.window.set_cursor(Gdk.Cursor.new_from_name(display, 'grabbing')); onClicked: 'echo hi',
}),
if (offset > 150) {
shouldDelete = true;
}
else if (shouldDelete) {
shouldDelete = false;
}
}, 'drag-update'],
[Gesture, event => {
if (shouldDelete) {
Draggable.destroy();
}
else {
Draggable.child.setStyle('transition: margin 0.5s ease; background: black; min-width: 40px; min-height: 20px');
Draggable.window.set_cursor(Gdk.Cursor.new_from_name(display, 'grab'));
}
print('end');
}, 'drag-end'],
],
}), }),
], ],
}), }),