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) => { on_row_activated: (row) => {
const app = (row.get_children()[0] as AppItem).app; const app = (row.get_children()[0] as AppItem).app;

View file

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

View file

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