Electron寫桌面應用入門

本節目標:你能夠在10分鐘內開始運行一個最簡單electron app。不要考慮太多的概念,直接複製粘貼開始吧。html

Electron是一個能幫你用JS來寫桌面程序的node項目。原先是爲Atom打造的,後來直接演化成兄弟項目。如今已經有不少大廠也開始使用Electron來寫桌面app了。好比獨角獸slack、微軟的VS code、avocode、weFlow等等等等。node

ok,接下來就讓咱們來開始動手吧。如下內容除去下載文件的時間,三分鐘就能夠完成。web

1.安裝Electron。chrome

// 全局安裝electron,由於咱們要用到它的命令行。
$ npm install electron -g

// 可是npm安裝electron要從國外下載一個45MB左右的zip包,特別的慢(8KB...),因此咱們能夠採用下面的這條安裝命令。
// 這條安裝命令會從淘寶的鏡像站下載這個zip包,速度很快,達到你的帶寬巔峯。
$ npm install electron -g --registry=http://registry.npm.taobao.org

// 固然你也能夠使用cnpm來全局安裝electron
$ npm install cnpm -g --registry=http://registry.npm.taobao.org
$ cnpm install electron -g

2.準備三個小文件(如下內容直接粘貼複製)
本部分來自於官網quick start。npm

新建一個項目目錄文件夾,名字就叫desktopApp好了。
新建三個文件json

desktopApp/
├── package.json
├── main.js
└── index.htmlwindows

三個文件填入如下內容:bash

在package.json中:app

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

在main.js中electron

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

// 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(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }))

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

3.運行
在這個項目目錄下面運行一條命令就能夠啓動咱們的app了。

$ electron .

Done!完美!天才第一步,達成!

相關文章
相關標籤/搜索