@gtkx/components 1.0.0-rc.2 → 1.0.0-rc.4
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/README.md +4 -5
- package/dist/adw/combo-row.d.ts +12 -0
- package/dist/adw/combo-row.d.ts.map +1 -0
- package/dist/adw/combo-row.js +14 -0
- package/dist/adw/combo-row.js.map +1 -0
- package/dist/adw/index.d.ts +2 -1
- package/dist/adw/index.d.ts.map +1 -1
- package/dist/adw/index.js +1 -0
- package/dist/adw/index.js.map +1 -1
- package/dist/adw/types.d.ts +11 -3
- package/dist/adw/types.d.ts.map +1 -1
- package/dist/adw/types.js.map +1 -1
- package/dist/column-view.d.ts.map +1 -1
- package/dist/column-view.js +4 -4
- package/dist/column-view.js.map +1 -1
- package/dist/drop-down.d.ts +4 -5
- package/dist/drop-down.d.ts.map +1 -1
- package/dist/drop-down.js +5 -112
- package/dist/drop-down.js.map +1 -1
- package/dist/grid-view.js +1 -1
- package/dist/grid-view.js.map +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/internal/cells.d.ts +3 -3
- package/dist/internal/cells.d.ts.map +1 -1
- package/dist/internal/cells.js.map +1 -1
- package/dist/internal/collection-index.d.ts +5 -5
- package/dist/internal/collection-index.d.ts.map +1 -1
- package/dist/internal/collection-index.js +3 -3
- package/dist/internal/collection-index.js.map +1 -1
- package/dist/internal/collection-model.d.ts.map +1 -1
- package/dist/internal/collection-model.js +2 -5
- package/dist/internal/collection-model.js.map +1 -1
- package/dist/internal/collection.d.ts +2 -2
- package/dist/internal/collection.d.ts.map +1 -1
- package/dist/internal/collection.js.map +1 -1
- package/dist/internal/drop-down.d.ts +16 -0
- package/dist/internal/drop-down.d.ts.map +1 -0
- package/dist/internal/drop-down.js +110 -0
- package/dist/internal/drop-down.js.map +1 -0
- package/dist/internal/expansion.d.ts +1 -1
- package/dist/internal/expansion.d.ts.map +1 -1
- package/dist/internal/expansion.js +3 -4
- package/dist/internal/expansion.js.map +1 -1
- package/dist/internal/selection.d.ts +1 -0
- package/dist/internal/selection.d.ts.map +1 -1
- package/dist/internal/selection.js +1 -0
- package/dist/internal/selection.js.map +1 -1
- package/dist/internal/use-collection.d.ts +5 -6
- package/dist/internal/use-collection.d.ts.map +1 -1
- package/dist/internal/use-collection.js +10 -18
- package/dist/internal/use-collection.js.map +1 -1
- package/dist/types.d.ts +70 -48
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +7 -7
- package/src/adw/combo-row.tsx +21 -0
- package/src/adw/index.ts +8 -1
- package/src/adw/types.ts +17 -3
- package/src/column-view.tsx +13 -13
- package/src/drop-down.tsx +10 -195
- package/src/grid-view.tsx +1 -1
- package/src/index.ts +7 -12
- package/src/internal/cells.tsx +12 -12
- package/src/internal/collection-index.ts +16 -16
- package/src/internal/collection-model.ts +2 -5
- package/src/internal/collection.ts +2 -2
- package/src/internal/drop-down.tsx +186 -0
- package/src/internal/expansion.ts +5 -6
- package/src/internal/selection.tsx +3 -0
- package/src/internal/use-collection.tsx +16 -29
- package/src/types.ts +83 -66
- package/dist/size-group.d.ts +0 -12
- package/dist/size-group.d.ts.map +0 -1
- package/dist/size-group.js +0 -41
- package/dist/size-group.js.map +0 -1
- package/src/size-group.tsx +0 -77
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cells.js","sourceRoot":"","sources":["../../src/internal/cells.tsx"],"names":[],"mappings":";AAEA,OAAO,KAAK,GAAG,MAAM,cAAc,CAAC;AACpC,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,eAAe,EAAE,QAAQ,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAG9E,OAAO,EAAE,KAAK,EAAE,MAAM,uBAA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,cAAc,EACd,WAAW,EACX,aAAa,GAGhB,CAAC","sourcesContent":["import type * as GObject from \"@gtkx/gi/gobject\";\nimport type { ReactNode } from \"react\";\nimport * as Gtk from \"@gtkx/gi/gtk\";\nimport { GtkTreeExpander } from \"@gtkx/jsx/gtk\";\nimport { createPortal, useProperty } from \"@gtkx/react\";\nimport { memo, useLayoutEffect, useState, useSyncExternalStore } from \"react\";\nimport type { HeaderRenderer, Item, ItemRenderer, RenderItemArgs } from \"../types.js\";\nimport type { Collection } from \"./collection.js\";\nimport { getId } from \"./collection-model.js\";\n\ntype CellSize = {\n width: number;\n height: number;\n};\n\ntype CellHost = GObject.Object & {\n getChild: () => Gtk.Widget | null;\n setChild: (child: Gtk.Widget | null) => void;\n};\n\ntype CellEntry<C extends CellHost> = {\n key: string;\n cell: C;\n item: GObject.Object | null;\n listeners: Set<() => void>;\n subscribe: (onChange: () => void) => () => void;\n getItem: () => GObject.Object | null;\n};\n\ntype FactoryHandlers = {\n onSetup: (cell: GObject.Object) => void;\n onBind: (cell: GObject.Object) => void;\n onUnbind: (cell: GObject.Object) => void;\n onTeardown: (cell: GObject.Object) => void;\n};\n\ntype CellStore<C extends CellHost> = {\n handlers: FactoryHandlers;\n setSize: (size: CellSize) => void;\n subscribe: (onChange: () => void) => () => void;\n getSnapshot: () => CellEntry<C>[];\n};\n\ntype StoreState<C extends CellHost> = {\n size: CellSize;\n entries: Map<C, CellEntry<C>>;\n snapshot: CellEntry<C>[] | null;\n listeners: Set<() => void>;\n serial: number;\n};\n\ntype CellGuard<C extends CellHost> = (value: GObject.Object) => value is C;\n\ntype ItemBodyOptions = {\n item: GObject.Object | null;\n position: number | undefined;\n row: Gtk.TreeListRow | null;\n isRowExpanded: boolean | undefined;\n render: ItemRenderer<never>;\n collection: Collection;\n expandedIds: string[] | null | undefined;\n};\n\ntype ItemCellProps = {\n entry: CellEntry<Gtk.ListItem>;\n render: ItemRenderer<never>;\n collection: Collection;\n expandedIds: string[] | null | undefined;\n};\n\ntype ItemPortalsProps = {\n store: CellStore<Gtk.ListItem>;\n render: ItemRenderer<never>;\n collection: Collection;\n expandedIds?: string[] | null | undefined;\n};\n\ntype HeaderCellProps = {\n entry: CellEntry<Gtk.ListHeader>;\n render: HeaderRenderer<never>;\n collection: Collection;\n};\n\ntype HeaderPortalsProps = {\n store: CellStore<Gtk.ListHeader>;\n render: HeaderRenderer<never>;\n collection: Collection;\n};\n\nconst ItemCell = memo(ItemCellImpl);\nconst HeaderCell = memo(HeaderCellImpl);\n\nconst isListItem = (value: GObject.Object): value is Gtk.ListItem => value instanceof Gtk.ListItem;\nconst isListHeader = (value: GObject.Object): value is Gtk.ListHeader => value instanceof Gtk.ListHeader;\n\nconst placeholder = (size: CellSize): Gtk.Widget =>\n new Gtk.Box({ widthRequest: size.width, heightRequest: size.height });\n\nfunction notifyEntry<C extends CellHost>(entry: CellEntry<C>): void {\n for (const listener of entry.listeners) {\n listener();\n }\n}\n\nfunction notifyRoster<C extends CellHost>(state: StoreState<C>): void {\n state.snapshot = null;\n\n for (const listener of state.listeners) {\n listener();\n }\n}\n\nfunction createEntry<C extends CellHost>(state: StoreState<C>, cell: C): CellEntry<C> {\n state.serial += 1;\n\n const entry: CellEntry<C> = {\n key: `gtkx-cell-${String(state.serial)}`,\n cell,\n item: null,\n listeners: new Set(),\n subscribe: (onChange) => {\n entry.listeners.add(onChange);\n\n return () => {\n entry.listeners.delete(onChange);\n };\n },\n getItem: () => entry.item,\n };\n\n return entry;\n}\n\nfunction setupCell<C extends CellHost>(state: StoreState<C>, cell: C): void {\n cell.setChild(placeholder(state.size));\n state.entries.set(cell, createEntry(state, cell));\n notifyRoster(state);\n}\n\nfunction teardownCell<C extends CellHost>(state: StoreState<C>, cell: C): void {\n if (state.entries.delete(cell)) {\n notifyRoster(state);\n }\n}\n\nfunction writeBinding<C extends CellHost>(state: StoreState<C>, cell: C, item: GObject.Object | null): void {\n const entry = state.entries.get(cell);\n\n if (entry === undefined) {\n return;\n }\n\n entry.item = item;\n notifyEntry(entry);\n}\n\nfunction boundItem(cell: CellHost): GObject.Object | null {\n if (cell instanceof Gtk.ListItem) {\n return cell.getItem();\n }\n\n return cell instanceof Gtk.ListHeader ? cell.getItem() : null;\n}\n\nfunction bindCell<C extends CellHost>(state: StoreState<C>, cell: C): void {\n if (cell.getChild() === null) {\n cell.setChild(placeholder(state.size));\n }\n\n writeBinding(state, cell, boundItem(cell));\n}\n\nfunction subscribeRoster<C extends CellHost>(state: StoreState<C>, onChange: () => void): () => void {\n state.listeners.add(onChange);\n\n return () => {\n state.listeners.delete(onChange);\n };\n}\n\nfunction rosterSnapshot<C extends CellHost>(state: StoreState<C>): CellEntry<C>[] {\n state.snapshot ??= state.entries.values().toArray();\n\n return state.snapshot;\n}\n\nfunction guarded<C extends CellHost>(isCell: CellGuard<C>, run: (cell: C) => void): (cell: GObject.Object) => void {\n return (cell) => {\n if (isCell(cell)) {\n run(cell);\n }\n };\n}\n\nfunction createHandlers<C extends CellHost>(state: StoreState<C>, isCell: CellGuard<C>): FactoryHandlers {\n return {\n onSetup: guarded(isCell, (cell) => {\n setupCell(state, cell);\n }),\n onBind: guarded(isCell, (cell) => {\n bindCell(state, cell);\n }),\n onUnbind: guarded(isCell, (cell) => {\n writeBinding(state, cell, null);\n }),\n onTeardown: guarded(isCell, (cell) => {\n teardownCell(state, cell);\n }),\n };\n}\n\nfunction createCellStore<C extends CellHost>(isCell: CellGuard<C>, size: CellSize): CellStore<C> {\n const state: StoreState<C> = {\n size,\n entries: new Map(),\n snapshot: null,\n listeners: new Set(),\n serial: 0,\n };\n\n return {\n handlers: createHandlers(state, isCell),\n setSize: (next) => {\n state.size = next;\n },\n subscribe: (onChange) => subscribeRoster(state, onChange),\n getSnapshot: () => rosterSnapshot(state),\n };\n}\n\nfunction useCellStore<C extends CellHost>(isCell: CellGuard<C>, size: CellSize): CellStore<C> {\n const [store] = useState(() => createCellStore(isCell, size));\n\n useLayoutEffect(() => {\n store.setSize(size);\n });\n\n return store;\n}\n\nfunction useItemCells(size: CellSize): CellStore<Gtk.ListItem> {\n return useCellStore(isListItem, size);\n}\n\nfunction useHeaderCells(size: CellSize): CellStore<Gtk.ListHeader> {\n return useCellStore(isListHeader, size);\n}\n\nfunction isRowWanted(options: ItemBodyOptions, item: Item): boolean {\n const { expandedIds } = options;\n\n return expandedIds == null ? options.isRowExpanded === true : expandedIds.includes(item.id);\n}\n\nfunction itemArgs(item: Item, options: ItemBodyOptions): RenderItemArgs<unknown> {\n const args: RenderItemArgs<unknown> = { item: item.value, index: options.position ?? 0 };\n const row = options.row;\n\n if (row === null) {\n return args;\n }\n\n args.depth = row.getDepth();\n\n if (row.isExpandable()) {\n args.isExpanded = isRowWanted(options, item);\n }\n\n return args;\n}\n\nfunction wrapExpander(item: Item, row: Gtk.TreeListRow | null, content: ReactNode): ReactNode {\n const expander: ReactNode =\n row === null\n ? (\n content\n )\n : (\n <GtkTreeExpander\n listRow={row}\n hideExpander={item.hideExpander ?? false}\n indentForDepth={item.indentForDepth ?? true}\n indentForIcon={item.indentForIcon ?? true}\n >\n {content}\n </GtkTreeExpander>\n );\n\n return expander;\n}\n\nfunction itemBody(options: ItemBodyOptions): ReactNode {\n const id = getId(options.item);\n const item = id === null ? undefined : options.collection.itemFor(id);\n\n if (item === undefined) {\n return null;\n }\n\n const render = options.render as ItemRenderer<unknown>;\n\n return wrapExpander(item, options.row, render(itemArgs(item, options)));\n}\n\nfunction ItemCellImpl({ entry, render, collection, expandedIds }: ItemCellProps): ReactNode {\n const item = useSyncExternalStore(entry.subscribe, entry.getItem);\n const position = useProperty(entry.cell, \"position\");\n const row = item instanceof Gtk.TreeListRow ? item : null;\n const isRowExpanded = useProperty(row, \"expanded\");\n const body = itemBody({ item, position, row, isRowExpanded, render, collection, expandedIds });\n\n return createPortal(body, entry.cell, entry.key);\n}\n\nfunction HeaderCellImpl({ entry, render, collection }: HeaderCellProps): ReactNode {\n const item = useSyncExternalStore(entry.subscribe, entry.getItem);\n const id = getId(item);\n const renderHeader = render as HeaderRenderer<unknown>;\n const body = id === null ? null : renderHeader({ section: collection.sectionFor(id) });\n\n return createPortal(body, entry.cell, entry.key);\n}\n\nfunction usePortalEntries<C extends CellHost>(store: CellStore<C>): CellEntry<C>[] {\n return useSyncExternalStore(store.subscribe, store.getSnapshot);\n}\n\nconst ItemPortals = ({ store, render, collection, expandedIds }: ItemPortalsProps): ReactNode =>\n usePortalEntries(store).map((entry) => (\n <ItemCell key={entry.key} entry={entry} render={render} collection={collection} expandedIds={expandedIds} />\n ));\n\nconst HeaderPortals = ({ store, render, collection }: HeaderPortalsProps): ReactNode =>\n usePortalEntries(store).map((entry) => (\n <HeaderCell key={entry.key} entry={entry} render={render} collection={collection} />\n ));\n\nexport {\n useItemCells,\n useHeaderCells,\n ItemPortals,\n HeaderPortals,\n type CellSize,\n type CellStore,\n};\n"]}
|
|
1
|
+
{"version":3,"file":"cells.js","sourceRoot":"","sources":["../../src/internal/cells.tsx"],"names":[],"mappings":";AAEA,OAAO,KAAK,GAAG,MAAM,cAAc,CAAC;AACpC,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,eAAe,EAAE,QAAQ,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAG9E,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAiF9C,MAAM,QAAQ,GAAG,IAAI,CAAC,YAAY,CAAC,CAAC;AACpC,MAAM,UAAU,GAAG,IAAI,CAAC,cAAc,CAAC,CAAC;AAExC,MAAM,UAAU,GAAG,CAAC,KAAqB,EAAyB,EAAE,CAAC,KAAK,YAAY,GAAG,CAAC,QAAQ,CAAC;AACnG,MAAM,YAAY,GAAG,CAAC,KAAqB,EAA2B,EAAE,CAAC,KAAK,YAAY,GAAG,CAAC,UAAU,CAAC;AAEzG,MAAM,WAAW,GAAG,CAAC,IAAc,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,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;AACrD,CAAC;AAED,SAAS,gBAAgB,CAAqB,KAAmB;IAC7D,OAAO,oBAAoB,CAAC,KAAK,CAAC,SAAS,EAAE,KAAK,CAAC,WAAW,CAAC,CAAC;AACpE,CAAC;AAED,MAAM,WAAW,GAAG,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,WAAW,EAAoB,EAAa,EAAE,CAC5F,gBAAgB,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CACnC,KAAC,QAAQ,IAAiB,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,WAAW,IAAzF,KAAK,CAAC,GAAG,CAAoF,CAC/G,CAAC,CAAC;AAEP,MAAM,aAAa,GAAG,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAsB,EAAa,EAAE,CACnF,gBAAgB,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CACnC,KAAC,UAAU,IAAiB,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,IAA/D,KAAK,CAAC,GAAG,CAA0D,CACvF,CAAC,CAAC;AAEP,OAAO,EACH,YAAY,EACZ,cAAc,EACd,WAAW,EACX,aAAa,GAGhB,CAAC","sourcesContent":["import type * as GObject from \"@gtkx/gi/gobject\";\nimport type { ReactNode } from \"react\";\nimport * as Gtk from \"@gtkx/gi/gtk\";\nimport { GtkTreeExpander } from \"@gtkx/jsx/gtk\";\nimport { createPortal, useProperty } from \"@gtkx/react\";\nimport { memo, useLayoutEffect, useState, useSyncExternalStore } from \"react\";\nimport type { ListItem, ListItemRenderArgs, ListItemRenderer, ListSectionRenderer } from \"../types.js\";\nimport type { Collection } from \"./collection.js\";\nimport { getId } from \"./collection-model.js\";\n\ntype CellSize = {\n width: number;\n height: number;\n};\n\ntype CellHost = GObject.Object & {\n getChild: () => Gtk.Widget | null;\n setChild: (child: Gtk.Widget | null) => void;\n};\n\ntype CellEntry<C extends CellHost> = {\n key: string;\n cell: C;\n item: GObject.Object | null;\n listeners: Set<() => void>;\n subscribe: (onChange: () => void) => () => void;\n getItem: () => GObject.Object | null;\n};\n\ntype FactoryHandlers = {\n onSetup: (cell: GObject.Object) => void;\n onBind: (cell: GObject.Object) => void;\n onUnbind: (cell: GObject.Object) => void;\n onTeardown: (cell: GObject.Object) => void;\n};\n\ntype CellStore<C extends CellHost> = {\n handlers: FactoryHandlers;\n setSize: (size: CellSize) => void;\n subscribe: (onChange: () => void) => () => void;\n getSnapshot: () => CellEntry<C>[];\n};\n\ntype StoreState<C extends CellHost> = {\n size: CellSize;\n entries: Map<C, CellEntry<C>>;\n snapshot: CellEntry<C>[] | null;\n listeners: Set<() => void>;\n serial: number;\n};\n\ntype CellGuard<C extends CellHost> = (value: GObject.Object) => value is C;\n\ntype ItemBodyOptions = {\n item: GObject.Object | null;\n position: number | undefined;\n row: Gtk.TreeListRow | null;\n isRowExpanded: boolean | undefined;\n render: ListItemRenderer<never>;\n collection: Collection;\n expandedIds: string[] | null | undefined;\n};\n\ntype ItemCellProps = {\n entry: CellEntry<Gtk.ListItem>;\n render: ListItemRenderer<never>;\n collection: Collection;\n expandedIds: string[] | null | undefined;\n};\n\ntype ItemPortalsProps = {\n store: CellStore<Gtk.ListItem>;\n render: ListItemRenderer<never>;\n collection: Collection;\n expandedIds?: string[] | null | undefined;\n};\n\ntype HeaderCellProps = {\n entry: CellEntry<Gtk.ListHeader>;\n render: ListSectionRenderer<never>;\n collection: Collection;\n};\n\ntype HeaderPortalsProps = {\n store: CellStore<Gtk.ListHeader>;\n render: ListSectionRenderer<never>;\n collection: Collection;\n};\n\nconst ItemCell = memo(ItemCellImpl);\nconst HeaderCell = memo(HeaderCellImpl);\n\nconst isListItem = (value: GObject.Object): value is Gtk.ListItem => value instanceof Gtk.ListItem;\nconst isListHeader = (value: GObject.Object): value is Gtk.ListHeader => value instanceof Gtk.ListHeader;\n\nconst placeholder = (size: CellSize): Gtk.Widget =>\n new Gtk.Box({ widthRequest: size.width, heightRequest: size.height });\n\nfunction notifyEntry<C extends CellHost>(entry: CellEntry<C>): void {\n for (const listener of entry.listeners) {\n listener();\n }\n}\n\nfunction notifyRoster<C extends CellHost>(state: StoreState<C>): void {\n state.snapshot = null;\n\n for (const listener of state.listeners) {\n listener();\n }\n}\n\nfunction createEntry<C extends CellHost>(state: StoreState<C>, cell: C): CellEntry<C> {\n state.serial += 1;\n\n const entry: CellEntry<C> = {\n key: `gtkx-cell-${String(state.serial)}`,\n cell,\n item: null,\n listeners: new Set(),\n subscribe: (onChange) => {\n entry.listeners.add(onChange);\n\n return () => {\n entry.listeners.delete(onChange);\n };\n },\n getItem: () => entry.item,\n };\n\n return entry;\n}\n\nfunction setupCell<C extends CellHost>(state: StoreState<C>, cell: C): void {\n cell.setChild(placeholder(state.size));\n state.entries.set(cell, createEntry(state, cell));\n notifyRoster(state);\n}\n\nfunction teardownCell<C extends CellHost>(state: StoreState<C>, cell: C): void {\n if (state.entries.delete(cell)) {\n notifyRoster(state);\n }\n}\n\nfunction writeBinding<C extends CellHost>(state: StoreState<C>, cell: C, item: GObject.Object | null): void {\n const entry = state.entries.get(cell);\n\n if (entry === undefined) {\n return;\n }\n\n entry.item = item;\n notifyEntry(entry);\n}\n\nfunction boundItem(cell: CellHost): GObject.Object | null {\n if (cell instanceof Gtk.ListItem) {\n return cell.getItem();\n }\n\n return cell instanceof Gtk.ListHeader ? cell.getItem() : null;\n}\n\nfunction bindCell<C extends CellHost>(state: StoreState<C>, cell: C): void {\n if (cell.getChild() === null) {\n cell.setChild(placeholder(state.size));\n }\n\n writeBinding(state, cell, boundItem(cell));\n}\n\nfunction subscribeRoster<C extends CellHost>(state: StoreState<C>, onChange: () => void): () => void {\n state.listeners.add(onChange);\n\n return () => {\n state.listeners.delete(onChange);\n };\n}\n\nfunction rosterSnapshot<C extends CellHost>(state: StoreState<C>): CellEntry<C>[] {\n state.snapshot ??= state.entries.values().toArray();\n\n return state.snapshot;\n}\n\nfunction guarded<C extends CellHost>(isCell: CellGuard<C>, run: (cell: C) => void): (cell: GObject.Object) => void {\n return (cell) => {\n if (isCell(cell)) {\n run(cell);\n }\n };\n}\n\nfunction createHandlers<C extends CellHost>(state: StoreState<C>, isCell: CellGuard<C>): FactoryHandlers {\n return {\n onSetup: guarded(isCell, (cell) => {\n setupCell(state, cell);\n }),\n onBind: guarded(isCell, (cell) => {\n bindCell(state, cell);\n }),\n onUnbind: guarded(isCell, (cell) => {\n writeBinding(state, cell, null);\n }),\n onTeardown: guarded(isCell, (cell) => {\n teardownCell(state, cell);\n }),\n };\n}\n\nfunction createCellStore<C extends CellHost>(isCell: CellGuard<C>, size: CellSize): CellStore<C> {\n const state: StoreState<C> = {\n size,\n entries: new Map(),\n snapshot: null,\n listeners: new Set(),\n serial: 0,\n };\n\n return {\n handlers: createHandlers(state, isCell),\n setSize: (next) => {\n state.size = next;\n },\n subscribe: (onChange) => subscribeRoster(state, onChange),\n getSnapshot: () => rosterSnapshot(state),\n };\n}\n\nfunction useCellStore<C extends CellHost>(isCell: CellGuard<C>, size: CellSize): CellStore<C> {\n const [store] = useState(() => createCellStore(isCell, size));\n\n useLayoutEffect(() => {\n store.setSize(size);\n });\n\n return store;\n}\n\nfunction useItemCells(size: CellSize): CellStore<Gtk.ListItem> {\n return useCellStore(isListItem, size);\n}\n\nfunction useHeaderCells(size: CellSize): CellStore<Gtk.ListHeader> {\n return useCellStore(isListHeader, size);\n}\n\nfunction isRowWanted(options: ItemBodyOptions, item: ListItem): boolean {\n const { expandedIds } = options;\n\n return expandedIds == null ? options.isRowExpanded === true : expandedIds.includes(item.id);\n}\n\nfunction itemArgs(item: ListItem, options: ItemBodyOptions): ListItemRenderArgs<unknown> {\n const args: ListItemRenderArgs<unknown> = { item: item.value, index: options.position ?? 0 };\n const row = options.row;\n\n if (row === null) {\n return args;\n }\n\n args.depth = row.getDepth();\n\n if (row.isExpandable()) {\n args.isExpanded = isRowWanted(options, item);\n }\n\n return args;\n}\n\nfunction wrapExpander(item: ListItem, row: Gtk.TreeListRow | null, content: ReactNode): ReactNode {\n const expander: ReactNode =\n row === null\n ? (\n content\n )\n : (\n <GtkTreeExpander\n listRow={row}\n hideExpander={item.hideExpander ?? false}\n indentForDepth={item.indentForDepth ?? true}\n indentForIcon={item.indentForIcon ?? true}\n >\n {content}\n </GtkTreeExpander>\n );\n\n return expander;\n}\n\nfunction itemBody(options: ItemBodyOptions): ReactNode {\n const id = getId(options.item);\n const item = id === null ? undefined : options.collection.itemFor(id);\n\n if (item === undefined) {\n return null;\n }\n\n const render = options.render as ListItemRenderer<unknown>;\n\n return wrapExpander(item, options.row, render(itemArgs(item, options)));\n}\n\nfunction ItemCellImpl({ entry, render, collection, expandedIds }: ItemCellProps): ReactNode {\n const item = useSyncExternalStore(entry.subscribe, entry.getItem);\n const position = useProperty(entry.cell, \"position\");\n const row = item instanceof Gtk.TreeListRow ? item : null;\n const isRowExpanded = useProperty(row, \"expanded\");\n const body = itemBody({ item, position, row, isRowExpanded, render, collection, expandedIds });\n\n return createPortal(body, entry.cell, entry.key);\n}\n\nfunction HeaderCellImpl({ entry, render, collection }: HeaderCellProps): ReactNode {\n const item = useSyncExternalStore(entry.subscribe, entry.getItem);\n const id = getId(item);\n const renderHeader = render as ListSectionRenderer<unknown>;\n const body = id === null ? null : renderHeader({ section: collection.sectionFor(id) });\n\n return createPortal(body, entry.cell, entry.key);\n}\n\nfunction usePortalEntries<C extends CellHost>(store: CellStore<C>): CellEntry<C>[] {\n return useSyncExternalStore(store.subscribe, store.getSnapshot);\n}\n\nconst ItemPortals = ({ store, render, collection, expandedIds }: ItemPortalsProps): ReactNode =>\n usePortalEntries(store).map((entry) => (\n <ItemCell key={entry.key} entry={entry} render={render} collection={collection} expandedIds={expandedIds} />\n ));\n\nconst HeaderPortals = ({ store, render, collection }: HeaderPortalsProps): ReactNode =>\n usePortalEntries(store).map((entry) => (\n <HeaderCell key={entry.key} entry={entry} render={render} collection={collection} />\n ));\n\nexport {\n useItemCells,\n useHeaderCells,\n ItemPortals,\n HeaderPortals,\n type CellSize,\n type CellStore,\n};\n"]}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ListItem, ListSection } from "../types.js";
|
|
2
2
|
type Level = {
|
|
3
3
|
key: string;
|
|
4
4
|
ids: string[];
|
|
5
|
-
|
|
5
|
+
expandableFlags: boolean[];
|
|
6
6
|
};
|
|
7
7
|
type CollectionIndex = {
|
|
8
8
|
isTree: boolean;
|
|
9
9
|
size: number;
|
|
10
10
|
groups: Level[];
|
|
11
11
|
children: Map<string, Level>;
|
|
12
|
-
|
|
13
|
-
itemFor: (id: string) =>
|
|
12
|
+
hasId: (id: string) => boolean;
|
|
13
|
+
itemFor: (id: string) => ListItem | undefined;
|
|
14
14
|
sectionFor: (id: string) => unknown;
|
|
15
15
|
positionFor: (id: string) => number;
|
|
16
16
|
};
|
|
17
17
|
declare const ROOT_LEVEL_KEY = "";
|
|
18
18
|
declare const childLevelKey: (id: string) => string;
|
|
19
|
-
declare function createCollectionIndex(items:
|
|
19
|
+
declare function createCollectionIndex(items: ListItem[] | undefined, sections: ListSection[] | undefined, isFlat: boolean): CollectionIndex;
|
|
20
20
|
export { createCollectionIndex, childLevelKey, ROOT_LEVEL_KEY, type CollectionIndex, type Level };
|
|
21
21
|
//# sourceMappingURL=collection-index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"collection-index.d.ts","sourceRoot":"","sources":["../../src/internal/collection-index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"collection-index.d.ts","sourceRoot":"","sources":["../../src/internal/collection-index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAEzD,KAAK,KAAK,GAAG;IACT,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,EAAE,CAAC;IACd,eAAe,EAAE,OAAO,EAAE,CAAC;CAC9B,CAAC;AAEF,KAAK,eAAe,GAAG;IACnB,MAAM,EAAE,OAAO,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,KAAK,EAAE,CAAC;IAChB,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAC7B,KAAK,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC;IAC/B,OAAO,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,QAAQ,GAAG,SAAS,CAAC;IAC9C,UAAU,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC;IACpC,WAAW,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,MAAM,CAAC;CACvC,CAAC;AAYF,QAAA,MAAM,cAAc,KAAK,CAAC;AAG1B,QAAA,MAAM,aAAa,GAAI,IAAI,MAAM,KAAG,MAAuB,CAAC;AAwG5D,iBAAS,qBAAqB,CAC1B,KAAK,EAAE,QAAQ,EAAE,GAAG,SAAS,EAC7B,QAAQ,EAAE,WAAW,EAAE,GAAG,SAAS,EACnC,MAAM,EAAE,OAAO,GAChB,eAAe,CAyBjB;AAED,OAAO,EAAE,qBAAqB,EAAE,aAAa,EAAE,cAAc,EAAE,KAAK,eAAe,EAAE,KAAK,KAAK,EAAE,CAAC"}
|
|
@@ -6,7 +6,7 @@ function collectChildren(state, level, item) {
|
|
|
6
6
|
return;
|
|
7
7
|
}
|
|
8
8
|
const canExpand = hasChildren(item);
|
|
9
|
-
level.
|
|
9
|
+
level.expandableFlags.push(canExpand);
|
|
10
10
|
if (!canExpand) {
|
|
11
11
|
return;
|
|
12
12
|
}
|
|
@@ -14,7 +14,7 @@ function collectChildren(state, level, item) {
|
|
|
14
14
|
state.children.set(key, collectLevel(state, key, item.children ?? []));
|
|
15
15
|
}
|
|
16
16
|
function collectLevel(state, key, items) {
|
|
17
|
-
const level = { key, ids: [],
|
|
17
|
+
const level = { key, ids: [], expandableFlags: [] };
|
|
18
18
|
for (const item of items) {
|
|
19
19
|
level.ids.push(item.id);
|
|
20
20
|
state.itemsById.set(item.id, item);
|
|
@@ -96,7 +96,7 @@ function createCollectionIndex(items, sections, isFlat) {
|
|
|
96
96
|
size: state.itemsById.size,
|
|
97
97
|
groups: state.groups,
|
|
98
98
|
children: state.children,
|
|
99
|
-
|
|
99
|
+
hasId: (id) => state.itemsById.has(id),
|
|
100
100
|
itemFor: (id) => state.itemsById.get(id),
|
|
101
101
|
sectionFor: (id) => sectionFor(state, id),
|
|
102
102
|
positionFor: (id) => positionFor(state, id),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"collection-index.js","sourceRoot":"","sources":["../../src/internal/collection-index.ts"],"names":[],"mappings":"AA6BA,MAAM,cAAc,GAAG,EAAE,CAAC;AAE1B,MAAM,WAAW,GAAG,CAAC,
|
|
1
|
+
{"version":3,"file":"collection-index.js","sourceRoot":"","sources":["../../src/internal/collection-index.ts"],"names":[],"mappings":"AA6BA,MAAM,cAAc,GAAG,EAAE,CAAC;AAE1B,MAAM,WAAW,GAAG,CAAC,IAAc,EAAW,EAAE,CAAC,IAAI,CAAC,QAAQ,KAAK,SAAS,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC;AACzG,MAAM,aAAa,GAAG,CAAC,EAAU,EAAU,EAAE,CAAC,SAAS,EAAE,EAAE,CAAC;AAE5D,SAAS,eAAe,CAAC,KAAiB,EAAE,KAAY,EAAE,IAAc;IACpE,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;QAChB,OAAO;IACX,CAAC;IAED,MAAM,SAAS,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IACpC,KAAK,CAAC,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M;QACpB,IAAI,EAAE,KAAK,CAAC,SAAS,CAAC,IAAI;QAC1B,MAAM,EAAE,KAAK,CAAC,MAAM;QACpB,QAAQ,EAAE,KAAK,CAAC,QAAQ;QACxB,KAAK,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;QACtC,OAAO,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;QACxC,UAAU,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,EAAE,CAAC;QACzC,WAAW,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,WAAW,CAAC,KAAK,EAAE,EAAE,CAAC;KAC9C,CAAC;AACN,CAAC;AAED,OAAO,EAAE,qBAAqB,EAAE,aAAa,EAAE,cAAc,EAAoC,CAAC","sourcesContent":["import type { ListItem, ListSection } from \"../types.js\";\n\ntype Level = {\n key: string;\n ids: string[];\n expandableFlags: boolean[];\n};\n\ntype CollectionIndex = {\n isTree: boolean;\n size: number;\n groups: Level[];\n children: Map<string, Level>;\n hasId: (id: string) => boolean;\n itemFor: (id: string) => ListItem | undefined;\n sectionFor: (id: string) => unknown;\n positionFor: (id: string) => number;\n};\n\ntype IndexState = {\n isTree: boolean;\n groups: Level[];\n children: Map<string, Level>;\n itemsById: Map<string, ListItem>;\n sections: ListSection[] | undefined;\n positions: Map<string, number> | null;\n starts: number[] | null;\n};\n\nconst ROOT_LEVEL_KEY = \"\";\n\nconst hasChildren = (item: ListItem): boolean => item.children !== undefined && item.children.length > 0;\nconst childLevelKey = (id: string): string => `child:${id}`;\n\nfunction collectChildren(state: IndexState, level: Level, item: ListItem): void {\n if (!state.isTree) {\n return;\n }\n\n const canExpand = hasChildren(item);\n level.expandableFlags.push(canExpand);\n\n if (!canExpand) {\n return;\n }\n\n const key = childLevelKey(item.id);\n state.children.set(key, collectLevel(state, key, item.children ?? []));\n}\n\nfunction collectLevel(state: IndexState, key: string, items: ListItem[]): Level {\n const level: Level = { key, ids: [], expandableFlags: [] };\n\n for (const item of items) {\n level.ids.push(item.id);\n state.itemsById.set(item.id, item);\n collectChildren(state, level, item);\n }\n\n return level;\n}\n\nfunction buildPositions(state: IndexState): void {\n const positions: Map<string, number> = new Map();\n const starts: number[] = [];\n let offset = 0;\n\n for (const level of state.groups) {\n starts.push(offset);\n\n for (const id of level.ids) {\n positions.set(id, offset);\n offset += 1;\n }\n }\n\n state.positions = positions;\n state.starts = starts;\n}\n\nfunction positionFor(state: IndexState, id: string): number {\n if (state.isTree) {\n return -1;\n }\n\n if (state.positions === null) {\n buildPositions(state);\n }\n\n return state.positions?.get(id) ?? -1;\n}\n\nfunction sectionAt(starts: number[], position: number): number {\n let low = 0;\n let high = starts.length - 1;\n\n while (low < high) {\n const middle = Math.ceil((low + high) / 2);\n\n if ((starts[middle] ?? 0) <= position) {\n low = middle;\n } else {\n high = middle - 1;\n }\n }\n\n return low;\n}\n\nfunction sectionFor(state: IndexState, id: string): unknown {\n const sections = state.sections;\n const position = positionFor(state, id);\n\n if (sections === undefined || position < 0 || state.starts === null) {\n return undefined;\n }\n\n return sections[sectionAt(state.starts, position)]?.value;\n}\n\nfunction buildGroups(state: IndexState, source: ListItem[], sections: ListSection[] | undefined): Level[] {\n if (sections === undefined) {\n return [collectLevel(state, ROOT_LEVEL_KEY, source)];\n }\n\n return sections.map((section) => collectLevel(state, section.id, section.data));\n}\n\nfunction isTreeSource(source: ListItem[], sections: ListSection[] | undefined, isFlat: boolean): boolean {\n if (sections !== undefined || isFlat) {\n return false;\n }\n\n return source.some((item) => hasChildren(item));\n}\n\nfunction createCollectionIndex(\n items: ListItem[] | undefined,\n sections: ListSection[] | undefined,\n isFlat: boolean,\n): CollectionIndex {\n const source = items ?? [];\n\n const state: IndexState = {\n isTree: isTreeSource(source, sections, isFlat),\n groups: [],\n children: new Map(),\n itemsById: new Map(),\n sections,\n positions: null,\n starts: null,\n };\n\n state.groups = buildGroups(state, source, sections);\n\n return {\n isTree: state.isTree,\n size: state.itemsById.size,\n groups: state.groups,\n children: state.children,\n hasId: (id) => state.itemsById.has(id),\n itemFor: (id) => state.itemsById.get(id),\n sectionFor: (id) => sectionFor(state, id),\n positionFor: (id) => positionFor(state, id),\n };\n}\n\nexport { createCollectionIndex, childLevelKey, ROOT_LEVEL_KEY, type CollectionIndex, type Level };\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"collection-model.d.ts","sourceRoot":"","sources":["../../src/internal/collection-model.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,OAAO,MAAM,kBAAkB,CAAC;AAC5C,OAAO,KAAK,GAAG,MAAM,cAAc,CAAC;AACpC,OAAO,KAAK,EAAE,eAAe,EAAS,MAAM,uBAAuB,CAAC;AAwBpE,KAAK,eAAe,GAAG;IACnB,KAAK,EAAE,GAAG,CAAC,gBAAgB,CAAC;IAC5B,SAAS,EAAE,MAAM,GAAG,CAAC,aAAa,GAAG,IAAI,CAAC;IAC1C,IAAI,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,CAAC;CAC1C,CAAC;AAMF,iBAAS,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,MAAM,GAAG,IAAI,GAAG,MAAM,GAAG,IAAI,CAI1D;
|
|
1
|
+
{"version":3,"file":"collection-model.d.ts","sourceRoot":"","sources":["../../src/internal/collection-model.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,OAAO,MAAM,kBAAkB,CAAC;AAC5C,OAAO,KAAK,GAAG,MAAM,cAAc,CAAC;AACpC,OAAO,KAAK,EAAE,eAAe,EAAS,MAAM,uBAAuB,CAAC;AAwBpE,KAAK,eAAe,GAAG;IACnB,KAAK,EAAE,GAAG,CAAC,gBAAgB,CAAC;IAC5B,SAAS,EAAE,MAAM,GAAG,CAAC,aAAa,GAAG,IAAI,CAAC;IAC1C,IAAI,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,CAAC;CAC1C,CAAC;AAMF,iBAAS,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,MAAM,GAAG,IAAI,GAAG,MAAM,GAAG,IAAI,CAI1D;AAoLD,iBAAS,qBAAqB,IAAI,eAAe,CAmBhD;AAED,OAAO,EAAE,qBAAqB,EAAE,KAAK,EAAE,KAAK,eAAe,EAAE,CAAC"}
|
|
@@ -52,7 +52,7 @@ function levelFor(state, key) {
|
|
|
52
52
|
function refreshExpandable(current, level) {
|
|
53
53
|
const next = new Map();
|
|
54
54
|
for (const [index, id] of level.ids.entries()) {
|
|
55
|
-
const isWanted = level.
|
|
55
|
+
const isWanted = level.expandableFlags[index] ?? false;
|
|
56
56
|
const previous = current.canExpand.get(id);
|
|
57
57
|
next.set(id, isWanted);
|
|
58
58
|
if (previous !== undefined && previous !== isWanted) {
|
|
@@ -78,15 +78,12 @@ function dropUnvisited(state, visited) {
|
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
|
-
// Items keep their identity by id, so a collection that shrinks keeps its objects cached: filtering a long
|
|
82
|
-
// list down and back up reuses them instead of rebuilding one GObject per row. Incremental filters shrink and
|
|
83
|
-
// grow the list many times per keystroke, so the cache is only swept once it passes an absolute bound.
|
|
84
81
|
function pruneObjects(state, index) {
|
|
85
82
|
if (state.objects.size <= OBJECT_CACHE_MAX) {
|
|
86
83
|
return;
|
|
87
84
|
}
|
|
88
85
|
for (const id of state.objects.keys()) {
|
|
89
|
-
if (!index.
|
|
86
|
+
if (!index.hasId(id)) {
|
|
90
87
|
state.objects.delete(id);
|
|
91
88
|
}
|
|
92
89
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"collection-model.js","sourceRoot":"","sources":["../../src/internal/collection-model.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,cAAc,CAAC;AACpC,OAAO,KAAK,OAAO,MAAM,kBAAkB,CAAC;AAC5C,OAAO,KAAK,GAA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AAE,IAAI,GAAG,EAAE;QAClB,MAAM,EAAE,IAAI,GAAG,EAAE;QACjB,IAAI,EAAE,IAAI;KACb,CAAC;IAEF,OAAO;QACH,KAAK,EAAE,KAAK,CAAC,KAAK;QAClB,SAAS,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,IAAI;QAC3B,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE;YACZ,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QAC5B,CAAC;KACJ,CAAC;AACN,CAAC;AAED,OAAO,EAAE,qBAAqB,EAAE,KAAK,EAAwB,CAAC","sourcesContent":["import * as Gio from \"@gtkx/gi/gio\";\nimport * as GObject from \"@gtkx/gi/gobject\";\nimport * as Gtk from \"@gtkx/gi/gtk\";\nimport type { CollectionIndex, Level } from \"./collection-index.js\";\nimport { childLevelKey, ROOT_LEVEL_KEY } from \"./collection-index.js\";\n\ntype LevelState = {\n store: Gio.ListStore;\n ids: string[];\n canExpand: Map<string, boolean>;\n};\n\ntype SpliceRange = {\n start: number;\n removed: number;\n added: string[];\n};\n\ntype ModelState = {\n root: Gio.ListStore;\n rootModels: GObject.Object[];\n model: Gtk.FlattenListModel;\n objects: Map<string, Gtk.StringObject>;\n levels: Map<string, LevelState>;\n tree: Gtk.TreeListModel | null;\n};\n\ntype CollectionModel = {\n model: Gtk.FlattenListModel;\n treeModel: () => Gtk.TreeListModel | null;\n sync: (index: CollectionIndex) => void;\n};\n\nconst OBJECT_CACHE_MAX = 1 << 20;\n\nconst newStore = (): Gio.ListStore => new Gio.ListStore({ itemType: GObject.TYPE_OBJECT });\n\nfunction getId(value: GObject.Object | null): string | null {\n const item = value instanceof Gtk.TreeListRow ? value.getItem() : value;\n\n return item instanceof Gtk.StringObject ? item.getString() : null;\n}\n\nfunction objectFor(state: ModelState, id: string): Gtk.StringObject {\n const existing = state.objects.get(id);\n\n if (existing !== undefined) {\n return existing;\n }\n\n const created = Gtk.StringObject.new(id);\n state.objects.set(id, created);\n\n return created;\n}\n\nfunction commonPrefix(previous: string[], next: string[], max: number): number {\n let count = 0;\n\n while (count < max && previous[count] === next[count]) {\n count += 1;\n }\n\n return count;\n}\n\nfunction commonSuffix(previous: string[], next: string[], max: number): number {\n let count = 0;\n\n while (count < max && previous[previous.length - 1 - count] === next[next.length - 1 - count]) {\n count += 1;\n }\n\n return count;\n}\n\nfunction spliceRange(previous: string[], next: string[]): SpliceRange | null {\n const shared = Math.min(previous.length, next.length);\n const start = commonPrefix(previous, next, shared);\n\n if (start === previous.length && start === next.length) {\n return null;\n }\n\n const tail = commonSuffix(previous, next, shared - start);\n\n return { start, removed: previous.length - start - tail, added: next.slice(start, next.length - tail) };\n}\n\nfunction levelFor(state: ModelState, key: string): LevelState {\n const existing = state.levels.get(key);\n\n if (existing !== undefined) {\n return existing;\n }\n\n const created: LevelState = { store: newStore(), ids: [], canExpand: new Map() };\n state.levels.set(key, created);\n\n return created;\n}\n\nfunction refreshExpandable(current: LevelState, level: Level): void {\n const next: Map<string, boolean> = new Map();\n\n for (const [index, id] of level.ids.entries()) {\n const isWanted = level.isExpandable[index] ?? false;\n const previous = current.canExpand.get(id);\n next.set(id, isWanted);\n\n if (previous !== undefined && previous !== isWanted) {\n current.store.itemsChanged(index, 1, 1);\n }\n }\n\n current.canExpand = next;\n}\n\nfunction didSpliceLevel(state: ModelState, level: Level): boolean {\n const current = levelFor(state, level.key);\n const range = spliceRange(current.ids, level.ids);\n\n if (range === null) {\n return false;\n }\n\n current.store.splice(range.start, range.removed, range.added.map((id) => objectFor(state, id)));\n current.ids = [...level.ids];\n\n return true;\n}\n\nfunction dropUnvisited(state: ModelState, visited: Set<string>): void {\n for (const key of state.levels.keys()) {\n if (!visited.has(key)) {\n state.levels.delete(key);\n }\n }\n}\n\n// Items keep their identity by id, so a collection that shrinks keeps its objects cached: filtering a long\n// list down and back up reuses them instead of rebuilding one GObject per row. Incremental filters shrink and\n// grow the list many times per keystroke, so the cache is only swept once it passes an absolute bound.\nfunction pruneObjects(state: ModelState, index: CollectionIndex): void {\n if (state.objects.size <= OBJECT_CACHE_MAX) {\n return;\n }\n\n for (const id of state.objects.keys()) {\n if (!index.has(id)) {\n state.objects.delete(id);\n }\n }\n}\n\nfunction childStoreFor(state: ModelState, object: GObject.Object): Gio.ListStore | null {\n const id = getId(object);\n\n if (id === null) {\n return null;\n }\n\n return state.levels.get(childLevelKey(id))?.store ?? null;\n}\n\nfunction ensureTree(state: ModelState): Gtk.TreeListModel {\n state.tree ??= Gtk.TreeListModel.new(levelFor(state, ROOT_LEVEL_KEY).store, false, false, (object) =>\n childStoreFor(state, object));\n\n return state.tree;\n}\n\nfunction desiredRootModels(state: ModelState, index: CollectionIndex): GObject.Object[] {\n if (index.isTree) {\n return [ensureTree(state)];\n }\n\n return index.groups.map((level) => levelFor(state, level.key).store);\n}\n\nfunction hasSameModels(previous: GObject.Object[], next: GObject.Object[]): boolean {\n return previous.length === next.length && next.every((model, index) => previous[index] === model);\n}\n\nfunction syncRoot(state: ModelState, index: CollectionIndex): void {\n const next = desiredRootModels(state, index);\n\n if (hasSameModels(state.rootModels, next)) {\n return;\n }\n\n state.root.splice(0, state.rootModels.length, next);\n state.rootModels = next;\n}\n\nfunction syncModel(state: ModelState, index: CollectionIndex): void {\n const levels = [...index.groups, ...index.children.values()];\n const visited: Set<string> = new Set();\n let didChange = false;\n\n for (const level of levels) {\n didChange = didSpliceLevel(state, level) || didChange;\n visited.add(level.key);\n }\n\n if (didChange || visited.size !== state.levels.size) {\n dropUnvisited(state, visited);\n pruneObjects(state, index);\n }\n\n syncRoot(state, index);\n refreshExpandableLevels(state, index, levels);\n}\n\nfunction refreshExpandableLevels(state: ModelState, index: CollectionIndex, levels: Level[]): void {\n if (!index.isTree) {\n return;\n }\n\n for (const level of levels) {\n refreshExpandable(levelFor(state, level.key), level);\n }\n}\n\nfunction createCollectionModel(): CollectionModel {\n const root = newStore();\n\n const state: ModelState = {\n root,\n rootModels: [],\n model: Gtk.FlattenListModel.new(root),\n objects: new Map(),\n levels: new Map(),\n tree: null,\n };\n\n return {\n model: state.model,\n treeModel: () => state.tree,\n sync: (index) => {\n syncModel(state, index);\n },\n };\n}\n\nexport { createCollectionModel, getId, type CollectionModel };\n"]}
|
|
1
|
+
{"version":3,"file":"collection-model.js","sourceRoot":"","sources":["../../src/internal/collection-model.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,cAAc,CAAC;AACpC,OAAO,KAAK,OAAO,MAAM,kBAAkB,CAAC;AAC5C,OAAO,KAAK,GAAG,MAAM,cAAc,CAAC;AAEpC,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AA6BtE,MAAM,gBAAgB,GAAG,CAAC,IAAI,EAAE,CAAC;AAEjC,MAAM,QAAQ,GAAG,GAAkB,EAAE,CAAC,IAAI,GAAG,CAAC,SAAS,CAAC,EAAE,QAAQ,EAAE,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;AAE3F,SAAS,KAAK,CAAC,KAA4B;IACvC,MAAM,IAAI,GAAG,KAAK,YAAY,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;IAExE,OAAO,IAAI,YAAY,GAAG,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;AACtE,CAAC;AAED,SAAS,SAAS,CAAC,KAAiB,EAAE,EAAU;IAC5C,MAAM,QAAQ,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAEvC,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;QACzB,OAAO,QAAQ,CAAC;IACpB,CAAC;IAED,MAAM,OAAO,GAAG,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IACzC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC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AAE,KAAK,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC;IACzD,CAAC;AACL,CAAC;AAED,SAAS,qBAAqB;IAC1B,MAAM,IAAI,GAAG,QAAQ,EAAE,CAAC;IAExB,MAAM,KAAK,GAAe;QACtB,IAAI;QACJ,UAAU,EAAE,EAAE;QACd,KAAK,EAAE,GAAG,CAAC,gBAAgB,CAAC,GAAG,CAAC,IAAI,CAAC;QACrC,OAAO,EAAE,IAAI,GAAG,EAAE;QAClB,MAAM,EAAE,IAAI,GAAG,EAAE;QACjB,IAAI,EAAE,IAAI;KACb,CAAC;IAEF,OAAO;QACH,KAAK,EAAE,KAAK,CAAC,KAAK;QAClB,SAAS,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,IAAI;QAC3B,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE;YACZ,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QAC5B,CAAC;KACJ,CAAC;AACN,CAAC;AAED,OAAO,EAAE,qBAAqB,EAAE,KAAK,EAAwB,CAAC","sourcesContent":["import * as Gio from \"@gtkx/gi/gio\";\nimport * as GObject from \"@gtkx/gi/gobject\";\nimport * as Gtk from \"@gtkx/gi/gtk\";\nimport type { CollectionIndex, Level } from \"./collection-index.js\";\nimport { childLevelKey, ROOT_LEVEL_KEY } from \"./collection-index.js\";\n\ntype LevelState = {\n store: Gio.ListStore;\n ids: string[];\n canExpand: Map<string, boolean>;\n};\n\ntype SpliceRange = {\n start: number;\n removed: number;\n added: string[];\n};\n\ntype ModelState = {\n root: Gio.ListStore;\n rootModels: GObject.Object[];\n model: Gtk.FlattenListModel;\n objects: Map<string, Gtk.StringObject>;\n levels: Map<string, LevelState>;\n tree: Gtk.TreeListModel | null;\n};\n\ntype CollectionModel = {\n model: Gtk.FlattenListModel;\n treeModel: () => Gtk.TreeListModel | null;\n sync: (index: CollectionIndex) => void;\n};\n\nconst OBJECT_CACHE_MAX = 1 << 20;\n\nconst newStore = (): Gio.ListStore => new Gio.ListStore({ itemType: GObject.TYPE_OBJECT });\n\nfunction getId(value: GObject.Object | null): string | null {\n const item = value instanceof Gtk.TreeListRow ? value.getItem() : value;\n\n return item instanceof Gtk.StringObject ? item.getString() : null;\n}\n\nfunction objectFor(state: ModelState, id: string): Gtk.StringObject {\n const existing = state.objects.get(id);\n\n if (existing !== undefined) {\n return existing;\n }\n\n const created = Gtk.StringObject.new(id);\n state.objects.set(id, created);\n\n return created;\n}\n\nfunction commonPrefix(previous: string[], next: string[], max: number): number {\n let count = 0;\n\n while (count < max && previous[count] === next[count]) {\n count += 1;\n }\n\n return count;\n}\n\nfunction commonSuffix(previous: string[], next: string[], max: number): number {\n let count = 0;\n\n while (count < max && previous[previous.length - 1 - count] === next[next.length - 1 - count]) {\n count += 1;\n }\n\n return count;\n}\n\nfunction spliceRange(previous: string[], next: string[]): SpliceRange | null {\n const shared = Math.min(previous.length, next.length);\n const start = commonPrefix(previous, next, shared);\n\n if (start === previous.length && start === next.length) {\n return null;\n }\n\n const tail = commonSuffix(previous, next, shared - start);\n\n return { start, removed: previous.length - start - tail, added: next.slice(start, next.length - tail) };\n}\n\nfunction levelFor(state: ModelState, key: string): LevelState {\n const existing = state.levels.get(key);\n\n if (existing !== undefined) {\n return existing;\n }\n\n const created: LevelState = { store: newStore(), ids: [], canExpand: new Map() };\n state.levels.set(key, created);\n\n return created;\n}\n\nfunction refreshExpandable(current: LevelState, level: Level): void {\n const next: Map<string, boolean> = new Map();\n\n for (const [index, id] of level.ids.entries()) {\n const isWanted = level.expandableFlags[index] ?? false;\n const previous = current.canExpand.get(id);\n next.set(id, isWanted);\n\n if (previous !== undefined && previous !== isWanted) {\n current.store.itemsChanged(index, 1, 1);\n }\n }\n\n current.canExpand = next;\n}\n\nfunction didSpliceLevel(state: ModelState, level: Level): boolean {\n const current = levelFor(state, level.key);\n const range = spliceRange(current.ids, level.ids);\n\n if (range === null) {\n return false;\n }\n\n current.store.splice(range.start, range.removed, range.added.map((id) => objectFor(state, id)));\n current.ids = [...level.ids];\n\n return true;\n}\n\nfunction dropUnvisited(state: ModelState, visited: Set<string>): void {\n for (const key of state.levels.keys()) {\n if (!visited.has(key)) {\n state.levels.delete(key);\n }\n }\n}\n\nfunction pruneObjects(state: ModelState, index: CollectionIndex): void {\n if (state.objects.size <= OBJECT_CACHE_MAX) {\n return;\n }\n\n for (const id of state.objects.keys()) {\n if (!index.hasId(id)) {\n state.objects.delete(id);\n }\n }\n}\n\nfunction childStoreFor(state: ModelState, object: GObject.Object): Gio.ListStore | null {\n const id = getId(object);\n\n if (id === null) {\n return null;\n }\n\n return state.levels.get(childLevelKey(id))?.store ?? null;\n}\n\nfunction ensureTree(state: ModelState): Gtk.TreeListModel {\n state.tree ??= Gtk.TreeListModel.new(levelFor(state, ROOT_LEVEL_KEY).store, false, false, (object) =>\n childStoreFor(state, object));\n\n return state.tree;\n}\n\nfunction desiredRootModels(state: ModelState, index: CollectionIndex): GObject.Object[] {\n if (index.isTree) {\n return [ensureTree(state)];\n }\n\n return index.groups.map((level) => levelFor(state, level.key).store);\n}\n\nfunction hasSameModels(previous: GObject.Object[], next: GObject.Object[]): boolean {\n return previous.length === next.length && next.every((model, index) => previous[index] === model);\n}\n\nfunction syncRoot(state: ModelState, index: CollectionIndex): void {\n const next = desiredRootModels(state, index);\n\n if (hasSameModels(state.rootModels, next)) {\n return;\n }\n\n state.root.splice(0, state.rootModels.length, next);\n state.rootModels = next;\n}\n\nfunction syncModel(state: ModelState, index: CollectionIndex): void {\n const levels = [...index.groups, ...index.children.values()];\n const visited: Set<string> = new Set();\n let didChange = false;\n\n for (const level of levels) {\n didChange = didSpliceLevel(state, level) || didChange;\n visited.add(level.key);\n }\n\n if (didChange || visited.size !== state.levels.size) {\n dropUnvisited(state, visited);\n pruneObjects(state, index);\n }\n\n syncRoot(state, index);\n refreshExpandableLevels(state, index, levels);\n}\n\nfunction refreshExpandableLevels(state: ModelState, index: CollectionIndex, levels: Level[]): void {\n if (!index.isTree) {\n return;\n }\n\n for (const level of levels) {\n refreshExpandable(levelFor(state, level.key), level);\n }\n}\n\nfunction createCollectionModel(): CollectionModel {\n const root = newStore();\n\n const state: ModelState = {\n root,\n rootModels: [],\n model: Gtk.FlattenListModel.new(root),\n objects: new Map(),\n levels: new Map(),\n tree: null,\n };\n\n return {\n model: state.model,\n treeModel: () => state.tree,\n sync: (index) => {\n syncModel(state, index);\n },\n };\n}\n\nexport { createCollectionModel, getId, type CollectionModel };\n"]}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type * as Gtk from "@gtkx/gi/gtk";
|
|
2
|
-
import type {
|
|
2
|
+
import type { ListItem } from "../types.js";
|
|
3
3
|
import type { CollectionIndex } from "./collection-index.js";
|
|
4
4
|
import type { CollectionModel } from "./collection-model.js";
|
|
5
5
|
type Collection = {
|
|
6
6
|
isTree: boolean;
|
|
7
7
|
model: Gtk.FlattenListModel;
|
|
8
8
|
treeModel: () => Gtk.TreeListModel | null;
|
|
9
|
-
itemFor: (id: string) =>
|
|
9
|
+
itemFor: (id: string) => ListItem | undefined;
|
|
10
10
|
sectionFor: (id: string) => unknown;
|
|
11
11
|
idAt: (position: number) => string | null;
|
|
12
12
|
positionFor: (id: string) => number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"collection.d.ts","sourceRoot":"","sources":["../../src/internal/collection.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,GAAG,MAAM,cAAc,CAAC;AACzC,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"collection.d.ts","sourceRoot":"","sources":["../../src/internal/collection.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,GAAG,MAAM,cAAc,CAAC;AACzC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAG7D,KA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,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 {
|
|
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,IA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AAI,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;AACzD,CAAC;AAED,SAAS,WAAW,CAAC,GAAoB,EAAE,KAAsB,EAAE,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 { ListItem } 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) => ListItem | 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, eachRow, type Collection };\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type * as GObject from "@gtkx/gi/gobject";
|
|
2
|
+
import type { ElementType, ReactNode, Ref } from "react";
|
|
3
|
+
import type { DropDownOwnProps } from "../types.js";
|
|
4
|
+
type SelectableWidget = GObject.Object & {
|
|
5
|
+
getSelected: () => number;
|
|
6
|
+
selected: number;
|
|
7
|
+
};
|
|
8
|
+
type DropDownBaseProps = DropDownOwnProps<unknown, unknown> & {
|
|
9
|
+
onNotifySelected?: ((value: number | null, self: SelectableWidget) => void) | null | undefined;
|
|
10
|
+
ref?: Ref<SelectableWidget | null> | undefined;
|
|
11
|
+
} & Record<string, unknown>;
|
|
12
|
+
declare function DropDownBase(props: DropDownBaseProps & {
|
|
13
|
+
component: ElementType;
|
|
14
|
+
}): ReactNode;
|
|
15
|
+
export { DropDownBase, type DropDownBaseProps };
|
|
16
|
+
//# sourceMappingURL=drop-down.d.ts.map
|
|
@@ -0,0 +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,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;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"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { GtkLabel, GtkSignalListItemFactory } from "@gtkx/jsx/gtk";
|
|
3
|
+
import { applyWrite } from "@gtkx/react/internal";
|
|
4
|
+
import { omit } from "@gtkx/utils";
|
|
5
|
+
import { useEffectEvent, useLayoutEffect, useRef } from "react";
|
|
6
|
+
import { HeaderPortals, ItemPortals, useHeaderCells, useItemCells } from "./cells.js";
|
|
7
|
+
import { useCollectionData } from "./use-collection.js";
|
|
8
|
+
import { useWidgetRef } from "./use-widget-ref.js";
|
|
9
|
+
const DROP_DOWN_PROPS = [
|
|
10
|
+
"component",
|
|
11
|
+
"items",
|
|
12
|
+
"sections",
|
|
13
|
+
"selectedId",
|
|
14
|
+
"onSelectionChanged",
|
|
15
|
+
"onNotifySelected",
|
|
16
|
+
"renderItem",
|
|
17
|
+
"renderListItem",
|
|
18
|
+
"renderHeader",
|
|
19
|
+
"ref",
|
|
20
|
+
];
|
|
21
|
+
const describeValue = (value) => {
|
|
22
|
+
if (typeof value === "string") {
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
return JSON.stringify(value);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return String(value);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
const defaultItemContent = (value) => value == null ? null : _jsx(GtkLabel, { children: describeValue(value) });
|
|
33
|
+
const defaultRenderItem = ({ item }) => defaultItemContent(item);
|
|
34
|
+
const faceRenderer = (props) => props.renderItem ?? defaultRenderItem;
|
|
35
|
+
const listRenderer = (props) => props.renderListItem ?? props.renderItem ?? defaultRenderItem;
|
|
36
|
+
const resolvePosition = (widget, collection, selectedId) => {
|
|
37
|
+
const requested = selectedId == null ? -1 : collection.positionFor(selectedId);
|
|
38
|
+
return requested >= 0 ? requested : widget.getSelected();
|
|
39
|
+
};
|
|
40
|
+
const updateKnownSelection = (tracker, effectiveId, selectedId) => {
|
|
41
|
+
const { known, onSelectionChanged } = tracker;
|
|
42
|
+
const expectedId = selectedId ?? known.current;
|
|
43
|
+
const isNew = effectiveId !== known.current;
|
|
44
|
+
known.current = effectiveId;
|
|
45
|
+
if (expectedId !== null && effectiveId !== expectedId && isNew) {
|
|
46
|
+
onSelectionChanged?.(effectiveId);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const reportKnownSelection = (tracker, id) => {
|
|
50
|
+
const { known, onSelectionChanged } = tracker;
|
|
51
|
+
if (id === null || id === known.current) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
known.current = id;
|
|
55
|
+
onSelectionChanged?.(id);
|
|
56
|
+
};
|
|
57
|
+
const reportSelectedNotify = (options, tracker) => {
|
|
58
|
+
const { widget, collection } = options;
|
|
59
|
+
if (widget === null) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
reportKnownSelection(tracker, collection.idAt(widget.getSelected()));
|
|
63
|
+
};
|
|
64
|
+
const applySelectedPosition = (widget, position) => {
|
|
65
|
+
applyWrite(() => {
|
|
66
|
+
widget.selected = position;
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
const useDropDownSelection = (options) => {
|
|
70
|
+
const { widget, collection } = options;
|
|
71
|
+
const { selectedId } = options.props;
|
|
72
|
+
const known = useRef(null);
|
|
73
|
+
const syncKnownSelection = useEffectEvent((position) => {
|
|
74
|
+
const effectiveId = collection.idAt(position);
|
|
75
|
+
if (effectiveId === null) {
|
|
76
|
+
known.current = null;
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const { onSelectionChanged } = options.props;
|
|
80
|
+
updateKnownSelection({ known, onSelectionChanged }, effectiveId, selectedId);
|
|
81
|
+
});
|
|
82
|
+
useLayoutEffect(() => {
|
|
83
|
+
if (widget === null) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const position = resolvePosition(widget, collection, selectedId);
|
|
87
|
+
applySelectedPosition(widget, position);
|
|
88
|
+
syncKnownSelection(position);
|
|
89
|
+
}, [widget, collection, selectedId]);
|
|
90
|
+
return (value, self) => {
|
|
91
|
+
reportSelectedNotify(options, { known, onSelectionChanged: options.props.onSelectionChanged });
|
|
92
|
+
options.props.onNotifySelected?.(value, self);
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
function DropDownBase(props) {
|
|
96
|
+
const { component: Component, items, sections, renderListItem, renderHeader, ref } = props;
|
|
97
|
+
const rest = omit(props, DROP_DOWN_PROPS);
|
|
98
|
+
const [widget, refCallback] = useWidgetRef(ref);
|
|
99
|
+
const collection = useCollectionData({ items, sections });
|
|
100
|
+
const faceCells = useItemCells({ width: -1, height: -1 });
|
|
101
|
+
const listCells = useItemCells({ width: -1, height: -1 });
|
|
102
|
+
const headerCells = useHeaderCells({ width: -1, height: -1 });
|
|
103
|
+
const handleNotifySelected = useDropDownSelection({ widget, collection, props });
|
|
104
|
+
const hasHeader = typeof renderHeader === "function";
|
|
105
|
+
return (_jsxs(_Fragment, { children: [_jsx(Component, { ref: refCallback, model: collection.model, factory: _jsx(GtkSignalListItemFactory, { ...faceCells.handlers }), ...(renderListItem != null && {
|
|
106
|
+
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 })] }));
|
|
108
|
+
}
|
|
109
|
+
export { DropDownBase };
|
|
110
|
+
//# sourceMappingURL=drop-down.js.map
|
|
@@ -0,0 +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,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AI,EACrF,cAAc,IAAI,IAAI,IAAI,CACvB,KAAC,WAAW,IAAC,KAAK,EAAE,SAAS,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, ListItemRenderArgs, ListItemRenderer } 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 }: ListItemRenderArgs<unknown>): ReactNode => defaultItemContent(item);\nconst faceRenderer = (props: DropDownBaseProps): ListItemRenderer<never> => props.renderItem ?? defaultRenderItem;\n\nconst listRenderer = (props: DropDownBaseProps): ListItemRenderer<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"]}
|
|
@@ -4,6 +4,6 @@ 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;
|
|
7
|
+
declare function useExpansion(options: ExpansionOptions): () => void;
|
|
8
8
|
export { useExpansion, type ExpansionOptions };
|
|
9
9
|
//# sourceMappingURL=expansion.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"expansion.d.ts","sourceRoot":"","sources":["../../src/internal/expansion.ts"],"names":[],"mappings":"
|
|
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,4 +1,3 @@
|
|
|
1
|
-
import { useSignal } from "@gtkx/react";
|
|
2
1
|
import { useEffectEvent, useLayoutEffect, useRef, useState } from "react";
|
|
3
2
|
import { eachRow } from "./collection.js";
|
|
4
3
|
function newLastExpansion() {
|
|
@@ -64,12 +63,12 @@ function useExpansion(options) {
|
|
|
64
63
|
const report = useEffectEvent(() => {
|
|
65
64
|
reportWhenIdle(collection, last, expanding, onExpandedChange);
|
|
66
65
|
});
|
|
67
|
-
useSignal(collection.isTree ? collection.model : null, "items-changed", () => {
|
|
68
|
-
reportWhenIdle(collection, last, expanding, onExpandedChange);
|
|
69
|
-
});
|
|
70
66
|
useLayoutEffect(() => {
|
|
71
67
|
runControlledExpansion(collection, expandedIds, expanding, report);
|
|
72
68
|
}, [collection, expandedIds]);
|
|
69
|
+
return () => {
|
|
70
|
+
reportWhenIdle(collection, last, expanding, onExpandedChange);
|
|
71
|
+
};
|
|
73
72
|
}
|
|
74
73
|
export { useExpansion };
|
|
75
74
|
//# sourceMappingURL=expansion.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"expansion.js","sourceRoot":"","sources":["../../src/internal/expansion.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,
|
|
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,SAAS,EAAE,CAAC;QACxD,GAAG,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;IAC/B,CAAC;AACL,CAAC;AAED,SAAS,cAAc,CAAC,IAAuB,EAAE,WAAqB;IAClE,MAAM,MAAM,GAAG,IAAI,GAAG,CA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S,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"]}
|
|
@@ -6,6 +6,7 @@ type SelectionOptions = {
|
|
|
6
6
|
selectedIds?: string[] | null | undefined;
|
|
7
7
|
onSelectionChanged?: ((ids: string[]) => void) | null | undefined;
|
|
8
8
|
selectionMode?: Gtk.SelectionMode | null | undefined;
|
|
9
|
+
onItemsChanged: () => void;
|
|
9
10
|
};
|
|
10
11
|
declare function useSelection(options: SelectionOptions): ReactElement;
|
|
11
12
|
export { useSelection, type SelectionOptions };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"selection.d.ts","sourceRoot":"","sources":["../../src/internal/selection.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAC1C,OAAO,KAAK,GAAG,MAAM,cAAc,CAAC;AAGpC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAElD,KAAK,gBAAgB,GAAG;IACpB,UAAU,EAAE,UAAU,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,EAAE,GAAG,IAAI,GAAG,SAAS,CAAC;IAC1C,kBAAkB,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC,GAAG,IAAI,GAAG,SAAS,CAAC;IAClE,aAAa,CAAC,EAAE,GAAG,CAAC,aAAa,GAAG,IAAI,GAAG,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"selection.d.ts","sourceRoot":"","sources":["../../src/internal/selection.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAC1C,OAAO,KAAK,GAAG,MAAM,cAAc,CAAC;AAGpC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAElD,KAAK,gBAAgB,GAAG;IACpB,UAAU,EAAE,UAAU,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,EAAE,GAAG,IAAI,GAAG,SAAS,CAAC;IAC1C,kBAAkB,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC,GAAG,IAAI,GAAG,SAAS,CAAC;IAClE,aAAa,CAAC,EAAE,GAAG,CAAC,aAAa,GAAG,IAAI,GAAG,SAAS,CAAC;IACrD,cAAc,EAAE,MAAM,IAAI,CAAC;CAC9B,CAAC;AA4HF,iBAAS,YAAY,CAAC,OAAO,EAAE,gBAAgB,GAAG,YAAY,CAsB7D;AAED,OAAO,EAAE,YAAY,EAAE,KAAK,gBAAgB,EAAE,CAAC"}
|