fix(grid): gate canvas surface flip to backing-store resizes

Refs: #10132
This commit is contained in:
t8y2 committed 2026-10-03 11:45:18 +08:00
1 parent 87b030d626
commit 0ef9388b81
1 file changed
+41 -6
+41 -6
View File
@@ -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);
}