duy 1 year ago
parent
commit
01318147ba
2 changed files with 35 additions and 39 deletions
  1. 34 35
      src/main/index.js
  2. 1 4
      src/renderer/src/App.vue

+ 34 - 35
src/main/index.js

@@ -5,6 +5,7 @@ import { BrowserWindow, Menu, app, dialog, ipcMain, shell } from 'electron'
 import { electronApp, is, optimizer } from '@electron-toolkit/utils'
 import icon from '../../resources/icon.png?asset'
 
+let mainWindow
 //菜单设置
 const menu = Menu.buildFromTemplate([
     {
@@ -41,7 +42,7 @@ const menu = Menu.buildFromTemplate([
 Menu.setApplicationMenu(menu)
 
 function createWindow() {
-    const mainWindow = new BrowserWindow({
+    mainWindow = new BrowserWindow({
         width: 900,
         height: 670,
         center: true,
@@ -80,42 +81,14 @@ app.whenReady().then(() => {
 
     // Open file
     app.on('open-file', (event, url) => {
-        //event.preventDefault()
-        /*if (process.platform === 'win32') {
-            process.argv.push(url)
-            const argv = process.argv
-            const filePath = argv[argv.length - 1]
+        event.preventDefault()
+        if (process.platform === 'darwin') {
             BrowserWindow.getFocusedWindow().webContents.send('open-file-vue', {
-                data: 'win',
-                argv: argv,
-                file: filePath,
+                data: url,
             })
-        }*/
-        BrowserWindow.getFocusedWindow().webContents.send('open-file-vue', {
-            data: url,
-        })
+        }
     })
 
-    //禁止多开 点击打开原来应用
-    if (!app.requestSingleInstanceLock()) {
-        app.quit()
-    } else {
-        // 有人试图运行第二个实例,我们应该关注我们的窗口
-        app.on('second-instance', (event, commandLine, workingDirectory, additionalData) => {
-            BrowserWindow.getFocusedWindow().webContents.send('open-file-second', {
-                commandLine: commandLine,
-                workingDirectory: workingDirectory,
-                additionalData: additionalData,
-            })
-            /*const win = BrowserWindow.getFocusedWindow().webContents
-            if (win) {
-                if (win.isMaximized()) win.restore()
-                if (!win.isVisible()) win.show()
-                win.focus()
-            }*/
-        })
-    }
-
     // IPC showOpenDialog
     ipcMain.handle('showOpenDialog', async (event, options) => {
         return new Promise((resolve) => {
@@ -162,13 +135,39 @@ app.whenReady().then(() => {
         }
     })
 
-    createWindow()
+    //禁止多开 点击打开原来应用
+    if (!app.requestSingleInstanceLock()) {
+        app.quit()
+    } else {
+        createWindow()
+        // 有人试图运行第二个实例,我们应该关注我们的窗口
+        app.on('second-instance', (event, argv) => {
+            if (process.platform !== 'darwin') {
+                const url = argv[argv.length - 1]
+                if (!mainWindow) {
+                    createWindow()
+                    return
+                }
+                if (mainWindow?.isMinimized()) {
+                    mainWindow?.restore()
+                }
+                if (mainWindow?.isVisible()) {
+                    mainWindow?.show()
+                }
+                mainWindow.focus()
+                mainWindow.webContents.send('open-file-vue', { data: url })
+            }
+        })
+    }
 
     app.on('activate', function () {
-        if (BrowserWindow.getAllWindows().length === 0) createWindow()
+        if (BrowserWindow.getAllWindows().length === 0) {
+            createWindow()
+        }
     })
 })
 
+
 app.on('will-quit', () => {
     if (process.platform !== 'darwin') {
         app.quit()

+ 1 - 4
src/renderer/src/App.vue

@@ -5,7 +5,7 @@
 </template>
 
 <script setup>
-import { nextTick, onMounted } from 'vue'
+import { nextTick } from 'vue'
 import { isNullES, setElementMainColor } from 'js-fast-way'
 import { toPage } from './utils/tools'
 import split from 'split.js'
@@ -29,9 +29,6 @@ const setIpcRenderer = () => {
             },
         })
     })
-    window.electron.ipcRenderer.on('open-file-second', (event, { data }) => {
-        console.log('open-file-second', data)
-    })
 }
 </script>