From 6e3709c73b2333714a85b10c2f05362f80a3ecad Mon Sep 17 00:00:00 2001 From: t8y2 Date: Sat, 26 Sep 2026 02:26:53 +0800 Subject: [PATCH] fix(sidebar): stop sticky headers at connection boundaries Closes #9996 --- .../src/components/sidebar/ConnectionTree.vue | 62 +++++++++---------- .../src/components/sidebar/TreeItem.vue | 18 +----- .../composables/__tests__/useFlatTree.spec.ts | 4 ++ apps/desktop/src/composables/useFlatTree.ts | 7 +++ packages/app-tests/useFlatTree.test.ts | 3 + 5 files changed, 46 insertions(+), 48 deletions(-) diff --git a/apps/desktop/src/components/sidebar/ConnectionTree.vue b/apps/desktop/src/components/sidebar/ConnectionTree.vue index c6f682c6f4..70d0e2f8fc 100644 --- a/apps/desktop/src/components/sidebar/ConnectionTree.vue +++ b/apps/desktop/src/components/sidebar/ConnectionTree.vue @@ -844,16 +844,6 @@ function readExpandedSidebarSchemas(): Array<{ id: string; label: string }> { return expanded; } -// The plain (non-virtualized) renderer uses the same container selection as the -// virtual sticky overlay: database containers take precedence, while schema -// containers stick only in trees without a database-level container. -function isPlainStickyContainerNode(index: number): boolean { - // Mirror the virtual branch's sticky overlay: both suppress sticky headers - // while a search filter is active so filtered rows don't pin at the top. - if (isTreeSearchFiltering.value) return false; - return flatTreeIndex.value.stickyContainerIndexByIndex[index] === index; -} - const sidebarLayoutMonitor = createSidebarLayoutMonitor({ readContext: () => ({ flatNodeCount: flatNodes.value.length, @@ -1058,10 +1048,9 @@ watch( ); // --- Sticky database header --- -// RecycleScroller positions each row absolutely, so CSS `position: sticky` on -// a database row can't work. Instead we overlay a pinned row from this parent -// component, tracking scroll offset to find the topmost visible database-level -// ancestor. The overlay reuses , so collapse/expand comes for free. +// Both renderers use the same overlay instead of CSS `position: sticky` on +// individual rows. A shared overlay can be pushed out by the next connection +// boundary, while native sticky rows would cover that non-sticky connection. const stickyScrollTop = ref(0); const sidebarScrollMetrics = ref({ scrollTop: 0, scrollLeft: 0, clientHeight: 0, clientWidth: 0, scrollHeight: 0, scrollWidth: 0 }); const isScrollingSidebar = ref(false); @@ -1080,7 +1069,7 @@ function updateSidebarScrollMetrics() { return; } - if (useVirtualTree.value) stickyScrollTop.value = scroller.scrollTop; + stickyScrollTop.value = scroller.scrollTop; sidebarScrollMetrics.value = { scrollTop: scroller.scrollTop, scrollLeft: scroller.scrollLeft, @@ -1141,28 +1130,30 @@ watch( { flush: "post" }, ); -const stickyNode = computed(() => { - if (!useVirtualTree.value || isTreeSearchFiltering.value) return null; +const stickyContainerIndex = computed(() => { + if (isTreeSearchFiltering.value) return -1; const nodes = flatNodes.value; const len = nodes.length; - if (len === 0) return null; + if (len === 0) return -1; const topIndex = Math.min(Math.floor(stickyScrollTop.value / SIDEBAR_TREE_ROW_HEIGHT), len - 1); const containerIndex = flatTreeIndex.value.stickyContainerIndexByIndex[topIndex] ?? -1; - if (containerIndex < 0) return null; - return stickyScrollTop.value > containerIndex * SIDEBAR_TREE_ROW_HEIGHT ? nodes[containerIndex] : null; + if (containerIndex < 0) return -1; + return stickyScrollTop.value > containerIndex * SIDEBAR_TREE_ROW_HEIGHT ? containerIndex : -1; }); +const stickyNode = computed(() => flatNodes.value[stickyContainerIndex.value] ?? null); + const stickyHeaderStyle = computed(() => { - const node = stickyNode.value; - if (!node) return {}; - const currentIndex = flatTreeIndex.value.flatNodeIndexById.get(node.id) ?? -1; + const currentIndex = stickyContainerIndex.value; if (currentIndex < 0) return {}; - // The next peer index is precomputed with the flat-tree snapshot so scrolling - // never scans the remaining tree. Connection boundaries reset the lookup. - const nextDatabaseIndex = SCHEMA_LEVEL_TYPES.has(node.type) ? flatTreeIndex.value.nextSchemaContainerIndexByIndex[currentIndex] : flatTreeIndex.value.nextDatabaseContainerIndexByIndex[currentIndex]; - if (nextDatabaseIndex < 0) return {}; - const distanceToNext = nextDatabaseIndex * SIDEBAR_TREE_ROW_HEIGHT - stickyScrollTop.value; + const node = flatNodes.value[currentIndex]; + if (!node) return {}; + const nextContainerIndex = SCHEMA_LEVEL_TYPES.has(node.type) ? flatTreeIndex.value.nextSchemaContainerIndexByIndex[currentIndex] : flatTreeIndex.value.nextDatabaseContainerIndexByIndex[currentIndex]; + const nextBoundaryIndex = flatTreeIndex.value.nextBoundaryIndexByIndex[currentIndex] ?? -1; + const nextCollisionIndex = nextContainerIndex < 0 ? nextBoundaryIndex : nextBoundaryIndex < 0 ? nextContainerIndex : Math.min(nextContainerIndex, nextBoundaryIndex); + if (nextCollisionIndex < 0) return {}; + const distanceToNext = nextCollisionIndex * SIDEBAR_TREE_ROW_HEIGHT - stickyScrollTop.value; if (distanceToNext >= SIDEBAR_TREE_ROW_HEIGHT) return {}; return { transform: `translateY(${Math.min(0, distanceToNext - SIDEBAR_TREE_ROW_HEIGHT)}px)`, @@ -1495,7 +1486,7 @@ async function flashSidebarNode(nodeId: string) { function topOcclusionHeightForSidebarNode(nodeId: string): number { const sticky = stickyNode.value; - if (!useVirtualTree.value || !sticky || sticky.id === nodeId) return 0; + if (!sticky || sticky.id === nodeId) return 0; return SIDEBAR_TREE_ROW_HEIGHT; } @@ -2712,7 +2703,7 @@ defineExpose({ focusSearch, createNewGroup, collapseAllTreeNodes, locateTabInSid +
+ +
(); const emit = defineEmits<{ @@ -1610,7 +1606,7 @@ function onKeydown(event: KeyboardEvent) {
-
+
{ expect(index.nextSchemaContainerIndexByIndex[2]).toBe(4); expect(index.nextSchemaContainerIndexByIndex[4]).toBe(-1); expect(index.nextSchemaContainerIndexByIndex[9]).toBe(-1); + expect(index.nextBoundaryIndexByIndex[1]).toBe(8); + expect(index.nextBoundaryIndexByIndex[6]).toBe(8); + expect(index.nextBoundaryIndexByIndex[9]).toBe(-1); }); it("keeps indexes isolated across connections without database containers", () => { @@ -65,6 +68,7 @@ describe("createFlatTreeIndex", () => { expect(index.stickyContainerIndexByIndex[2]).toBe(1); expect(index.stickyContainerIndexByIndex[4]).toBe(-1); expect(index.nextSchemaContainerIndexByIndex[1]).toBe(-1); + expect(index.nextBoundaryIndexByIndex[1]).toBe(3); expect(index.flatNodeIndexById.get("table-b")).toBe(4); }); diff --git a/apps/desktop/src/composables/useFlatTree.ts b/apps/desktop/src/composables/useFlatTree.ts index ecb776c7b0..f2e7087ff1 100644 --- a/apps/desktop/src/composables/useFlatTree.ts +++ b/apps/desktop/src/composables/useFlatTree.ts @@ -42,6 +42,7 @@ export interface FlatTreeIndex { stickyContainerIndexByIndex: Int32Array; nextDatabaseContainerIndexByIndex: Int32Array; nextSchemaContainerIndexByIndex: Int32Array; + nextBoundaryIndexByIndex: Int32Array; } interface FlatTreeIndexOptions { @@ -140,9 +141,11 @@ export function createFlatTreeIndex(nodes: readonly FlatTreeNode[], options: Fla const stickyContainerIndexByIndex = new Int32Array(nodes.length); const nextDatabaseContainerIndexByIndex = new Int32Array(nodes.length); const nextSchemaContainerIndexByIndex = new Int32Array(nodes.length); + const nextBoundaryIndexByIndex = new Int32Array(nodes.length); stickyContainerIndexByIndex.fill(-1); nextDatabaseContainerIndexByIndex.fill(-1); nextSchemaContainerIndexByIndex.fill(-1); + nextBoundaryIndexByIndex.fill(-1); let databaseContainerIndex = -1; let schemaContainerIndex = -1; @@ -170,9 +173,12 @@ export function createFlatTreeIndex(nodes: readonly FlatTreeNode[], options: Fla let nextDatabaseContainerIndex = -1; let nextSchemaContainerIndex = -1; + let nextBoundaryIndex = -1; for (let index = nodes.length - 1; index >= 0; index -= 1) { const item = nodes[index]; + nextBoundaryIndexByIndex[index] = nextBoundaryIndex; if (options.isBoundary(item.type)) { + nextBoundaryIndex = index; nextDatabaseContainerIndex = -1; nextSchemaContainerIndex = -1; continue; @@ -193,5 +199,6 @@ export function createFlatTreeIndex(nodes: readonly FlatTreeNode[], options: Fla stickyContainerIndexByIndex, nextDatabaseContainerIndexByIndex, nextSchemaContainerIndexByIndex, + nextBoundaryIndexByIndex, }; } diff --git a/packages/app-tests/useFlatTree.test.ts b/packages/app-tests/useFlatTree.test.ts index a4a4a8ca7b..fac40d20f2 100644 --- a/packages/app-tests/useFlatTree.test.ts +++ b/packages/app-tests/useFlatTree.test.ts @@ -104,6 +104,9 @@ test("sticky containers prefer databases and fall back to schemas", () => { assert.equal(index.stickyContainerIndexByIndex[3], 1); assert.equal(index.stickyContainerIndexByIndex[5], 5); assert.equal(index.stickyContainerIndexByIndex[6], 5); + assert.equal(index.nextBoundaryIndexByIndex[1], 4); + assert.equal(index.nextBoundaryIndexByIndex[2], 4); + assert.equal(index.nextBoundaryIndexByIndex[5], -1); }); test("shouldVirtualizeFlatTree keeps small/medium trees on the plain renderer", () => {