feat(ags): copy on enter in clipboard

This commit is contained in:
matt1432 2024-05-01 10:21:28 -04:00
parent ac29acb446
commit 85473011b2

View file

@ -1,11 +1,14 @@
const { Box, Entry, Icon, Label, ListBox, Scrollable } = Widget; const { Box, Entry, Icon, Label, ListBox, Scrollable } = Widget;
const { execAsync } = Utils; const { execAsync } = Utils;
const Hyprland = await Service.import('hyprland');
import { Fzf, FzfResultItem } from 'fzf'; import { Fzf, FzfResultItem } from 'fzf';
import Gtk from 'gi://Gtk?version=3.0'; import Gtk from 'gi://Gtk?version=3.0';
import CursorBox from '../misc/cursorbox.ts'; import CursorBox from '../misc/cursorbox.ts';
import PopupWindow from '../misc/popup.ts'; import PopupWindow from '../misc/popup.ts';
import { Monitor } from 'types/service/hyprland';
const N_ITEMS = 30; const N_ITEMS = 30;
@ -20,12 +23,14 @@ const copyOldItem = (key: string | number): void => {
export default () => { export default () => {
let CopiedItems = [] as [string, number][]; let CopiedItems = [] as [string, number][];
let fzfResults: FzfResultItem<[string, number]>[]; let fzfResults: FzfResultItem<[string, number]>[];
const list = ListBox();
const getKey = (r: Gtk.ListBoxRow) => parseInt(r.get_child()?.name ?? ''); const getKey = (r: Gtk.ListBoxRow) => parseInt(r.get_child()?.name ?? '');
const list = ListBox().on('row-activated', (_, row) => {
copyOldItem(getKey(row));
});
const updateItems = () => { const updateItems = () => {
(list.get_children() as Gtk.ListBoxRow[]).forEach((r) => { (list.get_children() as Gtk.ListBoxRow[]).forEach((r) => {
r.changed(); r.changed();
@ -99,14 +104,6 @@ export default () => {
text: '-', text: '-',
hexpand: true, hexpand: true,
on_accept: () => {
const copiedItem = CopiedItems.find((c) => c === fzfResults[0].item);
if (copiedItem) {
copyOldItem(copiedItem[1]);
}
},
on_change: ({ text }) => { on_change: ({ text }) => {
if (text !== null) { if (text !== null) {
setSort(text); setSort(text);
@ -118,7 +115,7 @@ export default () => {
CopiedItems = []; CopiedItems = [];
entry.text = ''; entry.text = '';
execAsync('clipboard-manager').then((out) => { execAsync('clipboard-manager').then(async(out) => {
list.get_children()?.forEach((ch) => { list.get_children()?.forEach((ch) => {
ch.destroy(); ch.destroy();
}); });
@ -133,6 +130,36 @@ export default () => {
decodeItem(items[i]); decodeItem(items[i]);
} }
} }
let x: number;
let y: number;
const monitor = (JSON.parse(await Hyprland.messageAsync('j/monitors')) as Monitor[])
.find((m) => m.focused) as Monitor;
switch (monitor.transform) {
case 1:
x = monitor.x - (monitor.height / 2);
y = monitor.y - (monitor.width / 2);
break;
case 2:
x = monitor.x - (monitor.width / 2);
y = monitor.y - (monitor.height / 2);
break;
case 3:
x = monitor.x + (monitor.height / 2);
y = monitor.y + (monitor.width / 2);
break;
default:
x = monitor.x + (monitor.width / 2);
y = monitor.y + (monitor.height / 2);
break;
}
await Hyprland.messageAsync(`dispatch movecursor ${x} ${y}`);
entry.grab_focus();
}).catch(console.log); }).catch(console.log);
}; };