ariaNgNativeElectronService.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. (function () {
  2. 'use strict';
  3. angular.module('ariaNg').factory('ariaNgNativeElectronService', ['ariaNgLocalizationService', function (ariaNgLocalizationService) {
  4. var electron = angular.isFunction(window.nodeRequire) ? nodeRequire('electron') : {};
  5. var remote = electron.remote || {
  6. require: function () {
  7. return {};
  8. }
  9. };
  10. var ipcRenderer = electron.ipcRenderer || {};
  11. var shell = electron.shell || {};
  12. var config = remote.require('./config') || {};
  13. var menu = remote.require('./menu') || {};
  14. var tray = remote.require('./tray') || {};
  15. var localfs = remote.require('./localfs') || {};
  16. var getSetting = function (item) {
  17. if (!remote || !remote.getGlobal) {
  18. return null;
  19. }
  20. var settings = remote.getGlobal('settings');
  21. if (!settings) {
  22. return null;
  23. }
  24. return settings[item];
  25. };
  26. var getCurrentWindow = function () {
  27. if (!remote || !remote.getCurrentWindow) {
  28. return {};
  29. }
  30. return remote.getCurrentWindow();
  31. };
  32. var onMainWindowEvent = function (event, callback) {
  33. getCurrentWindow().on && getCurrentWindow().on(event, callback);
  34. };
  35. var onMainProcessMessage = function (channel, callback) {
  36. ipcRenderer.on && ipcRenderer.on(channel, callback);
  37. };
  38. var removeMainProcessCallback = function (channel, callback) {
  39. ipcRenderer.removeListener && ipcRenderer.removeListener(channel, callback);
  40. };
  41. var sendMessageToMainProcess = function (channel, message) {
  42. ipcRenderer.send && ipcRenderer.send(channel, message);
  43. };
  44. return {
  45. getRuntimeEnvironment: function () {
  46. if (!remote.process || !remote.process.versions) {
  47. return null;
  48. }
  49. var versions = remote.process.versions;
  50. var items = [];
  51. items.push({name: 'Electron', value: versions.electron});
  52. items.push({name: 'Node.js', value: versions.node});
  53. items.push({name: 'Chrome', value: versions.chrome});
  54. items.push({name: 'V8', value: versions.v8});
  55. return items;
  56. },
  57. getVersion: function() {
  58. return getSetting('version');
  59. },
  60. getAriaNgVersion: function() {
  61. return getSetting('ariaNgVersion');
  62. },
  63. isDevMode: function () {
  64. return !!getSetting('isDevMode');
  65. },
  66. useCustomAppTitle: function () {
  67. return !!getSetting('useCustomAppTitle');
  68. },
  69. getNativeConfig: function () {
  70. var cfg = {};
  71. for (var key in config) {
  72. if (!config.hasOwnProperty(key)) {
  73. continue;
  74. }
  75. if (angular.isFunction(config[key])) {
  76. continue;
  77. }
  78. cfg[key] = angular.copy(config[key]);
  79. }
  80. return cfg;
  81. },
  82. setDefaultPosition: function (value) {
  83. config.defaultPosition = value;
  84. config.save('defaultPosition');
  85. },
  86. setMinimizedToTray: function (value) {
  87. config.minimizedToTray = !!value;
  88. config.save('minimizedToTray');
  89. },
  90. setMainWindowLanguage: function () {
  91. this.setApplicationMenu();
  92. this.setTrayMenu();
  93. },
  94. isLocalFSExists: function (fullpath) {
  95. return localfs.isExists(fullpath);
  96. },
  97. openProjectLink: function () {
  98. return shell.openExternal && shell.openExternal('https://github.com/mayswind/AriaNg-Native');
  99. },
  100. openProjectReleaseLink: function () {
  101. return shell.openExternal && shell.openExternal('https://github.com/mayswind/AriaNg-Native/releases');
  102. },
  103. openFileInDirectory: function (dir, filename) {
  104. var fullpath = localfs.getFullPath(dir, filename);
  105. if (localfs.isExists(fullpath)) {
  106. return shell.showItemInFolder && shell.showItemInFolder(fullpath);
  107. } else {
  108. return shell.openItem && shell.openItem(dir);
  109. }
  110. },
  111. onMainWindowMaximize: function (callback) {
  112. onMainWindowEvent('maximize', callback);
  113. },
  114. onMainWindowUnmaximize: function (callback) {
  115. onMainWindowEvent('unmaximize', callback);
  116. },
  117. onMainProcessNavigateTo: function (callback) {
  118. onMainProcessMessage('navigate-to', callback);
  119. },
  120. onMainProcessShowError: function (callback) {
  121. onMainProcessMessage('show-error', callback);
  122. },
  123. onMainProcessNewTaskFromFile: function (callback) {
  124. onMainProcessMessage('new-task-from-file', callback);
  125. },
  126. onMainProcessNewTaskFromText: function (callback) {
  127. onMainProcessMessage('new-task-from-text', callback);
  128. },
  129. removeMainProcessNewTaskFromFileCallback: function (callback) {
  130. removeMainProcessCallback('new-task-from-file', callback);
  131. },
  132. removeMainProcessNewTaskFromTextCallback: function (callback) {
  133. removeMainProcessCallback('new-task-from-text', callback);
  134. },
  135. sendViewLoadedMessageToMainProcess: function (message) {
  136. sendMessageToMainProcess('view-content-loaded', message);
  137. },
  138. sendNewDropFileMessageToMainProcess: function (message) {
  139. sendMessageToMainProcess('new-drop-file', message);
  140. },
  141. sendNewDropTextMessageToMainProcess: function (message) {
  142. sendMessageToMainProcess('new-drop-text', message);
  143. },
  144. setApplicationMenu: function () {
  145. if (menu.setApplicationMenu) {
  146. menu.setApplicationMenu({
  147. labels: {
  148. Quit: ariaNgLocalizationService.getLocalizedText('menu.Quit')
  149. }
  150. });
  151. }
  152. },
  153. setTrayMenu: function () {
  154. if (tray.setContextMenu) {
  155. tray.setContextMenu({
  156. labels: {
  157. ShowAriaNgNative: ariaNgLocalizationService.getLocalizedText('tray.ShowAriaNgNative'),
  158. Exit: ariaNgLocalizationService.getLocalizedText('tray.Exit')
  159. }
  160. });
  161. }
  162. },
  163. setTrayToolTip: function (value) {
  164. if (tray.setToolTip) {
  165. tray.setToolTip(value);
  166. }
  167. },
  168. reload: function () {
  169. getCurrentWindow().reload && getCurrentWindow().reload();
  170. },
  171. isMaximized: function () {
  172. return getCurrentWindow().isMaximized && getCurrentWindow().isMaximized();
  173. },
  174. minimizeWindow: function () {
  175. getCurrentWindow().minimize && getCurrentWindow().minimize();
  176. },
  177. maximizeOrRestoreWindow: function () {
  178. if (!this.isMaximized()) {
  179. getCurrentWindow().maximize && getCurrentWindow().maximize();
  180. } else {
  181. getCurrentWindow().unmaximize && getCurrentWindow().unmaximize();
  182. }
  183. },
  184. exitApp: function () {
  185. getCurrentWindow().close && getCurrentWindow().close();
  186. }
  187. };
  188. }]);
  189. }());