diff --git a/src/features/app/__tests__/closeGuard.test.ts b/src/features/app/__tests__/closeGuard.test.ts index 31a118ff53..4d7c30a7f4 100644 --- a/src/features/app/__tests__/closeGuard.test.ts +++ b/src/features/app/__tests__/closeGuard.test.ts @@ -281,3 +281,32 @@ it("confirmLeave asks about running processes before closing", async () => { expect(asked[0]?.title).toContain("app"); expect(asked[0]?.body).toContain("app"); }); + +it("counts untitled buffers only when leaving for Welcome, and stays when one isn't saved", async () => { + const service = await import("@/workbench/contributions"); + const { confirmLeave } = await import("../closeGuard"); + let untitled = [7]; + const spy = vi.spyOn(service, "maybeEditorService").mockReturnValue({ + dirtyDocumentIds: () => [7], + untitledDirtyDocumentIds: () => untitled, + saveAll: vi.fn(async () => {}), + } as never); + try { + // Another project in place keeps the buffer: nothing to ask. + const kept = deps([]); + await expect(confirmLeave(kept.d, "openidea", true)).resolves.toBe(true); + expect(kept.asked).toHaveLength(0); + // Close Project asks; Save with the Save As cancelled keeps the project open, quietly. + const left = deps([{ button: "save", checked: false }]); + await expect(confirmLeave(left.d, "openidea", false)).resolves.toBe(false); + expect(left.asked[0]?.body).toContain("1"); + expect(left.d.saveAll).toHaveBeenCalled(); + // Saved: the project closes. + untitled = []; + const saved = deps([{ button: "save", checked: false }]); + left.d.saveAll = vi.fn(async () => {}); + await expect(confirmLeave(saved.d, "openidea", false)).resolves.toBe(true); + } finally { + spy.mockRestore(); + } +}); diff --git a/src/features/app/closeGuard.ts b/src/features/app/closeGuard.ts index a573b8e322..dbc259e514 100644 --- a/src/features/app/closeGuard.ts +++ b/src/features/app/closeGuard.ts @@ -12,7 +12,7 @@ * `general.closeWithoutWelcome` is on, in which case it closes. When * `general.closeTabsWithWindow` is on, that close drops the saved editor tabs. */ -import { commands, isTauri, onIdeEvent, type Settings } from "@/platform/ipc"; +import { IpcError, commands, isCancelled, isTauri, onIdeEvent, type Settings } from "@/platform/ipc"; import { t } from "@/platform/i18n"; import { closeProject, provideLeaveProjectGuard, useProject } from "@/platform/project"; import { workflowSettings } from "@/platform/workflowSettings"; @@ -171,6 +171,7 @@ const defaultDeps: CloseDeps = { while ((await liveProcesses()).length > 0) await new Promise((r) => setTimeout(r, 100)); }, onSaveFailed: (e) => + !isCancelled(e) && notifications().notify({ level: "error", title: t("app.unsaved.saveFailed"), @@ -224,7 +225,7 @@ async function onCloseRequested(deps: CloseDeps): Promise { const s = useProject.getState(); // The last project or LightEdit window can return to Welcome. Otherwise it closes. if (returnsToWelcome(ctx.quitting, ctx.windows, !!(s.project || s.lightEdit), s.settings)) { - if (await confirmLeave(deps)) { + if (await confirmLeave(deps, undefined, false)) { forgot = forgetTabs(); try { await closeProject(); @@ -276,16 +277,27 @@ async function onCloseRequested(deps: CloseDeps): Promise { export async function confirmLeave( deps: CloseDeps = defaultDeps, project = (useProject.getState().project?.displayName || useProject.getState().project?.name) ?? null, + keepUntitled = true, ): Promise { if (!(await closeDocumentWindows())) return false; const confirmTerminate = workflowSettings().run.confirmTerminate; - // Untitled buffers follow the window across projects: they are neither - // counted nor saved by this guard (the window keeps them open). - const untitled = new Set(maybeEditorService()?.untitledDirtyDocumentIds?.() ?? []); + // Untitled buffers follow the window into another project: they are neither counted nor + // saved then (the window keeps them open). Closing to Welcome drops them, so they count. + const untitled = new Set(keepUntitled ? (maybeEditorService()?.untitledDirtyDocumentIds?.() ?? []) : []); const saveDeps: CloseDeps = untitled.size > 0 ? { ...deps, saveAll: () => Promise.resolve(maybeEditorService()?.saveAll({ skipUntitled: true })) } - : deps; + : keepUntitled + ? deps + : { + ...deps, + // Save asks for each untitled buffer's path; one left unsaved (Save As cancelled) keeps the project open. + saveAll: async () => { + await deps.saveAll(); + if (maybeEditorService()?.untitledDirtyDocumentIds?.().length) + throw new IpcError({ kind: "cancelled", message: "Untitled files were not saved" }); + }, + }; return decideClose( { quitting: false, @@ -331,7 +343,7 @@ export function installCloseGuard(deps: CloseDeps = defaultDeps): () => void { useProject.subscribe((s, prev) => { if (s.project?.root !== prev.project?.root || s.lightEdit !== prev.lightEdit) dirtyDocs.clear(); }), - provideLeaveProjectGuard(() => confirmLeave(deps)), + provideLeaveProjectGuard((keepUntitled) => confirmLeave(deps, undefined, keepUntitled)), ]; let unlisten: (() => void) | undefined; let disposed = false; diff --git a/src/features/remote-dev/index.ts b/src/features/remote-dev/index.ts index bc82e95a98..2c626f5ce0 100644 --- a/src/features/remote-dev/index.ts +++ b/src/features/remote-dev/index.ts @@ -79,7 +79,7 @@ export function activate(): () => void { keywords: ["ssh", "remote", "close"], when: "projectOpen", run: async () => { - if (!(await confirmLeaveProject())) return; + if (!(await confirmLeaveProject(false))) return; try { await disconnectRemote(); } catch (e) { diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index 59c447bbd8..4edfce68ae 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -2157,5 +2157,6 @@ "Extract Interface from '{name}'": "Extract Interface from '{name}'", "The interface could not be extracted": "The interface could not be extracted", "Extract Interface is not supported for {name}": "Extract Interface is not supported for {name}", - "Package for new interface": "Package for new interface" + "Package for new interface": "Package for new interface", + "Untitled files were not saved": "Untitled files were not saved" } diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index d71b95f518..584c3655b0 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -8540,5 +8540,6 @@ "Extract Interface from '{name}'": "从 '{name}' 提取接口", "The interface could not be extracted": "无法提取接口", "Extract Interface is not supported for {name}": "{name} 不支持提取接口", - "Package for new interface": "新接口的软件包" + "Package for new interface": "新接口的软件包", + "Untitled files were not saved": "未命名文件未保存" } diff --git a/src/platform/project.ts b/src/platform/project.ts index 8a721af221..5e812c7428 100644 --- a/src/platform/project.ts +++ b/src/platform/project.ts @@ -25,20 +25,26 @@ export const useProject = create(() => ({ useProject.subscribe((s) => setContext("projectOpening", s.openingPath !== null)); -let leaveGuard: (() => Promise) | null = null; +/** `keepUntitled`: the window opens another project in place and keeps its untitled buffers. */ +type LeaveGuard = (keepUntitled: boolean) => Promise; +let leaveGuard: LeaveGuard | null = null; /** Installs the check run before this window leaves its project (unsaved changes). */ -export function provideLeaveProjectGuard(guard: () => Promise): () => void { +export function provideLeaveProjectGuard(guard: LeaveGuard): () => void { leaveGuard = guard; return () => { if (leaveGuard === guard) leaveGuard = null; }; } -/** False when the user chose to stay (another project in place, Close Project). */ -export function confirmLeaveProject(): Promise { +/** + * False when the user chose to stay (another project in place, Close Project). Untitled buffers + * follow the window only into another local project (`keepUntitled`); leaving for Welcome or a + * remote host asks about them too. + */ +export function confirmLeaveProject(keepUntitled = true): Promise { const s = useProject.getState(); - return (s.project || s.lightEdit) && leaveGuard ? leaveGuard() : Promise.resolve(true); + return (s.project || s.lightEdit) && leaveGuard ? leaveGuard(keepUntitled) : Promise.resolve(true); } let startupLaunch: Promise | null = null; diff --git a/src/workbench/projectWindow.ts b/src/workbench/projectWindow.ts index c6ae8b15b0..882df40b97 100644 --- a/src/workbench/projectWindow.ts +++ b/src/workbench/projectWindow.ts @@ -65,7 +65,8 @@ export async function openRemoteProject( await call(commands.windowOpenRemote(target, path, true)); return true; } - if (!(await confirmLeaveProject())) return false; + // The remote host's project doesn't take this window's untitled buffers. + if (!(await confirmLeaveProject(false))) return false; try { await here(); return true; diff --git a/src/workbench/shellActions.ts b/src/workbench/shellActions.ts index 4d1e15d69e..097555bd0b 100644 --- a/src/workbench/shellActions.ts +++ b/src/workbench/shellActions.ts @@ -69,7 +69,7 @@ export function registerShellActions(): () => void { category: "File", when: "projectOpen", run: async () => { - if (!useProject.getState().project || !(await confirmLeaveProject())) return; + if (!useProject.getState().project || !(await confirmLeaveProject(false))) return; try { await closeProject(); } catch (e) {