2024-05-01 15:03:42 -04:00
|
|
|
const { Box, Icon, Label } = Widget;
|
2024-05-01 10:21:28 -04:00
|
|
|
|
2024-05-01 10:21:28 -04:00
|
|
|
import { Fzf, FzfResultItem } from 'fzf';
|
2024-05-01 10:21:28 -04:00
|
|
|
import Gtk from 'gi://Gtk?version=3.0';
|
2024-07-10 15:14:44 -04:00
|
|
|
import Clipboard from '../../services/clipboard.ts';
|
2024-05-01 10:21:28 -04:00
|
|
|
|
|
|
|
import CursorBox from '../misc/cursorbox.ts';
|
2024-05-01 15:03:42 -04:00
|
|
|
import SortedList from '../misc/sorted-list.ts';
|
2024-05-01 10:21:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
export default () => {
|
2024-07-10 15:14:44 -04:00
|
|
|
let fzfResults: FzfResultItem<[number, { clip: string, isImage: boolean }]>[];
|
2024-05-01 10:21:28 -04:00
|
|
|
|
2024-07-10 15:14:44 -04:00
|
|
|
const getKey = (r: Gtk.ListBoxRow): number => parseInt(r.get_child()?.name ?? '0');
|
2024-05-01 10:21:28 -04:00
|
|
|
|
2024-07-10 15:14:44 -04:00
|
|
|
const makeItem = (
|
|
|
|
list: Gtk.ListBox,
|
|
|
|
key: number,
|
|
|
|
val: string,
|
|
|
|
isImage: boolean,
|
|
|
|
): void => {
|
2024-05-01 10:21:28 -04:00
|
|
|
const widget = CursorBox({
|
|
|
|
class_name: 'item',
|
2024-07-10 15:14:44 -04:00
|
|
|
name: key.toString(),
|
2024-05-01 10:21:28 -04:00
|
|
|
|
2024-07-10 15:14:44 -04:00
|
|
|
on_primary_click_release: () => Clipboard.copyOldItem(key),
|
2024-05-01 10:21:28 -04:00
|
|
|
|
|
|
|
child: Box({
|
|
|
|
children: [
|
2024-07-10 15:14:44 -04:00
|
|
|
isImage ?
|
2024-05-01 10:21:28 -04:00
|
|
|
Icon({
|
|
|
|
icon: val.replace('img:', ''),
|
|
|
|
size: 100 * 2,
|
|
|
|
}) :
|
|
|
|
|
|
|
|
Label({
|
|
|
|
label: val,
|
|
|
|
truncate: 'end',
|
|
|
|
max_width_chars: 100,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
list.add(widget);
|
|
|
|
widget.show_all();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2024-05-01 15:03:42 -04:00
|
|
|
return SortedList({
|
|
|
|
name: 'clipboard',
|
|
|
|
class_name: 'clipboard',
|
|
|
|
transition: 'slide top',
|
2024-05-01 10:21:28 -04:00
|
|
|
|
2024-07-10 15:14:44 -04:00
|
|
|
on_select: (r) => Clipboard.copyOldItem(getKey(r)),
|
2024-05-01 10:21:28 -04:00
|
|
|
|
2024-05-01 15:03:42 -04:00
|
|
|
init_rows: (list) => {
|
2024-07-10 15:14:44 -04:00
|
|
|
Clipboard.getHistory();
|
2024-05-01 10:21:28 -04:00
|
|
|
|
2024-07-10 15:14:44 -04:00
|
|
|
const connectId = Clipboard.connect('history-searched', () => {
|
|
|
|
list.get_children().forEach((row) => {
|
|
|
|
row.destroy();
|
2024-05-01 15:03:42 -04:00
|
|
|
});
|
2024-07-10 15:14:44 -04:00
|
|
|
Clipboard.clips.forEach((clip, key) => {
|
|
|
|
makeItem(list, key, clip.clip, clip.isImage);
|
|
|
|
});
|
|
|
|
Clipboard.disconnect(connectId);
|
|
|
|
});
|
2024-05-01 15:03:42 -04:00
|
|
|
},
|
2024-05-01 10:21:28 -04:00
|
|
|
|
2024-05-01 15:03:42 -04:00
|
|
|
set_sort: (text, list) => {
|
|
|
|
if (text === '' || text === '-') {
|
|
|
|
list.set_sort_func((row1, row2) => getKey(row2) - getKey(row1));
|
2024-05-01 10:21:28 -04:00
|
|
|
}
|
2024-05-01 15:03:42 -04:00
|
|
|
else {
|
2024-07-10 15:14:44 -04:00
|
|
|
const fzf = new Fzf([...Clipboard.clips.entries()], {
|
|
|
|
selector: ([_key, { clip }]) => clip,
|
2024-05-01 10:21:28 -04:00
|
|
|
|
2024-07-10 15:14:44 -04:00
|
|
|
tiebreakers: [(a, b) => b[0] - a[0]],
|
2024-05-01 15:03:42 -04:00
|
|
|
});
|
2024-05-01 10:21:28 -04:00
|
|
|
|
2024-05-01 15:03:42 -04:00
|
|
|
fzfResults = fzf.find(text);
|
|
|
|
list.set_sort_func((a, b) => {
|
2024-07-10 15:14:44 -04:00
|
|
|
const row1 = fzfResults.find((f) => f.item[0] === getKey(a))?.score ?? 0;
|
|
|
|
const row2 = fzfResults.find((f) => f.item[0] === getKey(b))?.score ?? 0;
|
2024-05-01 10:21:28 -04:00
|
|
|
|
2024-05-01 15:03:42 -04:00
|
|
|
return row2 - row1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2024-05-01 10:21:28 -04:00
|
|
|
});
|
|
|
|
};
|