From 27689f09d3419f2ef38732fdab79764d164d588c Mon Sep 17 00:00:00 2001 From: audichuang <72485427+audichuang@users.noreply.github.com> Date: Wed, 2 Dec 2026 11:20:06 +0800 Subject: [PATCH] =?UTF-8?q?fix(project):=20close=20the=20window=20on=20Fil?= =?UTF-8?q?e=20=E2=86=92=20Close=20Project=20when=20another=20project=20is?= =?UTF-8?q?=20open=20(#7320)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit File → Close Project always switched the window to Welcome. Like the native close button, it now closes the emptied window when another project window is open, and shows Welcome only for the last one. Closes #7312 Co-authored-by: audichuang <72485427+audichuang@users.noreply.github.com> --- .../app/__tests__/closeProjectAction.test.ts | 113 ++++++++++++++++++ src/workbench/shellActions.ts | 8 +- 2 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 src/features/app/__tests__/closeProjectAction.test.ts diff --git a/src/features/app/__tests__/closeProjectAction.test.ts b/src/features/app/__tests__/closeProjectAction.test.ts new file mode 100644 index 0000000000..c174d55c57 --- /dev/null +++ b/src/features/app/__tests__/closeProjectAction.test.ts @@ -0,0 +1,113 @@ +import { afterEach, beforeEach, expect, it, vi } from "vitest"; +import { executeAction } from "@/platform/actions"; +import { clearContext, setContext } from "@/platform/context"; +import * as ipc from "@/platform/ipc"; +import { useProject } from "@/platform/project"; +import * as services from "@/workbench/contributions"; +import { registerShellActions } from "@/workbench/shellActions"; +import { installCloseGuard, type CloseDeps } from "../closeGuard"; + +const native = vi.hoisted(() => ({ + close: undefined as ((event: { preventDefault: () => void }) => Promise) | undefined, + closed: false, +})); +vi.mock("@tauri-apps/api/window", () => ({ + getAllWindows: async () => [], + getCurrentWindow: () => ({ + label: "main", + listen: async () => () => {}, + onCloseRequested: async (callback: typeof native.close) => { + native.close = callback; + return () => {}; + }, + // A close request runs the close guard; the window closes unless it prevents that. + close: async () => { + let prevented = false; + await native.close!({ preventDefault: () => (prevented = true) }); + native.closed = !prevented; + }, + unminimize: async () => {}, + show: async () => {}, + setFocus: async () => {}, + }), +})); + +const initialProject = useProject.getState(); +const dispose: (() => void)[] = []; +let deps: CloseDeps; +let dirty: number[] = []; + +const closeProjectAction = () => executeAction("file.closeProject"); + +function context(windows: number) { + vi.spyOn(ipc.commands, "windowCloseContext").mockResolvedValue({ + quitting: false, + windows, + hasDocumentWindows: false, + batch: false, + }); +} + +beforeEach(async () => { + vi.spyOn(ipc, "isTauri").mockReturnValue(true); + vi.spyOn(ipc, "onIdeEvent").mockReturnValue(() => {}); + vi.spyOn(ipc.commands, "windowBeginPrompt").mockResolvedValue(true); + vi.spyOn(ipc.commands, "windowEndPrompt").mockResolvedValue(undefined); + vi.spyOn(ipc.commands, "windowReadyToClose").mockResolvedValue(true); + vi.spyOn(ipc.commands, "windowGroupCancelClose").mockResolvedValue(undefined); + vi.spyOn(ipc.commands, "closeProject").mockResolvedValue(undefined); + vi.spyOn(services, "maybeEditorService").mockReturnValue({ + forgetSavedTabs: vi.fn(), + keepSavedTabs: vi.fn(), + flushSession: vi.fn(), + dirtyDocumentIds: () => dirty, + cancelClose: vi.fn(), + } as never); + deps = { + ask: vi.fn(async () => ({ button: "cancel", checked: false })), + saveAll: vi.fn(async () => {}), + disableConfirmExit: vi.fn(async () => {}), + disableConfirmTerminate: vi.fn(async () => {}), + runningProcesses: vi.fn(async () => []), + onSaveFailed: vi.fn(), + }; + useProject.setState({ project: { root: "/project", name: "project" }, lightEdit: false, settings: {} } as never); + setContext("projectOpen", true); + dispose.push(installCloseGuard(deps), registerShellActions(), () => clearContext("projectOpen")); + await vi.waitFor(() => expect(native.close).toBeDefined()); +}); + +afterEach(() => { + dispose.splice(0).forEach((f) => f()); + native.close = undefined; + native.closed = false; + dirty = []; + useProject.setState(initialProject, true); + vi.restoreAllMocks(); +}); + +it("closes the project, then this window, when another project window is open", async () => { + context(2); + await closeProjectAction(); + expect(ipc.commands.closeProject).toHaveBeenCalledOnce(); + expect(native.closed).toBe(true); + // The window has no project left, so closing it asks nothing more. + expect(deps.ask).not.toHaveBeenCalled(); +}); + +it("shows Welcome when this is the only window", async () => { + context(1); + await closeProjectAction(); + expect(native.closed).toBe(false); + expect(ipc.commands.closeProject).toHaveBeenCalledOnce(); +}); + +it.each([1, 2])("keeps the project open when the unsaved-changes prompt is cancelled (%i windows)", async (windows) => { + context(windows); + dirty = [7]; + await closeProjectAction(); + expect(deps.ask).toHaveBeenCalled(); + expect(native.closed).toBe(false); + expect(ipc.commands.closeProject).not.toHaveBeenCalled(); + expect(useProject.getState().project?.root).toBe("/project"); +}); diff --git a/src/workbench/shellActions.ts b/src/workbench/shellActions.ts index ee3a0d0911..e48ef881dc 100644 --- a/src/workbench/shellActions.ts +++ b/src/workbench/shellActions.ts @@ -1,6 +1,6 @@ import { showBackgroundTasks } from "./StatusBar"; import { executeAction, getAction, registerActions } from "@/platform/actions"; -import { isCancelled, isTauri } from "@/platform/ipc"; +import { commands, isCancelled, isTauri } from "@/platform/ipc"; import { confirmProjectTrust } from "@/platform/projectTrust"; import { closeProject, confirmLeaveProject, openProject, useProject } from "@/platform/project"; import { runSearchFieldCommand, showContextMenu } from "@/ui"; @@ -79,6 +79,12 @@ export function registerShellActions(): () => void { maybeEditorService()?.cancelClose?.(); throw e; } + // IDEA shows Welcome only when no other project is open (as `returnsToWelcome` does for the + // close button); otherwise the now empty window closes. + if (isTauri() && ((await commands.windowCloseContext().catch(() => null))?.windows ?? 1) > 1) { + const { getCurrentWindow } = await import("@tauri-apps/api/window"); + await getCurrentWindow().close(); + } }, }, {