美文网首页思科DevNet
Electron架构简析

Electron架构简析

作者: rill_ | 来源:发表于2016-11-18 09:50 被阅读0次

概述

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



electron_process

如上图,electron由主进程main process启动,通过BrowserWindow实例创建渲染进程renderer process,每个渲染进程都是相互独立渲染。考虑到安全问题,在渲染进程里面不允许直接调用GUI API,如果想要调用,必须通过和主进程通讯,请求主进程完成相应的调用。在 Electron,我们提供几种方法用于主进程和渲染进程之间的通讯。像ipcRendereripcMain模块用于发送消息,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

主进程与渲染进程的通讯

上面已经说过像ipcRendereripcMain模块用于发送消息,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架构简析

    概述 简单来说,Electron为用纯JavaScript创建桌面应用提供了运行时。原理是,Electron调用你...

  • HBase架构简析

    一、HBase架构简介 1、StoreFile HBase数据的存储单元,底层使用HDFS存储。数据按照Cell(...

  • Tomcat架构简析

    Tomcat服务器作为目前比较流行的一种服务器容器已经被广泛用于后台服务器的搭建、后台集成框架的嵌入(如Sprin...

  • Android架构简析

    浅谈MVP架构及开发模式MVC or MVP Pattern – Whats the difference?And...

  • OKHttp架构简析

    从创建到使用: 创建一个OkHttpClient 构建一个Request 构建一个Call 最后Call回调 Ok...

  • VSCode源码解读--IPC通信机制

    Electron 的 通信机制 我们知道 Electron 是基于 Chromium + Node.js 的架构。...

  • 【Electron】01. electron-quick-sta

    electron-quick-start解释 electron核心是chrome架构,主要包含Browser进程和...

  • electron 项目构建, 打包实践及避坑

    项目架构是React + electron,可以直接使用普通的 react 项目,然后加上electron配置。e...

  • KIE DROOLS 架构简析

    本文侧重对官方文档的解读及扩展。同时结合自身的实践分享一些自己的见解。请结合官方文档阅读https://docs....

  • Tomcat整体架构简析

    一、Tomcat 容器的总体架构 Tomcat 容器主要用来提供servlet运行环境,对其生命期进行管理,从而提...

网友评论

    本文标题:Electron架构简析

    本文链接:https://www.haomeiwen.com/subject/qxwdpttx.html