From fe82a4402fb59e124ff97961dbc0ca66245b70ea Mon Sep 17 00:00:00 2001 From: eddybean <38001995+eddybean@users.noreply.github.com> Date: Sat, 28 Nov 2026 18:05:37 +0900 Subject: [PATCH] =?UTF-8?q?Close=20=E6=9B=B8=E3=81=8D=E5=87=BA=E3=81=97=20?= =?UTF-8?q?on=20Esc=20and=20show=20each=20profile's=20own=20env=20on=20the?= =?UTF-8?q?=20=E3=83=A2=E3=83=87=E3=83=AB=20screen=20(D-57)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Dialog re-added its keydown listener on every render, so when the app's Esc handler re-rendered the export dialog mid-dispatch the new listener missed the key. It now keeps one listener and calls the latest onClose. The モデル form's env and extra-args fields are uncontrolled, so picking another profile kept the first one's text, and leaving the field saved it into the other profile. The form is now keyed by the profile. Co-Authored-By: Claude Opus 5.5 --- docs/deviations.md | 8 +++ src/app/Dialog.test.tsx | 45 ++++++++++++ src/app/Dialog.tsx | 13 +++- src/features/models/ModelManager.test.tsx | 85 +++++++++++++++++++++++ src/features/models/ModelManager.tsx | 4 +- 5 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 src/app/Dialog.test.tsx create mode 100644 src/features/models/ModelManager.test.tsx diff --git a/docs/deviations.md b/docs/deviations.md index fdc180a..c41b661 100644 --- a/docs/deviations.md +++ b/docs/deviations.md @@ -409,3 +409,11 @@ Requests: (1) a way to stop playback, (2) choosing a Drama-3 voice by text promp - **Why**: The user asked for the cause of the stuck release and a fix before releasing v0.1.6 again. - **Not done**: Why the runner stopped responding is not known (no log, no GitHub incident at that time); a heavier cold build may have played a part. The first CI run on `main` after this change saves the cache under the new key; a release started before that finishes builds without a cache (but without debug info). - **User impact**: None to the app. + +### D-57 Esc closes 書き出し, and the モデル screen shows each profile's own env 【修正】 +- **What**: Two bugs found while trying React 19 (D-56); both were there on React 18.3 too. + - **書き出し did not close on Esc.** The app's Esc handler clears the line selection first; the export dialog reads the selection, so it re-rendered during that keydown, and `Dialog` (`src/app/Dialog.tsx`) removed and re-added its window listener because `onClose` is a new function on every render. A listener added during a dispatch is not called for it. `Dialog` now adds one listener for its life and calls the latest `onClose` through a ref. 設定 was not affected because it does not read the selection. + - **The env and extra-args fields of the モデル screen kept the first profile's text** when another profile was picked (when the first one had any): they are uncontrolled (`defaultValue`, saved on blur), and the form was not remounted. Leaving the field then saved that text into the other profile, e.g. the Drama-3 Vulkan variables into Irodori. The form is now keyed by the profile id. +- **Checked**: `src/app/Dialog.test.tsx` and `src/features/models/ModelManager.test.tsx` fail without the fixes and pass with them. In the app (over the WebView2 DevTools protocol, real key and mouse input): Esc closes 書き出し and 設定, and switching between the Drama-3 and Irodori profiles shows each one's own env. +- **User impact**: Esc closes the export dialog. Picking a profile on the モデル screen no longer shows, or on leaving the field saves, another profile's environment variables. + diff --git a/src/app/Dialog.test.tsx b/src/app/Dialog.test.tsx new file mode 100644 index 0000000..f5b146d --- /dev/null +++ b/src/app/Dialog.test.tsx @@ -0,0 +1,45 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { fireEvent, render, screen } from "@testing-library/react"; +import { flushSync } from "react-dom"; +import { create } from "zustand"; +import { Dialog } from "./Dialog"; + +// Stands for the line selection, which the app's own Esc handler clears before the dialog sees the key. +const useTick = create<{ n: number }>(() => ({ n: 0 })); + +function Host({ onClose }: { onClose: () => void }) { + const n = useTick((s) => s.n); + // A new onClose on every render, as the dialogs pass an inline arrow. + return ( + onClose()}> +

{n}

+
+ ); +} + +describe("Dialog", () => { + let earlier: ((e: KeyboardEvent) => void) | null = null; + afterEach(() => { + if (earlier) window.removeEventListener("keydown", earlier); + earlier = null; + }); + + it("closes on Esc", () => { + const onClose = vi.fn(); + render(); + fireEvent.keyDown(window, { key: "Escape" }); + expect(onClose).toHaveBeenCalledTimes(1); + }); + + it("closes on Esc when an earlier keydown listener re-renders it", () => { + // In the WebView the earlier listener's store update renders between listeners (a microtask); + // flushSync does the same here. + earlier = (e) => e.key === "Escape" && flushSync(() => useTick.setState((s) => ({ n: s.n + 1 }))); + window.addEventListener("keydown", earlier); + const onClose = vi.fn(); + render(); + fireEvent.keyDown(window, { key: "Escape" }); + expect(screen.getByText("1")).toBeTruthy(); + expect(onClose).toHaveBeenCalledTimes(1); + }); +}); diff --git a/src/app/Dialog.tsx b/src/app/Dialog.tsx index 3c16525..f96e559 100644 --- a/src/app/Dialog.tsx +++ b/src/app/Dialog.tsx @@ -1,4 +1,4 @@ -import { useEffect, type ReactNode } from "react"; +import { useEffect, useLayoutEffect, useRef, type ReactNode } from "react"; import { Icon } from "./icons"; export function Dialog({ @@ -12,11 +12,18 @@ export function Dialog({ children: ReactNode; wide?: boolean; }) { + // One listener for the dialog's life, calling the latest onClose. Re-adding it whenever onClose changed (each + // render) lost Esc: the app's own Esc handler clears the line selection first, the dialog re-renders during + // that keydown, and a listener added mid-dispatch is not called for it. + const close = useRef(onClose); + useLayoutEffect(() => { + close.current = onClose; + }); useEffect(() => { - const k = (e: KeyboardEvent) => e.key === "Escape" && onClose(); + const k = (e: KeyboardEvent) => e.key === "Escape" && close.current(); window.addEventListener("keydown", k); return () => window.removeEventListener("keydown", k); - }, [onClose]); + }, []); return (
e.target === e.currentTarget && onClose()}>
diff --git a/src/features/models/ModelManager.test.tsx b/src/features/models/ModelManager.test.tsx new file mode 100644 index 0000000..9a88093 --- /dev/null +++ b/src/features/models/ModelManager.test.tsx @@ -0,0 +1,85 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { fireEvent, render, screen } from "@testing-library/react"; +import { ModelManager } from "./ModelManager"; +import { useApp } from "../../store/app"; +import type { AdapterDescriptor, AppSettings, ModelProfile } from "../../lib/types"; + +vi.mock("../../lib/ipc", () => ({ + ipc: { + listDevices: vi.fn(async () => ({ devices: [] })), + modelCatalog: vi.fn(async () => ({ models: [] })), + speechVersion: vi.fn(async () => "speech 0.11.2"), + }, +})); + +const profile = (id: string, name: string, env: Record, extraArgs: string[]): ModelProfile => ({ + id, + name, + adapter: "irodori", + model: "/m.gguf", + speechExe: "speech", + device: "auto", + threads: null, + env, + extraArgs, +}); +const settings: AppSettings = { + version: 1, + profiles: [profile("p1", "Drama-3", { GGML_VK_DISABLE_F16: "1" }, ["--foo"]), profile("p2", "Irodori Small", {}, [])], + activeProfileId: "p1", + speechExe: "speech", + asrModel: "", + asrDevice: "auto", + showNsfwTags: false, + theme: "dark", + recentTags: [], + recentProjects: [], + playbackVolume: 1, + playbackMuted: false, +}; +const adapter: AdapterDescriptor = { + id: "irodori", + displayName: "Irodori-TTS", + capabilities: { + tagSyntax: "emoji", + tagPalette: "irodori", + designPrompt: "instructions", + instructions: true, + speed: true, + multiSpeaker: false, + multiSpeakerReliable: false, + voiceNeedsTranscript: false, + voiceAcceptsWav: true, + sampleRate: 48000, + maxSecondsPerRequest: 30, + licenseNote: "MIT", + }, + options: [], +}; + +const saveSettings = vi.fn(async (_s: AppSettings) => {}); + +describe("ModelManager", () => { + beforeEach(() => { + vi.clearAllMocks(); + useApp.setState({ settings, adapters: [adapter], engine: { state: "stopped" }, view: "models", saveSettings }); + }); + + it("shows each profile's own env and extra args when another profile is picked", () => { + render(); + const env = () => (screen.getByLabelText(/環境変数/) as HTMLTextAreaElement).value; + const args = () => (screen.getByLabelText(/追加の引数/) as HTMLInputElement).value; + expect(env()).toBe("GGML_VK_DISABLE_F16=1"); + expect(args()).toBe("--foo"); + + fireEvent.click(screen.getByRole("button", { name: "Irodori Small" })); + expect(env()).toBe(""); + expect(args()).toBe(""); + + // Leaving the field must not copy the first profile's env into this one. + fireEvent.blur(screen.getByLabelText(/環境変数/)); + const calls = saveSettings.mock.calls; + const saved = calls[calls.length - 1]?.[0]; + expect(saved?.profiles.find((p) => p.id === "p2")?.env ?? {}).toEqual({}); + }); +}); diff --git a/src/features/models/ModelManager.tsx b/src/features/models/ModelManager.tsx index 01c9ea4..070165e 100644 --- a/src/features/models/ModelManager.tsx +++ b/src/features/models/ModelManager.tsx @@ -102,7 +102,9 @@ export function ModelManager() { ))}
{prof && adapter && ( -
+ // Keyed by profile: the env and extra-args fields are uncontrolled (saved on blur), so without a + // remount they would keep the previous profile's text, and leaving them would save it here. +