@gtkx/components 1.0.0-rc.1 → 1.0.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adw/index.d.ts +3 -0
- package/dist/adw/index.d.ts.map +1 -0
- package/dist/adw/index.js +2 -0
- package/dist/adw/index.js.map +1 -0
- package/dist/adw/toast.d.ts +10 -0
- package/dist/adw/toast.d.ts.map +1 -0
- package/dist/adw/toast.js +51 -0
- package/dist/adw/toast.js.map +1 -0
- package/dist/adw/types.d.ts +28 -0
- package/dist/adw/types.d.ts.map +1 -0
- package/dist/adw/types.js +2 -0
- package/dist/adw/types.js.map +1 -0
- package/dist/column-view.d.ts +7 -29
- package/dist/column-view.d.ts.map +1 -1
- package/dist/column-view.js +98 -70
- package/dist/column-view.js.map +1 -1
- package/dist/drop-down.d.ts +8 -27
- package/dist/drop-down.d.ts.map +1 -1
- package/dist/drop-down.js +112 -81
- package/dist/drop-down.js.map +1 -1
- package/dist/grid-view.d.ts +6 -16
- package/dist/grid-view.d.ts.map +1 -1
- package/dist/grid-view.js +25 -33
- package/dist/grid-view.js.map +1 -1
- package/dist/index.d.ts +6 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -6
- package/dist/index.js.map +1 -1
- package/dist/internal/cells.d.ts +50 -0
- package/dist/internal/cells.d.ts.map +1 -0
- package/dist/internal/cells.js +185 -0
- package/dist/internal/cells.js.map +1 -0
- package/dist/internal/collection-index.d.ts +21 -0
- package/dist/internal/collection-index.d.ts.map +1 -0
- package/dist/internal/collection-index.js +106 -0
- package/dist/internal/collection-index.js.map +1 -0
- package/dist/internal/collection-model.d.ts +12 -0
- package/dist/internal/collection-model.d.ts.map +1 -0
- package/dist/internal/collection-model.js +164 -0
- package/dist/internal/collection-model.js.map +1 -0
- package/dist/internal/collection.d.ts +18 -0
- package/dist/internal/collection.d.ts.map +1 -0
- package/dist/internal/collection.js +64 -0
- package/dist/internal/collection.js.map +1 -0
- package/dist/internal/expansion.d.ts +9 -0
- package/dist/internal/expansion.d.ts.map +1 -0
- package/dist/internal/expansion.js +75 -0
- package/dist/internal/expansion.js.map +1 -0
- package/dist/internal/selection.d.ts +12 -0
- package/dist/internal/selection.d.ts.map +1 -0
- package/dist/internal/selection.js +99 -0
- package/dist/internal/selection.js.map +1 -0
- package/dist/internal/use-collection.d.ts +18 -0
- package/dist/internal/use-collection.d.ts.map +1 -0
- package/dist/internal/use-collection.js +42 -0
- package/dist/internal/use-collection.js.map +1 -0
- package/dist/internal/use-widget-ref.d.ts +4 -0
- package/dist/internal/use-widget-ref.d.ts.map +1 -0
- package/dist/internal/use-widget-ref.js +8 -0
- package/dist/internal/use-widget-ref.js.map +1 -0
- package/dist/list-view.d.ts +5 -20
- package/dist/list-view.d.ts.map +1 -1
- package/dist/list-view.js +32 -53
- package/dist/list-view.js.map +1 -1
- package/dist/size-group.d.ts +8 -16
- package/dist/size-group.d.ts.map +1 -1
- package/dist/size-group.js +35 -51
- package/dist/size-group.js.map +1 -1
- package/dist/types.d.ts +97 -24
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +17 -6
- package/src/adw/index.ts +2 -0
- package/src/adw/toast.tsx +74 -0
- package/src/adw/types.ts +32 -0
- package/src/column-view.tsx +176 -160
- package/src/drop-down.tsx +180 -167
- package/src/grid-view.tsx +29 -73
- package/src/index.ts +18 -25
- package/src/internal/cells.tsx +345 -0
- package/src/internal/collection-index.ts +168 -0
- package/src/internal/collection-model.ts +246 -0
- package/src/internal/collection.ts +104 -0
- package/src/internal/expansion.ts +123 -0
- package/src/internal/selection.tsx +159 -0
- package/src/internal/use-collection.tsx +70 -0
- package/src/internal/use-widget-ref.ts +11 -0
- package/src/list-view.tsx +43 -139
- package/src/size-group.tsx +63 -82
- package/src/types.ts +155 -24
- package/dist/cell.d.ts +0 -30
- package/dist/cell.d.ts.map +0 -1
- package/dist/cell.js +0 -45
- package/dist/cell.js.map +0 -1
- package/dist/column-view-column.d.ts +0 -27
- package/dist/column-view-column.d.ts.map +0 -1
- package/dist/column-view-column.js +0 -30
- package/dist/column-view-column.js.map +0 -1
- package/dist/constraint-layout-apply.d.ts +0 -55
- package/dist/constraint-layout-apply.d.ts.map +0 -1
- package/dist/constraint-layout-apply.js +0 -107
- package/dist/constraint-layout-apply.js.map +0 -1
- package/dist/constraint-layout.d.ts +0 -20
- package/dist/constraint-layout.d.ts.map +0 -1
- package/dist/constraint-layout.js +0 -88
- package/dist/constraint-layout.js.map +0 -1
- package/dist/fixed.d.ts +0 -26
- package/dist/fixed.d.ts.map +0 -1
- package/dist/fixed.js +0 -40
- package/dist/fixed.js.map +0 -1
- package/dist/grid.d.ts +0 -27
- package/dist/grid.d.ts.map +0 -1
- package/dist/grid.js +0 -48
- package/dist/grid.js.map +0 -1
- package/dist/hooks/use-cell-containers.d.ts +0 -18
- package/dist/hooks/use-cell-containers.d.ts.map +0 -1
- package/dist/hooks/use-cell-containers.js +0 -67
- package/dist/hooks/use-cell-containers.js.map +0 -1
- package/dist/hooks/use-collection-model.d.ts +0 -26
- package/dist/hooks/use-collection-model.d.ts.map +0 -1
- package/dist/hooks/use-collection-model.js +0 -35
- package/dist/hooks/use-collection-model.js.map +0 -1
- package/dist/hooks/use-collection-widget.d.ts +0 -28
- package/dist/hooks/use-collection-widget.d.ts.map +0 -1
- package/dist/hooks/use-collection-widget.js +0 -21
- package/dist/hooks/use-collection-widget.js.map +0 -1
- package/dist/hooks/use-drop-down-selection.d.ts +0 -16
- package/dist/hooks/use-drop-down-selection.d.ts.map +0 -1
- package/dist/hooks/use-drop-down-selection.js +0 -35
- package/dist/hooks/use-drop-down-selection.js.map +0 -1
- package/dist/hooks/use-expansion-model.d.ts +0 -12
- package/dist/hooks/use-expansion-model.d.ts.map +0 -1
- package/dist/hooks/use-expansion-model.js +0 -71
- package/dist/hooks/use-expansion-model.js.map +0 -1
- package/dist/hooks/use-header-menu.d.ts +0 -4
- package/dist/hooks/use-header-menu.d.ts.map +0 -1
- package/dist/hooks/use-header-menu.js +0 -20
- package/dist/hooks/use-header-menu.js.map +0 -1
- package/dist/hooks/use-installed-model.d.ts +0 -4
- package/dist/hooks/use-installed-model.d.ts.map +0 -1
- package/dist/hooks/use-installed-model.js +0 -15
- package/dist/hooks/use-installed-model.js.map +0 -1
- package/dist/hooks/use-list-model.d.ts +0 -19
- package/dist/hooks/use-list-model.d.ts.map +0 -1
- package/dist/hooks/use-list-model.js +0 -97
- package/dist/hooks/use-list-model.js.map +0 -1
- package/dist/hooks/use-placed-child.d.ts +0 -18
- package/dist/hooks/use-placed-child.d.ts.map +0 -1
- package/dist/hooks/use-placed-child.js +0 -47
- package/dist/hooks/use-placed-child.js.map +0 -1
- package/dist/hooks/use-selection-model.d.ts +0 -13
- package/dist/hooks/use-selection-model.d.ts.map +0 -1
- package/dist/hooks/use-selection-model.js +0 -103
- package/dist/hooks/use-selection-model.js.map +0 -1
- package/dist/hooks/use-sort-handler.d.ts +0 -17
- package/dist/hooks/use-sort-handler.d.ts.map +0 -1
- package/dist/hooks/use-sort-handler.js +0 -87
- package/dist/hooks/use-sort-handler.js.map +0 -1
- package/dist/menu-items.d.ts +0 -5
- package/dist/menu-items.d.ts.map +0 -1
- package/dist/menu-items.js +0 -48
- package/dist/menu-items.js.map +0 -1
- package/dist/menu.d.ts +0 -14
- package/dist/menu.d.ts.map +0 -1
- package/dist/menu.js +0 -24
- package/dist/menu.js.map +0 -1
- package/dist/overlay.d.ts +0 -24
- package/dist/overlay.d.ts.map +0 -1
- package/dist/overlay.js +0 -49
- package/dist/overlay.js.map +0 -1
- package/dist/utils/cell-container-store.d.ts +0 -28
- package/dist/utils/cell-container-store.d.ts.map +0 -1
- package/dist/utils/cell-container-store.js +0 -98
- package/dist/utils/cell-container-store.js.map +0 -1
- package/dist/utils/item-models.d.ts +0 -10
- package/dist/utils/item-models.d.ts.map +0 -1
- package/dist/utils/item-models.js +0 -82
- package/dist/utils/item-models.js.map +0 -1
- package/dist/utils/item-resolver.d.ts +0 -25
- package/dist/utils/item-resolver.d.ts.map +0 -1
- package/dist/utils/item-resolver.js +0 -102
- package/dist/utils/item-resolver.js.map +0 -1
- package/dist/utils/list-item-flatten.d.ts +0 -24
- package/dist/utils/list-item-flatten.d.ts.map +0 -1
- package/dist/utils/list-item-flatten.js +0 -68
- package/dist/utils/list-item-flatten.js.map +0 -1
- package/src/cell.tsx +0 -144
- package/src/column-view-column.tsx +0 -82
- package/src/constraint-layout-apply.ts +0 -173
- package/src/constraint-layout.tsx +0 -135
- package/src/fixed.tsx +0 -86
- package/src/grid.tsx +0 -88
- package/src/hooks/use-cell-containers.ts +0 -93
- package/src/hooks/use-collection-model.ts +0 -60
- package/src/hooks/use-collection-widget.ts +0 -47
- package/src/hooks/use-drop-down-selection.ts +0 -52
- package/src/hooks/use-expansion-model.ts +0 -82
- package/src/hooks/use-header-menu.ts +0 -34
- package/src/hooks/use-installed-model.ts +0 -20
- package/src/hooks/use-list-model.ts +0 -161
- package/src/hooks/use-placed-child.ts +0 -79
- package/src/hooks/use-selection-model.ts +0 -114
- package/src/hooks/use-sort-handler.ts +0 -107
- package/src/menu-items.ts +0 -48
- package/src/menu.tsx +0 -32
- package/src/overlay.tsx +0 -88
- package/src/utils/cell-container-store.ts +0 -114
- package/src/utils/item-models.ts +0 -113
- package/src/utils/item-resolver.ts +0 -138
- package/src/utils/list-item-flatten.ts +0 -90
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import * as Gtk from "@gtkx/gi/gtk";
|
|
2
|
-
import { type ObjectProp, resolveObjectProp } from "@gtkx/react/internal";
|
|
3
|
-
import { useLayoutEffect, useRef } from "react";
|
|
4
|
-
|
|
5
|
-
export type ColumnRegistration = {
|
|
6
|
-
id: string;
|
|
7
|
-
column: Gtk.ColumnViewColumn;
|
|
8
|
-
sortable: boolean;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
type SortHandlerOptions = {
|
|
12
|
-
columnView: ObjectProp<Gtk.ColumnView>;
|
|
13
|
-
sortColumn: string | null | undefined;
|
|
14
|
-
sortOrder: Gtk.SortType | null | undefined;
|
|
15
|
-
onSortChanged: ((column: string | null, order: Gtk.SortType) => void) | null | undefined;
|
|
16
|
-
columns: ColumnRegistration[];
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const neutralSorter = (): Gtk.CustomSorter => Gtk.CustomSorter.new(() => 0);
|
|
20
|
-
|
|
21
|
-
const ensureColumnSorters = (columns: ColumnRegistration[], activeId: string | null): void => {
|
|
22
|
-
for (const registration of columns) {
|
|
23
|
-
const wantsSorter = registration.sortable || registration.id === activeId;
|
|
24
|
-
const hasSorter = registration.column.getSorter() !== null;
|
|
25
|
-
if (wantsSorter && !hasSorter) {
|
|
26
|
-
registration.column.setSorter(neutralSorter());
|
|
27
|
-
} else if (!wantsSorter && hasSorter) {
|
|
28
|
-
registration.column.setSorter(null);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
const columnForId = (columns: ColumnRegistration[], id: string | null): Gtk.ColumnViewColumn | null => {
|
|
34
|
-
if (id === null) return null;
|
|
35
|
-
for (const registration of columns) {
|
|
36
|
-
if (registration.id === id) return registration.column;
|
|
37
|
-
}
|
|
38
|
-
return null;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const idForColumn = (columns: ColumnRegistration[], column: Gtk.ColumnViewColumn | null): string | null => {
|
|
42
|
-
if (column === null) return null;
|
|
43
|
-
for (const registration of columns) {
|
|
44
|
-
if (registration.column === column) return registration.id;
|
|
45
|
-
}
|
|
46
|
-
return column.getId();
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const primarySort = (sorter: Gtk.Sorter): { column: Gtk.ColumnViewColumn | null; order: Gtk.SortType } =>
|
|
50
|
-
sorter instanceof Gtk.ColumnViewSorter
|
|
51
|
-
? { column: sorter.getPrimarySortColumn(), order: sorter.getPrimarySortOrder() }
|
|
52
|
-
: { column: null, order: Gtk.SortType.ASCENDING };
|
|
53
|
-
|
|
54
|
-
type SorterSubscription = {
|
|
55
|
-
sorter: Gtk.Sorter | null;
|
|
56
|
-
handler: ((change: Gtk.SorterChange) => void) | null;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
const disconnectSorter = (subscription: SorterSubscription): void => {
|
|
60
|
-
if (subscription.sorter !== null && subscription.handler !== null) {
|
|
61
|
-
subscription.sorter.off("changed", subscription.handler);
|
|
62
|
-
}
|
|
63
|
-
subscription.sorter = null;
|
|
64
|
-
subscription.handler = null;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export const useSortHandler = (options: SortHandlerOptions): void => {
|
|
68
|
-
const { columnView, sortColumn, sortOrder, onSortChanged, columns } = options;
|
|
69
|
-
const activeId = sortColumn ?? null;
|
|
70
|
-
const order = sortOrder ?? Gtk.SortType.ASCENDING;
|
|
71
|
-
|
|
72
|
-
const onChangedRef = useRef(onSortChanged);
|
|
73
|
-
onChangedRef.current = onSortChanged;
|
|
74
|
-
const columnsRef = useRef(columns);
|
|
75
|
-
columnsRef.current = columns;
|
|
76
|
-
const suppressRef = useRef(false);
|
|
77
|
-
const subscriptionRef = useRef<SorterSubscription>({ sorter: null, handler: null });
|
|
78
|
-
|
|
79
|
-
const resubscribe = (sorter: Gtk.Sorter | null): void => {
|
|
80
|
-
const subscription = subscriptionRef.current;
|
|
81
|
-
if (sorter === subscription.sorter) return;
|
|
82
|
-
disconnectSorter(subscription);
|
|
83
|
-
if (sorter === null) return;
|
|
84
|
-
const handler = (): void => {
|
|
85
|
-
if (suppressRef.current) return;
|
|
86
|
-
const callback = onChangedRef.current;
|
|
87
|
-
if (!callback) return;
|
|
88
|
-
const { column, order: primaryOrder } = primarySort(sorter);
|
|
89
|
-
callback(idForColumn(columnsRef.current, column), primaryOrder);
|
|
90
|
-
};
|
|
91
|
-
subscription.sorter = sorter;
|
|
92
|
-
subscription.handler = handler;
|
|
93
|
-
sorter.on("changed", handler);
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
useLayoutEffect(() => {
|
|
97
|
-
const view = resolveObjectProp(columnView);
|
|
98
|
-
if (view === null) return;
|
|
99
|
-
ensureColumnSorters(columns, activeId);
|
|
100
|
-
suppressRef.current = true;
|
|
101
|
-
view.sortByColumn(columnForId(columns, activeId), order);
|
|
102
|
-
suppressRef.current = false;
|
|
103
|
-
resubscribe(view.getSorter());
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
useLayoutEffect(() => () => disconnectSorter(subscriptionRef.current), []);
|
|
107
|
-
};
|
package/src/menu-items.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import * as Gio from "@gtkx/gi/gio";
|
|
2
|
-
import type { MenuEntry } from "./types.js";
|
|
3
|
-
|
|
4
|
-
const buildMenuModel = (entries: MenuEntry[]): Gio.Menu => {
|
|
5
|
-
const menu = new Gio.Menu();
|
|
6
|
-
appendEntries(menu, entries);
|
|
7
|
-
return menu;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
const appendEntries = (menu: Gio.Menu, entries: MenuEntry[]): void => {
|
|
11
|
-
for (const entry of entries) {
|
|
12
|
-
if (entry.section !== undefined) {
|
|
13
|
-
menu.appendSection(entry.label ?? null, buildMenuModel(entry.section));
|
|
14
|
-
continue;
|
|
15
|
-
}
|
|
16
|
-
if (entry.submenu !== undefined) {
|
|
17
|
-
menu.appendSubmenu(entry.label ?? null, buildMenuModel(entry.submenu));
|
|
18
|
-
continue;
|
|
19
|
-
}
|
|
20
|
-
const item = new Gio.MenuItem();
|
|
21
|
-
if (entry.label !== undefined) item.setLabel(entry.label);
|
|
22
|
-
if (entry.action !== undefined) item.setDetailedAction(entry.action);
|
|
23
|
-
menu.appendItem(item);
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export const applyMenuItems = (menu: Gio.Menu, entries: MenuEntry[] | null): void => {
|
|
28
|
-
menu.removeAll();
|
|
29
|
-
if (entries) appendEntries(menu, entries);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const entryEqual = (a: MenuEntry, b: MenuEntry): boolean =>
|
|
33
|
-
a.label === b.label &&
|
|
34
|
-
a.action === b.action &&
|
|
35
|
-
entryListEqual(a.submenu, b.submenu) &&
|
|
36
|
-
entryListEqual(a.section, b.section);
|
|
37
|
-
|
|
38
|
-
const entryListEqual = (a: MenuEntry[] | undefined, b: MenuEntry[] | undefined): boolean => {
|
|
39
|
-
if (a === b) return true;
|
|
40
|
-
if (a === undefined || b === undefined) return false;
|
|
41
|
-
return a.length === b.length && a.every((entry, index) => entryEqual(entry, b[index] as MenuEntry));
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
export const menuItemsEqual = (a: MenuEntry[] | null, b: MenuEntry[] | null): boolean => {
|
|
45
|
-
if (a === b) return true;
|
|
46
|
-
if (a === null || b === null) return false;
|
|
47
|
-
return entryListEqual(a, b);
|
|
48
|
-
};
|
package/src/menu.tsx
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type * as Gio from "@gtkx/gi/gio";
|
|
2
|
-
import { GMenu, type GMenuProps } from "@gtkx/jsx/gio";
|
|
3
|
-
import { useMergeRefs } from "@gtkx/react/internal";
|
|
4
|
-
import { type ReactNode, type Ref, useLayoutEffect, useRef } from "react";
|
|
5
|
-
import { applyMenuItems, menuItemsEqual } from "./menu-items.js";
|
|
6
|
-
import type { MenuEntry } from "./types.js";
|
|
7
|
-
|
|
8
|
-
export type MenuItemsProps = {
|
|
9
|
-
items?: MenuEntry[] | null | undefined;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
/** Props for {@link Menu}, combining Gio.Menu props with a declarative items array. */
|
|
13
|
-
export type MenuProps = Omit<GMenuProps, keyof MenuItemsProps> & MenuItemsProps & { ref?: Ref<Gio.Menu | null> };
|
|
14
|
-
|
|
15
|
-
/** Builds a Gio.Menu model from a declarative array of {@link MenuEntry} items. */
|
|
16
|
-
export const Menu = (props: MenuProps): ReactNode => {
|
|
17
|
-
const { items, ref, ...rest } = props;
|
|
18
|
-
const menuRef = useRef<Gio.Menu | null>(null);
|
|
19
|
-
const mergedRef = useMergeRefs(ref, menuRef);
|
|
20
|
-
const appliedRef = useRef<MenuEntry[] | null>(null);
|
|
21
|
-
|
|
22
|
-
useLayoutEffect(() => {
|
|
23
|
-
const menu = menuRef.current;
|
|
24
|
-
if (!menu) return;
|
|
25
|
-
const entries = items ?? null;
|
|
26
|
-
if (menuItemsEqual(appliedRef.current, entries)) return;
|
|
27
|
-
appliedRef.current = entries;
|
|
28
|
-
applyMenuItems(menu, entries);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
return <GMenu {...rest} ref={mergedRef} />;
|
|
32
|
-
};
|
package/src/overlay.tsx
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import type * as Gtk from "@gtkx/gi/gtk";
|
|
2
|
-
import { GtkOverlay, type GtkOverlayProps } from "@gtkx/jsx/gtk";
|
|
3
|
-
import { useMergeRefs } from "@gtkx/react/internal";
|
|
4
|
-
import { Children, type ElementType, isValidElement, type ReactNode, type Ref, useRef } from "react";
|
|
5
|
-
import { createParentContext, usePlacedChild } from "./hooks/use-placed-child.js";
|
|
6
|
-
import type { ChildProps } from "./types.js";
|
|
7
|
-
|
|
8
|
-
const { Context: OverlayContext, useParentRef: useOverlayRef } = createParentContext<Gtk.Overlay>(
|
|
9
|
-
"<Overlay.Child> must be a child of <Overlay>",
|
|
10
|
-
);
|
|
11
|
-
|
|
12
|
-
/** Props for {@link Overlay}. */
|
|
13
|
-
export type OverlayProps = GtkOverlayProps & { ref?: Ref<Gtk.Overlay | null> };
|
|
14
|
-
|
|
15
|
-
export type OverlayPlacementProps = {
|
|
16
|
-
/** Whether this overlay contributes to the Overlay's measured size. */
|
|
17
|
-
measure?: boolean | null | undefined;
|
|
18
|
-
/** Whether the overlay is clipped to the main child's allocation. */
|
|
19
|
-
clipOverlay?: boolean | null | undefined;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
/** Adds a single widget as an overlay on top of an {@link Overlay}'s main child. */
|
|
23
|
-
export type OverlayChildProps<C extends ElementType> = ChildProps<C, OverlayPlacementProps>;
|
|
24
|
-
|
|
25
|
-
type OverlayPlacement = { measure: boolean; clipOverlay: boolean };
|
|
26
|
-
|
|
27
|
-
const placementOf = (props: OverlayPlacementProps): OverlayPlacement => ({
|
|
28
|
-
measure: props.measure ?? false,
|
|
29
|
-
clipOverlay: props.clipOverlay ?? false,
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
const samePlacement = (a: OverlayPlacement, b: OverlayPlacement): boolean =>
|
|
33
|
-
a.measure === b.measure && a.clipOverlay === b.clipOverlay;
|
|
34
|
-
|
|
35
|
-
const OverlayChild = <C extends ElementType>({
|
|
36
|
-
component,
|
|
37
|
-
measure,
|
|
38
|
-
clipOverlay,
|
|
39
|
-
ref,
|
|
40
|
-
...rest
|
|
41
|
-
}: OverlayChildProps<C>): ReactNode => {
|
|
42
|
-
const overlayRef = useOverlayRef();
|
|
43
|
-
const Component: ElementType = component;
|
|
44
|
-
return usePlacedChild<Gtk.Widget, OverlayPlacement>({
|
|
45
|
-
render: (placeRef) => <Component {...rest} ref={placeRef} />,
|
|
46
|
-
ref,
|
|
47
|
-
placement: placementOf({ measure, clipOverlay }),
|
|
48
|
-
samePlacement,
|
|
49
|
-
place: (widget, placement) => {
|
|
50
|
-
const overlay = overlayRef.current;
|
|
51
|
-
if (!overlay) return;
|
|
52
|
-
if (widget.getParent() !== overlay) overlay.addOverlay(widget);
|
|
53
|
-
overlay.setMeasureOverlay(widget, placement.measure);
|
|
54
|
-
overlay.setClipOverlay(widget, placement.clipOverlay);
|
|
55
|
-
},
|
|
56
|
-
release: (widget) => {
|
|
57
|
-
const overlay = overlayRef.current;
|
|
58
|
-
if (overlay && widget.getParent() === overlay) overlay.removeOverlay(widget);
|
|
59
|
-
},
|
|
60
|
-
});
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
const isOverlayChild = (node: ReactNode): boolean => isValidElement(node) && node.type === OverlayChild;
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Renders a Gtk.Overlay: a main child with one or more widgets stacked on top,
|
|
67
|
-
* declared via {@link Overlay.Child}. Non-Child children form the main content.
|
|
68
|
-
*/
|
|
69
|
-
export const Overlay: ((props: OverlayProps) => ReactNode) & {
|
|
70
|
-
Child: <C extends ElementType>(props: OverlayChildProps<C>) => ReactNode;
|
|
71
|
-
} = Object.assign(
|
|
72
|
-
({ children, ref, ...rest }: OverlayProps): ReactNode => {
|
|
73
|
-
const overlayRef = useRef<Gtk.Overlay | null>(null);
|
|
74
|
-
const mergedRef = useMergeRefs<Gtk.Overlay>(ref, overlayRef);
|
|
75
|
-
const items = Children.toArray(children);
|
|
76
|
-
const overlays = items.filter(isOverlayChild);
|
|
77
|
-
const base = items.filter((node) => !isOverlayChild(node));
|
|
78
|
-
return (
|
|
79
|
-
<>
|
|
80
|
-
<GtkOverlay {...rest} ref={mergedRef}>
|
|
81
|
-
{base}
|
|
82
|
-
</GtkOverlay>
|
|
83
|
-
<OverlayContext.Provider value={overlayRef}>{overlays}</OverlayContext.Provider>
|
|
84
|
-
</>
|
|
85
|
-
);
|
|
86
|
-
},
|
|
87
|
-
{ Child: OverlayChild },
|
|
88
|
-
);
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import type * as GObject from "@gtkx/gi/gobject";
|
|
2
|
-
import type * as Gtk from "@gtkx/gi/gtk";
|
|
3
|
-
|
|
4
|
-
export type CellEntry = {
|
|
5
|
-
position: number;
|
|
6
|
-
treeRow: Gtk.TreeListRow | null;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
let nextKey = 0;
|
|
10
|
-
|
|
11
|
-
export class CellContainerStore {
|
|
12
|
-
private entries = new Map<GObject.Object, CellEntry>();
|
|
13
|
-
|
|
14
|
-
private published = new Map<GObject.Object, CellEntry>();
|
|
15
|
-
|
|
16
|
-
private setListeners = new Set<() => void>();
|
|
17
|
-
|
|
18
|
-
private positionListeners = new Map<GObject.Object, Set<() => void>>();
|
|
19
|
-
|
|
20
|
-
private positionFlushers = new Map<GObject.Object, () => void>();
|
|
21
|
-
|
|
22
|
-
private keys = new WeakMap<GObject.Object, string>();
|
|
23
|
-
|
|
24
|
-
private snapshot: GObject.Object[] = [];
|
|
25
|
-
|
|
26
|
-
subscribeSet = (onChange: () => void): (() => void) => {
|
|
27
|
-
this.setListeners.add(onChange);
|
|
28
|
-
return () => {
|
|
29
|
-
this.setListeners.delete(onChange);
|
|
30
|
-
};
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
getContainersSnapshot = (): GObject.Object[] => this.snapshot;
|
|
34
|
-
|
|
35
|
-
subscribePosition = (container: GObject.Object, onChange: () => void): (() => void) => {
|
|
36
|
-
let listeners = this.positionListeners.get(container);
|
|
37
|
-
if (listeners === undefined) {
|
|
38
|
-
listeners = new Set();
|
|
39
|
-
this.positionListeners.set(container, listeners);
|
|
40
|
-
}
|
|
41
|
-
listeners.add(onChange);
|
|
42
|
-
return () => {
|
|
43
|
-
const current = this.positionListeners.get(container);
|
|
44
|
-
if (current === undefined) return;
|
|
45
|
-
current.delete(onChange);
|
|
46
|
-
if (current.size === 0) this.positionListeners.delete(container);
|
|
47
|
-
};
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
getPosition = (container: GObject.Object): CellEntry => {
|
|
51
|
-
let entry = this.published.get(container);
|
|
52
|
-
if (entry === undefined) {
|
|
53
|
-
entry = { position: -1, treeRow: null };
|
|
54
|
-
this.published.set(container, entry);
|
|
55
|
-
}
|
|
56
|
-
return entry;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
keyFor = (container: GObject.Object): string => {
|
|
60
|
-
const existing = this.keys.get(container);
|
|
61
|
-
if (existing !== undefined) return existing;
|
|
62
|
-
const key = `cell-${nextKey++}`;
|
|
63
|
-
this.keys.set(container, key);
|
|
64
|
-
return key;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
addContainer = (container: GObject.Object): void => {
|
|
68
|
-
if (this.entries.has(container)) return;
|
|
69
|
-
this.entries.set(container, { position: -1, treeRow: null });
|
|
70
|
-
this.notifySet();
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
bind = (container: GObject.Object, position: number, treeRow: Gtk.TreeListRow | null): void => {
|
|
74
|
-
this.entries.set(container, { position, treeRow });
|
|
75
|
-
this.notifyPosition(container);
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
unbind = (container: GObject.Object): void => {
|
|
79
|
-
if (!this.entries.has(container)) return;
|
|
80
|
-
this.entries.set(container, { position: -1, treeRow: null });
|
|
81
|
-
this.notifyPosition(container);
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
removeContainer = (container: GObject.Object): void => {
|
|
85
|
-
if (!this.entries.delete(container)) return;
|
|
86
|
-
this.published.delete(container);
|
|
87
|
-
this.positionFlushers.delete(container);
|
|
88
|
-
this.notifySet();
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
private flushSet = (): void => {
|
|
92
|
-
this.snapshot = [...this.entries.keys()];
|
|
93
|
-
for (const listener of [...this.setListeners]) listener();
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
private notifySet(): void {
|
|
97
|
-
this.flushSet();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
private notifyPosition(container: GObject.Object): void {
|
|
101
|
-
let flush = this.positionFlushers.get(container);
|
|
102
|
-
if (flush === undefined) {
|
|
103
|
-
flush = (): void => {
|
|
104
|
-
const live = this.entries.get(container);
|
|
105
|
-
if (live !== undefined) this.published.set(container, live);
|
|
106
|
-
const listeners = this.positionListeners.get(container);
|
|
107
|
-
if (listeners === undefined) return;
|
|
108
|
-
for (const listener of [...listeners]) listener();
|
|
109
|
-
};
|
|
110
|
-
this.positionFlushers.set(container, flush);
|
|
111
|
-
}
|
|
112
|
-
flush();
|
|
113
|
-
}
|
|
114
|
-
}
|
package/src/utils/item-models.ts
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import * as Gio from "@gtkx/gi/gio";
|
|
2
|
-
import type * as GObject from "@gtkx/gi/gobject";
|
|
3
|
-
import * as Gtk from "@gtkx/gi/gtk";
|
|
4
|
-
import type { ItemNode, SectionNode } from "../types.js";
|
|
5
|
-
import type { RowValue } from "./item-resolver.js";
|
|
6
|
-
import { countDescendants, treeItemMetadata } from "./list-item-flatten.js";
|
|
7
|
-
|
|
8
|
-
const emptyStrings = (count: number): string[] => new Array<string>(Math.max(0, count)).fill("");
|
|
9
|
-
|
|
10
|
-
export const retagRows = <T>(
|
|
11
|
-
items: ItemNode<T>[],
|
|
12
|
-
rowValues: WeakMap<GObject.Object, RowValue<T>>,
|
|
13
|
-
placeholdersById: Map<string, GObject.Object>,
|
|
14
|
-
): void => {
|
|
15
|
-
const walk = (list: ItemNode<T>[]): void => {
|
|
16
|
-
for (const item of list) {
|
|
17
|
-
const placeholder = placeholdersById.get(item.id);
|
|
18
|
-
if (placeholder !== undefined) {
|
|
19
|
-
rowValues.set(placeholder, {
|
|
20
|
-
id: item.id,
|
|
21
|
-
value: item.value,
|
|
22
|
-
metadata: treeItemMetadata(item),
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
if (item.children !== undefined && item.children.length > 0) walk(item.children);
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
walk(items);
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export const createFlatModel = (count: number): Gtk.StringList => Gtk.StringList.new(emptyStrings(count));
|
|
32
|
-
|
|
33
|
-
export const resizeFlatModel = (model: Gtk.StringList, count: number): void => {
|
|
34
|
-
const target = Math.max(0, count);
|
|
35
|
-
const current = model.getNItems();
|
|
36
|
-
if (current === target) return;
|
|
37
|
-
if (target > current) {
|
|
38
|
-
model.splice(current, 0, emptyStrings(target - current));
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
model.splice(target, current - target, null);
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const createTaggedRow = <T>(
|
|
45
|
-
row: RowValue<T>,
|
|
46
|
-
rowValues: WeakMap<GObject.Object, RowValue<T>>,
|
|
47
|
-
placeholdersById?: Map<string, GObject.Object>,
|
|
48
|
-
): GObject.Object => {
|
|
49
|
-
const placeholder = Gtk.StringObject.new("");
|
|
50
|
-
rowValues.set(placeholder, row);
|
|
51
|
-
if (placeholdersById !== undefined) placeholdersById.set(row.id, placeholder);
|
|
52
|
-
return placeholder;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
const childModelOf = <T>(
|
|
56
|
-
children: ItemNode<T>[],
|
|
57
|
-
rowValues: WeakMap<GObject.Object, RowValue<T>>,
|
|
58
|
-
placeholdersById: Map<string, GObject.Object>,
|
|
59
|
-
): Gio.ListStore => {
|
|
60
|
-
const store = Gio.ListStore.new(Gtk.StringObject.prototype.__type__);
|
|
61
|
-
for (const child of children) {
|
|
62
|
-
store.append(
|
|
63
|
-
createTaggedRow(
|
|
64
|
-
{ id: child.id, value: child.value, metadata: treeItemMetadata(child) },
|
|
65
|
-
rowValues,
|
|
66
|
-
placeholdersById,
|
|
67
|
-
),
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
return store;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
export const createTreeModel = <T>(
|
|
74
|
-
items: ItemNode<T>[],
|
|
75
|
-
rowValues: WeakMap<GObject.Object, RowValue<T>>,
|
|
76
|
-
placeholdersById: Map<string, GObject.Object>,
|
|
77
|
-
): Gtk.TreeListModel => {
|
|
78
|
-
const childrenByRow = new WeakMap<GObject.Object, ItemNode<T>[]>();
|
|
79
|
-
const root = Gio.ListStore.new(Gtk.StringObject.prototype.__type__);
|
|
80
|
-
for (const item of items) {
|
|
81
|
-
const placeholder = createTaggedRow(
|
|
82
|
-
{ id: item.id, value: item.value, metadata: treeItemMetadata(item) },
|
|
83
|
-
rowValues,
|
|
84
|
-
placeholdersById,
|
|
85
|
-
);
|
|
86
|
-
if (item.children !== undefined && item.children.length > 0) {
|
|
87
|
-
childrenByRow.set(placeholder, item.children);
|
|
88
|
-
}
|
|
89
|
-
root.append(placeholder);
|
|
90
|
-
}
|
|
91
|
-
const createFunc = (rowItem: GObject.Object): Gio.ListModel | null => {
|
|
92
|
-
const children = childrenByRow.get(rowItem);
|
|
93
|
-
if (children === undefined) return null;
|
|
94
|
-
const store = childModelOf(children, rowValues, placeholdersById);
|
|
95
|
-
for (let index = 0; index < children.length; index++) {
|
|
96
|
-
const child = children[index];
|
|
97
|
-
if (child !== undefined && child.children !== undefined && child.children.length > 0) {
|
|
98
|
-
const placeholder = store.getItem(index);
|
|
99
|
-
if (placeholder !== null) childrenByRow.set(placeholder, child.children);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return store;
|
|
103
|
-
};
|
|
104
|
-
return Gtk.TreeListModel.new(root, false, false, createFunc);
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
export const createSectionModel = <S, T>(sections: SectionNode<S, T>[]): Gtk.FlattenListModel => {
|
|
108
|
-
const store = Gio.ListStore.new(Gtk.StringList.prototype.__type__);
|
|
109
|
-
for (const section of sections) {
|
|
110
|
-
store.append(Gtk.StringList.new(emptyStrings(countDescendants(section.data))));
|
|
111
|
-
}
|
|
112
|
-
return Gtk.FlattenListModel.new(store);
|
|
113
|
-
};
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import type * as GObject from "@gtkx/gi/gobject";
|
|
2
|
-
import type * as Gtk from "@gtkx/gi/gtk";
|
|
3
|
-
import type { ItemNode, SectionNode } from "../types.js";
|
|
4
|
-
import { countDescendants, flattenListItems, type TreeItemMetadata } from "./list-item-flatten.js";
|
|
5
|
-
|
|
6
|
-
const NO_TREE_METADATA: TreeItemMetadata = { hideExpander: false, indentForDepth: true, indentForIcon: true };
|
|
7
|
-
|
|
8
|
-
const treeItemCache = new WeakMap<Gtk.TreeListRow, GObject.Object>();
|
|
9
|
-
|
|
10
|
-
const treeRowItem = (treeRow: Gtk.TreeListRow): GObject.Object | null => {
|
|
11
|
-
const cached = treeItemCache.get(treeRow);
|
|
12
|
-
if (cached !== undefined) return cached;
|
|
13
|
-
const item = treeRow.getItem();
|
|
14
|
-
if (item !== null) treeItemCache.set(treeRow, item);
|
|
15
|
-
return item;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export type Resolved<T = unknown, S = unknown> = {
|
|
19
|
-
value: T | S | undefined;
|
|
20
|
-
present: boolean;
|
|
21
|
-
isHeader: boolean;
|
|
22
|
-
metadata: TreeItemMetadata;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export type ItemResolver<T = unknown, S = unknown> = {
|
|
26
|
-
resolve(position: number, treeRow: Gtk.TreeListRow | null): Resolved<T, S>;
|
|
27
|
-
positionOfId(id: string): number;
|
|
28
|
-
idOf(position: number): string | undefined;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export type RowValue<T = unknown> = {
|
|
32
|
-
id: string;
|
|
33
|
-
value: T;
|
|
34
|
-
metadata: TreeItemMetadata;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
export const rowIdOf = <T>(
|
|
38
|
-
rowValues: WeakMap<GObject.Object, RowValue<T>>,
|
|
39
|
-
row: Gtk.TreeListRow,
|
|
40
|
-
): string | undefined => {
|
|
41
|
-
const item = row.getItem();
|
|
42
|
-
return item === null ? undefined : rowValues.get(item)?.id;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export const createItemResolver = <T, S>(
|
|
46
|
-
items: ItemNode<T>[] | undefined,
|
|
47
|
-
flattenTreeChildren: boolean,
|
|
48
|
-
rowValues: WeakMap<GObject.Object, RowValue<T>>,
|
|
49
|
-
): ItemResolver<T, S> => {
|
|
50
|
-
const flattened = flattenListItems(items, flattenTreeChildren);
|
|
51
|
-
return {
|
|
52
|
-
positionOfId(id: string): number {
|
|
53
|
-
const position = flattened.idToPosition.get(id);
|
|
54
|
-
return position === undefined ? -1 : position;
|
|
55
|
-
},
|
|
56
|
-
idOf(position: number): string | undefined {
|
|
57
|
-
return flattened.positionToId.get(position);
|
|
58
|
-
},
|
|
59
|
-
resolve(position: number, treeRow: Gtk.TreeListRow | null): Resolved<T, S> {
|
|
60
|
-
if (treeRow !== null && !flattenTreeChildren) {
|
|
61
|
-
const rowItem = treeRowItem(treeRow);
|
|
62
|
-
if (rowItem !== null) {
|
|
63
|
-
const tagged = rowValues.get(rowItem);
|
|
64
|
-
if (tagged !== undefined) {
|
|
65
|
-
return {
|
|
66
|
-
value: tagged.value,
|
|
67
|
-
present: true,
|
|
68
|
-
isHeader: false,
|
|
69
|
-
metadata: tagged.metadata,
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
const record = flattened.records[position];
|
|
75
|
-
if (record === undefined) {
|
|
76
|
-
return {
|
|
77
|
-
value: undefined,
|
|
78
|
-
present: false,
|
|
79
|
-
isHeader: false,
|
|
80
|
-
metadata: NO_TREE_METADATA,
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
return {
|
|
84
|
-
value: record.value,
|
|
85
|
-
present: true,
|
|
86
|
-
isHeader: false,
|
|
87
|
-
metadata: record.metadata,
|
|
88
|
-
};
|
|
89
|
-
},
|
|
90
|
-
};
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
export const createSectionHeaderResolver = <T, S>(sections: SectionNode<S, T>[] | undefined): ItemResolver<T, S> => {
|
|
94
|
-
const valueByStart = new Map<number, S>();
|
|
95
|
-
let start = 0;
|
|
96
|
-
for (const section of sections ?? []) {
|
|
97
|
-
valueByStart.set(start, section.value);
|
|
98
|
-
start += countDescendants(section.data);
|
|
99
|
-
}
|
|
100
|
-
return {
|
|
101
|
-
positionOfId: () => -1,
|
|
102
|
-
idOf: () => undefined,
|
|
103
|
-
resolve(position: number): Resolved<T, S> {
|
|
104
|
-
if (!valueByStart.has(position)) {
|
|
105
|
-
return { value: undefined, present: false, isHeader: true, metadata: NO_TREE_METADATA };
|
|
106
|
-
}
|
|
107
|
-
return {
|
|
108
|
-
value: valueByStart.get(position),
|
|
109
|
-
present: true,
|
|
110
|
-
isHeader: true,
|
|
111
|
-
metadata: NO_TREE_METADATA,
|
|
112
|
-
};
|
|
113
|
-
},
|
|
114
|
-
};
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
export const createTreeResolver = <T, S>(
|
|
118
|
-
items: ItemNode<T>[] | undefined,
|
|
119
|
-
rowValues: WeakMap<GObject.Object, RowValue<T>>,
|
|
120
|
-
model: Gtk.TreeListModel,
|
|
121
|
-
): ItemResolver<T, S> => {
|
|
122
|
-
const base = createItemResolver<T, S>(items, false, rowValues);
|
|
123
|
-
return {
|
|
124
|
-
resolve: base.resolve,
|
|
125
|
-
idOf: (position: number): string | undefined => {
|
|
126
|
-
const row = model.getRow(position);
|
|
127
|
-
return row === null ? undefined : rowIdOf(rowValues, row);
|
|
128
|
-
},
|
|
129
|
-
positionOfId: (id: string): number => {
|
|
130
|
-
const count = model.getNItems();
|
|
131
|
-
for (let position = 0; position < count; position++) {
|
|
132
|
-
const row = model.getRow(position);
|
|
133
|
-
if (row !== null && rowIdOf(rowValues, row) === id) return position;
|
|
134
|
-
}
|
|
135
|
-
return -1;
|
|
136
|
-
},
|
|
137
|
-
};
|
|
138
|
-
};
|