Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //require electron module
- const electron = require('electron')
- //core node js module
- const url = require('url');
- const path = require('path');
- const {app,BrowserWindow,Menu,ipcMain}=electron;
- //SET ENV
- //remove developer tools if in production
- //process.env.NODE_ENV = "production";
- let mainWindow;
- let addWindow;
- //listen for app to be ready
- app.on('ready',function(){
- //create new window
- mainWindow=new BrowserWindow({});
- //Load html into window
- mainWindow.loadURL(url.format({
- pathname: path.join(__dirname, 'mainWindow.html'),
- protocol:'file:',
- slashes:true
- }));
- //Quit app when closed
- mainWindow.on('closed', function(){
- app.quit();
- });
- //Build menu from template
- const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
- //Insert menu
- Menu.setApplicationMenu(mainMenu);
- });
- //Handle create add window
- function createAddWindow(){
- //create new window
- addWindow=new BrowserWindow({
- width:300,
- height:200,
- title:'Add Shopping List item'
- });
- //Load html into window
- addWindow.loadURL(url.format({
- pathname: path.join(__dirname, 'addWindow.html'),
- protocol:'file:',
- slashes:true
- }));
- //Garbage collection handle
- addWindow.on('close',function(){
- addWindow=null;
- });
- }
- //Catch item:add
- ipcMain.on('item:add',function(e,item){
- // console.log(item);
- mainWindow.webContents.send('item:add',item);
- addWindow.close();
- });
- //Create menu template
- const mainMenuTemplate = [
- {
- label:'File',
- submenu:[
- {
- label:'Add Item',
- click(){
- createAddWindow();
- }
- },
- {
- label:'Clear Items',
- click(){
- //send event item:clear to main window
- mainWindow.webContents.send('item:clear');
- }
- },
- {
- label:'Quit',
- accelerator: process.platform =='darwin' ? 'Command+Q':'Ctrl+Q',
- click(){
- app.quit();
- }
- }
- ]
- }
- ];
- //If mac, add empty object to menu
- if(process.platform=='darwin'){
- //adds on empty object '{}' to beginning of array. Need this part for Macs so 'file' shows in top right of main window
- mainMenuTemplate.unshift({});
- }
- //Add developer tools item if not in production
- if(process.env.NODE_ENV !== 'production'){
- mainMenuTemplate.push({
- label:'Developer Tools',
- submenu:[
- {
- label:'Toggle DevTools',
- accelerator: process.platform =='darwin' ? 'Command+I':'Ctrl+I',
- click(item , focusedWindow){
- focusedWindow.toggleDevTools();
- }
- },
- {
- role:'reload'
- }
- ]
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement