Browse Source

code refactor

MaysWind 3 years ago
parent
commit
2ecd0c4cbc
2 changed files with 21 additions and 12 deletions
  1. 5 12
      app/scripts/services/ariaNgNativeElectronService.js
  2. 16 0
      main/ipc.js

+ 5 - 12
app/scripts/services/ariaNgNativeElectronService.js

@@ -9,7 +9,6 @@
             }
         };
         var ipcRenderer = electron.ipcRenderer || {};
-        var shell = electron.shell || {};
         var config = remote.require('./config') || {};
         var menu = remote.require('./menu') || {};
         var tray = remote.require('./tray') || {};
@@ -50,8 +49,8 @@
             ipcRenderer.removeListener && ipcRenderer.removeListener(channel, callback);
         };
 
-        var sendMessageToMainProcess = function (channel, message) {
-            ipcRenderer.send && ipcRenderer.send(channel, message);
+        var sendMessageToMainProcess = function (channel, ...args) {
+            ipcRenderer.send && ipcRenderer.send(channel, ...args);
         };
 
         return {
@@ -126,19 +125,13 @@
                 return info;
             },
             openProjectLink: function () {
-                return shell.openExternal && shell.openExternal('https://github.com/mayswind/AriaNg-Native');
+                sendMessageToMainProcess('open-external-url', 'https://github.com/mayswind/AriaNg-Native');
             },
             openProjectReleaseLink: function () {
-                return shell.openExternal && shell.openExternal('https://github.com/mayswind/AriaNg-Native/releases');
+                sendMessageToMainProcess('open-external-url', 'https://github.com/mayswind/AriaNg-Native/releases');
             },
             openFileInDirectory: function (dir, filename) {
-                var fullpath = localfs.getFullPath(dir, filename);
-
-                if (localfs.isExists(fullpath)) {
-                    return shell.showItemInFolder && shell.showItemInFolder(fullpath);
-                } else {
-                    return shell.openItem && shell.openItem(dir);
-                }
+                sendMessageToMainProcess('open-local-directory', dir, filename);
             },
             onMainWindowMaximize: function (callback) {
                 onMainWindowEvent('maximize', callback);

+ 16 - 0
main/ipc.js

@@ -7,7 +7,9 @@ const electron = require('electron');
 
 const pkgfile = require('../package');
 const core = require('./core');
+const localfs = require('./localfs');
 
+const shell = electron.shell;
 const ipcMain = electron.ipcMain;
 
 const supportedFileExtensions = {
@@ -127,6 +129,20 @@ let asyncNewTaskFromText = function (text) {
     });
 };
 
+ipcMain.on('open-external-url', (event, url) => {
+    shell.openExternal(url);
+});
+
+ipcMain.on('open-local-directory', (event, dir, filename) => {
+    var fullpath = localfs.getFullPath(dir, filename);
+
+    if (localfs.isExists(fullpath)) {
+        return shell.showItemInFolder && shell.showItemInFolder(fullpath);
+    } else {
+        return shell.openItem && shell.openItem(dir);
+    }
+});
+
 module.exports = {
     loadIndexUrl: loadIndexUrl,
     loadNewTaskUrl: loadNewTaskUrl,