You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Merge branch 'build-your-first-desktop-application-with-electron' of https://github.com/MCharming98/devrel into build-your-first-desktop-application-with-electron
Copy file name to clipboardExpand all lines: IMC_Electron/IMC_Chen/build-your-first-desktop-application-with-electron/blog.md
+21-21Lines changed: 21 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,22 +9,22 @@ This guide introduces Electron and will help you develop your first Electron app
9
9
This guide targets people interested in Electron but have little to no experience with it. It is recommended that readers have some experience in html, css and javascript since knowing them is very essential to build an Electron application.
10
10
11
11
## What is Electron?
12
-
Electron is a framework for creating cross-platform desktop applications with web programming languages like html, css, and javascript. By cross-platform, it means that you can build and deploy your application on all major operating systems(OS) including Windows, Linux, and MacOS with the same source code. The reason why you don’t have to adapt your program to different operating systems is that Electron runs your code in a Chromium environment independent of your OS. So, let’s jump into our first Electron app!
12
+
Electron is a framework for creating cross-platform desktop applications with web programming languages like html, css, and javascript. By cross-platform, it means that you can build and deploy your application on all major operating systems(OS) including Windows, Linux, and MacOS with the same source code. The reason why you don’t have to adapt your program to different operating systems is that Electron runs your code in a  environment independent of your OS. So, let’s jump into our first Electron app!
13
13
14
14
## Get Started: Hello World Application!
15
15
Of course, the easiest and simplest way to learn a new technology is to create a hello world project of your own, so we are going to build one.
16
16
17
17
### Prerequisite: Installation and Setup
18
-
Before diving into our hello world application, make sure you have node.js installed on your computer. node.js is a javascript runtime environment that can execute javascript code outside of a web browser, and Electron also uses it to run your javascript code.
18
+
Before diving into our hello world application, make sure you have  installed on your computer. node.js is a javascript runtime environment that can execute javascript code outside of a web browser, and Electron also uses it to run your javascript code.
19
19
20
20
An Electron application is essentially a node.js application, so the first step is to create a node project. Open your command line client and create a new folder to store your project, and in that folder, run:
21
21
22
22
```
23
23
npm init
24
24
```
25
25
26
-
npm will guide you to create a basic package.json file, in the end it might look like this:
27
-
```javascript
26
+
npm will guide you to create a basic **package.json** file, in the end it might look like this:
27
+
```json
28
28
{
29
29
"name": "Hello World",
30
30
"version": "1.0.0",
@@ -36,10 +36,10 @@ npm will guide you to create a basic package.json file, in the end it might look
36
36
}
37
37
```
38
38
39
-
Make sure you remember the main field, since it directs to the file being executed when the program starts. In this case, the main field directs to file main.js.
39
+
Make sure you remember the **main** field, since it directs to the file being executed when the program starts. In this case, the main field directs to file **main.js**.
40
40
41
-
Next, we need to add a start script as instruction for Electron to execute current package:
42
-
```javascript
41
+
Next, we need to add a **start** script as instruction for Electron to execute current package:
42
+
```json
43
43
{
44
44
"name": "Hello World",
45
45
"version": "1.0.0",
@@ -51,18 +51,18 @@ Next, we need to add a start script as instruction for Electron to execute curre
51
51
}
52
52
```
53
53
54
-
With package.json configured, we have to install Electron into our project. It is recommended to install it as a development dependency:
54
+
With **package.json** configured, we have to install Electron into our project. It is recommended to install it as a development dependency:
55
55
```
56
56
npm install --save-dev electron
57
57
```
58
58
59
59
### Writing Our Hello World App
60
-
First let’s look into main.js, the entry portal into our hello world application. For Electron, all of its APIs and features can be accessed via the electron module by using the require() function, just like every node module:
60
+
First let’s look into **main.js**, the entry portal into our hello world application. For Electron, all of its APIs and features can be accessed via the **electron** module by using the **require()** function, just like every node module:
61
61
```javascript
62
62
constelectron=require('electron')
63
63
```
64
64
65
-
The require function stores all of Electron’s features into the variable electron as namespaces, and each feature can be accessed via calling electron.feature_name. For every application, its life cycle is managed through electron.app. A window can be created using the electron.BrowserWindow class. We can access the features by calling:
65
+
The **require** function stores all of Electron’s features into the variable **electron** as namespaces, and each feature can be accessed via calling **electron.feature_name**. For every application, its life cycle is managed through **electron.app**. A window can be created using the **electron.BrowserWindow** class. We can access the features by calling:
For our hello world application, a simple main.js file can wait for app to be ready and then open a BrowserWindow, which then loads an html file as the main UI:
76
+
For our hello world application, a simple main.js file can wait for **app** to be ready and then open a **BrowserWindow**, which then loads an html file as the main UI:
Finally, we write a simple index.html to display our hello world page:
96
+
Finally, we write a simple **index.html** to display our hello world page:
97
97
```html
98
98
<html>
99
99
<head>
@@ -107,19 +107,19 @@ Finally, we write a simple index.html to display our hello world page:
107
107
</html>
108
108
```
109
109
### Running Your Application
110
-
After finishing our hello world project, it’s time to run it! To run, simply type this command. It will execute the “electron .” command we have set in package.json.
110
+
After finishing our hello world project, it’s time to run it! To run, simply type this command. It will execute the **“electron .”** command we have set in package.json.
111
111
```
112
112
npm start
113
113
```
114
+
A window will show up in your computer:
114
115
</br>
115
-
A window will show up in your computer: </br>
116
-

116
+

117
117
118
118
Watching the window show up is very exciting! yet … a bit boring. So, why not create a more interesting program?
119
119
120
120
## What Electron Can Do: Build A More Complicated Application
121
121
122
-
For a more complicated application, we are going to build a small timer widget that lives on the top corner of the screen. The source code can be found in this Github repo.
122
+
For a more complicated application, we are going to build a small timer widget that lives on the top corner of the screen. The source code can be found in this  repo.
123
123
124
124
### Step1: Start With the UI
125
125
@@ -162,7 +162,7 @@ button { width: 80px; }
162
162
```
163
163
164
164
### Step2: Implement the Body of the Timer
165
-
The next step is to implement the body of the timer in timer.js, which contains the button listeners to start and stop the timer. It also has the timer function which counts down and update the UI accordingly:
165
+
The next step is to implement the body of the timer in **timer.js**, which contains the button listeners to start and stop the timer. It also has the timer function which counts down and update the UI accordingly:
166
166
167
167
```javascript
168
168
// dialog is used to show message box in the app
@@ -259,10 +259,10 @@ function createWindow() {
259
259
app.whenReady().then(createWindow)
260
260
```
261
261
262
-
With index.html, timer.js, and main.js ready, our timer app is good to go! Let’s run it and see how it behaves.
262
+
With **index.html**, **timer.js**, and **main.js** ready, our timer app is good to go! Let’s run it and see how it behaves.
263
263
264
264
Step 4: Play With It
265
-
By running npm start, this window should pop up on the top left corner on your screen. It has two text input fields for entering desired minutes and seconds. </br>
265
+
By running **npm start**, this window should pop up on the top left corner on your screen. It has two text input fields for entering desired minutes and seconds. </br>
266
266
 </br>
267
267
We can set a countdown for 1 minute and 5 seconds, then press the start button. The countdown begins.
268
268
 </br>
@@ -271,10 +271,10 @@ After counting down to 0, the alert message shows up and the timer is reset. Our
271
271
272
272
## Conclusion
273
273
274
-
This guide should have given you an overview of how to build a simple cross-platform desktop application with Electron. If you have trouble following this guide, I suggest learning more about node.js before moving forward. Happy hacking!
274
+
This guide should have given you an overview of how to build a simple cross-platform desktop application with Electron. If you have trouble following this guide, I suggest learning more about **node.js** before moving forward. Happy hacking!
275
275
276
276
Relevant Resources: </br>
277
277
Electron Official Beginner's Guide: https://www.electronjs.org/docs/tutorial/first-app </br>
278
278
Electron Official Documentation: https://www.electronjs.org/docs </br>
279
279
Electron APIs: https://www.electronjs.org/docs/api </br>
0 commit comments