Skip to content

Commit

Permalink
feat: 初回ファイルの取得をbackendの関数として扱う
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenc-nanashi committed Feb 8, 2025
1 parent 074bcec commit 4eb2c27
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 60 deletions.
3 changes: 3 additions & 0 deletions src/backend/browser/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export const api: Sandbox = {
// NOTE: ブラウザ版ではサポートされていません
return Promise.resolve({});
},
getInitialProjectFilePath() {
return Promise.resolve(undefined);
},
showSaveDirectoryDialog(obj: { title: string }) {
return showOpenDirectoryDialogImpl(obj);
},
Expand Down
75 changes: 50 additions & 25 deletions src/backend/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
EngineId,
TextAsset,
} from "@/type/preload";
import { isMac } from "@/helpers/platform";
import { isMac, isProduction } from "@/helpers/platform";
import { createLogger } from "@/helpers/log";

type SingleInstanceLockData = {
Expand Down Expand Up @@ -245,7 +245,27 @@ function checkMultiEngineEnabled(): boolean {
return enabled;
}

let filePathOnMac: string | undefined = undefined;
let argv: string[] = [];
if (!isMac) {
// 製品版:引数はargv[1]以降をそのまま
if (isProduction) {
argv = process.argv.slice(1);
}
// 開発版:引数は`--`がある場合は`--`以降、無い場合は引数なしとして扱う
else {
const index = process.argv.indexOf("--");
if (index !== -1) {
argv = process.argv.slice(index + 1);
}
}
}

let initialFilePath: string | undefined = undefined;
let initialProjectFilePath: string | undefined = undefined;

if (!isMac) {
initialFilePath = argv[0];
}

const menuTemplateForMac: Electron.MenuItemConstructorOptions[] = [
{
Expand Down Expand Up @@ -355,6 +375,10 @@ registerIpcMainHandle<IpcMainHandle>({
return engineInfoManager.altPortInfos;
},

GET_INITIAL_PROJECT_FILE_PATH: async () => {
return initialProjectFilePath;
},

/**
* 保存先になるディレクトリを選ぶダイアログを表示する。
*/
Expand Down Expand Up @@ -689,7 +713,7 @@ app.once("will-finish-launching", () => {
// macOS only
app.once("open-file", (event, filePath) => {
event.preventDefault();
filePathOnMac = filePath;
initialFilePath = filePath;
});
});

Expand Down Expand Up @@ -817,45 +841,46 @@ void app.whenReady().then(async () => {
);
}

// runEngineAllの前にVVPPを読み込む
let filePath: string | undefined;
if (process.platform === "darwin") {
filePath = filePathOnMac;
} else {
if (process.argv.length > 1) {
filePath = process.argv[1];
}
}

// 多重起動防止
// TODO: readyを待たずにもっと早く実行すべき
if (
!isDevelopment &&
!isTest &&
!app.requestSingleInstanceLock({
filePath,
} as SingleInstanceLockData)
filePath: initialFilePath,
} satisfies SingleInstanceLockData)
) {
log.info("VOICEVOX already running. Cancelling launch.");
log.info(`File path sent: ${filePath}`);
log.info(`File path sent: ${initialFilePath}`);
appState.willQuit = true;
app.quit();
return;
}

if (filePath && isVvppFile(filePath)) {
log.info(`vvpp file install: ${filePath}`);
// FIXME: GUI側に合流させる
if (checkMultiEngineEnabled()) {
await engineAndVvppController.installVvppEngineWithWarning({
vvppPath: filePath,
reloadNeeded: false,
});
if (initialFilePath) {
if (isVvppFile(initialFilePath)) {
log.info(`vvpp file install: ${initialFilePath}`);
// FIXME: GUI側に合流させる
if (checkMultiEngineEnabled()) {
await engineAndVvppController.installVvppEngineWithWarning({
vvppPath: initialFilePath,
reloadNeeded: false,
});
}
} else if (
fs.existsSync(initialFilePath) &&
fs.statSync(initialFilePath).isFile() &&
initialFilePath.endsWith(".vvproj")
) {
log.info(`vvproj file load: ${initialFilePath}`);
initialProjectFilePath = initialFilePath;
} else {
log.warn(`Unrecognized file path: ${initialFilePath}`);
}
}

await engineAndVvppController.launchEngines();
await windowManager.createWindow(filePath);
await windowManager.createWindow();
});

// 他のプロセスが起動したとき、`requestSingleInstanceLock`経由で`rawData`が送信される。
Expand Down
34 changes: 3 additions & 31 deletions src/backend/electron/manager/windowManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fs from "fs";
import path from "path";
import {
BrowserWindow,
Expand All @@ -13,7 +12,6 @@ import windowStateKeeper from "electron-window-state";
import { getConfigManager } from "../electronConfig";
import { getEngineAndVvppController } from "../engineAndVvppController";
import { ipcMainSendProxy } from "../ipc";
import { isMac } from "@/helpers/platform";
import { themes } from "@/domain/theme";
import { createLogger } from "@/helpers/log";

Expand Down Expand Up @@ -57,7 +55,7 @@ class WindowManager {
return this._win;
}

public async createWindow(filePathOnMac: string | undefined) {
public async createWindow() {
if (this.win != undefined) {
throw new Error("Window has already been created");
}
Expand Down Expand Up @@ -88,27 +86,6 @@ class WindowManager {
icon: path.join(this.staticDir, "icon.png"),
});

let projectFilePath = "";
if (isMac) {
if (filePathOnMac) {
if (filePathOnMac.endsWith(".vvproj")) {
projectFilePath = filePathOnMac;
}
filePathOnMac = undefined;
}
} else {
if (process.argv.length >= 2) {
const filePath = process.argv[1];
if (
fs.existsSync(filePath) &&
fs.statSync(filePath).isFile() &&
filePath.endsWith(".vvproj")
) {
projectFilePath = filePath;
}
}
}

win.on("maximize", () => {
ipcMainSendProxy.DETECT_MAXIMIZED(win);
});
Expand Down Expand Up @@ -149,21 +126,17 @@ class WindowManager {
mainWindowState.manage(win);
this._win = win;

await this.load({ projectFilePath });
await this.load({});

if (this.isDevelopment && !this.isTest) win.webContents.openDevTools();
}

/**
* 画面の読み込みを開始する。
* @param obj.isMultiEngineOffMode マルチエンジンオフモードにするかどうか。無指定時はfalse扱いになる。
* @param obj.projectFilePath 初期化時に読み込むプロジェクトファイル。無指定時は何も読み込まない。
* @returns ロードの完了を待つPromise。
*/
public async load(obj: {
isMultiEngineOffMode?: boolean;
projectFilePath?: string;
}) {
public async load(obj: { isMultiEngineOffMode?: boolean }) {
const win = this.getWindow();
const firstUrl =
import.meta.env.VITE_DEV_SERVER_URL ?? "app://./index.html";
Expand All @@ -172,7 +145,6 @@ class WindowManager {
"isMultiEngineOffMode",
(obj?.isMultiEngineOffMode ?? false).toString(),
);
url.searchParams.append("projectFilePath", obj?.projectFilePath ?? "");
await win.loadURL(url.toString());
}

Expand Down
4 changes: 4 additions & 0 deletions src/backend/electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ const api: Sandbox = {
return await ipcRendererInvokeProxy.GET_ALT_PORT_INFOS();
},

getInitialProjectFilePath: async () => {
return await ipcRendererInvokeProxy.GET_INITIAL_PROJECT_FILE_PATH();
},

showSaveDirectoryDialog: ({ title }) => {
return ipcRendererInvokeProxy.SHOW_SAVE_DIRECTORY_DIALOG({ title });
},
Expand Down
4 changes: 1 addition & 3 deletions src/components/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ onMounted(async () => {
await store.actions.INIT_VUEX();
// プロジェクトファイルのパスを取得
const projectFilePath = urlParams.get("projectFilePath");
// ショートカットキーの設定を登録
const hotkeySettings = store.state.hotkeySettings;
hotkeyManager.load(structuredClone(toRaw(hotkeySettings)));
Expand Down Expand Up @@ -163,6 +160,7 @@ onMounted(async () => {
});
// プロジェクトファイルが指定されていればロード
const projectFilePath = await window.backend.getInitialProjectFilePath();
if (typeof projectFilePath === "string" && projectFilePath !== "") {
isProjectFileLoaded.value = await store.actions.LOAD_PROJECT_FILE({
type: "path",
Expand Down
5 changes: 5 additions & 0 deletions src/type/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export type IpcIHData = {
return: AltPortInfos;
};

GET_INITIAL_PROJECT_FILE_PATH: {
args: [];
return: string | undefined;
};

SHOW_SAVE_DIRECTORY_DIALOG: {
args: [obj: { title: string }];
return?: string;
Expand Down
1 change: 1 addition & 0 deletions src/type/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export interface Sandbox {
getAppInfos(): Promise<AppInfos>;
getTextAsset<K extends keyof TextAsset>(textType: K): Promise<TextAsset[K]>;
getAltPortInfos(): Promise<AltPortInfos>;
getInitialProjectFilePath(): Promise<string | undefined>;
showSaveDirectoryDialog(obj: { title: string }): Promise<string | undefined>;
showVvppOpenDialog(obj: {
title: string;
Expand Down
8 changes: 7 additions & 1 deletion vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@ export default defineConfig((options) => {
onstart: ({ startup }) => {
console.log("main process build is complete.");
if (!skipLaunchElectron) {
void startup([".", "--no-sandbox"]);
// process.argvは["node", ".../vite.js", "--", その他引数]の形式
void startup([
".",
"--no-sandbox",
"--",
...process.argv.slice(3),
]);
}
},
vite: {
Expand Down

0 comments on commit 4eb2c27

Please sign in to comment.