Not able to use ipc.Main module correctly


#1

I am trying to open up a new about window on-clicking a menu from tray.
main.js

'use strict';
const path = require('path');
const electron = require('electron');
const app = electron.app;
const remote = require('electron').remote;
const ipcMain = require('electron').ipcMain;
const tray = require('./tray');
require('electron-debug')();

// prevent window being garbage collected
   let mainWindow;
   let aboutWindow;

function onClosed() {
mainWindow = null;
aboutWindow = null;
}

function createMainWindow() {
const win = new electron.BrowserWindow({
	width: 1000,
	height: 600,
	icon: process.platform === 'linux' && path.join(__dirname, 'resources/Icon.png'),
	minWidth: 800,
	minHeight: 600,
	titleBarStyle: 'hidden-inset',
	autoHideMenuBar: true
});

win.loadURL('http://akashnimare.in');
win.on('closed', onClosed);
return win;
 }


function createAboutWindow() {
const abouturl = new electron.BrowserWindow({
width: 400,
height: 400,
show: false
})
abouturl.loadURL('file://' + __dirname + '/settings.html');
abouturl.on('closed', onClosed);
return abouturl;
}

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
	app.quit();
}
});

app.on('activate', () => {
if (!mainWindow) {
	mainWindow = createMainWindow();
}
});

app.on('ready', () => {
mainWindow = createMainWindow();
tray.create(mainWindow);

aboutWindow = createAboutWindow();
// tray.create(aboutWindow);

ipcMain.on('About', function () {
aboutWindow.show();
});	
});

tray.js

'use strict';
const path = require('path'); 
const electron = require('electron');
const app = require('electron').app;
const {shell} = require('electron');
const ipcMain = require('electron').ipcMain;
const remote = require('electron').remote;
let tray = null;

exports.create = win => {
if (process.platform === 'darwin' || tray) {
	return;
}

const iconPath = path.join(__dirname, 'resources', 'Icon.png');

const toggleWin = () => {
	if (win.isVisible()) {
		win.hide();
	} else {
		win.show();
	}
};

const reload = () => {
	win.reload();
};

const contextMenu = electron.Menu.buildFromTemplate([
   {
   		label: 'About',
    	click() {
      		ipcMain.send("About");
   		}
   }		
]);

tray = new electron.Tray(iconPath);
tray.setToolTip(`${app.getName()}`);
tray.setContextMenu(contextMenu);
tray.on('click', toggleWin);

};


#2

I don’t think you can fire events from main and receive them on main. When you ipcMain.send() its sending to ipcRenderer’s and vice versa. You may be better served just doing:

const events = require('events') const emitter = new events()
and just using the emitter to send and receive events. Especially since this appears to all be within the main process and not in the renderer


#3

Thanks. I will try it out.