浏览代码

code refactor

MaysWind 6 年之前
父节点
当前提交
7f00c44ea8

+ 3 - 3
app/index.html

@@ -77,9 +77,9 @@
                         </a>
                     </li>
                     <li>
-                        <a class="toolbar btn-title-maxmize" title="{{(windowContext.maximized ? 'Restore Down' : 'Maximize') | translate}}" ng-click="maximizeOrRestoreWindow()">
-                            <i class="fa fa-window-maximize" ng-if="!windowContext.maximized"></i>
-                            <i class="fa fa-window-restore ng-cloak" ng-if="windowContext.maximized"></i>
+                        <a class="toolbar btn-title-maxmize" title="{{(nativeWindowContext.maximized ? 'Restore Down' : 'Maximize') | translate}}" ng-click="maximizeOrRestoreWindow()">
+                            <i class="fa fa-window-maximize" ng-if="!nativeWindowContext.maximized"></i>
+                            <i class="fa fa-window-restore ng-cloak" ng-if="nativeWindowContext.maximized"></i>
                         </a>
                     </li>
                     <li>

+ 1 - 1
app/scripts/controllers/main.js

@@ -49,7 +49,7 @@
             ariaNgNotificationService.requestBrowserPermission();
         }
 
-        $scope.ariaNgVersion = ariaNgNativeElectronService.version();
+        $scope.ariaNgVersion = ariaNgNativeElectronService.getVersion();
 
         $scope.globalStatusContext = {
             isEnabled: ariaNgSettingService.getGlobalStatRefreshInterval() > 0,

+ 2 - 2
app/scripts/controllers/settings-ariang.js

@@ -51,8 +51,8 @@
 
         $scope.context = {
             currentTab: 'global',
-            ariaNgNativeVersion: ariaNgNativeElectronService.version(),
-            ariaNgVersion: ariaNgNativeElectronService.ariaNgVersion(),
+            ariaNgNativeVersion: ariaNgNativeElectronService.getVersion(),
+            ariaNgVersion: ariaNgNativeElectronService.getAriaNgVersion(),
             runtimeEnvironment: ariaNgNativeElectronService.getRuntimeEnvironment(),
             runtimeEnvironmentCollapsed: true,
             languages: ariaNgLanguages,

+ 7 - 11
app/scripts/core/root.js

@@ -200,22 +200,22 @@
             }
         };
 
-        $rootScope.windowContext = {
+        $rootScope.nativeWindowContext = {
             maximized: false
         };
 
         $rootScope.useCustomAppTitle = ariaNgNativeElectronService.useCustomAppTitle();
-        $rootScope.windowContext.maximized = ariaNgNativeElectronService.isMaximized();
+        $rootScope.nativeWindowContext.maximized = ariaNgNativeElectronService.isMaximized();
 
-        ariaNgNativeElectronService.registerEvent('maximize', function () {
-            $rootScope.windowContext.maximized = true;
+        ariaNgNativeElectronService.onMainWindowEvent('maximize', function () {
+            $rootScope.nativeWindowContext.maximized = true;
         });
 
-        ariaNgNativeElectronService.registerEvent('unmaximize', function () {
-            $rootScope.windowContext.maximized = false;
+        ariaNgNativeElectronService.onMainWindowEvent('unmaximize', function () {
+            $rootScope.nativeWindowContext.maximized = false;
         });
 
-        ariaNgNativeElectronService.onMessage('navigate-to', function (event, routeUrl) {
+        ariaNgNativeElectronService.onMainProcessMessage('navigate-to', function (event, routeUrl) {
             $location.path(routeUrl);
         });
 
@@ -285,10 +285,6 @@
             $document.unbind('keypress');
         });
 
-        $rootScope.$on('$viewContentLoaded', function () {
-            ariaNgNativeElectronService.initTray();
-        });
-
         $rootScope.$on('$translateChangeSuccess', function(event, current, previous) {
             ariaNgNativeElectronService.setTrayLanguage();
         });

+ 41 - 37
app/scripts/services/ariaNgNativeElectronService.js

@@ -6,30 +6,37 @@
         var remote = electron.remote || {
             require: function () {
                 return {};
-            },
-            getGlobal: function () {
-                return {};
-            },
-            getCurrentWindow: function () {
-                return {};
             }
         };
         var ipcRenderer = electron.ipcRenderer || {};
-        var shell = electron.shell || {
-            openExternal: function () {
-                return false;
+        var shell = electron.shell || {};
+        var cmd = remote.require('./cmd') || {};
+        var tray = remote.require('./tray') || {};
+        var localfs = remote.require('./localfs') || {};
+
+        var getSetting = function (item) {
+            if (!remote || !remote.getGlobal) {
+                return null;
+            }
+
+            var settings = remote.getGlobal('settings');
+
+            if (!settings) {
+                return null;
             }
+
+            return settings[item];
+        };
+
+        var getCurrentWindow = function () {
+            if (!remote || !remote.getCurrentWindow) {
+                return {};
+            }
+
+            return remote.getCurrentWindow();
         };
-        var cmd = remote.require('./cmd');
-        var tray = remote.require('./tray');
-        var localfs = remote.require('./localfs');
 
         return {
-            remote: remote,
-            shell: shell,
-            getSettings: function () {
-                return remote.getGlobal('settings');
-            },
             getRuntimeEnvironment: function () {
                 if (!remote.process || !remote.process.versions) {
                     return null;
@@ -45,35 +52,32 @@
 
                 return items;
             },
-            version: function() {
-                return this.getSettings().version;
+            getVersion: function() {
+                return getSetting('version');
             },
-            ariaNgVersion: function() {
-                return this.getSettings().ariaNgVersion;
+            getAriaNgVersion: function() {
+                return getSetting('ariaNgVersion');
             },
             isDevMode: function () {
-                return !!this.getSettings().isDevMode;
+                return !!getSetting('isDevMode');
             },
             useCustomAppTitle: function () {
-                return !!this.getSettings().useCustomAppTitle;
-            },
-            getCurrentWindow: function () {
-                return remote.getCurrentWindow();
+                return !!getSetting('useCustomAppTitle');
             },
             isLocalFSExists: function (fullpath) {
                 return localfs.isExists(fullpath);
             },
             openExternalLink: function (url) {
-                return shell.openExternal(url);
+                return shell.openExternal && shell.openExternal(url);
             },
             openFileInDirectory: function (dir, filename) {
                 var fullpath = localfs.getFullPath(dir, filename);
-                return shell.showItemInFolder(fullpath);
+                return shell.showItemInFolder && shell.showItemInFolder(fullpath);
             },
-            registerEvent: function (event, callback) {
-                this.getCurrentWindow().on && this.getCurrentWindow().on(event, callback);
+            onMainWindowEvent: function (event, callback) {
+                getCurrentWindow().on && getCurrentWindow().on(event, callback);
             },
-            onMessage: function (messageType, callback) {
+            onMainProcessMessage: function (messageType, callback) {
                 ipcRenderer.on && ipcRenderer.on(messageType, callback);
             },
             initTray: function () {
@@ -92,20 +96,20 @@
                 return cmd.getAndClearToBeCreatedTaskFilePath();
             },
             isMaximized: function () {
-                return this.getCurrentWindow().isMaximized && this.getCurrentWindow().isMaximized();
+                return getCurrentWindow().isMaximized && getCurrentWindow().isMaximized();
             },
             minimizeWindow: function () {
-                this.getCurrentWindow().minimize();
+                getCurrentWindow().minimize && getCurrentWindow().minimize();
             },
             maximizeOrRestoreWindow: function () {
-                if (!this.getCurrentWindow().isMaximized()) {
-                    this.getCurrentWindow().maximize();
+                if (!this.isMaximized()) {
+                    getCurrentWindow().maximize && getCurrentWindow().maximize();
                 } else {
-                    this.getCurrentWindow().unmaximize();
+                    getCurrentWindow().unmaximize && getCurrentWindow().unmaximize();
                 }
             },
             exitApp: function () {
-                this.getCurrentWindow().close();
+                getCurrentWindow().close && getCurrentWindow().close();
             }
         };
     }]);