main.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict';
  6. const config = require('./config');
  7. const app = require('app'); // Module to control application life.
  8. const BrowserWindow = require('browser-window'); // Module to create native browser window.
  9. const http = require('http');
  10. const Menu = require('menu');
  11. //const Tray = require('tray');
  12. //let is_debug = true;
  13. let is_debug = false;
  14. // Report crashes to our server.
  15. require('crash-reporter').start();
  16. // Keep a global reference of the window object, if you don't, the window will
  17. // be closed automatically when the javascript object is GCed.
  18. let mainWindow = null;
  19. let force_quit = false;
  20. // Quit when all windows are closed.
  21. app.on('window-all-closed', function () {
  22. if (process.platform != 'darwin') {
  23. app.quit();
  24. }
  25. });
  26. let appIcon = null;
  27. // This method will be called when atom-shell has done everything
  28. // initialization and ready for creating browser windows.
  29. app.on('ready', function () {
  30. // Create the browser window.
  31. mainWindow = new BrowserWindow({width: 800, height: 600});
  32. // and load the index.html of the app.
  33. mainWindow.loadURL('file://' + __dirname + '/index.html');
  34. if (is_debug) {
  35. mainWindow.toggleDevTools();
  36. }
  37. mainWindow.on('close', function (e) {
  38. if (!force_quit) {
  39. e.preventDefault();
  40. mainWindow.hide();
  41. }
  42. });
  43. // You can use 'before-quit' instead of (or with) the close event
  44. app.on('before-quit', function (e) {
  45. // Handle menu-item or keyboard shortcut quit here
  46. if (!force_quit) {
  47. e.preventDefault();
  48. mainWindow.hide();
  49. }
  50. });
  51. // Remove mainWindow.on('closed'), as it is redundant
  52. /*
  53. // Emitted when the window is closed.
  54. mainWindow.on('closed', function () {
  55. // Dereference the window object, usually you would store windows
  56. // in an array if your app supports multi windows, this is the time
  57. // when you should delete the corresponding element.
  58. mainWindow = null;
  59. app.quit();
  60. });*/
  61. app.on('activate', function () {
  62. mainWindow.show();
  63. });
  64. /*
  65. //console.log('file://' + __dirname + '/images/t.png');
  66. // @see https://github.com/atom/electron/blob/master/docs/api/tray.md
  67. appIcon = new Tray(__dirname + '/images/t.png');
  68. //appIcon = new Tray('/Users/wu/studio/owl/sh3/app/images/t.png');
  69. let contextMenu = Menu.buildFromTemplate([
  70. {label: 'Item1', type: 'radio'},
  71. {label: 'Item2', type: 'radio'},
  72. {label: 'Item3', type: 'radio', checked: true},
  73. {label: 'Item4', type: 'radio'}
  74. ]);
  75. appIcon.setToolTip('This is my application.');
  76. appIcon.setContextMenu(contextMenu);
  77. */
  78. let template = [{
  79. label: 'Edit',
  80. submenu: [{
  81. label: 'Undo',
  82. accelerator: 'CmdOrCtrl+Z',
  83. role: 'undo'
  84. }, {
  85. label: 'Redo',
  86. accelerator: 'Shift+CmdOrCtrl+Z',
  87. role: 'redo'
  88. }, {
  89. type: 'separator'
  90. }, {
  91. label: 'Cut',
  92. accelerator: 'CmdOrCtrl+X',
  93. role: 'cut'
  94. }, {
  95. label: 'Copy',
  96. accelerator: 'CmdOrCtrl+C',
  97. role: 'copy'
  98. }, {
  99. label: 'Paste',
  100. accelerator: 'CmdOrCtrl+V',
  101. role: 'paste'
  102. }, {
  103. label: 'Select All',
  104. accelerator: 'CmdOrCtrl+A',
  105. role: 'selectall'
  106. }]
  107. }, {
  108. label: 'View',
  109. submenu: [{
  110. label: 'Toggle Full Screen',
  111. accelerator: (function () {
  112. if (process.platform == 'darwin') {
  113. return 'Ctrl+Command+F';
  114. } else {
  115. return 'F11';
  116. }
  117. })(),
  118. click: function (item, focusedWindow) {
  119. if (focusedWindow) {
  120. focusedWindow.setFullScreen(!focusedWindow.isFullScreen());
  121. }
  122. }
  123. //},
  124. //{
  125. // label: 'Toggle Developer Tools',
  126. // accelerator: (function () {
  127. // if (process.platform == 'darwin') {
  128. // return 'Alt+Command+I';
  129. // } else {
  130. // return 'Ctrl+Shift+I';
  131. // }
  132. // })(),
  133. // click: function (item, focusedWindow) {
  134. // if (focusedWindow) {
  135. // focusedWindow.toggleDevTools();
  136. // }
  137. // }
  138. }]
  139. }, {
  140. label: 'Window',
  141. role: 'window',
  142. submenu: [{
  143. label: 'Minimize',
  144. accelerator: 'CmdOrCtrl+M',
  145. role: 'minimize'
  146. }, {
  147. label: 'Close',
  148. accelerator: 'CmdOrCtrl+W',
  149. role: 'close'
  150. }]
  151. }, {
  152. label: 'Help',
  153. role: 'help',
  154. submenu: [{
  155. label: 'Homepage',
  156. click: function () {
  157. require('electron').shell.openExternal(config.url_homepage);
  158. }
  159. }, {
  160. label: 'Feedback',
  161. click: function () {
  162. require('electron').shell.openExternal(config.url_feedback);
  163. }
  164. }]
  165. }];
  166. if (is_debug) {
  167. template[1].submenu.push({
  168. label: 'Reload',
  169. accelerator: 'CmdOrCtrl+R',
  170. click: function (item, focusedWindow) {
  171. if (focusedWindow)
  172. focusedWindow.reload();
  173. }
  174. });
  175. }
  176. if (process.platform == 'darwin') {
  177. let name = require('electron').app.getName();
  178. template.unshift({
  179. label: name,
  180. submenu: [{
  181. label: 'About ' + name,
  182. role: 'about'
  183. }, {
  184. label: 'Check for Updates...',
  185. click: function () {
  186. require('./js/chk').chkUpdate(config.VERSION, mainWindow);
  187. }
  188. }, {
  189. type: 'separator'
  190. }, {
  191. label: 'Services',
  192. role: 'services',
  193. submenu: []
  194. }, {
  195. type: 'separator'
  196. }, {
  197. label: 'Hide ' + name,
  198. accelerator: 'Command+H',
  199. role: 'hide'
  200. }, {
  201. label: 'Hide Others',
  202. accelerator: 'Command+Shift+H',
  203. role: 'hideothers'
  204. }, {
  205. label: 'Show All',
  206. role: 'unhide'
  207. }, {
  208. type: 'separator'
  209. }, {
  210. label: 'Quit',
  211. accelerator: 'Command+Q',
  212. click: function () {
  213. force_quit = true;
  214. app.quit();
  215. }
  216. }]
  217. });
  218. // Window menu.
  219. template[3].submenu.push(
  220. {
  221. type: 'separator'
  222. },
  223. {
  224. label: 'Bring All to Front',
  225. role: 'front'
  226. }
  227. );
  228. }
  229. let menu = Menu.buildFromTemplate(template);
  230. Menu.setApplicationMenu(menu);
  231. });