如何用Web前端技術(shù)就能開發(fā)出桌面應(yīng)用程序曲饱?
Web前端技術(shù)開發(fā)桌面應(yīng)用的方式
CEF:用 Chromium&Webkit 來呈現(xiàn) web 頁面,是客戶端里面嵌瀏覽器蕴潦,瀏覽器里面跑網(wǎng)頁奋隶。
heX:基于CEF童漩,內(nèi)部整合了開源項(xiàng)目 Chromium 及 node.js荣恐。
nw:基于Chromium 和 node.js火惊,利用 web 方式開發(fā)跨平臺(tái)桌面應(yīng)用的平臺(tái)技術(shù)求类。
electron:底層也是基于Chromium 和 node.js。
等等屹耐。尸疆。。
案例實(shí)操~electron
electron 是 github 開發(fā)的惶岭,用來開發(fā)桌面應(yīng)用的一款前端框架
開發(fā)環(huán)境
安裝 node.js
為了避免網(wǎng)絡(luò)問題對(duì) Node 工作時(shí)的影響寿弱,我們安裝國內(nèi)的 node 鏡像 cnpm。
npm install -g cnpm --registry=https://registry.npm.taobao.org
安裝 electron
npm install --save-dev electron
或者全局安裝
npm install -g electron
開發(fā)工具
其實(shí) vscode 就是 electron 開發(fā)的
開發(fā)一個(gè)簡易的 electron
Electron 應(yīng)用使用 JavaScript 開發(fā)按灶,其工作原理和方法與 Node.js 開發(fā)相同症革。 electron
模塊包含了 Electron 提供的所有 API 和功能,引入方法和普通 Node.js 模塊一樣:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" contenteditable="false" cid="n37" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">const electron = require('electron')</pre>
electron
模塊所提供的功能都是通過命名空間暴露出來的兆衅。 比如說: electron.app
負(fù)責(zé)管理 Electron 應(yīng)用程序的生命周期, electron.BrowserWindow
類負(fù)責(zé)創(chuàng)建窗口嗜浮。下面是一個(gè)簡單的main.js
文件羡亩,它將在應(yīng)用程序準(zhǔn)備就緒后打開一個(gè)窗口:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" contenteditable="false" cid="n40" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">const { app, BrowserWindow } = require('electron')
?
function createWindow () {
// 創(chuàng)建瀏覽器窗口
let win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
?
// 加載index.html文件
win.loadFile('index.html')
}
?
app.whenReady().then(createWindow)</pre>
創(chuàng)建你想展示的 index.html
:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html" contenteditable="false" cid="n42" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</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></pre>
啟動(dòng)
我們?cè)?code>package.json中已經(jīng)寫好了啟動(dòng)命令,所以這里直接用 node 啟動(dòng)命令npm start
就可以了危融,如果沒有配畏铆,也可以用electron .
命令啟動(dòng)。
主進(jìn)程和渲染進(jìn)程
我們可以理解package.json
中定義的入口文件就是主進(jìn)程吉殃,那一般一個(gè)程序只有一個(gè)主進(jìn)程辞居,而我們可以利用一個(gè)主進(jìn)程,打開多個(gè)子窗口蛋勺。
由于 Electron 使用了 Chromium 來展示 web 頁面瓦灶,所以 Chromium 的多進(jìn)程架構(gòu)也被使用到。每個(gè) Electron 中的 web 頁面運(yùn)行在它自己的渲染進(jìn)程中抱完,也就是我們說的渲染進(jìn)程贼陶。
也就是說主進(jìn)程控制渲染進(jìn)程,一個(gè)主進(jìn)程可以控制多個(gè)渲染進(jìn)程巧娱。
您應(yīng)當(dāng)在 main.js
中創(chuàng)建窗口碉怔,并處理程序中可能遇到的所有系統(tǒng)事件。下面我們將完善上述例子禁添,添加以下功能:打開開發(fā)者工具撮胧、處理窗口關(guān)閉事件、在 macOS 用戶點(diǎn)擊 dock 上圖標(biāo)時(shí)重建窗口老翘,添加后芹啥,main. js 就像下面這樣:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" contenteditable="false" cid="n54" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">const { app, BrowserWindow } = require('electron')
?
function createWindow () {
// 創(chuàng)建瀏覽器窗口
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
?
// 并且為你的應(yīng)用加載index.html
win.loadFile('index.html')
?
// 打開開發(fā)者工具
win.webContents.openDevTools()
}
?
app.whenReady().then(createWindow)
?
// 當(dāng)所有窗口都關(guān)閉時(shí)觸發(fā)此事件.
app.on('window-all-closed', () => {
// 在 macOS 上锻离,除非用戶用 Cmd + Q 確定地退出,
// 否則絕大部分應(yīng)用及其菜單欄會(huì)保持激活叁征。
if (process.platform !== 'darwin') {
app.quit()
}
})
?
app.on('activate', () => {
// 在macOS上纳账,當(dāng)單擊dock圖標(biāo)并且沒有其他窗口打開時(shí),
// 通常在應(yīng)用程序中重新創(chuàng)建一個(gè)窗口捺疼。
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
?
// In this file you can include the rest of your app's specific main process
// code. 也可以拆分成幾個(gè)文件疏虫,然后用 require 導(dǎo)入。
?</pre>
git 上面的 demo 示例
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="false" cid="n57" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 克隆這倉庫
git clone https://github.com/electron/electron-quick-start
進(jìn)入倉庫
cd electron-quick-start
安裝依賴庫
npm install
運(yùn)行應(yīng)用
npm start</pre>
electron-forge 構(gòu)建項(xiàng)目
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="false" cid="n60" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 全局安裝electron-forge
npm install electron-forge -g
?
用electron-forge初始化一個(gè)項(xiàng)目
electron-forge init demo02
?
進(jìn)入到項(xiàng)目目錄
cd demo02
?
啟動(dòng)項(xiàng)目
npm start</pre>
擴(kuò)展~開發(fā)桌面百度
主進(jìn)程代碼
我們創(chuàng)建了主進(jìn)程對(duì)象 win 后啤呼,讓它直接加載百度的地址卧秘。
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="js" contenteditable="false" cid="n65" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">const { app, BrowserWindow } = require('electron')
?
function createWindow () {
// 創(chuàng)建瀏覽器窗口
let win = new BrowserWindow({
width: 800,
height: 800,
webPreferences: {
nodeIntegration: true
}
})
?
// 加載index.html文件
// win.loadFile('index.html')
win.loadURL("http://www.baidu.com")
}
?
app.whenReady().then(createWindow)</pre>
打包發(fā)布
我們希望使用electron-packager
對(duì)應(yīng)用進(jìn)行打包發(fā)布,electron-packager
的安裝方式如下:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="false" cid="n68" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#下載electron-packager打包插件
npm install electron-packager -g
開始打包
electron-packager ./ demo01 --win --out ./ --arch=x64 --app-version=0.0.1 --electron-version=8.2.5</pre>
我們還可以進(jìn)一步打包成可執(zhí)行文件