Skip to content

Commit a06fbfa

Browse files
committed
Merge branch 'build-your-first-desktop-application-with-electron' of https://github.com/MCharming98/devrel into build-your-first-desktop-application-with-electron
2 parents 90e68c2 + d237094 commit a06fbfa

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

IMC_Electron/IMC_Chen/.DS_Store

-6 KB
Binary file not shown.
Binary file not shown.

IMC_Electron/IMC_Chen/build-your-first-desktop-application-with-electron/blog.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ This guide introduces Electron and will help you develop your first Electron app
99
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.
1010

1111
## 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 ![Chromium](https://www.chromium.org) environment independent of your OS. So, let’s jump into our first Electron app!
1313

1414
## Get Started: Hello World Application!
1515
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.
1616

1717
### 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 ![node.js](https://nodejs.org/en/download/) 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.
1919

2020
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:
2121

2222
```
2323
npm init
2424
```
2525

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
2828
{
2929
"name": "Hello World",
3030
"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
3636
}
3737
```
3838

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**.
4040

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
4343
{
4444
"name": "Hello World",
4545
"version": "1.0.0",
@@ -51,18 +51,18 @@ Next, we need to add a start script as instruction for Electron to execute curre
5151
}
5252
```
5353

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:
5555
```
5656
npm install --save-dev electron
5757
```
5858

5959
### 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:
6161
```javascript
6262
const electron = require('electron')
6363
```
6464

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:
6666
```javascript
6767
const app = electron.app;
6868
const BrowserWindow = electron.BrowserWindow;
@@ -73,7 +73,7 @@ Or alternatively:
7373
const { app, BrowserWindow } = require('electron');
7474
```
7575

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:
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:
7777
```javascript
7878
const { app, BrowserWindow } = require('electron');
7979
function createWindow() {
@@ -93,7 +93,7 @@ function createWindow() {
9393
app.whenReady().then(createWindow)
9494
```
9595

96-
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:
9797
```html
9898
<html>
9999
<head>
@@ -107,19 +107,19 @@ Finally, we write a simple index.html to display our hello world page:
107107
</html>
108108
```
109109
### 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.
111111
```
112112
npm start
113113
```
114+
A window will show up in your computer:
114115
</br>
115-
A window will show up in your computer: </br>
116-
![screenshot](./1.png)
116+
![1](./1.png)
117117

118118
Watching the window show up is very exciting! yet … a bit boring. So, why not create a more interesting program?
119119

120120
## What Electron Can Do: Build A More Complicated Application
121121

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 ![Github](https://github.com/MCharming98/myTimer) repo.
123123

124124
### Step1: Start With the UI
125125

@@ -162,7 +162,7 @@ button { width: 80px; }
162162
```
163163

164164
### 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:
166166

167167
```javascript
168168
// dialog is used to show message box in the app
@@ -259,10 +259,10 @@ function createWindow() {
259259
app.whenReady().then(createWindow)
260260
```
261261

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.
263263

264264
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>
266266
![2](./2.png) </br>
267267
We can set a countdown for 1 minute and 5 seconds, then press the start button. The countdown begins.
268268
![3](./3.png) </br>
@@ -271,10 +271,10 @@ After counting down to 0, the alert message shows up and the timer is reset. Our
271271

272272
## Conclusion
273273

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

276276
Relevant Resources: </br>
277277
Electron Official Beginner's Guide: https://www.electronjs.org/docs/tutorial/first-app </br>
278278
Electron Official Documentation: https://www.electronjs.org/docs </br>
279279
Electron APIs: https://www.electronjs.org/docs/api </br>
280-
Node.js Introduction: https://nodejs.dev/learn </br>
280+
Node.js Introduction: https://nodejs.dev/learn </br>

0 commit comments

Comments
 (0)