From b4816f6294646157b50bb49f1d19eaf306e0ac8c Mon Sep 17 00:00:00 2001
From: he wei <858544502@qq.com>
Date: 星期日, 13 十一月 2022 22:06:22 +0800
Subject: [PATCH] UA 0.1.4发布

---
 src/background.js |  130 ++++++++++++++++++++++++++-----------------
 1 files changed, 79 insertions(+), 51 deletions(-)

diff --git a/src/background.js b/src/background.js
index f2a15cb..ee05c10 100644
--- a/src/background.js
+++ b/src/background.js
@@ -3,7 +3,7 @@
 import { app, protocol, BrowserWindow, ipcMain, ipcRenderer, dialog, shell, Menu, webContents } from 'electron'
 import { autoUpdater } from 'electron-updater';
 import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
-import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
+// import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
 import path from 'path';
 import child_process from 'child_process';
 import log from 'electron-log';
@@ -11,6 +11,10 @@
 
 autoUpdater.logger = log;
 autoUpdater.logger.transports.file.level = 'info';
+
+let win = null;
+
+
 
 // 璋冭瘯鑷姩鍗囩骇
 /**
@@ -28,11 +32,25 @@
  * 
  * 
  */
-Object.defineProperty(app, 'isPackaged', {
-  get() {
-    return true;
-  }
-});
+
+// 鍗曚緥閿�
+const gotTheLock = app.requestSingleInstanceLock()
+
+if (!gotTheLock) {
+  app.quit()
+} else {
+  app.on('second-instance', (event, commandLine, workingDirectory) => {
+    // 褰撹繍琛岀浜屼釜瀹炰緥鏃�,灏嗕細鑱氱劍鍒癿yWindow杩欎釜绐楀彛
+    if (win) {
+      if (win.isMinimized()) win.restore()
+      win.focus()
+      if (commandLine[2]) {
+        win.webContents.send('selected-file', { filePaths: [commandLine[2]] }, 'MenuList');
+      }
+    }
+  })
+
+}
 
 
 const remote = require('@electron/remote/main');
@@ -50,11 +68,10 @@
 if (!isDevelopment) {
   Menu.setApplicationMenu(null);
 }
-let win;
-async function createWindow() {
+async function createWindow(cb) {
   // Create the browser window.
   win = new BrowserWindow({
-    width: 800,
+    width: 1000,
     height: 600,
     webPreferences: {
       // Use pluginOptions.nodeIntegration, leave this alone
@@ -80,8 +97,52 @@
     createProtocol('app')
     // Load the index.html when not in development
     win.loadURL('app://./index.html')
-    win.webContents.openDevTools()
   }
+  if (cb && 'function' == typeof cb) {
+    win.once('ready-to-show', () => {
+      cb(win.webContents);
+    })
+  }
+
+  win.webContents.session.on('will-download', (e, item) => {
+    // const filePath = path.join(saveUrl, item.getFilename());
+    // item.setSavePath(filePath); // 'C:\Users\kim\Downloads\绗�12娆�.zip'
+    // let value;
+    // //鐩戝惉涓嬭浇杩囩▼锛岃绠楀苟璁剧疆杩涘害鏉¤繘搴�
+    // item.on('updated', (evt, state) => {
+    //   if ('progressing' === state) {
+    //     //姝ゅ  鐢ㄦ帴鏀跺埌鐨勫瓧鑺傛暟鍜屾�诲瓧鑺傛暟姹備竴涓瘮渚�  灏辨槸杩涘害鐧惧垎姣�
+    //     if (item.getReceivedBytes() && item.getTotalBytes()) {
+    //       value = parseInt(
+    //         100 * (
+    //           item.getReceivedBytes() / item.getTotalBytes()
+    //         )
+    //       )
+    //     }
+    //     // 鎶婄櫨鍒嗘瘮鍙戠粰娓叉煋杩涚▼杩涜灞曠ず
+    //     // win.webContents.send('updateProgressing', value);
+    //     // mac 绋嬪簭鍧炪�亀indows 浠诲姟鏍忔樉绀鸿繘搴�
+    //     win.setProgressBar(value);
+    //   }
+    // });
+    //鐩戝惉涓嬭浇缁撴潫浜嬩欢
+    item.on('done', (e, state) => {
+      //濡傛灉绐楀彛杩樺湪鐨勮瘽锛屽幓鎺夎繘搴︽潯
+      // if (!win.isDestroyed()) {
+      //   win.setProgressBar(-1);
+      // }
+      //涓嬭浇琚彇娑堟垨涓柇浜�
+      if (state === 'interrupted') {
+        dialog.showErrorBox('涓嬭浇澶辫触', `鏂囦欢 ${item.getFilename()} 鍥犱负鏌愪簺鍘熷洜琚腑鏂笅杞絗);
+      }
+      // 涓嬭浇鎴愬姛鍚庢墦寮�鏂囦欢鎵�鍦ㄦ枃浠跺す
+      if (state === 'completed') {
+        setTimeout(() => {
+          shell.showItemInFolder(item.getSavePath())
+        }, 1000);
+      }
+    });
+  });
 }
 
 // Quit when all windows are closed.
@@ -103,28 +164,14 @@
 // initialization and is ready to create browser windows.
 // Some APIs can only be used after this event occurs.
 app.on('ready', async () => {
-  // if (isDevelopment && !process.env.IS_TEST) {
-  //   // Install Vue Devtools
-  //   try {
-  //     await installExtension(VUEJS_DEVTOOLS)
-  //   } catch (e) {
-  //     console.error('Vue Devtools failed to install:', e.toString())
-  //   }
-  // }
-  createWindow();
+  let cb = undefined;
+  if (process.argv[1]) {
+    cb = (sender) => {
+      sender.send('selected-file', { filePaths: [process.argv[1]] }, 'MenuList');
+    }
+  }
+  createWindow(cb);
 })
-
-// ipcMain 浜嬩欢澶勭悊
-// ipcMain.on('open-file-dialog', (event) => {
-//   dialog.showOpenDialog({
-//     properties: ['openFile', 'openDirectory']
-//   }, (files) => {
-//     // child_process.spawn('cmd.exe', ['/c', 'echo '+JSON.stringify(files)+' success >> 112233.txt'])
-//     if(files) {
-//       event.sender.send('selected-directory', files);
-//     }
-//   })
-// });
 
 // 鎵�鏈夌殑鏂板紑绐楀彛浜嬩欢閮借皟鐢ㄧ郴缁熼粯璁ゆ祻瑙堝櫒鏉ユ墦寮�椤甸潰
 app.on('web-contents-created', (e, webContents) => {
@@ -155,25 +202,6 @@
   })
 });
 
-// 瀛樺湪鏂扮増鏈椂锛岄粯璁よ嚜鍔ㄤ笅杞芥洿鏂�
-// autoUpdater.autoDownload = false // 鑻ユ兂閫氳繃娓叉煋杩涚▼鎵嬪姩瑙﹀彂锛岄渶瑕佽缃產utoDownload涓篺alse
-// 褰撴洿鏂板彂鐢熼敊璇殑鏃跺�欒Е鍙�
-// autoUpdater.on('error', (err) => {})
-// // 褰撳紑濮嬫鏌ユ洿鏂扮殑鏃跺�欒Е鍙�
-// autoUpdater.on('checking-for-update', (event, arg) => {}) // 閫氱煡娓叉煋杩涚▼锛屾鏃跺紑濮嬫娴嬬増鏈�
-// // 鍙戠幇鍙洿鏂扮増鏈椂瑙﹀彂
-// autoUpdater.on('update-available', (info) => {}) // 鍙戠幇鍙洿鏂扮増鏈�-寮瑰眰璇㈤棶娓叉煋杩涚▼鏄惁闇�瑕佲�滅珛鍗虫洿鏂扳��
-// // 娌℃湁鍙洿鏂扮増鏈椂瑙﹀彂
-// autoUpdater.on('update-not-available', (info) => {}) // 閫氱煡娓叉煋杩涚▼锛屾病鏈夋娴嬪埌鏂扮増鏈�
-// // 鍙戠幇鍙洿鏂扮増鏈悗-鐢ㄦ埛鎿嶄綔鈥滅珛鍗虫洿鏂扳�濓紝姝ゆ椂瑙﹀彂寮�濮嬩笅杞芥洿鏂�
-// autoUpdater.downloadUpdate().then((path)=>{})
-// // 涓嬭浇鐩戝惉-涓嬭浇杩涘害鏁版嵁(0-100%)
-// autoUpdater.on('download-progress', (progressObj) => {}) //閫氱煡娓叉煋杩涚▼锛屼笅杞借繘搴︽潯鐣岄潰UI灞曠ず
-// // 鏇存柊鍖呬笅杞藉畬鎴愭椂瑙﹀彂
-// autoUpdater.on('update-downloaded', () => {}) // 鐢ㄤ簬閫氱煡娓叉煋杩涚▼涓嬭浇瀹屾垚-寮瑰眰璇㈤棶鏄惁绔嬪埢閲嶅惎骞跺畨瑁�
-// 鎵ц 閲嶅惎搴旂敤绋嬪簭骞朵笅杞藉悗瀹夎鏇存柊锛堝彧鑳藉湪update-downloaded鍙戝嚭鍚庤皟鐢級
-// autoUpdater.quitAndInstall()
-
 ipcMain.on('check-update', (event) => {
   // checkForUpdates();
   update(event.sender);
@@ -185,7 +213,7 @@
 
 
 // Exit cleanly on request from parent process in development mode.
-if (isDevelopment) {
+if (isDevelopment) { false;
   if (process.platform === 'win32') {
     process.on('message', (data) => {
       if (data === 'graceful-exit') {

--
Gitblit v1.9.1