diff --git a/app/electron/main.js b/app/electron/main.js index 26b3b204f..79d2cbb46 100644 --- a/app/electron/main.js +++ b/app/electron/main.js @@ -52,10 +52,8 @@ try { } } catch (e) { console.error(e) - require('electron'). - dialog. - showErrorBox('创建配置目录失败 Failed to create config directory', - '思源需要在用户家目录下创建配置文件夹(~/.config/siyuan),请确保该路径具有写入权限。\n\nSiYuan needs to create a configuration folder (~/.config/siyuan) in the user\'s home directory. Please make sure that the path has write permissions.') + require('electron').dialog.showErrorBox('创建配置目录失败 Failed to create config directory', + '思源需要在用户家目录下创建配置文件夹(~/.config/siyuan),请确保该路径具有写入权限。\n\nSiYuan needs to create a configuration folder (~/.config/siyuan) in the user\'s home directory. Please make sure that the path has write permissions.') app.exit() } @@ -352,6 +350,20 @@ const boot = () => { }) } +const showWindow = (wnd) => { + if (!wnd || wnd.isDestroyed()) { + return + } + + if (wnd.isMinimized()) { + wnd.restore() + } + if (!wnd.isVisible()) { + wnd.show() + } + wnd.focus() +} + const initKernel = (workspace, lang) => { return new Promise(async (resolve) => { bootWindow = new BrowserWindow({ @@ -428,8 +440,7 @@ const initKernel = (workspace, lang) => { kernelPort + ']') kernelProcess.on('close', (code) => { - writeLog( - `kernel [pid=${kernelProcessPid}] exited with code [${code}]`) + writeLog(`kernel [pid=${kernelProcessPid}] exited with code [${code}]`) if (0 !== code) { switch (code) { case 20: @@ -454,7 +465,12 @@ const initKernel = (workspace, lang) => { '⚠️ 无法读写块树文件 Failed to access blocktree file', `
块树文件正在被其他程序锁定或者已经损坏,请删除 工作空间/temp/ 文件夹后重启
The block tree file is being locked by another program or is corrupted, please delete the workspace/temp/ folder and restart.
`) break - case 24: + case 24: // 工作空间已被锁定,尝试切换到第一个打开的工作空间 + if (workspaces && 0 < workspaces.length) { + showWindow(workspaces[0].browserWindow) + return + } + showErrorWindow( '⚠️ 工作空间已被锁定 The workspace is locked', `
该工作空间正在被使用。
The workspace is in use.
`) @@ -567,7 +583,7 @@ app.whenReady().then(() => { ? lang.hideWindow : lang.showWindow, click: () => { - showHideWnd(tray, lang, mainWindow) + showHideWindow(tray, lang, mainWindow) }, }, { @@ -617,7 +633,8 @@ app.whenReady().then(() => { const contextMenu = Menu.buildFromTemplate(trayMenuTemplate) tray.setContextMenu(contextMenu) } - const showHideWnd = (tray, lang, mainWindow) => { + + const showHideWindow = (tray, lang, mainWindow) => { if (!mainWindow.isVisible()) { if (mainWindow.isMinimized()) { mainWindow.restore() @@ -634,14 +651,7 @@ app.whenReady().then(() => { app.exit() }) ipcMain.on('siyuan-show', (event, id) => { - const mainWindow = BrowserWindow.fromId(id) - if (mainWindow.isMinimized()) { - mainWindow.restore() - } - if (!mainWindow.isVisible()) { - mainWindow.show() - } - mainWindow.focus() + showWindow(BrowserWindow.fromId(id)) }) ipcMain.on('siyuan-config-tray', (event, data) => { workspaces.find(item => { @@ -712,20 +722,13 @@ app.whenReady().then(() => { } }) ipcMain.on('siyuan-open-workspace', (event, data) => { - const exitWorkspace = workspaces.find((item, index) => { + const foundWorkspace = workspaces.find((item, index) => { if (item.workspaceDir === data.workspace) { - const mainWindow = item.browserWindow - if (mainWindow.isMinimized()) { - mainWindow.restore() - } - if (!mainWindow.isVisible()) { - mainWindow.show() - } - mainWindow.focus() + showWindow(item.browserWindow) return true } }) - if (!exitWorkspace) { + if (!foundWorkspace) { initKernel(data.workspace, data.lang).then((isSucc) => { if (isSucc) { boot() @@ -750,7 +753,7 @@ app.whenReady().then(() => { const mainWindow = BrowserWindow.fromId(data.id) resetTrayMenu(tray, data.languages, mainWindow) tray.on('click', () => { - showHideWnd(tray, data.languages, mainWindow) + showHideWindow(tray, data.languages, mainWindow) }) } workspaces.find(item => { @@ -843,8 +846,8 @@ app.whenReady().then(() => { } else { const getArg = (name) => { for (let i = 0; i < process.argv.length; i++) { - if (process.argv[i] === name) { - return process.argv[i + 1] + if (process.argv[i].startsWith(name)) { + return process.argv[i].split('=')[1] } } } @@ -871,13 +874,36 @@ app.on('open-url', (event, url) => { // for macOS } }) -app.on('second-instance', (event, commandLine) => { +app.on('second-instance', (event, argv) => { + writeLog('second-instance [' + argv + ']') + const siyuanURL = argv.find((arg) => arg.startsWith('siyuan://')) + let workspace = argv.find((arg) => arg.startsWith('--workspace=')) + if (workspace) { + workspace = workspace.split('=')[1] + writeLog('got second-instance arg [--workspace=' + workspace + ']') + } + + let foundWorkspace = false workspaces.forEach(item => { if (item.browserWindow && !item.browserWindow.isDestroyed()) { - item.browserWindow.webContents.send('siyuan-openurl', - commandLine.find((arg) => arg.startsWith('siyuan://'))) + if (siyuanURL) { + item.browserWindow.webContents.send('siyuan-openurl', siyuanURL) + return + } + + if (workspace && workspace === item.workspaceDir) { + showWindow(item.browserWindow) + foundWorkspace = true + } } }) + if (!foundWorkspace) { + initKernel(workspace).then((isSucc) => { + if (isSucc) { + boot() + } + }) + } }) app.on('activate', () => {