Electron程序开发助手Electron Fiddle是一款非常优秀的Electron程序开发编辑软件,大家可以使用这款Electron Fiddle创建并运行小段 Electron 程序,让我们可以轻松掌握Electron的入门技巧,对此感兴趣的程序爱好者可以下载试试。
探索 Electron
尝试使用 Electron 而不安装任何依赖项:Fiddle 包含了您探索 Electron 平台所需的一切。它还包括了 Electron 中可用的每个 API 的示例,因此,如果您想快速查看 BrowserView 是什么或者 desktopCapturer 是如何工作的, Fiddle 都已帮您搞定。
用类型编码
Fiddle 包含了与 Visual Studio Code 同样的来自微软的杰出的 Monaco 编辑器。它还会自动为当前选定的 Electron 版本安装类型定义,确保您只需几次击键即可获得所有的 Electron API 。
编译、打包。
Fiddle 可以自动将您的实验变成您可以与朋友、同事或祖父母分享的二进制文件。这要归功于 electron-forge ,它允许您将您的 Fiddle 打包成一个适用于 Windows 、 macOS 或 Linux 的应用程序。
从 Fiddle 开始,在任何地方继续。
Fiddle 不是一个 IDE - 但它是一个很好的起点。一旦您的 Fiddle 长大,利用或不利用 electron-forge 将其导出为一个项目。然后,使用您最喜欢的编辑器,走向世界!
现在,您需要安装electron。 我们推荐的安装方法是把它作为您 app 中的开发依赖项,这使您可以在不同的 app 中使用不同的 Electron 版本。 在您的app所在文件夹中运行下面的命令:
npm install --save-dev electron
除此之外,也有其他安装 Electron 的途径。 请咨询安装指南来了解如何用代理、镜像和自定义缓存。
Electron apps 使用JavaScript开发,其工作原理和方法与Node.js 开发相同。 electron模块包含了Electron提供的所有API和功能,引入方法和普通Node.js模块一样:
const electron = require('electron')复制
electron 模块所提供的功能都是通过命名空间暴露出来的。 比如说: electron.app负责管理Electron 应用程序的生命周期, electron.BrowserWindow类负责创建窗口。 下面是一个简单的main.js文件,它将在应用程序准备就绪后打开一个窗口:
const { app, BrowserWindow } = require('electron')function createWindow () {
// 创建浏览器窗口
const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true
}
}) // 并且为你的应用加载index.html
win.loadFile('index.html')
}
app.whenReady().then(createWindow)
您应当在 main.js 中创建窗口,并处理程序中可能遇到的所有系统事件。 下面我们将完善上述例子,添加以下功能:打开开发者工具、处理窗口关闭事件、在macOS用户点击dock上图标时重建窗口,添加后,main. js 就像下面这样:
const { app, BrowserWindow } = require('electron')function createWindow () {
// 创建浏览器窗口
const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true
}
}) // 并且为你的应用加载index.html
win.loadFile('index.html') // 打开开发者工具
win.webContents.openDevTools()
}// Electron会在初始化完成并且准备好创建浏览器窗口时调用这个方法// 部分 API 在 ready 事件触发后才能使用。app.whenReady().then(createWindow)// Quit when all windows are closed, except on macOS. There, it's common// for applications and their menu bar to stay active until the user quits// explicitly with Cmd + Q.app.on('window-all-closed', () => { 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 (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})// 您可以把应用程序其他的流程写在在此文件中// 代码 也可以拆分成几个文件,然后用 require 导入。
最后,创建你想展示的 index.html:
<!DOCTYPE html><html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<!-- https://electronjs.org/docs/tutorial/security#csp-meta-tag -->
<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>
140.5M / 09-05
76.4M / 03-25
55M / 06-05
237.9M / 04-13
900.9M / 03-02
96.2M / 07-06
311.2M / 07-06
335M / 07-06
200M / 07-06
413.8M / 07-06
484.7M / 09-27
165.4M / 09-05
353.9M / 06-05
131.8M / 04-13
195.6M / 03-03
45.6M / 09-08
665.2M / 07-06
2.84G / 07-06
93M / 07-06
338.3M / 07-06
1.38G / 07-26
488.3M / 07-16
109.8M / 06-03
142M / 01-08
1.2M / 11-23
548.8M / 04-13
1.6M / 04-13
1.48G / 03-18
646.6M / 03-03
133.7M / 03-03
110.5M / 09-05
33.4M / 09-05
325.8M / 08-12
60M / 04-29
254M / 04-25
659M / 04-23
1M / 12-26
253.4M / 12-08
253M / 12-08
1.19G / 11-16
369M / 09-22
181.5M / 09-22
201.2M / 09-05
488.3M / 07-16
248.9M / 12-08
248.9M / 12-08
100.6M / 03-06
148.9M / 03-06
1.12G / 07-06
1.25G / 07-06
1.76G / 09-22
1.92G / 04-17
116.2M / 04-10
201.5M / 04-13
7.31G / 07-01
94.3M / 07-06
2.48G / 07-06
7.63G / 07-06
1M / 07-06
778.1M / 07-06
561.8M / 07-11
72M / 07-06
548.7M / 07-06
1.00G / 07-06
9.13G / 07-06
126.2M / 07-06
72M / 07-06
105.1M / 07-06
132M / 07-06
132M / 07-06