@gtkx/components 1.0.0-rc.3 → 1.0.0

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.
Files changed (104) hide show
  1. package/README.md +8 -9
  2. package/dist/adw/combo-row.d.ts +1 -0
  3. package/dist/adw/combo-row.d.ts.map +1 -1
  4. package/dist/adw/combo-row.js.map +1 -1
  5. package/dist/adw/types.d.ts +2 -1
  6. package/dist/adw/types.d.ts.map +1 -1
  7. package/dist/adw/types.js.map +1 -1
  8. package/dist/column-view.d.ts +2 -2
  9. package/dist/column-view.d.ts.map +1 -1
  10. package/dist/column-view.js +15 -13
  11. package/dist/column-view.js.map +1 -1
  12. package/dist/drop-down.d.ts +1 -0
  13. package/dist/drop-down.d.ts.map +1 -1
  14. package/dist/drop-down.js.map +1 -1
  15. package/dist/grid-view.js +2 -2
  16. package/dist/grid-view.js.map +1 -1
  17. package/dist/index.d.ts +1 -2
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +0 -1
  20. package/dist/index.js.map +1 -1
  21. package/dist/internal/cells.d.ts +32 -24
  22. package/dist/internal/cells.d.ts.map +1 -1
  23. package/dist/internal/cells.js +168 -77
  24. package/dist/internal/cells.js.map +1 -1
  25. package/dist/internal/collection-index.d.ts +9 -13
  26. package/dist/internal/collection-index.d.ts.map +1 -1
  27. package/dist/internal/collection-index.js +33 -70
  28. package/dist/internal/collection-index.js.map +1 -1
  29. package/dist/internal/collection-model.d.ts +23 -2
  30. package/dist/internal/collection-model.d.ts.map +1 -1
  31. package/dist/internal/collection-model.js +210 -98
  32. package/dist/internal/collection-model.js.map +1 -1
  33. package/dist/internal/collection.d.ts +9 -12
  34. package/dist/internal/collection.d.ts.map +1 -1
  35. package/dist/internal/collection.js +38 -49
  36. package/dist/internal/collection.js.map +1 -1
  37. package/dist/internal/controlled-sync.d.ts +11 -0
  38. package/dist/internal/controlled-sync.d.ts.map +1 -0
  39. package/dist/internal/controlled-sync.js +19 -0
  40. package/dist/internal/controlled-sync.js.map +1 -0
  41. package/dist/internal/drop-down.d.ts.map +1 -1
  42. package/dist/internal/drop-down.js +27 -15
  43. package/dist/internal/drop-down.js.map +1 -1
  44. package/dist/internal/expansion.d.ts +3 -2
  45. package/dist/internal/expansion.d.ts.map +1 -1
  46. package/dist/internal/expansion.js +80 -47
  47. package/dist/internal/expansion.js.map +1 -1
  48. package/dist/internal/keys.d.ts +5 -0
  49. package/dist/internal/keys.d.ts.map +1 -0
  50. package/dist/internal/keys.js +13 -0
  51. package/dist/internal/keys.js.map +1 -0
  52. package/dist/internal/selection.d.ts +3 -2
  53. package/dist/internal/selection.d.ts.map +1 -1
  54. package/dist/internal/selection.js +68 -35
  55. package/dist/internal/selection.js.map +1 -1
  56. package/dist/internal/slots.d.ts +10 -0
  57. package/dist/internal/slots.d.ts.map +1 -0
  58. package/dist/internal/slots.js +43 -0
  59. package/dist/internal/slots.js.map +1 -0
  60. package/dist/internal/tree-expansion.d.ts +20 -0
  61. package/dist/internal/tree-expansion.d.ts.map +1 -0
  62. package/dist/internal/tree-expansion.js +68 -0
  63. package/dist/internal/tree-expansion.js.map +1 -0
  64. package/dist/internal/tree-order.d.ts +27 -0
  65. package/dist/internal/tree-order.d.ts.map +1 -0
  66. package/dist/internal/tree-order.js +71 -0
  67. package/dist/internal/tree-order.js.map +1 -0
  68. package/dist/internal/use-collection.d.ts +5 -5
  69. package/dist/internal/use-collection.d.ts.map +1 -1
  70. package/dist/internal/use-collection.js +6 -6
  71. package/dist/internal/use-collection.js.map +1 -1
  72. package/dist/list-view.d.ts.map +1 -1
  73. package/dist/list-view.js +6 -6
  74. package/dist/list-view.js.map +1 -1
  75. package/dist/types.d.ts +127 -54
  76. package/dist/types.d.ts.map +1 -1
  77. package/dist/types.js.map +1 -1
  78. package/package.json +7 -7
  79. package/src/adw/combo-row.tsx +1 -0
  80. package/src/adw/types.ts +2 -1
  81. package/src/column-view.tsx +38 -35
  82. package/src/drop-down.tsx +1 -0
  83. package/src/grid-view.tsx +2 -2
  84. package/src/index.ts +10 -11
  85. package/src/internal/cells.tsx +345 -123
  86. package/src/internal/collection-index.ts +51 -99
  87. package/src/internal/collection-model.ts +259 -110
  88. package/src/internal/collection.ts +46 -68
  89. package/src/internal/controlled-sync.ts +34 -0
  90. package/src/internal/drop-down.tsx +44 -23
  91. package/src/internal/expansion.ts +104 -67
  92. package/src/internal/keys.ts +18 -0
  93. package/src/internal/selection.tsx +100 -51
  94. package/src/internal/slots.ts +67 -0
  95. package/src/internal/tree-expansion.ts +110 -0
  96. package/src/internal/tree-order.ts +138 -0
  97. package/src/internal/use-collection.tsx +11 -11
  98. package/src/list-view.tsx +14 -10
  99. package/src/types.ts +140 -65
  100. package/dist/size-group.d.ts +0 -12
  101. package/dist/size-group.d.ts.map +0 -1
  102. package/dist/size-group.js +0 -41
  103. package/dist/size-group.js.map +0 -1
  104. package/src/size-group.tsx +0 -77
@@ -1,64 +1,53 @@
1
- import { getId } from "./collection-model.js";
2
- const ascending = (a, b) => a - b;
3
- function visitRow(tree, position, visit) {
4
- const row = tree.getRow(position);
5
- if (row === null) {
6
- return;
1
+ import { slotPathAt, slotRefFor } from "./collection-model.js";
2
+ import { findPositions } from "./tree-order.js";
3
+ const NO_POSITIONS = [];
4
+ function positionsFor(collectionModel, ids) {
5
+ if (ids.length === 0) {
6
+ return NO_POSITIONS;
7
7
  }
8
- visit(row, getId(row.getItem()));
8
+ const { expansion } = collectionModel;
9
+ return findPositions(expansion.index, expansion.slots, new Set(ids));
9
10
  }
10
- function eachRow(tree, visit) {
11
- for (let position = 0; position < tree.getNItems(); position++) {
12
- visitRow(tree, position, visit);
13
- }
14
- }
15
- function collectWanted(item, position, wanted, out) {
16
- const id = getId(item);
17
- if (id !== null && wanted.has(id)) {
18
- out.push(position);
19
- }
11
+ function positionFor(collectionModel, id) {
12
+ const [first = -1] = positionsFor(collectionModel, [id]);
13
+ return first;
20
14
  }
21
- function scanPositions(tree, ids) {
22
- const wanted = new Set(ids);
23
- const positions = [];
24
- const count = tree.getNItems();
25
- for (let position = 0; position < count; position++) {
26
- collectWanted(tree.getItem(position), position, wanted, positions);
15
+ function refAt(collectionModel, position) {
16
+ if (position < 0) {
17
+ return null;
27
18
  }
28
- return positions;
19
+ return slotRefFor(collectionModel.model.getItem(position));
29
20
  }
30
- function indexPositions(index, ids) {
31
- const positions = [];
32
- for (const id of ids) {
33
- const position = index.positionFor(id);
34
- if (position >= 0) {
35
- positions.push(position);
36
- }
21
+ function idAt(collectionModel, index, position) {
22
+ const ref = refAt(collectionModel, position);
23
+ if (ref === null) {
24
+ return null;
37
25
  }
38
- return positions.toSorted(ascending);
26
+ return index.itemAt(ref.store.path, ref.slot)?.id ?? null;
39
27
  }
40
- function positionsFor(gtk, index, ids) {
41
- if (!index.isTree) {
42
- return indexPositions(index, ids);
28
+ function pathAt(collectionModel, position) {
29
+ const ref = refAt(collectionModel, position);
30
+ if (ref === null) {
31
+ return null;
43
32
  }
44
- const tree = gtk.treeModel();
45
- return tree === null ? [] : scanPositions(tree, ids);
33
+ return slotPathAt(ref.store, ref.slot);
46
34
  }
47
- function positionFor(gtk, index, id) {
48
- const [first = -1] = positionsFor(gtk, index, [id]);
49
- return first;
35
+ function isCollectionIdle(collection) {
36
+ const { expansion } = collection;
37
+ return !expansion.isApplying && !expansion.isSyncing;
50
38
  }
51
- function createCollection(gtk, index) {
39
+ function createCollection(collectionModel, index) {
52
40
  return {
53
- isTree: index.isTree,
54
- model: gtk.model,
55
- treeModel: () => (index.isTree ? gtk.treeModel() : null),
56
- itemFor: index.itemFor,
41
+ model: collectionModel.model,
42
+ expansion: collectionModel.expansion,
43
+ treeModel: () => (index.isTree ? collectionModel.treeModel() : null),
44
+ itemAt: (ref) => index.itemAt(ref.store.path, ref.slot),
57
45
  sectionFor: index.sectionFor,
58
- idAt: (position) => getId(gtk.model.getItem(position)),
59
- positionFor: (id) => positionFor(gtk, index, id),
60
- positionsFor: (ids) => positionsFor(gtk, index, ids),
46
+ idAt: (position) => idAt(collectionModel, index, position),
47
+ pathAt: (position) => pathAt(collectionModel, position),
48
+ positionFor: (id) => positionFor(collectionModel, id),
49
+ positionsFor: (ids) => positionsFor(collectionModel, ids),
61
50
  };
62
51
  }
63
- export { createCollection, eachRow };
52
+ export { createCollection, isCollectionIdle };
64
53
  //# sourceMappingURL=collection.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"collection.js","sourceRoot":"","sources":["../../src/internal/collection.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAa9C,MAAM,SAAS,GAAG,CAAC,CAAS,EAAE,CAAS,EAAU,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC;AAE1D,SAAS,QAAQ,CACb,IAAuB,EACvB,QAAgB,EAChB,KAAwD;IAExD,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAElC,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;QACf,OAAO;IACX,CAAC;IAED,KAAK,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;AACrC,CAAC;AAED,SAAS,OAAO,CAAC,IAAuB,EAAE,KAAwD;IAC9F,KAAK,IAAI,QAAQ,GAAG,CAAC,EAAE,QAAQ,GAAG,IAAI,CAAC,SAAS,EAAE,EAAE,QAAQ,EAAE,EAAE,CAAC;QAC7D,QAAQ,CAAC,IAAI,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;IACpC,CAAC;AACL,CAAC;AAED,SAAS,aAAa,CAAC,IAA2B,EAAE,QAAgB,EAAE,MAAmB,EAAE,GAAa;IACpG,MAAM,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC;IAEvB,IAAI,EAAE,KAAK,IAAI,IAAI,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC;QAChC,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACvB,CAA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E,EAAU;IACzE,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,GAAG,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IAEpD,OAAO,KAAK,CAAC;AACjB,CAAC;AAED,SAAS,gBAAgB,CAAC,GAAoB,EAAE,KAAsB;IAClE,OAAO;QACH,MAAM,EAAE,KAAK,CAAC,MAAM;QACpB,KAAK,EAAE,GAAG,CAAC,KAAK;QAChB,SAAS,EAAE,GAAG,EAAE,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;QACxD,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,UAAU,EAAE,KAAK,CAAC,UAAU;QAC5B,IAAI,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QACtD,WAAW,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,WAAW,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,CAAC;QAChD,YAAY,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,YAAY,CAAC,GAAG,EAAE,KAAK,EAAE,GAAG,CAAC;KACvD,CAAC;AACN,CAAC;AAED,OAAO,EAAE,gBAAgB,EAAE,OAAO,EAAmB,CAAC","sourcesContent":["import type * as GObject from \"@gtkx/gi/gobject\";\nimport type * as Gtk from \"@gtkx/gi/gtk\";\nimport type { Item } from \"../types.js\";\nimport type { CollectionIndex } from \"./collection-index.js\";\nimport type { CollectionModel } from \"./collection-model.js\";\nimport { getId } from \"./collection-model.js\";\n\ntype Collection = {\n isTree: boolean;\n model: Gtk.FlattenListModel;\n treeModel: () => Gtk.TreeListModel | null;\n itemFor: (id: string) => Item | undefined;\n sectionFor: (id: string) => unknown;\n idAt: (position: number) => string | null;\n positionFor: (id: string) => number;\n positionsFor: (ids: string[]) => number[];\n};\n\nconst ascending = (a: number, b: number): number => a - b;\n\nfunction visitRow(\n tree: Gtk.TreeListModel,\n position: number,\n visit: (row: Gtk.TreeListRow, id: string | null) => void,\n): void {\n const row = tree.getRow(position);\n\n if (row === null) {\n return;\n }\n\n visit(row, getId(row.getItem()));\n}\n\nfunction eachRow(tree: Gtk.TreeListModel, visit: (row: Gtk.TreeListRow, id: string | null) => void): void {\n for (let position = 0; position < tree.getNItems(); position++) {\n visitRow(tree, position, visit);\n }\n}\n\nfunction collectWanted(item: GObject.Object | null, position: number, wanted: Set<string>, out: number[]): void {\n const id = getId(item);\n\n if (id !== null && wanted.has(id)) {\n out.push(position);\n }\n}\n\nfunction scanPositions(tree: Gtk.TreeListModel, ids: string[]): number[] {\n const wanted = new Set(ids);\n const positions: number[] = [];\n const count = tree.getNItems();\n\n for (let position = 0; position < count; position++) {\n collectWanted(tree.getItem(position), position, wanted, positions);\n }\n\n return positions;\n}\n\nfunction indexPositions(index: CollectionIndex, ids: string[]): number[] {\n const positions: number[] = [];\n\n for (const id of ids) {\n const position = index.positionFor(id);\n\n if (position >= 0) {\n positions.push(position);\n }\n }\n\n return positions.toSorted(ascending);\n}\n\nfunction positionsFor(gtk: CollectionModel, index: CollectionIndex, ids: string[]): number[] {\n if (!index.isTree) {\n return indexPositions(index, ids);\n }\n\n const tree = gtk.treeModel();\n\n return tree === null ? [] : scanPositions(tree, ids);\n}\n\nfunction positionFor(gtk: CollectionModel, index: CollectionIndex, id: string): number {\n const [first = -1] = positionsFor(gtk, index, [id]);\n\n return first;\n}\n\nfunction createCollection(gtk: CollectionModel, index: CollectionIndex): Collection {\n return {\n isTree: index.isTree,\n model: gtk.model,\n treeModel: () => (index.isTree ? gtk.treeModel() : null),\n itemFor: index.itemFor,\n sectionFor: index.sectionFor,\n idAt: (position) => getId(gtk.model.getItem(position)),\n positionFor: (id) => positionFor(gtk, index, id),\n positionsFor: (ids) => positionsFor(gtk, index, ids),\n };\n}\n\nexport { createCollection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AE,CAAC,GAAG,EAAE,EAAE,CAAC,YAAY,CAAC,eAAe,EAAE,GAAG,CAAC;KAC5D,CAAC;AACN,CAAC;AAED,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAmB,CAAC","sourcesContent":["import type { ListItem } from \"../types.js\";\nimport type { CollectionIndex } from \"./collection-index.js\";\nimport type { CollectionModel, SlotRef } from \"./collection-model.js\";\nimport { slotPathAt, slotRefFor } from \"./collection-model.js\";\nimport { findPositions } from \"./tree-order.js\";\n\ntype Collection = Pick<CollectionModel, \"model\" | \"expansion\" | \"treeModel\"> & {\n itemAt: (ref: SlotRef) => ListItem | undefined;\n sectionFor: (levelPath: string) => unknown;\n idAt: (position: number) => string | null;\n pathAt: (position: number) => string | null;\n positionFor: (id: string) => number;\n positionsFor: (ids: string[]) => number[];\n};\n\nconst NO_POSITIONS: number[] = [];\n\nfunction positionsFor(collectionModel: CollectionModel, ids: string[]): number[] {\n if (ids.length === 0) {\n return NO_POSITIONS;\n }\n\n const { expansion } = collectionModel;\n\n return findPositions(expansion.index, expansion.slots, new Set(ids));\n}\n\nfunction positionFor(collectionModel: CollectionModel, id: string): number {\n const [first = -1] = positionsFor(collectionModel, [id]);\n\n return first;\n}\n\nfunction refAt(collectionModel: CollectionModel, position: number): SlotRef | null {\n if (position < 0) {\n return null;\n }\n\n return slotRefFor(collectionModel.model.getItem(position));\n}\n\nfunction idAt(collectionModel: CollectionModel, index: CollectionIndex, position: number): string | null {\n const ref = refAt(collectionModel, position);\n\n if (ref === null) {\n return null;\n }\n\n return index.itemAt(ref.store.path, ref.slot)?.id ?? null;\n}\n\nfunction pathAt(collectionModel: CollectionModel, position: number): string | null {\n const ref = refAt(collectionModel, position);\n\n if (ref === null) {\n return null;\n }\n\n return slotPathAt(ref.store, ref.slot);\n}\n\nfunction isCollectionIdle(collection: Collection): boolean {\n const { expansion } = collection;\n\n return !expansion.isApplying && !expansion.isSyncing;\n}\n\nfunction createCollection(collectionModel: CollectionModel, index: CollectionIndex): Collection {\n return {\n model: collectionModel.model,\n expansion: collectionModel.expansion,\n treeModel: () => (index.isTree ? collectionModel.treeModel() : null),\n itemAt: (ref) => index.itemAt(ref.store.path, ref.slot),\n sectionFor: index.sectionFor,\n idAt: (position) => idAt(collectionModel, index, position),\n pathAt: (position) => pathAt(collectionModel, position),\n positionFor: (id) => positionFor(collectionModel, id),\n positionsFor: (ids) => positionsFor(collectionModel, ids),\n };\n}\n\nexport { createCollection, isCollectionIdle, type Collection };\n"]}
@@ -0,0 +1,11 @@
1
+ import type { Collection } from "./collection.js";
2
+ type ControlledIds = string[] | null | undefined;
3
+ type ControlledSyncOptions = {
4
+ ids: ControlledIds;
5
+ collection: Collection;
6
+ widget?: object | null | undefined;
7
+ apply: (ids: ControlledIds) => void;
8
+ };
9
+ declare function useControlledSync(options: ControlledSyncOptions): () => void;
10
+ export { useControlledSync };
11
+ //# sourceMappingURL=controlled-sync.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"controlled-sync.d.ts","sourceRoot":"","sources":["../../src/internal/controlled-sync.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAElD,KAAK,aAAa,GAAG,MAAM,EAAE,GAAG,IAAI,GAAG,SAAS,CAAC;AAEjD,KAAK,qBAAqB,GAAG;IACzB,GAAG,EAAE,aAAa,CAAC;IACnB,UAAU,EAAE,UAAU,CAAC;IACvB,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;IACnC,KAAK,EAAE,CAAC,GAAG,EAAE,aAAa,KAAK,IAAI,CAAC;CACvC,CAAC;AAEF,iBAAS,iBAAiB,CAAC,OAAO,EAAE,qBAAqB,GAAG,MAAM,IAAI,CAmBrE;AAED,OAAO,EAAE,iBAAiB,EAAE,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { startTransition, useEffectEvent, useLayoutEffect, useState } from "react";
2
+ function useControlledSync(options) {
3
+ const { ids, collection, widget } = options;
4
+ const [drift, setDrift] = useState(0);
5
+ const markDrift = () => {
6
+ startTransition(() => {
7
+ setDrift((count) => count + 1);
8
+ });
9
+ };
10
+ const apply = useEffectEvent((next) => {
11
+ options.apply(next);
12
+ });
13
+ useLayoutEffect(() => {
14
+ apply(ids);
15
+ }, [widget, collection, ids, drift]);
16
+ return markDrift;
17
+ }
18
+ export { useControlledSync };
19
+ //# sourceMappingURL=controlled-sync.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"controlled-sync.js","sourceRoot":"","sources":["../../src/internal/controlled-sync.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAYnF,SAAS,iBAAiB,CAAC,OAA8B;IACrD,MAAM,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAC5C,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;IAEtC,MAAM,SAAS,GAAG,GAAS,EAAE;QACzB,eAAe,CAAC,GAAG,EAAE;YACjB,QAAQ,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;QACnC,CAAC,CAAC,CAAC;IACP,CAAC,CAAC;IAEF,MAAM,KAAK,GAAG,cAAc,CAAC,CAAC,IAAmB,EAAQ,EAAE;QACvD,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC,CAAC,CAAC;IAEH,eAAe,CAAC,GAAG,EAAE;QACjB,KAAK,CAAC,GAAG,CAAC,CAAC;IACf,CAAC,EAAE,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC;IAErC,OAAO,SAAS,CAAC;AACrB,CAAC;AAED,OAAO,EAAE,iBAAiB,EAAE,CAAC","sourcesContent":["import { startTransition, useEffectEvent, useLayoutEffect, useState } from \"react\";\nimport type { Collection } from \"./collection.js\";\n\ntype ControlledIds = string[] | null | undefined;\n\ntype ControlledSyncOptions = {\n ids: ControlledIds;\n collection: Collection;\n widget?: object | null | undefined;\n apply: (ids: ControlledIds) => void;\n};\n\nfunction useControlledSync(options: ControlledSyncOptions): () => void {\n const { ids, collection, widget } = options;\n const [drift, setDrift] = useState(0);\n\n const markDrift = (): void => {\n startTransition(() => {\n setDrift((count) => count + 1);\n });\n };\n\n const apply = useEffectEvent((next: ControlledIds): void => {\n options.apply(next);\n });\n\n useLayoutEffect(() => {\n apply(ids);\n }, [widget, collection, ids, drift]);\n\n return markDrift;\n}\n\nexport { useControlledSync };\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"drop-down.d.ts","sourceRoot":"","sources":["../../src/internal/drop-down.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,OAAO,MAAM,kBAAkB,CAAC;AACjD,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AAKzD,OAAO,KAAK,EAAE,gBAAgB,EAAgC,MAAM,aAAa,CAAC;AAMlF,KAAK,gBAAgB,GAAG,OAAO,CAAC,MAAM,GAAG;IAAE,WAAW,EAAE,MAAM,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;AAEzF,KAAK,iBAAiB,GAAG,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG;IAC1D,gBAAgB,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,EAAE,IAAI,EAAE,gBAAgB,KAAK,IAAI,CAAC,GAAG,IAAI,GAAG,SAAS,CAAC;IAC/F,GAAG,CAAC,EAAE,GAAG,CAAC,gBAAgB,GAAG,IAAI,CAAC,GAAG,SAAS,CAAC;CAClD,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAuI5B,iBAAS,YAAY,CAAC,KAAK,EAAE,iBAAiB,GAAG;IAAE,SAAS,EAAE,WAAW,CAAA;CAAE,GAAG,SAAS,CA+BtF;AAED,OAAO,EAAE,YAAY,EAAE,KAAK,iBAAiB,EAAE,CAAC"}
1
+ {"version":3,"file":"drop-down.d.ts","sourceRoot":"","sources":["../../src/internal/drop-down.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,OAAO,MAAM,kBAAkB,CAAC;AACjD,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AAIzD,OAAO,KAAK,EAAE,gBAAgB,EAAwC,MAAM,aAAa,CAAC;AAM1F,KAAK,gBAAgB,GAAG,OAAO,CAAC,MAAM,GAAG;IAAE,WAAW,EAAE,MAAM,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;AAEzF,KAAK,iBAAiB,GAAG,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG;IAC1D,gBAAgB,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,EAAE,IAAI,EAAE,gBAAgB,KAAK,IAAI,CAAC,GAAG,IAAI,GAAG,SAAS,CAAC;IAC/F,GAAG,CAAC,EAAE,GAAG,CAAC,gBAAgB,GAAG,IAAI,CAAC,GAAG,SAAS,CAAC;CAClD,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AA8J5B,iBAAS,YAAY,CAAC,KAAK,EAAE,iBAAiB,GAAG;IAAE,SAAS,EAAE,WAAW,CAAA;CAAE,GAAG,SAAS,CA8BtF;AAED,OAAO,EAAE,YAAY,EAAE,KAAK,iBAAiB,EAAE,CAAC"}
@@ -1,9 +1,8 @@
1
1
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { GtkLabel, GtkSignalListItemFactory } from "@gtkx/jsx/gtk";
3
- import { applyWrite } from "@gtkx/react/internal";
4
3
  import { omit } from "@gtkx/utils";
5
- import { useEffectEvent, useLayoutEffect, useRef } from "react";
6
- import { HeaderPortals, ItemPortals, useHeaderCells, useItemCells } from "./cells.js";
4
+ import { useEffectEvent, useLayoutEffect, useRef, useState } from "react";
5
+ import { ItemPortals, useItemCells, useSectionHeader } from "./cells.js";
7
6
  import { useCollectionData } from "./use-collection.js";
8
7
  import { useWidgetRef } from "./use-widget-ref.js";
9
8
  const DROP_DOWN_PROPS = [
@@ -18,6 +17,9 @@ const DROP_DOWN_PROPS = [
18
17
  "renderHeader",
19
18
  "ref",
20
19
  ];
20
+ function newApplyState() {
21
+ return { isApplying: false };
22
+ }
21
23
  const describeValue = (value) => {
22
24
  if (typeof value === "string") {
23
25
  return value;
@@ -32,7 +34,6 @@ const describeValue = (value) => {
32
34
  const defaultItemContent = (value) => value == null ? null : _jsx(GtkLabel, { children: describeValue(value) });
33
35
  const defaultRenderItem = ({ item }) => defaultItemContent(item);
34
36
  const faceRenderer = (props) => props.renderItem ?? defaultRenderItem;
35
- const listRenderer = (props) => props.renderListItem ?? props.renderItem ?? defaultRenderItem;
36
37
  const resolvePosition = (widget, collection, selectedId) => {
37
38
  const requested = selectedId == null ? -1 : collection.positionFor(selectedId);
38
39
  return requested >= 0 ? requested : widget.getSelected();
@@ -61,15 +62,28 @@ const reportSelectedNotify = (options, tracker) => {
61
62
  }
62
63
  reportKnownSelection(tracker, collection.idAt(widget.getSelected()));
63
64
  };
64
- const applySelectedPosition = (widget, position) => {
65
- applyWrite(() => {
65
+ const applySelectedPosition = (widget, position, state) => {
66
+ state.isApplying = true;
67
+ try {
66
68
  widget.selected = position;
67
- });
69
+ }
70
+ finally {
71
+ state.isApplying = false;
72
+ }
73
+ };
74
+ const dispatchSelectedNotify = (context, value, self) => {
75
+ const { options, known, state } = context;
76
+ if (state.isApplying) {
77
+ return;
78
+ }
79
+ reportSelectedNotify(options, { known, onSelectionChanged: options.props.onSelectionChanged });
80
+ options.props.onNotifySelected?.(value, self);
68
81
  };
69
82
  const useDropDownSelection = (options) => {
70
83
  const { widget, collection } = options;
71
84
  const { selectedId } = options.props;
72
85
  const known = useRef(null);
86
+ const [applyState] = useState(newApplyState);
73
87
  const syncKnownSelection = useEffectEvent((position) => {
74
88
  const effectiveId = collection.idAt(position);
75
89
  if (effectiveId === null) {
@@ -84,27 +98,25 @@ const useDropDownSelection = (options) => {
84
98
  return;
85
99
  }
86
100
  const position = resolvePosition(widget, collection, selectedId);
87
- applySelectedPosition(widget, position);
101
+ applySelectedPosition(widget, position, applyState);
88
102
  syncKnownSelection(position);
89
- }, [widget, collection, selectedId]);
103
+ }, [widget, collection, selectedId, applyState]);
90
104
  return (value, self) => {
91
- reportSelectedNotify(options, { known, onSelectionChanged: options.props.onSelectionChanged });
92
- options.props.onNotifySelected?.(value, self);
105
+ dispatchSelectedNotify({ options, known, state: applyState }, value, self);
93
106
  };
94
107
  };
95
108
  function DropDownBase(props) {
96
109
  const { component: Component, items, sections, renderListItem, renderHeader, ref } = props;
97
110
  const rest = omit(props, DROP_DOWN_PROPS);
98
111
  const [widget, refCallback] = useWidgetRef(ref);
99
- const collection = useCollectionData({ items, sections });
112
+ const collection = useCollectionData({ items, sections, isFlat: true });
100
113
  const faceCells = useItemCells({ width: -1, height: -1 });
101
114
  const listCells = useItemCells({ width: -1, height: -1 });
102
- const headerCells = useHeaderCells({ width: -1, height: -1 });
115
+ const header = useSectionHeader(renderHeader, collection, { width: -1, height: -1 });
103
116
  const handleNotifySelected = useDropDownSelection({ widget, collection, props });
104
- const hasHeader = typeof renderHeader === "function";
105
117
  return (_jsxs(_Fragment, { children: [_jsx(Component, { ref: refCallback, model: collection.model, factory: _jsx(GtkSignalListItemFactory, { ...faceCells.handlers }), ...(renderListItem != null && {
106
118
  listFactory: _jsx(GtkSignalListItemFactory, { ...listCells.handlers }),
107
- }), ...(hasHeader && { headerFactory: _jsx(GtkSignalListItemFactory, { ...headerCells.handlers }) }), ...rest, onNotifySelected: handleNotifySelected }), _jsx(ItemPortals, { store: faceCells, render: faceRenderer(props), collection: collection }), renderListItem != null && (_jsx(ItemPortals, { store: listCells, render: listRenderer(props), collection: collection })), hasHeader && _jsx(HeaderPortals, { store: headerCells, render: renderHeader, collection: collection })] }));
119
+ }), ...header.factoryProps, ...rest, onNotifySelected: handleNotifySelected }), _jsx(ItemPortals, { registry: faceCells, render: faceRenderer(props), collection: collection }), renderListItem != null && (_jsx(ItemPortals, { registry: listCells, render: renderListItem, collection: collection })), header.portals] }));
108
120
  }
109
121
  export { DropDownBase };
110
122
  //# sourceMappingURL=drop-down.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"drop-down.js","sourceRoot":"","sources":["../../src/internal/drop-down.tsx"],"names":[],"mappings":";AAEA,OAAO,EAAE,QAAQ,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC;AACnE,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAGhE,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AACtF,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAsBnD,MAAM,eAAe,GAAa;IAC9B,WAAW;IACX,OAAO;IACP,UAAU;IACV,YAAY;IACZ,oBAAoB;IACpB,kBAAkB;IAClB,YAAY;IACZ,gBAAgB;IAChB,cAAc;IACd,KAAK;CACR,CAAC;AAEF,MAAM,aAAa,GAAG,CAAC,KAAc,EAAU,EAAE;IAC7C,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC5B,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,IAAI,CAAC;QACD,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IACjC,CAAC;IAAC,MAAM,C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AS,EAAE,MAAM,EAAE,YAAY,CAAC,KAAK,CAAC,EAAE,UAAU,EAAE,UAAU,GAAI,CACzF,EACA,SAAS,IAAI,KAAC,aAAa,IAAC,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,YAAY,EAAE,UAAU,EAAE,UAAU,GAAI,IAClG,CACN,CAAC;AACN,CAAC;AAED,OAAO,EAAE,YAAY,EAA0B,CAAC","sourcesContent":["import type * as GObject from \"@gtkx/gi/gobject\";\nimport type { ElementType, ReactNode, Ref } from \"react\";\nimport { GtkLabel, GtkSignalListItemFactory } from \"@gtkx/jsx/gtk\";\nimport { applyWrite } from \"@gtkx/react/internal\";\nimport { omit } from \"@gtkx/utils\";\nimport { useEffectEvent, useLayoutEffect, useRef } from \"react\";\nimport type { DropDownOwnProps, ItemRenderer, RenderItemArgs } from \"../types.js\";\nimport type { Collection } from \"./collection.js\";\nimport { HeaderPortals, ItemPortals, useHeaderCells, useItemCells } from \"./cells.js\";\nimport { useCollectionData } from \"./use-collection.js\";\nimport { useWidgetRef } from \"./use-widget-ref.js\";\n\ntype SelectableWidget = GObject.Object & { getSelected: () => number; selected: number };\n\ntype DropDownBaseProps = DropDownOwnProps<unknown, unknown> & {\n onNotifySelected?: ((value: number | null, self: SelectableWidget) => void) | null | undefined;\n ref?: Ref<SelectableWidget | null> | undefined;\n} & Record<string, unknown>;\n\ntype SelectionOptions = {\n widget: SelectableWidget | null;\n collection: Collection;\n props: DropDownBaseProps;\n};\n\ntype NotifySelectedHandler = NonNullable<DropDownBaseProps[\"onNotifySelected\"]>;\n\ntype KnownSelection = {\n known: { current: string | null };\n onSelectionChanged: ((id: string) => void) | null | undefined;\n};\n\nconst DROP_DOWN_PROPS: string[] = [\n \"component\",\n \"items\",\n \"sections\",\n \"selectedId\",\n \"onSelectionChanged\",\n \"onNotifySelected\",\n \"renderItem\",\n \"renderListItem\",\n \"renderHeader\",\n \"ref\",\n];\n\nconst describeValue = (value: unknown): string => {\n if (typeof value === \"string\") {\n return value;\n }\n\n try {\n return JSON.stringify(value);\n } catch {\n return String(value);\n }\n};\n\nconst defaultItemContent = (value: unknown): ReactNode =>\n value == null ? null : <GtkLabel>{describeValue(value)}</GtkLabel>;\n\nconst defaultRenderItem = ({ item }: RenderItemArgs<unknown>): ReactNode => defaultItemContent(item);\nconst faceRenderer = (props: DropDownBaseProps): ItemRenderer<never> => props.renderItem ?? defaultRenderItem;\n\nconst listRenderer = (props: DropDownBaseProps): ItemRenderer<never> =>\n props.renderListItem ?? props.renderItem ?? defaultRenderItem;\n\nconst resolvePosition = (\n widget: SelectableWidget,\n collection: Collection,\n selectedId: string | null | undefined,\n): number => {\n const requested = selectedId == null ? -1 : collection.positionFor(selectedId);\n\n return requested >= 0 ? requested : widget.getSelected();\n};\n\nconst updateKnownSelection = (\n tracker: KnownSelection,\n effectiveId: string,\n selectedId: string | null | undefined,\n): void => {\n const { known, onSelectionChanged } = tracker;\n const expectedId = selectedId ?? known.current;\n const isNew = effectiveId !== known.current;\n known.current = effectiveId;\n\n if (expectedId !== null && effectiveId !== expectedId && isNew) {\n onSelectionChanged?.(effectiveId);\n }\n};\n\nconst reportKnownSelection = (tracker: KnownSelection, id: string | null): void => {\n const { known, onSelectionChanged } = tracker;\n\n if (id === null || id === known.current) {\n return;\n }\n\n known.current = id;\n onSelectionChanged?.(id);\n};\n\nconst reportSelectedNotify = (options: SelectionOptions, tracker: KnownSelection): void => {\n const { widget, collection } = options;\n\n if (widget === null) {\n return;\n }\n\n reportKnownSelection(tracker, collection.idAt(widget.getSelected()));\n};\n\nconst applySelectedPosition = (widget: SelectableWidget, position: number): void => {\n applyWrite(() => {\n widget.selected = position;\n });\n};\n\nconst useDropDownSelection = (options: SelectionOptions): NotifySelectedHandler => {\n const { widget, collection } = options;\n const { selectedId } = options.props;\n const known = useRef<string | null>(null);\n\n const syncKnownSelection = useEffectEvent((position: number): void => {\n const effectiveId = collection.idAt(position);\n\n if (effectiveId === null) {\n known.current = null;\n\n return;\n }\n\n const { onSelectionChanged } = options.props;\n updateKnownSelection({ known, onSelectionChanged }, effectiveId, selectedId);\n });\n\n useLayoutEffect(() => {\n if (widget === null) {\n return;\n }\n\n const position = resolvePosition(widget, collection, selectedId);\n applySelectedPosition(widget, position);\n syncKnownSelection(position);\n }, [widget, collection, selectedId]);\n\n return (value, self) => {\n reportSelectedNotify(options, { known, onSelectionChanged: options.props.onSelectionChanged });\n options.props.onNotifySelected?.(value, self);\n };\n};\n\nfunction DropDownBase(props: DropDownBaseProps & { component: ElementType }): ReactNode {\n const { component: Component, items, sections, renderListItem, renderHeader, ref } = props;\n const rest = omit(props, DROP_DOWN_PROPS);\n const [widget, refCallback] = useWidgetRef<SelectableWidget>(ref);\n const collection = useCollectionData({ items, sections });\n const faceCells = useItemCells({ width: -1, height: -1 });\n const listCells = useItemCells({ width: -1, height: -1 });\n const headerCells = useHeaderCells({ width: -1, height: -1 });\n const handleNotifySelected = useDropDownSelection({ widget, collection, props });\n const hasHeader = typeof renderHeader === \"function\";\n\n return (\n <>\n <Component\n ref={refCallback}\n model={collection.model}\n factory={<GtkSignalListItemFactory {...faceCells.handlers} />}\n {...(renderListItem != null && {\n listFactory: <GtkSignalListItemFactory {...listCells.handlers} />,\n })}\n {...(hasHeader && { headerFactory: <GtkSignalListItemFactory {...headerCells.handlers} /> })}\n {...rest}\n onNotifySelected={handleNotifySelected}\n />\n <ItemPortals store={faceCells} render={faceRenderer(props)} collection={collection} />\n {renderListItem != null && (\n <ItemPortals store={listCells} render={listRenderer(props)} collection={collection} />\n )}\n {hasHeader && <HeaderPortals store={headerCells} render={renderHeader} collection={collection} />}\n </>\n );\n}\n\nexport { DropDownBase, type DropDownBaseProps };\n"]}
1
+ {"version":3,"file":"drop-down.js","sourceRoot":"","sources":["../../src/internal/drop-down.tsx"],"names":[],"mappings":";AAEA,OAAO,EAAE,QAAQ,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC;AACnE,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAC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* as GObject from \"@gtkx/gi/gobject\";\nimport type { ElementType, ReactNode, Ref } from \"react\";\nimport { GtkLabel, GtkSignalListItemFactory } from \"@gtkx/jsx/gtk\";\nimport { omit } from \"@gtkx/utils\";\nimport { useEffectEvent, useLayoutEffect, useRef, useState } from \"react\";\nimport type { DropDownOwnProps, ListItemRenderArgs, ListItemRenderer } from \"../types.js\";\nimport type { Collection } from \"./collection.js\";\nimport { ItemPortals, useItemCells, useSectionHeader } from \"./cells.js\";\nimport { useCollectionData } from \"./use-collection.js\";\nimport { useWidgetRef } from \"./use-widget-ref.js\";\n\ntype SelectableWidget = GObject.Object & { getSelected: () => number; selected: number };\n\ntype DropDownBaseProps = DropDownOwnProps<unknown, unknown> & {\n onNotifySelected?: ((value: number | null, self: SelectableWidget) => void) | null | undefined;\n ref?: Ref<SelectableWidget | null> | undefined;\n} & Record<string, unknown>;\n\ntype SelectionOptions = {\n widget: SelectableWidget | null;\n collection: Collection;\n props: DropDownBaseProps;\n};\n\ntype NotifySelectedHandler = NonNullable<DropDownBaseProps[\"onNotifySelected\"]>;\ntype ApplyState = { isApplying: boolean };\n\ntype NotifyContext = {\n options: SelectionOptions;\n known: { current: string | null };\n state: ApplyState;\n};\n\ntype KnownSelection = {\n known: { current: string | null };\n onSelectionChanged: ((id: string) => void) | null | undefined;\n};\n\nconst DROP_DOWN_PROPS: string[] = [\n \"component\",\n \"items\",\n \"sections\",\n \"selectedId\",\n \"onSelectionChanged\",\n \"onNotifySelected\",\n \"renderItem\",\n \"renderListItem\",\n \"renderHeader\",\n \"ref\",\n];\n\nfunction newApplyState(): ApplyState {\n return { isApplying: false };\n}\n\nconst describeValue = (value: unknown): string => {\n if (typeof value === \"string\") {\n return value;\n }\n\n try {\n return JSON.stringify(value);\n } catch {\n return String(value);\n }\n};\n\nconst defaultItemContent = (value: unknown): ReactNode =>\n value == null ? null : <GtkLabel>{describeValue(value)}</GtkLabel>;\n\nconst defaultRenderItem = ({ item }: ListItemRenderArgs<unknown>): ReactNode => defaultItemContent(item);\nconst faceRenderer = (props: DropDownBaseProps): ListItemRenderer<never> => props.renderItem ?? defaultRenderItem;\n\nconst resolvePosition = (\n widget: SelectableWidget,\n collection: Collection,\n selectedId: string | null | undefined,\n): number => {\n const requested = selectedId == null ? -1 : collection.positionFor(selectedId);\n\n return requested >= 0 ? requested : widget.getSelected();\n};\n\nconst updateKnownSelection = (\n tracker: KnownSelection,\n effectiveId: string,\n selectedId: string | null | undefined,\n): void => {\n const { known, onSelectionChanged } = tracker;\n const expectedId = selectedId ?? known.current;\n const isNew = effectiveId !== known.current;\n known.current = effectiveId;\n\n if (expectedId !== null && effectiveId !== expectedId && isNew) {\n onSelectionChanged?.(effectiveId);\n }\n};\n\nconst reportKnownSelection = (tracker: KnownSelection, id: string | null): void => {\n const { known, onSelectionChanged } = tracker;\n\n if (id === null || id === known.current) {\n return;\n }\n\n known.current = id;\n onSelectionChanged?.(id);\n};\n\nconst reportSelectedNotify = (options: SelectionOptions, tracker: KnownSelection): void => {\n const { widget, collection } = options;\n\n if (widget === null) {\n return;\n }\n\n reportKnownSelection(tracker, collection.idAt(widget.getSelected()));\n};\n\nconst applySelectedPosition = (widget: SelectableWidget, position: number, state: ApplyState): void => {\n state.isApplying = true;\n\n try {\n widget.selected = position;\n } finally {\n state.isApplying = false;\n }\n};\n\nconst dispatchSelectedNotify = (context: NotifyContext, value: number | null, self: SelectableWidget): void => {\n const { options, known, state } = context;\n\n if (state.isApplying) {\n return;\n }\n\n reportSelectedNotify(options, { known, onSelectionChanged: options.props.onSelectionChanged });\n options.props.onNotifySelected?.(value, self);\n};\n\nconst useDropDownSelection = (options: SelectionOptions): NotifySelectedHandler => {\n const { widget, collection } = options;\n const { selectedId } = options.props;\n const known = useRef<string | null>(null);\n const [applyState] = useState<ApplyState>(newApplyState);\n\n const syncKnownSelection = useEffectEvent((position: number): void => {\n const effectiveId = collection.idAt(position);\n\n if (effectiveId === null) {\n known.current = null;\n\n return;\n }\n\n const { onSelectionChanged } = options.props;\n updateKnownSelection({ known, onSelectionChanged }, effectiveId, selectedId);\n });\n\n useLayoutEffect(() => {\n if (widget === null) {\n return;\n }\n\n const position = resolvePosition(widget, collection, selectedId);\n applySelectedPosition(widget, position, applyState);\n syncKnownSelection(position);\n }, [widget, collection, selectedId, applyState]);\n\n return (value, self) => {\n dispatchSelectedNotify({ options, known, state: applyState }, value, self);\n };\n};\n\nfunction DropDownBase(props: DropDownBaseProps & { component: ElementType }): ReactNode {\n const { component: Component, items, sections, renderListItem, renderHeader, ref } = props;\n const rest = omit(props, DROP_DOWN_PROPS);\n const [widget, refCallback] = useWidgetRef<SelectableWidget>(ref);\n const collection = useCollectionData({ items, sections, isFlat: true });\n const faceCells = useItemCells({ width: -1, height: -1 });\n const listCells = useItemCells({ width: -1, height: -1 });\n const header = useSectionHeader(renderHeader, collection, { width: -1, height: -1 });\n const handleNotifySelected = useDropDownSelection({ widget, collection, props });\n\n return (\n <>\n <Component\n ref={refCallback}\n model={collection.model}\n factory={<GtkSignalListItemFactory {...faceCells.handlers} />}\n {...(renderListItem != null && {\n listFactory: <GtkSignalListItemFactory {...listCells.handlers} />,\n })}\n {...header.factoryProps}\n {...rest}\n onNotifySelected={handleNotifySelected}\n />\n <ItemPortals registry={faceCells} render={faceRenderer(props)} collection={collection} />\n {renderListItem != null && (\n <ItemPortals registry={listCells} render={renderListItem} collection={collection} />\n )}\n {header.portals}\n </>\n );\n}\n\nexport { DropDownBase, type DropDownBaseProps };\n"]}
@@ -4,6 +4,7 @@ type ExpansionOptions = {
4
4
  expandedIds?: string[] | null | undefined;
5
5
  onExpandedChange?: ((ids: string[]) => void) | null | undefined;
6
6
  };
7
- declare function useExpansion(options: ExpansionOptions): () => void;
8
- export { useExpansion, type ExpansionOptions };
7
+ type ItemsChangeHandler = (position: number, removed: number, added: number) => void;
8
+ declare function useExpansion(options: ExpansionOptions): ItemsChangeHandler;
9
+ export { useExpansion, type ItemsChangeHandler };
9
10
  //# sourceMappingURL=expansion.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"expansion.d.ts","sourceRoot":"","sources":["../../src/internal/expansion.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAGlD,KAAK,gBAAgB,GAAG;IACpB,UAAU,EAAE,UAAU,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,EAAE,GAAG,IAAI,GAAG,SAAS,CAAC;IAC1C,gBAAgB,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC,GAAG,IAAI,GAAG,SAAS,CAAC;CACnE,CAAC;AA6FF,iBAAS,YAAY,CAAC,OAAO,EAAE,gBAAgB,GAAG,MAAM,IAAI,CAgB3D;AAED,OAAO,EAAE,YAAY,EAAE,KAAK,gBAAgB,EAAE,CAAC"}
1
+ {"version":3,"file":"expansion.d.ts","sourceRoot":"","sources":["../../src/internal/expansion.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAUlD,KAAK,gBAAgB,GAAG;IACpB,UAAU,EAAE,UAAU,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,EAAE,GAAG,IAAI,GAAG,SAAS,CAAC;IAC1C,gBAAgB,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC,GAAG,IAAI,GAAG,SAAS,CAAC;CACnE,CAAC;AAQF,KAAK,kBAAkB,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;AAoHrF,iBAAS,YAAY,CAAC,OAAO,EAAE,gBAAgB,GAAG,kBAAkB,CAgBnE;AAED,OAAO,EAAE,YAAY,EAAE,KAAK,kBAAkB,EAAE,CAAC"}
@@ -1,73 +1,106 @@
1
- import { useEffectEvent, useLayoutEffect, useRef, useState } from "react";
2
- import { eachRow } from "./collection.js";
1
+ import { useState } from "react";
2
+ import { isCollectionIdle } from "./collection.js";
3
+ import { useControlledSync } from "./controlled-sync.js";
4
+ import { joinParts } from "./keys.js";
5
+ import { trackPaths } from "./slots.js";
6
+ import { adoptOrder, markExpanded, orderFor } from "./tree-expansion.js";
7
+ import { walkVisible } from "./tree-order.js";
3
8
  function newLastExpansion() {
4
9
  return { key: "" };
5
10
  }
6
- function collectExpanded(row, id, ids) {
7
- if (id !== null && row.getExpanded()) {
8
- ids.push(id);
11
+ function wantedSet(expansion, expandedIds) {
12
+ const wanted = new Set();
13
+ for (const id of expandedIds) {
14
+ for (const path of expansion.index.expandablePathsFor(id)) {
15
+ wanted.add(path);
16
+ }
9
17
  }
18
+ return wanted;
10
19
  }
11
- function expandRow(row, id, wanted) {
12
- const isDesired = id !== null && wanted.has(id);
13
- if (row.isExpandable() && row.getExpanded() !== isDesired) {
14
- row.setExpanded(isDesired);
15
- }
20
+ function hasSameExpansion(expanded, wanted) {
21
+ return expanded.size === wanted.size && wanted.isSubsetOf(expanded);
16
22
  }
17
- function applyExpansion(tree, expandedIds) {
18
- const wanted = new Set(expandedIds);
19
- eachRow(tree, (row, id) => {
20
- expandRow(row, id, wanted);
21
- });
23
+ function toggleOptions(expansion, tree, wanted) {
24
+ return {
25
+ index: expansion.index,
26
+ slots: trackPaths(wanted),
27
+ marks: trackPaths(wanted.symmetricDifference(expansion.expanded)),
28
+ visit: (row) => {
29
+ if (row.isMarked) {
30
+ tree.getRow(row.position)?.setExpanded(row.isOpen);
31
+ }
32
+ },
33
+ };
22
34
  }
23
- function reportExpansion(collection, last, onExpandedChange) {
24
- const tree = collection.treeModel();
25
- if (tree === null) {
26
- return;
35
+ function walkApplying(expansion, options) {
36
+ expansion.isApplying = true;
37
+ try {
38
+ return walkVisible(options);
27
39
  }
28
- const ids = [];
29
- eachRow(tree, (row, id) => {
30
- collectExpanded(row, id, ids);
31
- });
32
- const key = ids.join(" ");
33
- if (last.key === key) {
40
+ finally {
41
+ expansion.isApplying = false;
42
+ }
43
+ }
44
+ function applyWanted(expansion, tree, expandedIds) {
45
+ const wanted = wantedSet(expansion, expandedIds);
46
+ if (hasSameExpansion(expansion.expanded, wanted)) {
34
47
  return;
35
48
  }
36
- last.key = key;
37
- onExpandedChange?.(ids);
49
+ adoptOrder(expansion, walkApplying(expansion, toggleOptions(expansion, tree, wanted)));
38
50
  }
39
- function reportWhenIdle(collection, last, expanding, onExpandedChange) {
40
- if (expanding.current) {
51
+ function reportExpansion(context) {
52
+ const { expandedIds, expandedPaths } = orderFor(context.collection.expansion);
53
+ const key = joinParts(expandedPaths);
54
+ if (context.last.key === key) {
41
55
  return;
42
56
  }
43
- reportExpansion(collection, last, onExpandedChange);
57
+ context.last.key = key;
58
+ context.onExpandedChange?.([...expandedIds]);
44
59
  }
45
- function runControlledExpansion(collection, expandedIds, expanding, report) {
46
- const tree = collection.treeModel();
47
- if (tree === null || expandedIds == null) {
60
+ function applyControlledExpansion(context, expandedIds) {
61
+ const tree = context.collection.treeModel();
62
+ if (tree === null) {
48
63
  return;
49
64
  }
50
- expanding.current = true;
51
- try {
52
- applyExpansion(tree, expandedIds);
65
+ applyWanted(context.collection.expansion, tree, expandedIds ?? []);
66
+ reportExpansion(context);
67
+ }
68
+ function isExpansionIdle(collection) {
69
+ return collection.treeModel() !== null && isCollectionIdle(collection);
70
+ }
71
+ function didRowDrift(collection, change) {
72
+ const path = collection.pathAt(change.position - 1);
73
+ const isExpanded = change.removed === 0 && change.added > 0;
74
+ const isCollapsed = change.added === 0 && change.removed > 0;
75
+ if (path === null || isExpanded === isCollapsed) {
76
+ return false;
53
77
  }
54
- finally {
55
- expanding.current = false;
78
+ markExpanded(collection.expansion, path, isExpanded);
79
+ return true;
80
+ }
81
+ function observeExpansion(context, change, onDrift) {
82
+ if (!isExpansionIdle(context.collection)) {
83
+ return;
84
+ }
85
+ const didDrift = didRowDrift(context.collection, change);
86
+ reportExpansion(context);
87
+ if (didDrift) {
88
+ onDrift();
56
89
  }
57
- report();
58
90
  }
59
91
  function useExpansion(options) {
60
92
  const { collection, expandedIds, onExpandedChange } = options;
61
93
  const [last] = useState(newLastExpansion);
62
- const expanding = useRef(false);
63
- const report = useEffectEvent(() => {
64
- reportWhenIdle(collection, last, expanding, onExpandedChange);
94
+ const context = { collection, last, onExpandedChange };
95
+ const markDrift = useControlledSync({
96
+ ids: expandedIds,
97
+ collection,
98
+ apply: (ids) => {
99
+ applyControlledExpansion(context, ids);
100
+ },
65
101
  });
66
- useLayoutEffect(() => {
67
- runControlledExpansion(collection, expandedIds, expanding, report);
68
- }, [collection, expandedIds]);
69
- return () => {
70
- reportWhenIdle(collection, last, expanding, onExpandedChange);
102
+ return (position, removed, added) => {
103
+ observeExpansion(context, { position, removed, added }, markDrift);
71
104
  };
72
105
  }
73
106
  export { useExpansion };
@@ -1 +1 @@
1
- {"version":3,"file":"expansion.js","sourceRoot":"","sources":["../../src/internal/expansion.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAE1E,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAU1C,SAAS,gBAAgB;IACrB,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC;AACvB,CAAC;AAED,SAAS,eAAe,CAAC,GAAoB,EAAE,EAAiB,EAAE,GAAa;IAC3E,IAAI,EAAE,KAAK,IAAI,IAAI,GAAG,CAAC,WAAW,EAAE,EAAE,CAAC;QACnC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACjB,CAAC;AACL,CAAC;AAED,SAAS,SAAS,CAAC,GAAoB,EAAE,EAAiB,EAAE,MAAmB;IAC3E,MAAM,SAAS,GAAG,EAAE,KAAK,IAAI,IAAI,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAEhD,IAAI,GAAG,CAAC,YAAY,EAAE,IAAI,GAAG,CAAC,WAAW,EAAE,KAAK,S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6B,EAC7B,MAAkB;IAElB,MAAM,IAAI,GAAG,UAAU,CAAC,SAAS,EAAE,CAAC;IAEpC,IAAI,IAAI,KAAK,IAAI,IAAI,WAAW,IAAI,IAAI,EAAE,CAAC;QACvC,OAAO;IACX,CAAC;IAED,SAAS,CAAC,OAAO,GAAG,IAAI,CAAC;IAEzB,IAAI,CAAC;QACD,cAAc,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;IACtC,CAAC;YAAS,CAAC;QACP,SAAS,CAAC,OAAO,GAAG,KAAK,CAAC;IAC9B,CAAC;IAED,MAAM,EAAE,CAAC;AACb,CAAC;AAED,SAAS,YAAY,CAAC,OAAyB;IAC3C,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,gBAAgB,EAAE,GAAG,OAAO,CAAC;IAC9D,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAgB,gBAAgB,CAAC,CAAC;IACzD,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAEhC,MAAM,MAAM,GAAG,cAAc,CAAC,GAAS,EAAE;QACrC,cAAc,CAAC,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE,gBAAgB,CAAC,CAAC;IAClE,CAAC,CAAC,CAAC;IAEH,eAAe,CAAC,GAAG,EAAE;QACjB,sBAAsB,CAAC,UAAU,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;IACvE,CAAC,EAAE,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC,CAAC;IAE9B,OAAO,GAAG,EAAE;QACR,cAAc,CAAC,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE,gBAAgB,CAAC,CAAC;IAClE,CAAC,CAAC;AACN,CAAC;AAED,OAAO,EAAE,YAAY,EAAyB,CAAC","sourcesContent":["import type * as Gtk from \"@gtkx/gi/gtk\";\nimport type { RefObject } from \"react\";\nimport { useEffectEvent, useLayoutEffect, useRef, useState } from \"react\";\nimport type { Collection } from \"./collection.js\";\nimport { eachRow } from \"./collection.js\";\n\ntype ExpansionOptions = {\n collection: Collection;\n expandedIds?: string[] | null | undefined;\n onExpandedChange?: ((ids: string[]) => void) | null | undefined;\n};\n\ntype LastExpansion = { key: string };\n\nfunction newLastExpansion(): LastExpansion {\n return { key: \"\" };\n}\n\nfunction collectExpanded(row: Gtk.TreeListRow, id: string | null, ids: string[]): void {\n if (id !== null && row.getExpanded()) {\n ids.push(id);\n }\n}\n\nfunction expandRow(row: Gtk.TreeListRow, id: string | null, wanted: Set<string>): void {\n const isDesired = id !== null && wanted.has(id);\n\n if (row.isExpandable() && row.getExpanded() !== isDesired) {\n row.setExpanded(isDesired);\n }\n}\n\nfunction applyExpansion(tree: Gtk.TreeListModel, expandedIds: string[]): void {\n const wanted = new Set(expandedIds);\n\n eachRow(tree, (row, id) => {\n expandRow(row, id, wanted);\n });\n}\n\nfunction reportExpansion(\n collection: Collection,\n last: LastExpansion,\n onExpandedChange: ((ids: string[]) => void) | null | undefined,\n): void {\n const tree = collection.treeModel();\n\n if (tree === null) {\n return;\n }\n\n const ids: string[] = [];\n\n eachRow(tree, (row, id) => {\n collectExpanded(row, id, ids);\n });\n\n const key = ids.join(\" \");\n\n if (last.key === key) {\n return;\n }\n\n last.key = key;\n onExpandedChange?.(ids);\n}\n\nfunction reportWhenIdle(\n collection: Collection,\n last: LastExpansion,\n expanding: RefObject<boolean>,\n onExpandedChange: ((ids: string[]) => void) | null | undefined,\n): void {\n if (expanding.current) {\n return;\n }\n\n reportExpansion(collection, last, onExpandedChange);\n}\n\nfunction runControlledExpansion(\n collection: Collection,\n expandedIds: string[] | null | undefined,\n expanding: RefObject<boolean>,\n report: () => void,\n): void {\n const tree = collection.treeModel();\n\n if (tree === null || expandedIds == null) {\n return;\n }\n\n expanding.current = true;\n\n try {\n applyExpansion(tree, expandedIds);\n } finally {\n expanding.current = false;\n }\n\n report();\n}\n\nfunction useExpansion(options: ExpansionOptions): () => void {\n const { collection, expandedIds, onExpandedChange } = options;\n const [last] = useState<LastExpansion>(newLastExpansion);\n const expanding = useRef(false);\n\n const report = useEffectEvent((): void => {\n reportWhenIdle(collection, last, expanding, onExpandedChange);\n });\n\n useLayoutEffect(() => {\n runControlledExpansion(collection, expandedIds, expanding, report);\n }, [collection, expandedIds]);\n\n return () => {\n reportWhenIdle(collection, last, expanding, onExpandedChange);\n };\n}\n\nexport { useExpansion, type ExpansionOptions };\n"]}
1
+ {"version":3,"file":"expansion.js","sourceRoot":"","sources":["../../src/internal/expansion.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAIjC,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AACzE,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAuB9C,SAAS,gBAAgB;IACrB,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC;AACvB,CAAC;AAED,SAAS,SAAS,CAAC,SAAwB,EAAE,WAAqB;IAC9D,MAAM,MAAM,GAAgB,IAAI,GAAG,EAAE,CAAC;IAEtC,KAAK,MAAM,EAAE,IAAI,WAAW,EAAE,CAAC;QAC3B,KAAK,MAAM,IAAI,IAAI,SAAS,CAAC,KAAK,CAAC,kBAAkB,CAAC,EAAE,CAAC,EAAE,CAAC;YACxD,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACrB,CAAC;IACL,CAAC;IAED,OAAO,MAAM,CAAC;AAClB,CAAC;AAED,SAAS,gBAAgB,CAAC,QAAqB,EAAE,MAAmB;IAChE,OAAO,QAAQ,CAAC,IAAI,KAAK,MAAM,CAAC,IAAI,IAAI,MAAM,CA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UAAU,CAAC,CAAC;IAErD,OAAO,IAAI,CAAC;AAChB,CAAC;AAED,SAAS,gBAAgB,CAAC,OAAyB,EAAE,MAAmB,EAAE,OAAmB;IACzF,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC;QACvC,OAAO;IACX,CAAC;IAED,MAAM,QAAQ,GAAG,WAAW,CAAC,OAAO,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IACzD,eAAe,CAAC,OAAO,CAAC,CAAC;IAEzB,IAAI,QAAQ,EAAE,CAAC;QACX,OAAO,EAAE,CAAC;IACd,CAAC;AACL,CAAC;AAED,SAAS,YAAY,CAAC,OAAyB;IAC3C,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,gBAAgB,EAAE,GAAG,OAAO,CAAC;IAC9D,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAgB,gBAAgB,CAAC,CAAC;IACzD,MAAM,OAAO,GAAqB,EAAE,UAAU,EAAE,IAAI,EAAE,gBAAgB,EAAE,CAAC;IAEzE,MAAM,SAAS,GAAG,iBAAiB,CAAC;QAChC,GAAG,EAAE,WAAW;QAChB,UAAU;QACV,KAAK,EAAE,CAAC,GAAG,EAAE,EAAE;YACX,wBAAwB,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;QAC3C,CAAC;KACJ,CAAC,CAAC;IAEH,OAAO,CAAC,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE;QAChC,gBAAgB,CAAC,OAAO,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,SAAS,CAAC,CAAC;IACvE,CAAC,CAAC;AACN,CAAC;AAED,OAAO,EAAE,YAAY,EAA2B,CAAC","sourcesContent":["import type * as Gtk from \"@gtkx/gi/gtk\";\nimport { useState } from \"react\";\nimport type { Collection } from \"./collection.js\";\nimport type { TreeExpansion } from \"./tree-expansion.js\";\nimport type { VisibleOrder, WalkOptions } from \"./tree-order.js\";\nimport { isCollectionIdle } from \"./collection.js\";\nimport { useControlledSync } from \"./controlled-sync.js\";\nimport { joinParts } from \"./keys.js\";\nimport { trackPaths } from \"./slots.js\";\nimport { adoptOrder, markExpanded, orderFor } from \"./tree-expansion.js\";\nimport { walkVisible } from \"./tree-order.js\";\n\ntype ExpansionOptions = {\n collection: Collection;\n expandedIds?: string[] | null | undefined;\n onExpandedChange?: ((ids: string[]) => void) | null | undefined;\n};\n\ntype ItemsChange = {\n position: number;\n removed: number;\n added: number;\n};\n\ntype ItemsChangeHandler = (position: number, removed: number, added: number) => void;\ntype LastExpansion = { key: string };\n\ntype ExpansionContext = {\n collection: Collection;\n last: LastExpansion;\n onExpandedChange?: ((ids: string[]) => void) | null | undefined;\n};\n\nfunction newLastExpansion(): LastExpansion {\n return { key: \"\" };\n}\n\nfunction wantedSet(expansion: TreeExpansion, expandedIds: string[]): Set<string> {\n const wanted: Set<string> = new Set();\n\n for (const id of expandedIds) {\n for (const path of expansion.index.expandablePathsFor(id)) {\n wanted.add(path);\n }\n }\n\n return wanted;\n}\n\nfunction hasSameExpansion(expanded: Set<string>, wanted: Set<string>): boolean {\n return expanded.size === wanted.size && wanted.isSubsetOf(expanded);\n}\n\nfunction toggleOptions(expansion: TreeExpansion, tree: Gtk.TreeListModel, wanted: Set<string>): WalkOptions {\n return {\n index: expansion.index,\n slots: trackPaths(wanted),\n marks: trackPaths(wanted.symmetricDifference(expansion.expanded)),\n visit: (row) => {\n if (row.isMarked) {\n tree.getRow(row.position)?.setExpanded(row.isOpen);\n }\n },\n };\n}\n\nfunction walkApplying(expansion: TreeExpansion, options: WalkOptions): VisibleOrder {\n expansion.isApplying = true;\n\n try {\n return walkVisible(options);\n } finally {\n expansion.isApplying = false;\n }\n}\n\nfunction applyWanted(expansion: TreeExpansion, tree: Gtk.TreeListModel, expandedIds: string[]): void {\n const wanted = wantedSet(expansion, expandedIds);\n\n if (hasSameExpansion(expansion.expanded, wanted)) {\n return;\n }\n\n adoptOrder(expansion, walkApplying(expansion, toggleOptions(expansion, tree, wanted)));\n}\n\nfunction reportExpansion(context: ExpansionContext): void {\n const { expandedIds, expandedPaths } = orderFor(context.collection.expansion);\n const key = joinParts(expandedPaths);\n\n if (context.last.key === key) {\n return;\n }\n\n context.last.key = key;\n context.onExpandedChange?.([...expandedIds]);\n}\n\nfunction applyControlledExpansion(context: ExpansionContext, expandedIds: string[] | null | undefined): void {\n const tree = context.collection.treeModel();\n\n if (tree === null) {\n return;\n }\n\n applyWanted(context.collection.expansion, tree, expandedIds ?? []);\n reportExpansion(context);\n}\n\nfunction isExpansionIdle(collection: Collection): boolean {\n return collection.treeModel() !== null && isCollectionIdle(collection);\n}\n\nfunction didRowDrift(collection: Collection, change: ItemsChange): boolean {\n const path = collection.pathAt(change.position - 1);\n const isExpanded = change.removed === 0 && change.added > 0;\n const isCollapsed = change.added === 0 && change.removed > 0;\n\n if (path === null || isExpanded === isCollapsed) {\n return false;\n }\n\n markExpanded(collection.expansion, path, isExpanded);\n\n return true;\n}\n\nfunction observeExpansion(context: ExpansionContext, change: ItemsChange, onDrift: () => void): void {\n if (!isExpansionIdle(context.collection)) {\n return;\n }\n\n const didDrift = didRowDrift(context.collection, change);\n reportExpansion(context);\n\n if (didDrift) {\n onDrift();\n }\n}\n\nfunction useExpansion(options: ExpansionOptions): ItemsChangeHandler {\n const { collection, expandedIds, onExpandedChange } = options;\n const [last] = useState<LastExpansion>(newLastExpansion);\n const context: ExpansionContext = { collection, last, onExpandedChange };\n\n const markDrift = useControlledSync({\n ids: expandedIds,\n collection,\n apply: (ids) => {\n applyControlledExpansion(context, ids);\n },\n });\n\n return (position, removed, added) => {\n observeExpansion(context, { position, removed, added }, markDrift);\n };\n}\n\nexport { useExpansion, type ItemsChangeHandler };\n"]}
@@ -0,0 +1,5 @@
1
+ declare const encodePart: (value: string) => string;
2
+ declare const joinParts: (values: string[]) => string;
3
+ declare const decodePartAt: (value: string, offset: number) => string | null;
4
+ export { decodePartAt, encodePart, joinParts };
5
+ //# sourceMappingURL=keys.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keys.d.ts","sourceRoot":"","sources":["../../src/internal/keys.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,UAAU,GAAI,OAAO,MAAM,KAAG,MAA8D,CAAC;AACnG,QAAA,MAAM,SAAS,GAAI,QAAQ,MAAM,EAAE,KAAG,MAA2D,CAAC;AAElG,QAAA,MAAM,YAAY,GAAI,OAAO,MAAM,EAAE,QAAQ,MAAM,KAAG,MAAM,GAAG,IAU9D,CAAC;AAEF,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,CAAC"}
@@ -0,0 +1,13 @@
1
+ const LENGTH_SEPARATOR = "\u{1}";
2
+ const encodePart = (value) => `${String(value.length)}${LENGTH_SEPARATOR}${value}`;
3
+ const joinParts = (values) => values.map((value) => encodePart(value)).join("");
4
+ const decodePartAt = (value, offset) => {
5
+ const separator = value.indexOf(LENGTH_SEPARATOR, offset);
6
+ if (separator === -1) {
7
+ return null;
8
+ }
9
+ const length = Number(value.slice(offset, separator));
10
+ return value.slice(separator + 1, separator + 1 + length);
11
+ };
12
+ export { decodePartAt, encodePart, joinParts };
13
+ //# sourceMappingURL=keys.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keys.js","sourceRoot":"","sources":["../../src/internal/keys.ts"],"names":[],"mappings":"AAAA,MAAM,gBAAgB,GAAG,OAAO,CAAC;AAEjC,MAAM,UAAU,GAAG,CAAC,KAAa,EAAU,EAAE,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,gBAAgB,GAAG,KAAK,EAAE,CAAC;AACnG,MAAM,SAAS,GAAG,CAAC,MAAgB,EAAU,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAElG,MAAM,YAAY,GAAG,CAAC,KAAa,EAAE,MAAc,EAAiB,EAAE;IAClE,MAAM,SAAS,GAAG,KAAK,CAAC,OAAO,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAC;IAE1D,IAAI,SAAS,KAAK,CAAC,CAAC,EAAE,CAAC;QACnB,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC;IAEtD,OAAO,KAAK,CAAC,KAAK,CAAC,SAAS,GAAG,CAAC,EAAE,SAAS,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC;AAC9D,CAAC,CAAC;AAEF,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,CAAC","sourcesContent":["const LENGTH_SEPARATOR = \"\\u{1}\";\n\nconst encodePart = (value: string): string => `${String(value.length)}${LENGTH_SEPARATOR}${value}`;\nconst joinParts = (values: string[]): string => values.map((value) => encodePart(value)).join(\"\");\n\nconst decodePartAt = (value: string, offset: number): string | null => {\n const separator = value.indexOf(LENGTH_SEPARATOR, offset);\n\n if (separator === -1) {\n return null;\n }\n\n const length = Number(value.slice(offset, separator));\n\n return value.slice(separator + 1, separator + 1 + length);\n};\n\nexport { decodePartAt, encodePart, joinParts };\n"]}