A window manager and message dispatcher for Electron.
npm install
npm start
var WindowManagerServer = require('./path/to/window_manager_server.js');
var windowManagerServer = new WindowManagerServer({
// a window named `mainWindow`
mainWindow: {
create: function() {
var win = new BrowserWindow({width: 1024, height: 768});
win.openDevTools();
win.loadURL('file://' + __dirname + '/app/mainWindow.html');
// do not need to listen ipc.on('closed') event,
// WindowManager will help you to do that
//
// ipc.on('closed', function() {...}); // no need this
return win;
}
},
// a window named `window2`
window2: {
create: function() {
...
}
}
});
windowManagerServer.registerIPC(); // register ipc to dispatch events
// This method will be called when Electron has done everything
// initialization and ready for creating browser windows.
app.on('ready', function() {
windowManagerServer.open('mainWindow');
});
With React
var WindowManagerClient = require('./path/to/window_manager_client.js');
var MainWindow = React.createClass({
componentDidMount: function() {
this.windowManagerClient = new WindowManagerClient('mainWindow', function(event, arg){
this.setState({message: arg});
}.bind(this));
// windowManagerClient.init() should be called in `componentDidMount` life cycle
// which is all DOM are rendered
this.windowManagerClient.init();
},
handleWindowClose: function() {
this.windowManagerClient.close();
},
render: function() {
return (
...
);
}
});
ReactDOM.render(
<MainWindow/>,
document.getElementById('content')
);
windowDefinition
(Object), properties:- create (Function):
window_name
(String): the window name in the definition object
create and open the window process
window_name
(String): the window name in the definition object
close the window
window_name
(String): the window name in the definition object
check the window whether is opened.
let server listen to the channel 'WindowManager' and register the message handler.
window_name
(String): the window name in the definition objectdispatcher
(Functaion): the dispatcher of the receiving message.dispatcher(event, arg)
Initial the client, should be called after window rendered.
This method will do following steps:
- listen to the ipc channel 'WindowManager'
- send 'ready' signal to server
window_name
(String): the window name in the definition objectargs
(Object, optional): open the window with initial data
notify server to open window with initial data.
window_name
(String, optional): if not provide, it will close current window
notify server to close window.
window_name
(String): the window name in the definition objectpayload
(any): the data / message you want to send
send data/message to the window with window_name
through server.
PR & feedback are welcome.
MIT