fix(agsV2): improve refresh of sorted-list
All checks were successful
Discord / discord commits (push) Has been skipped

This commit is contained in:
matt1432 2024-11-01 15:55:57 -04:00
parent a138f3a5ea
commit 07474f1209
3 changed files with 28 additions and 23 deletions

View file

@ -23,7 +23,7 @@ export default () => SortedList({
],
},
compare_props: ['name', 'frequency'],
unique_props: ['name', 'executable'],
on_row_activated: (row) => {
const app = (row.get_children()[0] as AppItem).app;

View file

@ -34,7 +34,7 @@ export default () => SortedList<EntryObject>({
selector: (item) => item.content,
},
compare_props: ['id'],
unique_props: ['id'],
on_row_activated: (row) => {
const clip = row.get_children()[0] as ClipItem;

View file

@ -4,7 +4,7 @@
import { Astal, Gtk, Widget } from 'astal/gtk3';
import { idle } from 'astal';
import { AsyncFzf, FzfOptions, FzfResultItem } from 'fzf';
import { AsyncFzf, AsyncFzfOptions, FzfResultItem } from 'fzf';
import PopupWindow from '../misc/popup-window';
import { centerCursor } from '../../lib';
@ -12,8 +12,8 @@ import { centerCursor } from '../../lib';
export interface SortedListProps<T> {
create_list: () => T[] | Promise<T[]>
create_row: (item: T) => Gtk.Widget
fzf_options?: FzfOptions<T>
compare_props?: (keyof T)[]
fzf_options?: AsyncFzfOptions<T>
unique_props?: (keyof T)[]
on_row_activated: (row: Gtk.ListBoxRow) => void
sort_func: (
a: Gtk.ListBoxRow,
@ -34,8 +34,8 @@ export class SortedList<T> {
readonly create_list: () => T[] | Promise<T[]>;
readonly create_row: (item: T) => Gtk.Widget;
readonly fzf_options: FzfOptions<T> | undefined;
readonly compare_props: (keyof T)[] | undefined;
readonly fzf_options: AsyncFzfOptions<T>;
readonly unique_props: (keyof T)[] | undefined;
readonly on_row_activated: (row: Gtk.ListBoxRow) => void;
@ -50,8 +50,8 @@ export class SortedList<T> {
constructor({
create_list,
create_row,
fzf_options,
compare_props,
fzf_options = {} as AsyncFzfOptions<T>,
unique_props,
on_row_activated,
sort_func,
name,
@ -74,8 +74,8 @@ export class SortedList<T> {
) as Widget.Revealer;
const on_text_change = (text: string) => {
// @ts-expect-error this should be okay
(new AsyncFzf(this.item_list, this.fzf_options)).find(text)
// @ts-expect-error this works
(new AsyncFzf<T[]>(this.item_list, this.fzf_options)).find(text)
.then((out) => {
this.fzf_results = out;
list.invalidate_sort();
@ -102,22 +102,26 @@ export class SortedList<T> {
// Delete items that don't exist anymore
const new_list = await this.create_list();
for (const [item, widget] of this._item_map) {
if (!new_list.some((child) =>
this.compare_props?.every((prop) => child[prop] === item[prop]) ?? child === item)) {
widget.destroy();
[...this._item_map].forEach(([item, widget]) => {
if (!new_list.some((new_item) =>
this.unique_props?.every((prop) => item[prop] === new_item[prop]) ??
item === new_item)) {
widget.get_parent()?.destroy();
this._item_map.delete(item);
}
}
});
// Add missing items
for (const item of new_list) {
if (!this.item_list.some((child) =>
this.compare_props?.every((prop) => child[prop] === item[prop]) ?? child === item)) {
const _item = this.create_row(item);
new_list.forEach((item) => {
if (!this.item_list.some((old_item) =>
this.unique_props?.every((prop) => old_item[prop] === item[prop]) ??
old_item === item)) {
const itemWidget = this.create_row(item);
list.add(_item);
list.add(itemWidget);
this._item_map.set(item, itemWidget);
}
}
});
this.item_list = new_list;
@ -131,6 +135,7 @@ export class SortedList<T> {
keymode={Astal.Keymode.ON_DEMAND}
on_open={() => {
entry.text = '';
refreshItems();
centerCursor();
entry.grab_focus();
}}
@ -176,7 +181,7 @@ export class SortedList<T> {
this.create_list = create_list;
this.create_row = create_row;
this.fzf_options = fzf_options;
this.compare_props = compare_props;
this.unique_props = unique_props;
this.on_row_activated = on_row_activated;
this.sort_func = sort_func;