From 346e16dd5b655c18928962c70e8f4aa1bfb8faee Mon Sep 17 00:00:00 2001 From: lqtdys Date: Wed, 2 Dec 2026 18:19:48 +0800 Subject: [PATCH] =?UTF-8?q?fix(video):=20=E7=BC=A9=E7=95=A5=E5=9B=BE?= =?UTF-8?q?=E7=AD=89=E7=9C=9F=E5=B8=A7=E5=B0=B1=E7=BB=AA=E6=89=8D=E4=BA=AE?= =?UTF-8?q?=E5=87=BA=EF=BC=8C=E6=B6=88=E9=99=A4=E7=A9=BA=E7=99=BD=E9=97=AA?= =?UTF-8?q?=E5=88=87=EF=BC=88#1953=20=E5=A4=8D=E6=B5=8B=E2=91=A2=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因:弹层 {thumb && (...)} 每次重悬停重挂空白 canvas(仅底色盒),setThumb 立即亮弹层,真帧等异步 seek 的 onSeeked 才绘入(~50-300ms)→ 空白盒→真帧闪切。修法:thumbReady 门控——onSeeked 绘帧成功才置真(先绘后置真,构造上无「可见且空白」窗口);弹层 opacity 门控,会话起始 useLayoutEffect 复位(paint 前完成);vp-thumb-show 改 thumbActive && thumbReady,等待期保留 Artplayer 时间气泡作即时反馈、就绪与气泡隐藏同帧原子切换。音频文件顺带不再亮空盒。测试:新增「等真帧就绪才亮出」正向(像素级非空画布 + opacity 1 + 贴轨道)+ 反向(源卸载→弹层在 DOM 但不亮、气泡保留);摘门控突变时用例失败(已自查)。e2e 夹具补 withRealThumbVideo(缩略图源独立于主视频)。fixes #1953 --- e2e/video-controls.spec.ts | 105 +++++++++++++++++++++++-- src/components/Content/VideoPlayer.tsx | 35 +++++++-- 2 files changed, 127 insertions(+), 13 deletions(-) diff --git a/e2e/video-controls.spec.ts b/e2e/video-controls.spec.ts index 714dbc28..3cc99843 100644 --- a/e2e/video-controls.spec.ts +++ b/e2e/video-controls.spec.ts @@ -60,6 +60,23 @@ async function withRealVideo(page: Page): Promise { await page.waitForTimeout(200); } +// 缩略图源(thumbVideo)独立于主视频:withRealVideo 只注主视频源,e2e 下 +// 缩略图源仍是 mock 路径 → 永无真帧 → 就绪门控(#1953 复测③)永不打开。 +async function withRealThumbVideo(page: Page): Promise { + await page.evaluate(async (b64) => { + const thumb = document.querySelector( + '[data-testid="video-thumb-source"]', + ) as HTMLVideoElement | null; + if (!thumb) return; + thumb.src = `data:video/mp4;base64,${b64}`; + await new Promise((resolve) => { + if (thumb.readyState >= 1) return resolve(); + thumb.addEventListener("loadedmetadata", () => resolve(), { once: true }); + }); + }, TINY_MP4); + await page.waitForTimeout(100); +} + test("控制条顺序:上下集夹住播放键,截图→画中画", async ({ page }) => { await openMedia(page, "/Users/test/clip.mp4"); @@ -264,6 +281,7 @@ test("控制条 38px、右侧按钮与图标统一尺寸", async ({ page }) => { test("进度条悬停显示当前位置缩略图,移开消失", async ({ page }) => { await openMedia(page, "/Users/test/clip.mp4"); await withRealVideo(page); + await withRealThumbVideo(page); const progress = page.locator(".art-progress").first(); const box = await progress.boundingBox(); if (!box) throw new Error("进度条不可测量"); @@ -282,14 +300,18 @@ test("进度条悬停显示当前位置缩略图,移开消失", async ({ page const gap = trackBox.y - (tb.y + tb.height); expect(gap).toBeGreaterThanOrEqual(0); expect(gap).toBeLessThanOrEqual(12); - // 缩略图显示时,Artplayer 自带的时间气泡隐藏——同屏只留一个时间来源(#1953) - const tipDisplay = await page.evaluate(() => { - const tip = document.querySelector( - ".art-video-player .art-progress-tip", - ) as HTMLElement | null; - return tip ? getComputedStyle(tip).display : "absent"; - }); - expect(tipDisplay).toBe("none"); + // 缩略图显示时,Artplayer 自带的时间气泡隐藏——同屏只留一个时间来源(#1953)。 + // 就绪门控下气泡在真帧绘入后才隐藏(等待期作即时反馈),故轮询到就绪。 + await expect + .poll(() => + page.evaluate(() => { + const tip = document.querySelector( + ".art-video-player .art-progress-tip", + ) as HTMLElement | null; + return tip ? getComputedStyle(tip).display : "absent"; + }), + ) + .toBe("none"); await page.mouse.move(box.x + box.width / 2, box.y - 80); await expect(page.locator('[data-testid="video-thumb"]')).toHaveCount(0); }); @@ -317,6 +339,73 @@ test("缩略图底边贴近可见进度轨道(#1953 复测)", async ({ page expect(gap).toBeLessThanOrEqual(12); }); +test("缩略图等真帧就绪才亮出,无空白闪切(#1953 复测③)", async ({ page }) => { + await openMedia(page, "/Users/test/clip.mp4"); + await withRealVideo(page); + await withRealThumbVideo(page); + const progress = page.locator(".art-progress").first(); + const box = await progress.boundingBox(); + if (!box) throw new Error("进度条不可测量"); + await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2); + const thumb = page.locator('[data-testid="video-thumb"]'); + await expect(thumb).toHaveCount(1); + // 弹层出现即应有真帧:空白画布全像素 alpha=0,绘帧后存在不透明像素。 + // 像素级判定(而非 dataURL 长度)——纯色测试片绘帧后 PNG 同样极小压缩。 + // 证明「亮出即有真帧」,而非先空白盒再被真帧替换(#1953 复测③)。 + await expect + .poll(() => + page.evaluate(() => { + const canvas = document.querySelector( + '[data-testid="video-thumb"] canvas', + ) as HTMLCanvasElement | null; + const ctx = canvas?.getContext("2d"); + if (!canvas || !ctx) return false; + const data = ctx.getImageData(0, 0, canvas.width, canvas.height).data; + for (let i = 3; i < data.length; i += 4) { + if (data[i] > 0) return true; + } + return false; + }), + ) + .toBe(true); + await expect(thumb).toHaveCSS("opacity", "1"); + const tb = (await thumb.boundingBox())!; + const trackBox = await page + .locator(".art-control-progress-inner") + .first() + .boundingBox(); + if (!trackBox) throw new Error("可见进度轨道不可测量"); + const gap = trackBox.y - (tb.y + tb.height); + expect(gap).toBeGreaterThan(0); + expect(gap).toBeLessThanOrEqual(12); + + // 反向钉死门控语义:真帧不可得(源被卸载)时弹层必须不亮—— + // 旧实现此时会亮空白盒(本用例失败),新实现保留时间气泡作反馈。 + await page.mouse.move(box.x + box.width / 2, box.y - 80); + await expect(page.locator('[data-testid="video-thumb"]')).toHaveCount(0); + await page.evaluate(() => { + const thumbSource = document.querySelector( + '[data-testid="video-thumb-source"]', + ) as HTMLVideoElement | null; + thumbSource?.removeAttribute("src"); + thumbSource?.load(); + }); + await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2); + await expect(page.locator('[data-testid="video-thumb"]')).toHaveCount(1); + await page.waitForTimeout(400); + await expect(page.locator('[data-testid="video-thumb"]')).toHaveCSS( + "opacity", + "0", + ); + const tipDisplay = await page.evaluate(() => { + const tip = document.querySelector( + ".art-video-player .art-progress-tip", + ) as HTMLElement | null; + return tip ? getComputedStyle(tip).display : "absent"; + }); + expect(tipDisplay).not.toBe("none"); +}); + test("全屏态仍有二级菜单", async ({ page }) => { await openMedia(page, "/Users/test/clip.mp4"); await withRealVideo(page); diff --git a/src/components/Content/VideoPlayer.tsx b/src/components/Content/VideoPlayer.tsx index 56f62045..ca2d4b4b 100644 --- a/src/components/Content/VideoPlayer.tsx +++ b/src/components/Content/VideoPlayer.tsx @@ -1,4 +1,10 @@ -import { useCallback, useEffect, useRef, useState } from "react"; +import { + useCallback, + useEffect, + useLayoutEffect, + useRef, + useState, +} from "react"; import { createPortal } from "react-dom"; import Artplayer from "artplayer"; import { invoke } from "@tauri-apps/api/core"; @@ -321,15 +327,27 @@ export default function VideoPlayer({ bottom: number; t: number; } | null>(null); + // #1953 复测③:弹层亮出 ≠ 真帧就绪。thumb 只携带定位,canvas 内容要等 + // 异步 seek 的 onSeeked 绘入;此前弹层先以空白盒出现 ~50-300ms 再被真帧替换 = 闪切。 + // thumbReady 由 onSeeked 绘帧成功后置真,弹层 opacity 与 Artplayer 时间气泡 + // 隐藏都门控在它之上——等待期保留气泡作即时反馈,真帧就绪与气泡隐藏同帧原子切换。 + const [thumbReady, setThumbReady] = useState(false); + const thumbActive = thumb !== null; const thumbCanvasRef = useRef(null); const thumbVideoRef = useRef(null); + // 会话起始复位:canvas 随弹层卸载重建,旧会话 ready 残留会亮空画布; + // layout effect + setState 保证 paint 前完成复位,绝不亮空白帧。 + useLayoutEffect(() => { + if (thumbActive) setThumbReady(false); + }, [thumbActive]); // 缩略图显隐同步到 $player 类:隐藏 Artplayer 自带的时间气泡, // 避免与缩略图同屏两处时间(#1953 复测;音频无缩略图,气泡保留)。 - useEffect(() => { + // 门控 thumbReady:真帧就绪才隐藏气泡,等待期保留气泡作即时反馈。 + useLayoutEffect(() => { const player = artRef.current?.template?.$player; if (!player) return; - player.classList.toggle("vp-thumb-show", thumb !== null); - }, [thumb]); + player.classList.toggle("vp-thumb-show", thumbActive && thumbReady); + }, [thumbActive, thumbReady]); const [shotNotice, setShotNotice] = useState<{ text: string; left: number; @@ -1427,6 +1445,8 @@ export default function VideoPlayer({ const ctx2d = canvas.getContext("2d"); if (!ctx2d) return; ctx2d.drawImage(thumbVideo, 0, 0, canvas.width, canvas.height); + // 先绘后置真:就绪翻转与画布内容同帧,构造上不存在「可见且空白」窗口(#1953 复测③)。 + setThumbReady(true); }; const onProgressMove = (e: MouseEvent) => { if (!progressEl) return; @@ -1458,7 +1478,10 @@ export default function VideoPlayer({ // 元数据未就绪:只显示时间 } }; - const onProgressLeave = () => setThumb(null); + const onProgressLeave = () => { + setThumb(null); + setThumbReady(false); + }; progressEl?.addEventListener("mousemove", onProgressMove); progressEl?.addEventListener("mouseleave", onProgressLeave); thumbVideo.addEventListener("seeked", onSeeked); @@ -2795,6 +2818,8 @@ export default function VideoPlayer({ overflow: "hidden", background: "var(--app-surface)", boxShadow: "0 4px 16px rgba(0,0,0,0.4)", + // 就绪才亮:帧已在画布上,硬出现无闪感(不加 transition,#1953 复测③)。 + opacity: thumbReady ? 1 : 0, pointerEvents: "none", }} >