From 07474f1209e8bdca6933699406a59668eebd27d4 Mon Sep 17 00:00:00 2001 From: matt1432 Date: Fri, 1 Nov 2024 15:55:57 -0400 Subject: [PATCH] fix(agsV2): improve refresh of sorted-list --- .../config/widgets/applauncher/main.tsx | 2 +- .../ags-v2/config/widgets/clipboard/main.tsx | 2 +- .../config/widgets/misc/sorted-list.tsx | 47 ++++++++++--------- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/nixosModules/ags-v2/config/widgets/applauncher/main.tsx b/nixosModules/ags-v2/config/widgets/applauncher/main.tsx index de9110c1..88818320 100644 --- a/nixosModules/ags-v2/config/widgets/applauncher/main.tsx +++ b/nixosModules/ags-v2/config/widgets/applauncher/main.tsx @@ -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; diff --git a/nixosModules/ags-v2/config/widgets/clipboard/main.tsx b/nixosModules/ags-v2/config/widgets/clipboard/main.tsx index b3bf726c..6c30ed1f 100644 --- a/nixosModules/ags-v2/config/widgets/clipboard/main.tsx +++ b/nixosModules/ags-v2/config/widgets/clipboard/main.tsx @@ -34,7 +34,7 @@ export default () => SortedList({ selector: (item) => item.content, }, - compare_props: ['id'], + unique_props: ['id'], on_row_activated: (row) => { const clip = row.get_children()[0] as ClipItem; diff --git a/nixosModules/ags-v2/config/widgets/misc/sorted-list.tsx b/nixosModules/ags-v2/config/widgets/misc/sorted-list.tsx index 6757e1be..2ba41efa 100644 --- a/nixosModules/ags-v2/config/widgets/misc/sorted-list.tsx +++ b/nixosModules/ags-v2/config/widgets/misc/sorted-list.tsx @@ -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 { create_list: () => T[] | Promise create_row: (item: T) => Gtk.Widget - fzf_options?: FzfOptions - compare_props?: (keyof T)[] + fzf_options?: AsyncFzfOptions + unique_props?: (keyof T)[] on_row_activated: (row: Gtk.ListBoxRow) => void sort_func: ( a: Gtk.ListBoxRow, @@ -34,8 +34,8 @@ export class SortedList { readonly create_list: () => T[] | Promise; readonly create_row: (item: T) => Gtk.Widget; - readonly fzf_options: FzfOptions | undefined; - readonly compare_props: (keyof T)[] | undefined; + readonly fzf_options: AsyncFzfOptions; + readonly unique_props: (keyof T)[] | undefined; readonly on_row_activated: (row: Gtk.ListBoxRow) => void; @@ -50,8 +50,8 @@ export class SortedList { constructor({ create_list, create_row, - fzf_options, - compare_props, + fzf_options = {} as AsyncFzfOptions, + unique_props, on_row_activated, sort_func, name, @@ -74,8 +74,8 @@ export class SortedList { ) 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(this.item_list, this.fzf_options)).find(text) .then((out) => { this.fzf_results = out; list.invalidate_sort(); @@ -102,22 +102,26 @@ export class SortedList { // 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 { keymode={Astal.Keymode.ON_DEMAND} on_open={() => { entry.text = ''; + refreshItems(); centerCursor(); entry.grab_focus(); }} @@ -176,7 +181,7 @@ export class SortedList { 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;