main.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. 'use strict';
  2. const os = require('os');
  3. const electron = require('electron');
  4. const electronLocalshortcut = require('electron-localshortcut');
  5. const pkgfile = require('../package');
  6. const core = require('./core');
  7. const cmd = require('./cmd');
  8. const config = require('./config/config');
  9. const constants = require('./config/constants');
  10. const menu = require('./components/menu');
  11. const tray = require('./components/tray');
  12. const file = require('./lib/file');
  13. const page = require('./lib/page');
  14. const websocket = require('./lib/websocket');
  15. const process = require('./lib/process');
  16. const ipcRender = require('./ipc/render-proecss');
  17. require('./ipc/main-process');
  18. const app = electron.app;
  19. const BrowserWindow = electron.BrowserWindow;
  20. const singletonLock = app.requestSingleInstanceLock({
  21. argv: cmd.argv
  22. });
  23. if (!singletonLock) {
  24. app.quit();
  25. return;
  26. }
  27. let filePathInCommandLine = cmd.argv.file;
  28. function isEnableCloseToHide() {
  29. return (tray.isEnabled() || os.platform() === 'darwin') && config.minimizedToTray;
  30. }
  31. global.settings = {
  32. version: pkgfile.version,
  33. ariaNgVersion: pkgfile['ariang-version'],
  34. isDevMode: cmd.argv.development,
  35. useCustomAppTitle: false
  36. };
  37. if (!app.isPackaged) {
  38. global.settings.isDevMode = true;
  39. }
  40. app.setAppUserModelId(pkgfile.appId);
  41. if (os.platform() === 'win32' && !cmd.argv.classic) {
  42. global.settings.useCustomAppTitle = true;
  43. }
  44. if (os.platform() === 'darwin') {
  45. app.on('will-finish-launching', () => {
  46. app.on('open-file', (event, filePath) => {
  47. if (filePath) {
  48. filePathInCommandLine = filePath;
  49. }
  50. });
  51. });
  52. app.on('before-quit', () => {
  53. core.isConfirmExit = true;
  54. });
  55. app.on('activate', () => {
  56. if (core.mainWindow) {
  57. core.mainWindow.show();
  58. }
  59. });
  60. }
  61. if (config.execCommandOnStartup) {
  62. const options = {
  63. command: config.execCommandOnStartup,
  64. args: config.execCommandArgumentsOnStartup,
  65. detached: config.execDetachedCommandOnStartup,
  66. };
  67. if (global.settings.isDevMode) {
  68. options.onoutput = function (output) {
  69. const lastOutput = (core.startupCommandOutput.length > 1 ? core.startupCommandOutput[core.startupCommandOutput.length - 1] : null);
  70. if (lastOutput && lastOutput.source === output.source && lastOutput.content === output.content) {
  71. lastOutput.count++
  72. } else {
  73. if (core.startupCommandOutput.length >= constants.startupCommandConstants.outputLogLimit) {
  74. core.startupCommandOutput.shift();
  75. }
  76. core.startupCommandOutput.push({
  77. time: new Date(),
  78. type: 'output',
  79. source: output.source,
  80. content: output.content,
  81. count: output.count
  82. });
  83. }
  84. };
  85. options.onerror = function (error) {
  86. if (core.startupCommandOutput.length >= constants.startupCommandConstants.outputLogLimit) {
  87. core.startupCommandOutput.shift();
  88. }
  89. core.startupCommandOutput.push({
  90. time: new Date(),
  91. type: 'error',
  92. source: error.type,
  93. content: error.error
  94. });
  95. };
  96. }
  97. process.execCommandAsync(options);
  98. }
  99. app.on('window-all-closed', () => {
  100. app.quit();
  101. });
  102. app.on('second-instance', (event, argv, workingDirectory, additionalData) => {
  103. if (core.mainWindow) {
  104. if (core.mainWindow.isMinimized()) {
  105. core.mainWindow.restore();
  106. } else if (!core.mainWindow.isVisible()) {
  107. core.mainWindow.show();
  108. }
  109. core.mainWindow.focus();
  110. let filePath = null;
  111. if (additionalData && additionalData.argv) {
  112. filePath = additionalData.argv.file;
  113. }
  114. if (!filePath) {
  115. filePath = cmd.parseFilePath(argv);
  116. }
  117. if (filePath && file.isContainsSupportedFileArg(filePath)) {
  118. ipcRender.notifyRenderProcessNewNewTaskFromFileAfterViewLoaded(filePath);
  119. ipcRender.notifyRenderProcessNavigateToNewTask();
  120. }
  121. }
  122. });
  123. app.on('ready', () => {
  124. core.mainWindow = new BrowserWindow({
  125. title: 'AriaNg Native',
  126. width: config.width,
  127. height: config.height,
  128. minWidth: 800,
  129. minHeight: 400,
  130. fullscreenable: false,
  131. frame: !global.settings.useCustomAppTitle,
  132. show: false,
  133. webPreferences: {
  134. nodeIntegration: true,
  135. contextIsolation: false,
  136. spellcheck: false
  137. }
  138. });
  139. let displays = electron.screen.getAllDisplays();
  140. let isLastPositionInScreen = false;
  141. if (config.x >= 0 && config.y >= 0) {
  142. for (let i = 0; i < displays.length; i++) {
  143. let x1 = displays[i].bounds.x;
  144. let x2 = x1 + displays[i].workAreaSize.width;
  145. let y1 = displays[i].bounds.y;
  146. let y2 = y1 + displays[i].workAreaSize.height;
  147. if (config.x >= x1 && config.x <= x2 && config.y >= y1 && config.y <= y2) {
  148. isLastPositionInScreen = true;
  149. break;
  150. }
  151. }
  152. }
  153. if (config.defaultPosition === 'last-position' && isLastPositionInScreen) {
  154. core.mainWindow.setPosition(config.x, config.y);
  155. } else if (config.defaultPosition === 'screen-center') {
  156. core.mainWindow.center();
  157. }
  158. if (config.maximized) {
  159. core.mainWindow.maximize();
  160. }
  161. if (global.settings.isDevMode) {
  162. electronLocalshortcut.register(core.mainWindow, 'F12', () => {
  163. core.mainWindow.webContents.openDevTools();
  164. });
  165. }
  166. menu.init();
  167. tray.init();
  168. if (file.isContainsSupportedFileArg(filePathInCommandLine)) {
  169. ipcRender.notifyRenderProcessNewNewTaskFromFileAfterViewLoaded(filePathInCommandLine);
  170. core.mainWindow.loadURL(page.getPageFullUrl(constants.ariaNgPageLocations.NewTask));
  171. } else {
  172. core.mainWindow.loadURL(page.getPageFullUrl());
  173. }
  174. core.mainWindow.once('ready-to-show', () => {
  175. core.mainWindow.show();
  176. });
  177. core.mainWindow.on('resize', () => {
  178. let sizes = core.mainWindow.getSize();
  179. config.width = sizes[0];
  180. config.height = sizes[1];
  181. });
  182. core.mainWindow.on('maximize', () => {
  183. config.maximized = core.mainWindow.isMaximized();
  184. ipcRender.notifyRenderProcessWindowMaximized(core.mainWindow.isMaximized());
  185. });
  186. core.mainWindow.on('unmaximize', () => {
  187. config.maximized = core.mainWindow.isMaximized();
  188. ipcRender.notifyRenderProcessWindowUnmaximized(core.mainWindow.isMaximized());
  189. });
  190. core.mainWindow.on('move', () => {
  191. let positions = core.mainWindow.getPosition();
  192. config.x = positions[0];
  193. config.y = positions[1];
  194. });
  195. core.mainWindow.on('close', (event) => {
  196. if (isEnableCloseToHide() && !core.isConfirmExit) {
  197. event.preventDefault();
  198. core.mainWindow.hide();
  199. event.returnValue = false;
  200. }
  201. });
  202. core.mainWindow.on('closed', () => {
  203. try {
  204. if (!config.maximized) {
  205. if (config.width > 0) {
  206. config.save('width');
  207. }
  208. if (config.height > 0) {
  209. config.save('height');
  210. }
  211. if (config.x > 0 && config.y > 0) {
  212. config.save('x');
  213. config.save('y');
  214. }
  215. }
  216. config.save('maximized');
  217. } catch (ex) {
  218. ; // Do Nothing
  219. }
  220. core.mainWindow = null;
  221. });
  222. ipcRender.onRenderProcessElectronServiceInited((event) => {
  223. websocket.init();
  224. });
  225. ipcRender.onRenderProcessNewDropFile((event, arg) => {
  226. if (!arg) {
  227. return;
  228. }
  229. let filePath = arg.filePath;
  230. let location = arg.location;
  231. if (location.indexOf('/new') === 0) {
  232. ipcRender.notifyRenderProcessNewTaskFromFile(filePath);
  233. } else {
  234. ipcRender.notifyRenderProcessNewNewTaskFromFileAfterViewLoaded(filePath);
  235. ipcRender.notifyRenderProcessNavigateToNewTask();
  236. }
  237. });
  238. ipcRender.onRenderProcessNewDropText((event, arg) => {
  239. if (!arg) {
  240. return;
  241. }
  242. let text = arg.text;
  243. let location = arg.location;
  244. if (location.indexOf('/new') === 0) {
  245. ipcRender.notifyRenderProcessNewTaskFromText(text);
  246. } else {
  247. ipcRender.notifyRenderProcessNewNewTaskFromTextAfterViewLoaded(text);
  248. ipcRender.notifyRenderProcessNavigateToNewTask();
  249. }
  250. });
  251. });