Electron(一)--初步瞭解並動手HelloWorld

如今須要作一個桌面應用,內心有點不甘,由於想作出一個簡單的客戶端,你要麼使用Java的Swing編程,要麼會使用MFC等等,這樣學習的代價過高,也不便維護,因而瞭解了一下Electron,Electron提供了豐富的本地(操做系統)的API,使你可以使用純JavaScript來建立桌面應用程序。與其它各類的Node.js運行時不一樣的是Electron專一於桌面應用程序而不是Web服務器,這並不意味着Electron是一個綁定圖形用戶界面(GUI)的JavaScript庫,取而代之的是,Electron使用Web頁面做爲它的圖形界面,因此你也能夠將它看做是一個由JavaScript控制的迷你的Chrominum瀏覽器,像釘訂客戶端就是使用這種方式開發的,可是是使用的nw.js,我想要去學習和掌握的是Electron,ATOM就是這樣造出來的。html

去官網下載並解壓打開electron彈出以下界面java

這樣最簡單的就給你安裝好了electron的環境了,如今就能夠開始寫一個桌面程序,而後拖入它打開就行了,固然也能夠去按照官網的說明使用npm下載node

如今來書寫第一個應用hello worldweb

一般,一個Electron應用的結構相似下面:chrome

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

package.json 的格式與Node的模塊格式是一致的,其中 main 字段指定的腳本就是你應用的啓動腳本,該腳本將運行在主進程中。你的 package.json 也許看上去像下面這個例子:npm

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

注意 若是在 package.json 中的 main 字段沒有指定,那麼Electron將嘗試裝載一個名爲 index.js 的腳本。編程

main.js 應當建立窗口而且處理系統事件,一個典型的例子以下:json

const electron = require('electron')
// Module to control application life.
const app = electron.app
// 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({width: 800, height: 600})

  // and load the index.html of the app.
  mainWindow.loadURL(`file://${__dirname}/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.
// 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', 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()
  }
})

// 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 則是你想要展現在窗口中:windows

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

而後拖入electron運行就行了瀏覽器

相關文章
相關標籤/搜索