Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. //require electron module
  2. const electron = require('electron')
  3.  
  4. //core node js module
  5. const url = require('url');
  6.  
  7. const path = require('path');
  8.  
  9. const {app,BrowserWindow,Menu,ipcMain}=electron;
  10.  
  11. //SET ENV
  12. //remove developer tools if in production
  13. //process.env.NODE_ENV = "production";
  14.  
  15. let mainWindow;
  16. let addWindow;
  17.  
  18. //listen for app to be ready
  19.  
  20. app.on('ready',function(){
  21.  
  22. //create new window
  23.  
  24. mainWindow=new BrowserWindow({});
  25.  
  26. //Load html into window
  27.  
  28. mainWindow.loadURL(url.format({
  29. pathname: path.join(__dirname, 'mainWindow.html'),
  30. protocol:'file:',
  31. slashes:true
  32.  
  33. }));
  34.  
  35. //Quit app when closed
  36. mainWindow.on('closed', function(){
  37. app.quit();
  38.  
  39. });
  40.  
  41. //Build menu from template
  42. const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
  43. //Insert menu
  44. Menu.setApplicationMenu(mainMenu);
  45.  
  46. });
  47.  
  48. //Handle create add window
  49.  
  50. function createAddWindow(){
  51.  
  52. //create new window
  53.  
  54. addWindow=new BrowserWindow({
  55. width:300,
  56. height:200,
  57. title:'Add Shopping List item'
  58.  
  59. });
  60.  
  61. //Load html into window
  62.  
  63. addWindow.loadURL(url.format({
  64. pathname: path.join(__dirname, 'addWindow.html'),
  65. protocol:'file:',
  66. slashes:true
  67.  
  68. }));
  69.  
  70.  
  71.  
  72. //Garbage collection handle
  73. addWindow.on('close',function(){
  74. addWindow=null;
  75.  
  76. });
  77.  
  78. }
  79.  
  80.  
  81. //Catch item:add
  82.  
  83. ipcMain.on('item:add',function(e,item){
  84.  
  85. // console.log(item);
  86.  
  87. mainWindow.webContents.send('item:add',item);
  88. addWindow.close();
  89.  
  90.  
  91.  
  92. });
  93.  
  94.  
  95.  
  96.  
  97.  
  98. //Create menu template
  99.  
  100. const mainMenuTemplate = [
  101.  
  102.  
  103.  
  104. {
  105. label:'File',
  106.  
  107. submenu:[
  108. {
  109. label:'Add Item',
  110. click(){
  111. createAddWindow();
  112.  
  113.  
  114. }
  115.  
  116. },
  117. {
  118. label:'Clear Items',
  119. click(){
  120.  
  121. //send event item:clear to main window
  122.  
  123. mainWindow.webContents.send('item:clear');
  124.  
  125. }
  126. },
  127. {
  128. label:'Quit',
  129. accelerator: process.platform =='darwin' ? 'Command+Q':'Ctrl+Q',
  130.  
  131. click(){
  132. app.quit();
  133. }
  134.  
  135. }
  136.  
  137. ]
  138.  
  139. }
  140. ];
  141.  
  142. //If mac, add empty object to menu
  143.  
  144. if(process.platform=='darwin'){
  145.  
  146. //adds on empty object '{}' to beginning of array. Need this part for Macs so 'file' shows in top right of main window
  147. mainMenuTemplate.unshift({});
  148.  
  149. }
  150.  
  151. //Add developer tools item if not in production
  152.  
  153. if(process.env.NODE_ENV !== 'production'){
  154. mainMenuTemplate.push({
  155. label:'Developer Tools',
  156. submenu:[
  157. {
  158. label:'Toggle DevTools',
  159. accelerator: process.platform =='darwin' ? 'Command+I':'Ctrl+I',
  160. click(item , focusedWindow){
  161. focusedWindow.toggleDevTools();
  162. }
  163. },
  164. {
  165. role:'reload'
  166. }
  167. ]
  168.  
  169. });
  170.  
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement