Electron 快速入门

转自 http://electron.atom.io/docs/tutorial/quick-start/

Electron允许你使用JavaScript及丰富的系统级API创建桌面应用程序。你可以把Electron看做一个Node.js运行时的变体,专注于桌面应用程序,而Node.js专注于Web服务器。

Electron并不是一个用JavaScript写的GUI库,而是Electron使用网页作为GUI,实际上Electron是一个小型的谷歌浏览器,使用JavaScript控制程序逻辑。

主进程(Main Process)

在Electron中,运行package.json文件中main脚本的进程被称为主进程(main process)。 运行在主进程中的脚本可以通过创建网页窗口显示GUI。

渲染进程(Renderer Process)

因为Electron使用谷歌浏览器内核(Chromium)显示网页,所以Electron也继承了Chromium的多进程架构。Electron中的每个网页运行在独立的进程中,被称为渲染进程(renderer process)。

在普通的浏览器中,网页通常运行在一个沙盒环境中,不允许访问本地资源。然而,在Electron中,可以使用Node.js提供的系统级API与操作系统交互。

主进程与渲染进程的区别

主进程通过创建BrowserWindow实例来创建网页。每个BrowserWindow实例在第一个独立的渲染进程中运行网页。当BrowserWindow实例被销毁时,对应的渲染进程也被终结。

主进程管理着所有的网页和它们对应的渲染进程。每个渲染进程是相互独立的,并且仅仅关心与之对应的网页。

在网页中,调用GUI相关的API是不被允许的,因为在网页中管理GUI资源是不安全的并且容易导致资源泄漏。如果在网页中想执行GUI相关的操作,网页所在的渲染进程必须与主进程通信,请求主进程执行相关操作。

在Electron中,有几种方法在主进程和渲染进程中通信。ipcRendereripcMain用于发送消息,remote模块用于RPC通信。另外在FAQ中有一项是讨论如何在网页之间共享数据的。

编写第一个Electron应用程序

通常一个Electron应用程序结构如下:

your-app/
├── package.json
├── main.js
└── index.html

package.json文件内容的格式和Node.js中的模块是相同的。 “main”属性指定的是应用程序的启动脚本,将作为主进程运行。一个package.json的例子如下:

{
  "name"    : "your-app",
  "version" : "0.1.0",
  "main"    : "main.js"
}

注意:如果“main”属性没有指定,Electron将默认加载文件名为index.js的脚本文件。

main.js应该创建窗口并且处理系统事件,一个典型的例子为:

const {app, BrowserWindow} = require('electron')

// 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 win

function createWindow () {
  // Create the browser window.
  win = new BrowserWindow({width: 800, height: 600})

  // and load the index.html of the app.
  win.loadURL(`file://${__dirname}/index.html`)

  // Open the DevTools.
  win.webContents.openDevTools()

  // Emitted when the window is closed.
  win.on('closed', () => {
    // 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.
    win = null
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS 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', () => {
  // On macOS 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 (win === null) {
    createWindow()
  }
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

最后,index.html是需要显示的网页内容:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    We are using node <script>document.write(process.versions.node)</script>,
    Chrome <script>document.write(process.versions.chrome)</script>,
    and Electron <script>document.write(process.versions.electron)</script>.
  </body>
</html>

运行应用程序

一旦创建了main.jsindex.htmlpackage.json文件,需要运行程序并测试一下程序是否是预期的效果。

electron

electron是一个npm模块,包含了预编译的Electron。

使用npm安装electron模块并执行以下命令:

macOS / Linux

$ ./node_modules/.bin/electron .

Windows

$ .\node_modules\.bin\electron .

手动下载的Electron

如果是手动下载的Electron,则:

Windows

$ .\electron\electron.exe your-app\

Linux

$ ./electron/electron your-app/

macOS

$ ./Electron.app/Contents/MacOS/Electron your-app/

Electron可以在这里下载https://github.com/electron/electron/releases

发布应用程序

http://electron.atom.io/docs/tutorial/application-distribution/

例子

# Clone the repository
$ git clone https://github.com/electron/electron-quick-start
# Go into the repository
$ cd electron-quick-start
# Install dependencies and run the app
$ npm install && npm start
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章