Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: SHOW_EXPORT_FILE_DIALOGとSHOW_PROJECT_SAVE_DIALOGを統合 #2549

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/backend/browser/fileImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export type WritableFilePath =

// NOTE: fixedExportEnabled が有効になっている GENERATE_AND_SAVE_AUDIO action では、ファイル名に加えディレクトリ名も指定された状態でfilePathが渡ってくる
// また GENERATE_AND_SAVE_ALL_AUDIO action では fixedExportEnabled の有効の有無に関わらず、ディレクトリ名も指定された状態でfilePathが渡ってくる
// showExportFilePicker での疑似パスが渡ってくる可能性もある。
// showSaveFilePicker での疑似パスが渡ってくる可能性もある。
export const writeFileImpl = async (obj: {
filePath: WritableFilePath;
buffer: ArrayBuffer;
Expand Down Expand Up @@ -265,10 +265,10 @@ export const readFileImpl = async (filePath: string) => {

// ファイル選択ダイアログを開く
// 返り値はファイルパスではなく、疑似パスを返す
export const showExportFilePickerImpl: (typeof window)[typeof SandboxKey]["showExportFileDialog"] =
export const showSaveFilePickerImpl: (typeof window)[typeof SandboxKey]["showSaveFileDialog"] =
async (obj: {
defaultPath?: string;
extensionName: string;
name: string;
extensions: string[];
title: string;
}) => {
Expand Down
26 changes: 6 additions & 20 deletions src/backend/browser/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defaultEngine } from "./contract";
import {
checkFileExistsImpl,
readFileImpl,
showExportFilePickerImpl,
showSaveFilePickerImpl,
showOpenDirectoryDialogImpl,
showOpenFilePickerImpl,
WritableFilePath,
Expand Down Expand Up @@ -85,20 +85,6 @@ export const api: Sandbox = {
showOpenDirectoryDialog(obj: { title: string }) {
return showOpenDirectoryDialogImpl(obj);
},
showProjectSaveDialog(obj: { title: string; defaultPath?: string }) {
return new Promise((resolve, reject) => {
if (obj.defaultPath == undefined) {
reject(
// storeやvue componentからdefaultPathを設定していなかったらrejectされる
new Error(
"ブラウザ版ではファイルの保存機能が一部サポートされていません。",
),
);
} else {
resolve(obj.defaultPath);
}
});
},
async showOpenFileDialog(obj: {
title: string;
name: string;
Expand All @@ -116,13 +102,13 @@ export const api: Sandbox = {
});
return fileHandle?.[0];
},
async showExportFileDialog(obj: {
defaultPath?: string;
extensionName: string;
extensions: string[];
async showSaveFileDialog(obj: {
title: string;
name: string;
extensions: string[];
defaultPath?: string;
}) {
const fileHandle = await showExportFilePickerImpl(obj);
const fileHandle = await showSaveFilePickerImpl(obj);
return fileHandle;
},
writeFile(obj: { filePath: string; buffer: ArrayBuffer }) {
Expand Down
24 changes: 6 additions & 18 deletions src/backend/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,21 +418,6 @@ registerIpcMainHandle<IpcMainHandle>({
return result.filePaths[0];
},

SHOW_PROJECT_SAVE_DIALOG: async (_, { title, defaultPath }) => {
const result = await retryShowSaveDialogWhileSafeDir(() =>
windowManager.showSaveDialog({
title,
defaultPath,
filters: [{ name: "VOICEVOX Project file", extensions: ["vvproj"] }],
properties: ["showOverwriteConfirmation"],
}),
);
if (result.canceled) {
return undefined;
}
return result.filePath;
},

SHOW_WARNING_DIALOG: (_, { title, message }) => {
return windowManager.showMessageBox({
type: "warning",
Expand All @@ -458,18 +443,21 @@ registerIpcMainHandle<IpcMainHandle>({
})?.[0];
},

SHOW_EXPORT_FILE_DIALOG: async (
SHOW_SAVE_FILE_DIALOG: async (
_,
{ title, defaultPath, extensionName, extensions },
{ title, defaultPath, name, extensions },
) => {
const result = await retryShowSaveDialogWhileSafeDir(() =>
windowManager.showSaveDialog({
title,
defaultPath,
filters: [{ name: extensionName, extensions: extensions }],
filters: [{ name, extensions }],
properties: ["createDirectory"],
}),
);
if (result.canceled) {
return undefined;
}
return result.filePath;
},

Expand Down
15 changes: 4 additions & 11 deletions src/backend/electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ const api: Sandbox = {
return ipcRendererInvokeProxy.SHOW_OPEN_DIRECTORY_DIALOG({ title });
},

showProjectSaveDialog: ({ title, defaultPath }) => {
return ipcRendererInvokeProxy.SHOW_PROJECT_SAVE_DIALOG({
title,
defaultPath,
});
},

showOpenFileDialog: ({ title, name, extensions, defaultPath }) => {
return ipcRendererInvokeProxy.SHOW_OPEN_FILE_DIALOG({
title,
Expand All @@ -61,12 +54,12 @@ const api: Sandbox = {
});
},

showExportFileDialog: ({ title, defaultPath, extensionName, extensions }) => {
return ipcRendererInvokeProxy.SHOW_EXPORT_FILE_DIALOG({
showSaveFileDialog: ({ title, name, extensions, defaultPath }) => {
return ipcRendererInvokeProxy.SHOW_SAVE_FILE_DIALOG({
title,
defaultPath,
extensionName,
name,
extensions,
defaultPath,
});
},

Expand Down
18 changes: 9 additions & 9 deletions src/store/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1376,11 +1376,11 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
defaultAudioFileName,
);
} else {
filePath ??= await window.backend.showExportFileDialog({
filePath ??= await window.backend.showSaveFileDialog({
title: "音声を保存",
defaultPath: defaultAudioFileName,
extensionName: "WAV ファイル",
name: "WAV ファイル",
extensions: ["wav"],
defaultPath: defaultAudioFileName,
});
}

Expand Down Expand Up @@ -1525,11 +1525,11 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
defaultFileName,
);
} else {
filePath ??= await window.backend.showExportFileDialog({
filePath ??= await window.backend.showSaveFileDialog({
title: "音声を全て繋げて保存",
defaultPath: defaultFileName,
extensionName: "WAV ファイル",
name: "WAV ファイル",
extensions: ["wav"],
defaultPath: defaultFileName,
});
}

Expand Down Expand Up @@ -1670,11 +1670,11 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
defaultFileName,
);
} else {
filePath ??= await window.backend.showExportFileDialog({
filePath ??= await window.backend.showSaveFileDialog({
title: "文章を全て繋げてテキストファイルに保存",
defaultPath: defaultFileName,
extensionName: "テキストファイル",
name: "テキストファイル",
extensions: ["txt"],
defaultPath: defaultFileName,
});
}

Expand Down
4 changes: 3 additions & 1 deletion src/store/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,10 @@ export const projectStore = createPartialStore<ProjectStoreTypes>({
}

// Write the current status to a project file.
const ret = await window.backend.showProjectSaveDialog({
const ret = await window.backend.showSaveFileDialog({
title: "プロジェクトファイルの保存",
name: "VOICEVOX Project file",
extensions: ["vvproj"],
defaultPath,
});
if (ret == undefined) {
Expand Down
12 changes: 6 additions & 6 deletions src/store/singing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2756,11 +2756,11 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
if (state.savingSetting.fixedExportEnabled) {
filePath = path.join(state.savingSetting.fixedExportDir, fileName);
} else {
filePath ??= await window.backend.showExportFileDialog({
filePath ??= await window.backend.showSaveFileDialog({
title: "音声を保存",
defaultPath: fileName,
name: "WAV ファイル",
extensions: ["wav"],
extensionName: "WAV ファイル",
defaultPath: fileName,
});
}
if (!filePath) {
Expand Down Expand Up @@ -3519,11 +3519,11 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
buffer = (await ufProjectToMultiFile(project, fileType))[0];
}

let filePath = await window.backend.showExportFileDialog({
let filePath = await window.backend.showSaveFileDialog({
title: "プロジェクトを書き出し",
defaultPath: fileBaseName,
extensionName: fileTypeLabel,
name: fileTypeLabel,
extensions: [extension],
defaultPath: fileBaseName,
});
if (!filePath) {
return { result: "CANCELED", path: "" };
Expand Down
9 changes: 2 additions & 7 deletions src/type/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ export type IpcIHData = {
return?: string;
};

SHOW_PROJECT_SAVE_DIALOG: {
args: [obj: { title: string; defaultPath?: string }];
return?: string;
};

SHOW_WARNING_DIALOG: {
args: [
obj: {
Expand All @@ -85,12 +80,12 @@ export type IpcIHData = {
return: MessageBoxReturnValue;
};

SHOW_EXPORT_FILE_DIALOG: {
SHOW_SAVE_FILE_DIALOG: {
args: [
obj: {
title: string;
defaultPath?: string;
extensionName: string;
name: string;
extensions: string[];
},
];
Expand Down
10 changes: 3 additions & 7 deletions src/type/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,18 @@ export interface Sandbox {
getInitialProjectFilePath(): Promise<string | undefined>;
showSaveDirectoryDialog(obj: { title: string }): Promise<string | undefined>;
showOpenDirectoryDialog(obj: { title: string }): Promise<string | undefined>;
showProjectSaveDialog(obj: {
title: string;
defaultPath?: string;
}): Promise<string | undefined>;
showOpenFileDialog(obj: {
title: string;
name: string;
mimeType: string;
extensions: string[];
defaultPath?: string;
}): Promise<string | undefined>;
showExportFileDialog(obj: {
showSaveFileDialog(obj: {
title: string;
defaultPath?: string;
extensionName: string;
name: string;
extensions: string[];
defaultPath?: string;
}): Promise<string | undefined>;
writeFile(obj: {
filePath: string;
Expand Down
Loading