fix(project): close the window on File → Close Project when another project is open (#7320)

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 <[email protected]>
Цей коміт міститься в:
audichuang authored and GitHub committed 2026-12-02 11:20:06 +08:00
1 джерело 9b468b0873
коміт 27689f09d3
2 files changed
+120 -1

No files matched your search

+113
Переглянути файл
@@ -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<void>) | 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");
});
+7 -1
Переглянути файл
@@ -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();
}
},
},
{