fix(pet): surface context menu failures instead of swallowing them

Right-clicking the pet showed the native WebView menu and never the
application's own menu. The handler was a bare void invoke(...) with no
catch, so any failure to build or pop the menu was completely invisible
and indistinguishable from preventDefault not taking effect.

Report the error in the pet window and stop propagation as well as the
default action. This does not yet explain which of the two is happening
— it makes the difference observable, which is the prerequisite for
fixing the right one.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
这个提交存在于:
YokiiroBW 2026-07-27 00:59:20 +08:00
父节点 fa48a21e65
当前提交 0bfd12ed1a

查看文件

@ -3,6 +3,7 @@ import { listen } from "@tauri-apps/api/event";
import { getCurrentWindow } from "@tauri-apps/api/window";
import { useEffect, useRef, useState } from "react";
import { errorMessage } from "../../lib/errorMessage";
import {
boundRuntimeEventSchema,
connectionRuntimeStatusSchema,
@ -31,6 +32,7 @@ export function DesktopPetView({ embedded = false }: { embedded?: boolean }) {
const [frameIndex, setFrameIndex] = useState(0);
const [spriteReady, setSpriteReady] = useState(false);
const [spriteError, setSpriteError] = useState<string | null>(null);
const [menuError, setMenuError] = useState<string | null>(null);
const [gameMode, setGameMode] = useState(false);
const spriteImageRef = useRef<HTMLImageElement | null>(null);
const spriteCanvasRef = useRef<HTMLCanvasElement | null>(null);
@ -221,7 +223,13 @@ export function DesktopPetView({ embedded = false }: { embedded?: boolean }) {
}}
onContextMenu={(event) => {
event.preventDefault();
void invoke("show_pet_context_menu");
event.stopPropagation();
// Previously a bare `void invoke(...)`, so a failure to build or pop
// the menu was invisible and looked like the native WebView menu had
// simply won. Surface it instead.
void invoke("show_pet_context_menu").catch((reason) => {
setMenuError(errorMessage(reason));
});
}}
>
<div
@ -238,6 +246,11 @@ export function DesktopPetView({ embedded = false }: { embedded?: boolean }) {
aria-label={`pet-${state}`}
/>
</div>
{menuError ? (
<span className="pet-render-error" role="alert">
{menuError}
</span>
) : null}
{spriteError ? (
<span className="pet-render-error">{spriteError}</span>
) : null}