feat(launcher): remember last selected directory in file and folder pickers

Persist per-category dialog directories under userData/dialog-paths.json and resolve existing parent fallbacks for folder pickers, save archive import/export, online backup selection, and mod preset/folder dialogs.
This commit is contained in:
N0VAN0M4LY committed 2026-12-04 19:55:04 +08:00
1 parent 8f1d175d55
commit b3211b89cd
6 files changed
+226 -12

No files matched your search

@@ -17,6 +17,7 @@ import { AppError } from '@shared/errors.ts';
import type { InstallRecord } from '@shared/schemas/install.ts';
import { ModPresetTransferSchema, type ModListItem, type ModsStateView } from '@shared/schemas/mods.ts';
import { readJsonOr, writeJsonAtomic } from '../../util/atomic-json.ts';
import { rememberDialogPath, resolveDialogDefaultPath } from '../../util/dialog-paths.ts';
import { getSettings, updateSettings } from '../config/settings-store.ts';
import { listCopies } from '../install/install-record.ts';
import { resourcePaths } from '../resources/resource-paths.ts';
@@ -236,8 +237,10 @@ export async function exportModPreset(
if (!preset) throw new AppError('mods_preset_invalid');
const safeName = preset.name.replace(/[^A-Za-z0-9\u4e00-\u9fa5._-]+/g, '-').replace(/^-+|-+$/g, '') || 'preset';
const defaultDir = await resolveDialogDefaultPath('mod-preset');
const fileName = `${safeName}.wto-mods-preset.json`;
const dialogOptions = {
defaultPath: `${safeName}.wto-mods-preset.json`,
defaultPath: defaultDir ? join(defaultDir, fileName) : fileName,
filters: [{ name: 'WTOffline Mod Preset', extensions: ['json'] }],
};
const choice = window
@@ -252,6 +255,7 @@ export async function exportModPreset(
enabled: { ...preset.enabled },
};
await writeFile(choice.filePath, JSON.stringify(payload, null, 2) + '\n', 'utf8');
await rememberDialogPath('mod-preset', choice.filePath);
return { path: choice.filePath };
}
@@ -260,9 +264,11 @@ export async function importModPreset(
window: BrowserWindow | null,
): Promise<{ readonly imported: boolean; readonly state: ModsStateView }> {
const copy = await requireCopy(copyId);
const defaultPath = await resolveDialogDefaultPath('mod-preset');
const dialogOptions = {
properties: ['openFile'] as Array<'openFile'>,
filters: [{ name: 'WTOffline Mod Preset', extensions: ['json'] }],
...(defaultPath ? { defaultPath } : {}),
};
const choice = window
? await dialog.showOpenDialog(window, dialogOptions)
@@ -270,6 +276,7 @@ export async function importModPreset(
if (choice.canceled || !choice.filePaths[0]) {
return { imported: false, state: await buildModsStateView(copy) };
}
await rememberDialogPath('mod-preset', choice.filePaths[0]);
let parsedRaw: unknown;
try {
@@ -309,6 +316,7 @@ export async function setModsFolder(copyId: string, folderPath: string | null):
}
}
await updateSettings({ modsFolder: folderPath });
if (folderPath !== null) await rememberDialogPath('mods-folder', folderPath);
const copy = await requireCopy(copyId);
return buildModsStateView(copy);
}
@@ -6,6 +6,7 @@ import { AppError } from '@shared/errors.ts';
import type { InstallRecord } from '@shared/schemas/install.ts';
import type { AccountEdits, CurrencyEdits, OfficialImportOptions, OfficialPreview, OnlineDataSource, SaveBackup, SaveDetails, SaveList, SaveRegistration, SaveTransfer, ResearchBranch, ResearchOperation, SaveAvatarChoices, SaveResearch } from '@shared/schemas/saves.ts';
import type { SaveReply, SaveResult, SaveTask } from '../../workers/save-protocol.ts';
import { rememberDialogPath, resolveDialogDefaultPath } from '../../util/dialog-paths.ts';
import { maintenanceInProgress } from '../copies/copy-maintenance.ts';
import { canonicalCopyPath, copyPathContains, listCopies, updateCopyRecord } from '../install/install-record.ts';
import { gameProcessRunning, launchInProgress } from '../launch/launch-controller.ts';
@@ -95,15 +96,27 @@ export async function setSaveCurrencies(copyId: string, userId: string, values:
}
export async function pickSaveArchive(window: BrowserWindow | null): Promise<{ path: string | null }> {
const options = { properties: ['openFile' as const], filters: [{ name: 'ZIP', extensions: ['zip'] }] };
const defaultPath = await resolveDialogDefaultPath('save-archive');
const options = {
properties: ['openFile' as const],
filters: [{ name: 'ZIP', extensions: ['zip'] }],
...(defaultPath ? { defaultPath } : {}),
};
const result = window ? await dialog.showOpenDialog(window, options) : await dialog.showOpenDialog(options);
return { path: result.canceled ? null : result.filePaths[0] ?? null };
const chosen = result.canceled ? null : (result.filePaths[0] ?? null);
if (chosen) await rememberDialogPath('save-archive', chosen);
return { path: chosen };
}
export async function exportSave(copyId: string, userId: string, window: BrowserWindow | null): Promise<{ path: string | null }> {
const copy = await requireCopy(copyId);
await getSaveDetails(copyId, userId);
const options = { defaultPath: `WTOffline-save-${userId}.zip`, filters: [{ name: 'ZIP', extensions: ['zip'] }] };
const defaultDir = await resolveDialogDefaultPath('save-archive');
const fileName = `WTOffline-save-${userId}.zip`;
const options = {
defaultPath: defaultDir ? join(defaultDir, fileName) : fileName,
filters: [{ name: 'ZIP', extensions: ['zip'] }],
};
const result = window ? await dialog.showSaveDialog(window, options) : await dialog.showSaveDialog(options);
if (result.canceled || !result.filePath) return { path: null };
try {
@@ -113,6 +126,7 @@ export async function exportSave(copyId: string, userId: string, window: Browser
console.error('[saves] could not export the save:', error);
throw new AppError('save_operation_failed');
}
await rememberDialogPath('save-archive', result.filePath);
return { path: result.filePath };
}
@@ -275,9 +289,16 @@ export async function copyOnlineData(copyId: string, userId: string, accountId:
}
export async function pickOfficialBackups(window: BrowserWindow | null): Promise<string[]> {
const options = { properties: ['openFile' as const, 'multiSelections' as const], filters: [{ name: 'BLK', extensions: ['blk'] }] };
const defaultPath = await resolveDialogDefaultPath('official-backups');
const options = {
properties: ['openFile' as const, 'multiSelections' as const],
filters: [{ name: 'BLK', extensions: ['blk'] }],
...(defaultPath ? { defaultPath } : {}),
};
const result = window ? await dialog.showOpenDialog(window, options) : await dialog.showOpenDialog(options);
return result.canceled ? [] : result.filePaths;
if (result.canceled || result.filePaths.length === 0) return [];
if (result.filePaths[0]) await rememberDialogPath('official-backups', result.filePaths[0]);
return result.filePaths;
}
export async function previewSelectedBackups(