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

This commit is contained in:
matt1432 2024-10-30 01:42:00 -04:00
parent 7334ac5f7e
commit cd8384a556
3 changed files with 30 additions and 14 deletions

View file

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

View file

@ -1,4 +1,4 @@
.icon-browser .icon-list row box { .icon-browser .list row box {
margin: 20px; margin: 20px;
font-size: 16px; font-size: 16px;
} }

View file

@ -10,9 +10,10 @@ import PopupWindow, { PopupWindow as PopupWindowClass } from '../misc/popup-wind
import { centerCursor } from '../../lib'; import { centerCursor } from '../../lib';
export interface SortedListProps<T> { export interface SortedListProps<T> {
create_list: () => T[] create_list: () => T[] | Promise<T[]>
create_row: (item: T) => Gtk.Widget create_row: (item: T) => Gtk.Widget
fzf_options?: FzfOptions<T> fzf_options?: FzfOptions<T>
compare_props?: (keyof T)[]
on_row_activated: (row: Gtk.ListBoxRow) => void on_row_activated: (row: Gtk.ListBoxRow) => void
sort_func: ( sort_func: (
a: Gtk.ListBoxRow, a: Gtk.ListBoxRow,
@ -29,10 +30,12 @@ export class SortedList<T> {
private fzf_results: FzfResultItem<T>[] = []; private fzf_results: FzfResultItem<T>[] = [];
readonly window: PopupWindowClass; readonly window: PopupWindowClass;
private _item_map = new Map<T, Gtk.Widget>();
readonly create_list: () => T[]; readonly create_list: () => T[] | Promise<T[]>;
readonly create_row: (item: T) => Gtk.Widget; readonly create_row: (item: T) => Gtk.Widget;
readonly fzf_options: FzfOptions<T> | undefined; readonly fzf_options: FzfOptions<T> | undefined;
readonly compare_props: (keyof T)[] | undefined;
readonly on_row_activated: (row: Gtk.ListBoxRow) => void; readonly on_row_activated: (row: Gtk.ListBoxRow) => void;
@ -48,6 +51,7 @@ export class SortedList<T> {
create_list, create_list,
create_row, create_row,
fzf_options, fzf_options,
compare_props,
on_row_activated, on_row_activated,
sort_func, sort_func,
name, name,
@ -94,19 +98,28 @@ export class SortedList<T> {
return this.sort_func(a, b, entry, this.fzf_results); return this.sort_func(a, b, entry, this.fzf_results);
}); });
const refreshItems = () => idle(() => { const refreshItems = () => idle(async() => {
(list.get_children() as Gtk.ListBoxRow[]) // Delete items that don't exist anymore
.forEach((child) => { const new_list = await this.create_list();
child.destroy();
});
this.item_list = 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_list // Add missing items
.flatMap((prop) => this.create_row(prop)) for (const item of new_list) {
.forEach((child) => { if (!this.item_list.some((child) =>
list.add(child); this.compare_props?.every((prop) => child[prop] === item[prop]) ?? child === item)) {
}); const _item = this.create_row(item);
list.add(_item);
}
}
this.item_list = new_list;
list.show_all(); list.show_all();
on_text_change(''); on_text_change('');
@ -163,6 +176,7 @@ export class SortedList<T> {
this.create_list = create_list; this.create_list = create_list;
this.create_row = create_row; this.create_row = create_row;
this.fzf_options = fzf_options; this.fzf_options = fzf_options;
this.compare_props = compare_props;
this.on_row_activated = on_row_activated; this.on_row_activated = on_row_activated;
this.sort_func = sort_func; this.sort_func = sort_func;