From 0ef9388b81c4e998266fd81a2a3489ec8ebeebd2 Mon Sep 17 00:00:00 2001 From: t8y2 Date: Sat, 3 Oct 2026 11:45:18 +0800 Subject: [PATCH] fix(grid): gate canvas surface flip to backing-store resizes Refs: #10132 --- apps/desktop/src/components/grid/DataGrid.vue | 47 ++++++++++++++++--- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/apps/desktop/src/components/grid/DataGrid.vue b/apps/desktop/src/components/grid/DataGrid.vue index a2e523aa01..b3e7c2142a 100644 --- a/apps/desktop/src/components/grid/DataGrid.vue +++ b/apps/desktop/src/components/grid/DataGrid.vue @@ -246,7 +246,17 @@ import { type DataGridScrollPosition, } from "@/lib/dataGrid/dataGridInfiniteScroll"; import { resolveDataGridWheelScroll } from "@/lib/dataGrid/dataGridWheel"; -import { CANVAS_DATA_GRID_ROW_HEIGHT, MAX_CANVAS_DATA_GRID_PIXEL_RATIO, canvasDataGridActionOverlayWidth, canvasDataGridActionReservedWidth, dataGridSearchMatchKey, drawCanvasDataGrid, resolveCanvasCellTextLayout, type CanvasDevicePixelSize } from "@/lib/dataGrid/canvasDataGridRenderer"; +import { + CANVAS_DATA_GRID_ROW_HEIGHT, + MAX_CANVAS_DATA_GRID_PIXEL_RATIO, + canvasDataGridActionOverlayWidth, + canvasDataGridActionReservedWidth, + dataGridSearchMatchKey, + drawCanvasDataGrid, + resolveCanvasBackingStoreMetrics, + resolveCanvasCellTextLayout, + type CanvasDevicePixelSize, +} from "@/lib/dataGrid/canvasDataGridRenderer"; import { resolveDataGridRowNumberLabel } from "@/lib/dataGrid/dataGridRowNumber"; import { resolveCrosshairTarget, type CrosshairTarget } from "@/lib/dataGrid/crosshairHighlight"; import { DATA_GRID_DARK_STRIPED_ROW_BG, DATA_GRID_LIGHT_STRIPED_ROW_BG, dataGridActiveRowBackground } from "@/lib/dataGrid/dataGridPaintTheme"; @@ -7261,6 +7271,9 @@ function attachCanvasPixelRatioWatcher() { function attachCanvasResizeObserver() { if (!dataGridIsActive) return; if (!useCanvasGridRows.value) return; + // 画布(重)挂载后 canvas 元素是全新的,重置签名强制下一帧走翻转, + // 甩掉空/错误/正常分支切换时旧元素可能滞留的幽灵层。 + lastPresentedCanvasBackingKey = null; attachCanvasPixelRatioWatcher(); canvasRuntime.observeViewport(); } @@ -7792,17 +7805,38 @@ const canvasRightAlignedActionCell = computed(() => { }; }); +// 幽灵合成层只在后备存储(canvas.width/height)被重建时滞留,而后备存储仅随像素 +// 尺寸/DPR 变化而重建(见 canvasDataGridRenderer 对 pixelWidth/pixelHeight 的比较)。 +// 因此只有尺寸变化这一帧才画进隐藏 canvas 再翻转(换掉显示元素甩掉幽灵层);纯滚动、 +// hover、选区、搜索、编辑等尺寸不变的重绘直接就地画在可见 canvas 上、不翻转,避免每帧 +// 销毁+重建两个合成层造成的卡顿(issue #10132 / tauri-apps/wry#1848)。 +let lastPresentedCanvasBackingKey: string | null = null; + function drawCanvasGrid() { - const canvas = inactiveCanvasSurface(); const scroller = canvasScrollerElement(); - if (!canvas || !scroller || !useCanvasGridRows.value) return; + if (!scroller || !useCanvasGridRows.value) return; + + const width = Math.max(1, canvasSurfaceWidth.value || scroller.clientWidth); + const height = Math.max(1, canvasViewportHeight.value || scroller.clientHeight); + const backingMetrics = resolveCanvasBackingStoreMetrics({ + width, + height, + // 与渲染器 drawCanvasDataGrid 内部 fallbackRatio=Math.max(1, pixelRatio) 对齐, + // 保证签名算出的后备存储尺寸与渲染器实际重建判定逐位一致,不受上游 ratio 口径变化影响。 + pixelRatio: Math.max(1, canvasBackingPixelRatio.value), + devicePixelSize: canvasMeasuredDevicePixelSize.value, + }); + const backingKey = `${backingMetrics.pixelWidth}x${backingMetrics.pixelHeight}`; + const needsSurfaceSwap = backingKey !== lastPresentedCanvasBackingKey; + const canvas = needsSurfaceSwap ? inactiveCanvasSurface() : activeCanvasSurface(); + if (!canvas) return; const drawnResult = props.result; const drawn = drawCanvasDataGrid({ canvas, scroller, - width: Math.max(1, canvasSurfaceWidth.value || scroller.clientWidth), - height: Math.max(1, canvasViewportHeight.value || scroller.clientHeight), + width, + height, pixelRatio: canvasBackingPixelRatio.value, devicePixelSize: canvasMeasuredDevicePixelSize.value, isDark: isDark.value, @@ -7843,7 +7877,8 @@ function drawCanvasGrid() { rowNumberMode: dataGridRowNumberMode.value, }); if (!drawn) return; - flipCanvasSurface(); + if (needsSurfaceSwap) flipCanvasSurface(); + lastPresentedCanvasBackingKey = backingKey; completeResultCanvasDraw(drawnResult); }