概述
簡(jiǎn)單來(lái)說(shuō),Electron為用純JavaScript創(chuàng)建桌面應(yīng)用提供了運(yùn)行時(shí)。原理是,Electron調(diào)用你在package.json中定義的main文件并執(zhí)行它。main文件(通常被命名為main.js)會(huì)創(chuàng)建一個(gè)內(nèi)含渲染完的web頁(yè)面的應(yīng)用窗口,并添加與你操作系統(tǒng)的原生GUI(圖形界面)交互的功能。
詳細(xì)地說(shuō),當(dāng)用Electron啟動(dòng)一個(gè)應(yīng)用,會(huì)創(chuàng)建一個(gè)主進(jìn)程。這個(gè)主進(jìn)程負(fù)責(zé)與你系統(tǒng)原生的GUI進(jìn)行交互并為你的應(yīng)用創(chuàng)建GUI(在你的應(yīng)用窗口)。

electron_process
如上圖,electron由主進(jìn)程main process啟動(dòng),通過(guò)BrowserWindow實(shí)例創(chuàng)建渲染進(jìn)程renderer process,每個(gè)渲染進(jìn)程都是相互獨(dú)立渲染。考慮到安全問(wèn)題,在渲染進(jìn)程里面不允許直接調(diào)用GUI API,如果想要調(diào)用,必須通過(guò)和主進(jìn)程通訊,請(qǐng)求主進(jìn)程完成相應(yīng)的調(diào)用。在 Electron,我們提供幾種方法用于主進(jìn)程和渲染進(jìn)程之間的通訊。像ipcRenderer和ipcMain模塊用于發(fā)送消息,remote模塊用于 RPC 方式通訊。這些內(nèi)容都可以在一個(gè) FAQ 中查看how to share data between web pages。
打造你第一個(gè) Electron 應(yīng)用
主進(jìn)程 main.js:
'use strict';
const electron = require('electron');
// Module to control application life.
const app = electron.app;
app.title = "AppTitle";
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
"title": "AppTitle",
"toolbar": true,
"width": 1000,
"height": 600,
"min_width": 1000,
"min_height": 600,
"resizable": true,
// "frame": false,
"autoHideMenuBar" : false,
"icon":__dirname+"/app/icons/application.ico"
});
// and load the index.html of the app.
mainWindow.loadURL('file://' + __dirname + '/app/index.html');
// Open the DevTools.
mainWindow.webContents.openDevTools();
// Emitted when the window is closed.
mainWindow.on('closed', function() {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});
代碼里面的
mainWindow.loadURL('file://' + __dirname + '/app/index.html');
加載本地app的URL地址,就可以運(yùn)行App
主進(jìn)程與渲染進(jìn)程的通訊
上面已經(jīng)說(shuō)過(guò)像ipcRenderer和ipcMain模塊用于發(fā)送消息,remote模塊用于 RPC 方式通訊
ipcMain
// In main process.
const {ipcMain} = require('electron');
ipcMain.on('asynchronous-message', (event, arg) => {
console.log(arg); // prints "ping"
event.sender.send('asynchronous-reply', 'pong');
});
ipcMain.on('synchronous-message', (event, arg) => {
console.log(arg); // prints "ping"
event.returnValue = 'pong';
});
ipcRenderer
// In renderer process (web page).
const {ipcRenderer} = require('electron');
console.log(ipcRenderer.sendSync('synchronous-message', 'ping')); // prints "pong"
ipcRenderer.on('asynchronous-reply', (event, arg) => {
console.log(arg); // prints "pong"
});
ipcRenderer.send('asynchronous-message', 'ping');
remote
main process
// main process mapNumbers.js
exports.withRendererCallback = (mapper) => {
return [1,2,3].map(mapper);
};
exports.withLocalCallback = () => {
return exports.mapNumbers(x => x + 1);
};
renderer process
// renderer process
const mapNumbers = require('remote').require('./mapNumbers');
const withRendererCb = mapNumbers.withRendererCallback(x => x + 1);
const withLocalCb = mapNumbers.withLocalCallback();
console.log(withRendererCb, withLocalCb); // [true, true, true], [2, 3, 4]
參考:
https://github.com/electron/electron/tree/master/docs-translations/zh-CN
http://www.voidcn.com/blog/DJY1992/article/p-5793178.html