refactor(ags): separate drag n drop code from main.js

This commit is contained in:
matt1432 2023-09-23 17:06:16 -04:00
parent 471e9e5730
commit db735af50b
2 changed files with 64 additions and 41 deletions

View file

@ -0,0 +1,57 @@
const { Gtk, Gdk } = imports.gi;
const { EventBox } = ags.Widget;
const { execAsync } = ags.Utils;
const { getWindow } = ags.App;
import Cairo from 'cairo';
import { Button } from '../misc/cursorbox.js';
const TARGET = [Gtk.TargetEntry.new('text/plain', Gtk.TargetFlags.SAME_APP, 0)];
function createSurfaceFromWidget(widget) {
const alloc = widget.get_allocation();
const surface = new Cairo.ImageSurface(
Cairo.Format.ARGB32,
alloc.width,
alloc.height,
);
const cr = new Cairo.Context(surface);
cr.setSourceRGBA(255, 255, 255, 0);
cr.rectangle(0, 0, alloc.width, alloc.height);
cr.fill();
widget.draw(cr);
return surface;
};
export const WorkspaceDrop = ({id, ...params} = {}) => EventBox({
...params,
tooltipText: `Workspace: ${id}`,
setup: eventbox => {
eventbox.drag_dest_set(Gtk.DestDefaults.ALL, TARGET, Gdk.DragAction.COPY);
eventbox.connect('drag-data-received', (_w, _c, _x, _y, data) => {
execAsync(`hyprctl dispatch movetoworkspacesilent ${id},address:${data.get_text()}`)
.catch(print);
});
},
});
export const WindowButton = ({address, ...params} = {}) => Button({
...params,
setup: button => {
button.drag_source_set(Gdk.ModifierType.BUTTON1_MASK, TARGET, Gdk.DragAction.COPY);
button.connect('drag-data-get', (_w, _c, data) => {
data.set_text(address, address.length);
});
button.connect('drag-begin', (_, context) => {
Gtk.drag_set_icon_surface(context, createSurfaceFromWidget(button));
button.get_parent().revealChild = false;
});
button.connect('drag-end', () => {
button.get_parent().destroy();
let mainBox = getWindow('overview').child.child;
mainBox._updateApps(mainBox);
});
},
});

View file

@ -1,12 +1,11 @@
const { Window, Box, CenterBox, Icon, Revealer, EventBox } = ags.Widget; const { Window, Box, CenterBox, Icon, Revealer } = ags.Widget;
const { closeWindow } = ags.App; const { closeWindow } = ags.App;
const { execAsync } = ags.Utils; const { execAsync } = ags.Utils;
const { Hyprland } = ags.Service; const { Hyprland } = ags.Service;
const { Gtk, Gdk } = imports.gi; const { Gtk } = imports.gi;
import Cairo from 'cairo';
import { Button } from '../misc/cursorbox.js';
import { PopUp } from '../misc/popup.js'; import { PopUp } from '../misc/popup.js';
import { WorkspaceDrop, WindowButton } from './dragndrop.js';
const WORKSPACE_PER_ROW = 6; const WORKSPACE_PER_ROW = 6;
const SCALE = 0.11; const SCALE = 0.11;
@ -28,24 +27,6 @@ const IconStyle = app => `min-width: ${app.size[0] * SCALE - MARGIN}px;
app.size[1] * SCALE - MARGIN) * ICON_SCALE}px;`; app.size[1] * SCALE - MARGIN) * ICON_SCALE}px;`;
Array.prototype.remove = function (el) { this.splice(this.indexOf(el), 1) }; Array.prototype.remove = function (el) { this.splice(this.indexOf(el), 1) };
const TARGET = [Gtk.TargetEntry.new('text/plain', Gtk.TargetFlags.SAME_APP, 0)];
export function createSurfaceFromWidget(widget) {
const alloc = widget.get_allocation();
const surface = new Cairo.ImageSurface(
Cairo.Format.ARGB32,
alloc.width,
alloc.height,
);
const cr = new Cairo.Context(surface);
cr.setSourceRGBA(255, 255, 255, 0);
cr.rectangle(0, 0, alloc.width, alloc.height);
cr.fill();
widget.draw(cr);
return surface;
}
const WorkspaceRow = (className, i) => Revealer({ const WorkspaceRow = (className, i) => Revealer({
transition: 'slide_down', transition: 'slide_down',
connections: [[Hyprland, rev => { connections: [[Hyprland, rev => {
@ -131,19 +112,12 @@ const OverviewWidget = Box({
box.child.child.toggleClassName('active', active); box.child.child.toggleClassName('active', active);
box.revealChild = Hyprland.getWorkspace(box._id)?.windows > 0 || active; box.revealChild = Hyprland.getWorkspace(box._id)?.windows > 0 || active;
}]], }]],
child: EventBox({ child: WorkspaceDrop({
tooltipText: `Workspace: ${ws.id}`, id: ws.id,
child: Box({ child: Box({
className: 'workspace', className: 'workspace',
style: `min-width: ${SCREEN.X * SCALE}px; style: `min-width: ${SCREEN.X * SCALE}px;
min-height: ${SCREEN.Y * SCALE}px;`, min-height: ${SCREEN.Y * SCALE}px;`,
setup: eventbox => {
eventbox.drag_dest_set(Gtk.DestDefaults.ALL, TARGET, Gdk.DragAction.COPY);
eventbox.connect('drag-data-received', (_w, _c, _x, _y, data) => {
execAsync(`hyprctl dispatch movetoworkspacesilent ${ws.id},address:${data.get_text()}`)
.catch(print);
});
},
child: ags.Widget({ child: ags.Widget({
type: Gtk.Fixed, type: Gtk.Fixed,
}), }),
@ -209,7 +183,8 @@ const OverviewWidget = Box({
['address', app.address], ['address', app.address],
['toDestroy', false] ['toDestroy', false]
], ],
child: Button({ child: WindowButton({
address: app.address,
onSecondaryClickRelease: () => { onSecondaryClickRelease: () => {
execAsync(`hyprctl dispatch closewindow address:${address}`) execAsync(`hyprctl dispatch closewindow address:${address}`)
.catch(print) .catch(print)
@ -224,15 +199,6 @@ const OverviewWidget = Box({
.then(() => closeWindow('overview')) .then(() => closeWindow('overview'))
.catch(print); .catch(print);
}, },
setup: button => {
button.drag_source_set(Gdk.ModifierType.BUTTON1_MASK, TARGET, Gdk.DragAction.COPY);
button.connect('drag-data-get', (_w, _c, data) => data.set_text(app.address, app.address.length));
button.connect('drag-begin', (_, context) => {
Gtk.drag_set_icon_surface(context, createSurfaceFromWidget(button));
button.get_parent().revealChild = false;
});
button.connect('drag-end', () => button.get_parent().revealChild = true);
},
child: Icon({ child: Icon({
className: `window ${active}`, className: `window ${active}`,
style: IconStyle(app), style: IconStyle(app),