main.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. const {app, BrowserWindow, Menu, ipcMain} = require('electron');
  2. const Store = require('electron-store');
  3. const path = require('path');
  4. const url = require('url');
  5. let safeExit = false;
  6. const store = new Store({
  7. defaults: {
  8. // 1000x600 is the default size of our window
  9. windowSize: [ 1000, 600 ],
  10. windowPos: [ 200, 200 ],
  11. filePath: null
  12. }
  13. });
  14. function sendReq(msg, data=null) {
  15. win.webContents.send('mainprocess-request', {msg: msg, data: data});
  16. }
  17. // Window menu template
  18. const menuTemplate = [
  19. {
  20. label: 'File',
  21. submenu : [
  22. {
  23. label: 'New', accelerator: 'CmdOrCtrl+N',
  24. click () { sendReq('NewFile'); }
  25. },{
  26. label: 'Open', accelerator: 'CmdOrCtrl+O',
  27. click () { sendReq('OpenFile'); }
  28. },{
  29. label: 'Save', accelerator: 'CmdOrCtrl+S',
  30. click () { sendReq('SaveFile'); }
  31. },{
  32. label: 'Save As', accelerator: 'CmdOrCtrl+Shift+S',
  33. click () { sendReq('SaveAsFile'); }
  34. },{type: 'separator'},{
  35. label: 'Export',
  36. accelerator: 'CmdOrCtrl+E',
  37. click () { sendReq('ExportImage'); }
  38. }
  39. ]
  40. },{
  41. label: 'Edit',
  42. submenu:[
  43. {
  44. label: 'Undo', accelerator: 'CmdOrCtrl+Z',
  45. click () { sendReq('Undo'); }
  46. },{
  47. label: 'Redo', accelerator: 'CmdOrCtrl+Shift+Z',
  48. click () { sendReq('Redo'); }
  49. },{
  50. label: 'Clear', accelerator: 'CmdOrCtrl+Shift+C',
  51. click () { sendReq('Clear'); }
  52. }
  53. ]
  54. },{
  55. label: 'View',
  56. submenu: [
  57. // {role: 'reload'},
  58. // {role: 'forcereload'},
  59. // {role: 'toggledevtools'},
  60. // {type: 'separator'},
  61. {role: 'resetzoom'},
  62. {role: 'zoomin'},
  63. {role: 'zoomout'},
  64. {type: 'separator'},
  65. {role: 'togglefullscreen'}
  66. ]
  67. },{
  68. role: 'help',
  69. submenu:[
  70. {
  71. label: 'Learn More',
  72. accelerator: 'CmdOrCtrl+H',
  73. click () {
  74. createInfoWindow();
  75. }
  76. }
  77. ]
  78. }
  79. ];
  80. // add mac menu (which is a little bit different from Linux/Win)
  81. if (process.platform === 'darwin') {
  82. menuTemplate.unshift({
  83. label: app.getName(),
  84. submenu: [
  85. {role: 'about'},
  86. {type: 'separator'},
  87. {role: 'services', submenu: []},
  88. {type: 'separator'},
  89. {role: 'hide'},
  90. {role: 'hideothers'},
  91. {role: 'unhide'},
  92. {type: 'separator'},
  93. {role: 'quit'}
  94. ]
  95. })
  96. }
  97. // Init win
  98. let win;
  99. let infoWin;
  100. function createWindow() {
  101. // Create browser window
  102. let pos = store.get('windowPos');
  103. let x = pos[0];
  104. let y = pos[1];
  105. let shape = store.get('windowSize');
  106. let width = shape[0];
  107. let height = shape[1];
  108. win = new BrowserWindow({width: width, height: height, x: x, y: y, icon: path.join(__dirname, '/res/icons/icon.png')});
  109. win.loadURL(url.format({
  110. pathname: path.join(__dirname, 'index.html'),
  111. protocol: 'file',
  112. slashes: true
  113. }));
  114. // Open devtool
  115. // win.webContents.openDevTools();
  116. const menu = Menu.buildFromTemplate(menuTemplate);
  117. Menu.setApplicationMenu(menu);
  118. win.on('resize', () => {
  119. let size = win.getSize();
  120. store.set('windowSize', size);
  121. });
  122. win.on('move', () => {
  123. let pos = win.getPosition();
  124. store.set('windowPos', pos);
  125. })
  126. win.on('close', (e) => {
  127. if(!safeExit) {
  128. e.preventDefault();
  129. sendReq('Exitting');
  130. }
  131. });
  132. }
  133. function createInfoWindow() {
  134. infoWin = new BrowserWindow({
  135. width: 400, height: 450,
  136. resizable: false,
  137. parent: win,
  138. icon: path.join(__dirname, '/res/icons/icon.png')
  139. });
  140. infoWin.loadURL(url.format({
  141. pathname: path.join(__dirname, '/src/info.html'),
  142. protocol: 'file',
  143. slashes: true
  144. }));
  145. // infoWin.webContents.openDevTools();
  146. infoWin.on('close', (e) => {
  147. infoWin = null;
  148. });
  149. }
  150. // Run create window
  151. app.on('ready', createWindow)
  152. // Quit when all windows are closed
  153. app.on('window-all-closed', () => {
  154. if(process.platform != 'darwin'){
  155. app.quit();
  156. }
  157. });
  158. ipcMain.on('renderer-response', (event, arg) => {
  159. switch(arg.msg) {
  160. case 'Exit':
  161. // https://github.com/sindresorhus/electron-store
  162. safeExit=true;
  163. app.quit();
  164. break;
  165. }
  166. });
  167. ipcMain.on('renderer-request', (event, arg) => {
  168. switch(arg.msg) {
  169. case 'TitleChanged':
  170. store.set('filePath', arg.data);
  171. // console.log(arg.data);
  172. break;
  173. case 'ToInit':
  174. sendReq("Init", store.get('filePath'));
  175. break;
  176. default:
  177. break;
  178. }
  179. });