Electron架构简析

概述

简单来说,Electron为用纯JavaScript创建桌面应用提供了运行时。原理是,Electron调用你在package.json中定义的main文件并执行它。main文件(通常被命名为main.js)会创建一个内含渲染完的web页面的应用窗口,并添加与你操作系统的原生GUI(图形界面)交互的功能。
详细地说,当用Electron启动一个应用,会创建一个主进程。这个主进程负责与你系统原生的GUI进行交互并为你的应用创建GUI(在你的应用窗口)。


Electron架构简析_第1张图片

electron_process

如上图,electron由主进程main process启动,通过BrowserWindow实例创建渲染进程renderer process,每个渲染进程都是相互独立渲染。考虑到安全问题,在渲染进程里面不允许直接调用GUI API,如果想要调用,必须通过和主进程通讯,请求主进程完成相应的调用。在 Electron,我们提供几种方法用于主进程和渲染进程之间的通讯。像ipcRenderer和ipcMain模块用于发送消息,remote模块用于 RPC 方式通讯。这些内容都可以在一个 FAQ 中查看how to share data between web pages。

打造你第一个 Electron 应用

主进程 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地址,就可以运行App

主进程与渲染进程的通讯

上面已经说过像ipcRenderer和ipcMain模块用于发送消息,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

你可能感兴趣的:(Electron架构简析)