From cd8384a55612eb08942d5734ab6b8c4644a980f8 Mon Sep 17 00:00:00 2001 From: matt1432 Date: Wed, 30 Oct 2024 01:42:00 -0400 Subject: [PATCH] feat(agsV2): improve logic of sorted-list --- .../ags/v2/widgets/applauncher/main.tsx | 2 + .../ags/v2/widgets/icon-browser/style.scss | 2 +- .../ags/v2/widgets/misc/sorted-list.tsx | 40 +++++++++++++------ 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/nixosModules/ags/v2/widgets/applauncher/main.tsx b/nixosModules/ags/v2/widgets/applauncher/main.tsx index 5a6bab62..65df9e49 100644 --- a/nixosModules/ags/v2/widgets/applauncher/main.tsx +++ b/nixosModules/ags/v2/widgets/applauncher/main.tsx @@ -23,6 +23,8 @@ export default () => SortedList({ ], }, + compare_props: ['name', 'frequency'], + on_row_activated: (row) => { const app = (row.get_children()[0] as AppItem).app; diff --git a/nixosModules/ags/v2/widgets/icon-browser/style.scss b/nixosModules/ags/v2/widgets/icon-browser/style.scss index c5669131..d0788534 100644 --- a/nixosModules/ags/v2/widgets/icon-browser/style.scss +++ b/nixosModules/ags/v2/widgets/icon-browser/style.scss @@ -1,4 +1,4 @@ -.icon-browser .icon-list row box { +.icon-browser .list row box { margin: 20px; font-size: 16px; } diff --git a/nixosModules/ags/v2/widgets/misc/sorted-list.tsx b/nixosModules/ags/v2/widgets/misc/sorted-list.tsx index 39689e22..94ac1928 100644 --- a/nixosModules/ags/v2/widgets/misc/sorted-list.tsx +++ b/nixosModules/ags/v2/widgets/misc/sorted-list.tsx @@ -10,9 +10,10 @@ import PopupWindow, { PopupWindow as PopupWindowClass } from '../misc/popup-wind import { centerCursor } from '../../lib'; export interface SortedListProps { - create_list: () => T[] + create_list: () => T[] | Promise create_row: (item: T) => Gtk.Widget fzf_options?: FzfOptions + compare_props?: (keyof T)[] on_row_activated: (row: Gtk.ListBoxRow) => void sort_func: ( a: Gtk.ListBoxRow, @@ -29,10 +30,12 @@ export class SortedList { private fzf_results: FzfResultItem[] = []; readonly window: PopupWindowClass; + private _item_map = new Map(); - readonly create_list: () => T[]; + readonly create_list: () => T[] | Promise; readonly create_row: (item: T) => Gtk.Widget; readonly fzf_options: FzfOptions | undefined; + readonly compare_props: (keyof T)[] | undefined; readonly on_row_activated: (row: Gtk.ListBoxRow) => void; @@ -48,6 +51,7 @@ export class SortedList { create_list, create_row, fzf_options, + compare_props, on_row_activated, sort_func, name, @@ -94,19 +98,28 @@ export class SortedList { return this.sort_func(a, b, entry, this.fzf_results); }); - const refreshItems = () => idle(() => { - (list.get_children() as Gtk.ListBoxRow[]) - .forEach((child) => { - child.destroy(); - }); + const refreshItems = () => idle(async() => { + // Delete items that don't exist anymore + const new_list = await this.create_list(); - 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 - .flatMap((prop) => this.create_row(prop)) - .forEach((child) => { - list.add(child); - }); + // 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); + + list.add(_item); + } + } + + this.item_list = new_list; list.show_all(); on_text_change(''); @@ -163,6 +176,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.on_row_activated = on_row_activated; this.sort_func = sort_func;