Beginning
Getting started
Developing Applications
Advanced Examples

Title
Basic Concepts

Lecture 11. Introduction to Electron Framework
Cross-Platform Application Development

December 27, 2016

Lecture 11

1 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Title
Basic Concepts

Concepts

Deﬁnitions

Electron: a framework for developing desktop applications (2013). Features:

Development by GitHub;
Open-Source, cross-plaform;
Uses Node.js runtime and Chromium browser.

Node.js: a runtime environment for server and web applications (2009).

Features:

Development by Node.js Foundation;
Open-Source, cross-plaform;
Asynchronous I/0;
Event-driven concept;
Uses Google V8 engine to interpret JavaScript.

Lecture 11

2 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Title
Basic Concepts

Concepts (end)

Deﬁnitions

npm: a package manager for Node.js. Features:

Works with remote repositories (default:
https://registry.npmjs.org/);
Installs packages locally (current directory)/globally
(system-wide directories);
Manages the local project dependencies (read from
package.json);
Optionally searches for packages with particular version ranges.

Lecture 11

3 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Title
Basic Concepts

Example Applications

Title

Year
2014 Atom
2015 Visual Studio Code Microsoft
2015 GitKraken

Developer
GitHub

Axosoft

Table 1: applications using Electron framework

Lecture 11

4 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Downloading
Writing an application
Debugging
Deploying

Downloading Electron Package

Example

> npm install --global electron

...
> electron

Lecture 11

5 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Downloading
Writing an application
Debugging
Deploying

Hello Example

(cid:104)working_directory(cid:105)

01-hello

index.html ..main window contents
index.js ...............start script
package.json ...project description

Figure 1: a directory structure for a simple Electron
project

Example (package.json)

{

}

"name": "01-hello",
"version": "0.0.1",
"main": "index.js"

Example (running)

> cd 01-hello
> electron .

Lecture 11

6 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Downloading
Writing an application
Debugging
Deploying

Hello Example (cont.)

Example (index.js)

const {app, BrowserWindow} = require(’electron’)
const path = require(’path’)
const url = require(’url’)

let win

function createWindow()
{

win = new BrowserWindow(

{

width: 400,
height: 200

})

Lecture 11

7 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Downloading
Writing an application
Debugging
Deploying

Hello Example (cont.)

Example (index.js, cont.)

win.loadURL(

url.format(

{

pathname: path.join(__dirname, ’index.html’),
protocol: ’file:’,
slashes: true

}))

// win.webContents.openDevTools()

Lecture 11

8 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Downloading
Writing an application
Debugging
Deploying

Hello Example (cont.)

Example (index.js, cont.)

Example (index.js, cont.)

win.on(

’closed’,
() =>
{

win = null

})

}

// createWindow()

app.on(

’ready’, createWindow)

app.on(

’window-all-closed’,
() =>
{

if (process.platform !== ’darwin’)

app.quit()

})

Lecture 11

9 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Downloading
Writing an application
Debugging
Deploying

Hello Example (end)

Example (index.js, end)

Example (index.html)

app.on(

’activate’,
() =>
{

if (win === null)

createWindow()

})

// ...

<!DOCTYPE html>
<html>

<head>

<meta charset = "UTF-8">

<title>Hello App</title>

</head>
<body>

<center>

Hello World!

</center>

</body>

</html>

Lecture 11

10 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Downloading
Writing an application
Debugging
Deploying

Electron Processes

Deﬁnitions

Main Process: runs the main script (index.js).

Renderer Process: runs each web page with Chromium (index.html).

Main Process

Renderer Process

Started/terminated with the
whole application;
Has access to the native GUI API;

Started when the main process
creates a BrowserWindow instance;
terminated when it is destroyed;

Has no access to the native API,
isolated with its page.

Lecture 11

11 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Downloading
Writing an application
Debugging
Deploying

Debugging the Main Process

Using Electron Inspector

1 npm install --global node-gyp
2 On Windows, either:

npm install --global --production windows-build-tools (admin)
or manually:

1 Install Visual Studio 2015 or Visual C++ Build Tools;
2 Install Python 2.7
3 npm config set python python2.7
4 npm config set msvs_version 2015

3 npm install electron-rebuild --save-dev

4 npm install electron-inspector --save-dev

5 npm install yargs --save-dev

6 npm install node-pre-gyp --save-dev

7 npm install electron --save-dev

Lecture 11

12 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Downloading
Writing an application
Debugging
Deploying

Debugging the Main Process (end)

Using Electron Inspector

1 Either:

electron --debug=5858 . or:
electron --debug-brk=5858 .

2 node_modules\.bin\electron-inspector

3 ...\chrome http://127.0.0.1:8080/?port=5858

Lecture 11

13 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Downloading
Writing an application
Debugging
Deploying

Deploying an Application

(cid:104)application_directory(cid:105)

locales
resources

app ..............create

(cid:104)application_directory(cid:105)

index.html
index.js
package.json

...
electron.exe

locales
resources

app.asar ..........asar

...
electron.exe

Figure 2: a directory structure for a
deployed Electron project

Figure 3: another option for a directory
structure

Lecture 11

14 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Using External Modules
Managing Windows
Using Menus

External Module Example

Example (index.js)

const {app, BrowserWindow} = require(’electron’)

const path = require(’path’)
const url = require(’url’)

require(’./functionality.js’)

// ...

Lecture 11

15 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Using External Modules
Managing Windows
Using Menus

External Module Example (cont.)

Example (functionality.js)

var chalk = require(’chalk’);

var my_style = chalk.green.bold.underline

chalk.enabled = true

if (chalk.supportsColor)

console.log(

chalk.bgRed.dim(’Warning: ’) + my_style(’starting’) +

’ an application’)

else

console.log(’No color support’)

Lecture 11

16 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Using External Modules
Managing Windows
Using Menus

External Module Example (cont.)

Example (console output)

> electron .

App threw an error during load
Error: Cannot find module ’chalk’

at Module._resolveFilename (module.js:455:15)

> npm install --save chalk

Lecture 11

17 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Using External Modules
Managing Windows
Using Menus

External Module Example (end)

Example (package.json)

Example

{

> npm install

"name": "02-module",

"version": "0.0.1",
"main": "index.js",
"dependencies": {

"chalk": "^1.1.3"

}

}

Lecture 11

18 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Using External Modules
Managing Windows
Using Menus

Managing Windows Example

Example (index.js)

Example (index.js, end)

function createWindow()
{

win = new BrowserWindow(

{

})

width: 400,
height: 200,
show: false

win.once(

’ready-to-show’,

() =>

{

win.show()

})
// ...

child = new BrowserWindow(

{

parent: win,
modal: true,
width: 300,
height: 300

})

}

// createWindow()

Lecture 11

19 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Using External Modules
Managing Windows
Using Menus

Menu Example

Example (index.js)

const {app, BrowserWindow, Menu, dialog} = require(’electron’)

Lecture 11

20 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Using External Modules
Managing Windows
Using Menus

Menu Example (cont.)

Example (index.js, cont.)

Example (index.js, cont.)

const template =
[

{

label: ’File’,

submenu:
[

role: ’quit’

{

}

]

},

{

label: ’View’,
submenu:
[

{

role: ’reload’

},
{

role: ’toggledevtools’

}

]

},

Lecture 11

21 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Using External Modules
Managing Windows
Using Menus

Menu Example (cont.)

Example (index.js, cont.)

Example (index.js, cont.)

{

label: ’Help’,
submenu:
[

{

label: ’About’,
click()
{

dialog.showMessageBox(

{

})

type: ’info’,
title: ’About my app’,

message: ’My elaborate app’,
buttons: [’OK’]

}

// click()

}

]

}

]

Lecture 11

22 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Using External Modules
Managing Windows
Using Menus

Menu Example (end)

Example (index.js, end)

const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)

let win

function createWindow()
{

// ...

}

// ...

Lecture 11

23 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Implementing a Text Editor
Using System Tray Icons
Using Network Requests

Editor Example

Example (index.html)

Example (index.html, cont)

<!DOCTYPE html>
<html>

<head>

<meta charset = "UTF-8">
<title>My Editor</title>
<style type = "text/css">

html, body
{

height: 100%;

}

textarea
{

width: 100%;

height: 90%;
min-height: 90%;

}

</style>

</head>

Lecture 11

24 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Implementing a Text Editor
Using System Tray Icons
Using Network Requests

Editor Example (cont.)

Example (index.html, end)

<body>

<script src = "./renderer.js"></script>
<textarea

id = "editor",
autofocus = "true",
oninput = "onInput(this.value)"></textarea>

</body>

</html>

Lecture 11

25 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Implementing a Text Editor
Using System Tray Icons
Using Network Requests

Editor Example (cont.)

Example (renderer.js)

const {ipcRenderer} = require(’electron’)

ipcRenderer.on(

’opened’,
(event, message) =>
{

var editor = document.getElementById(’editor’)

editor.innerHTML = message

})

Lecture 11

26 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Implementing a Text Editor
Using System Tray Icons
Using Network Requests

Editor Example (cont.)

Example (renderer.js, end)

function onInput(value)

{

}

ipcRenderer.send(’message’, value)

Lecture 11

27 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Implementing a Text Editor
Using System Tray Icons
Using Network Requests

Editor Example (cont.)

Example (index.js)

const {app, BrowserWindow, globalShortcut, ipcMain, dialog} =

require(’electron’)

const path = require(’path’)
const url = require(’url’)
var fs = require(’fs’);

var text = ’’;

Lecture 11

28 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Implementing a Text Editor
Using System Tray Icons
Using Network Requests

Editor Example (cont.)

Example (index.js, cont.)

Example (index.js, cont.)

ipcMain.on(

’message’,

(event, arg) =>
{

text = arg

})

let win

function createWindow()
{

win = new BrowserWindow(

{

width: 800,
height: 600

})

Lecture 11

29 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Implementing a Text Editor
Using System Tray Icons
Using Network Requests

Editor Example (cont.)

Example (index.js, cont.)

win.loadURL(

url.format(

{

pathname: path.join(__dirname, ’index.html’),

protocol: ’file:’,
slashes: true

}))
win.on(

’closed’,
() =>

{

win = null

})

Lecture 11

30 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Implementing a Text Editor
Using System Tray Icons
Using Network Requests

Editor Example (cont.)

Example (index.js, cont.)

Example (index.js, cont.)

const filters =
[

name: ’Text Files’,

extensions: [’txt’],

name: ’All Files’,
extensions: [’*’],

{

},
{

},

]

globalShortcut.register(
’CommandOrControl+S’,
() =>
{

dialog.showSaveDialog(

{

},

filters: filters

Lecture 11

31 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Implementing a Text Editor
Using System Tray Icons
Using Network Requests

Editor Example (cont.)

Example (index.js, cont.)

(filename) =>
{

if (filename != undefined)

fs.open(

filename, ’w’,
(err, fd) =>
{

if (!err)

fs.writeSync(fd, text, 0, ’utf-8’);

})
// dialog.showSaveDialog()

})

})

// globalShortcut.register()

Lecture 11

32 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Implementing a Text Editor
Using System Tray Icons
Using Network Requests

Editor Example (cont.)

Example (index.js, cont.)

globalShortcut.register(
’CommandOrControl+O’,

() =>
{

dialog.showOpenDialog(

{

filters: filters,

properties: [dialog.openFile]

},

Lecture 11

33 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Implementing a Text Editor
Using System Tray Icons
Using Network Requests

Editor Example (cont.)

Example (index.js, cont.)

(filenames) =>
{

if (filenames != undefined && filenames.length > 0)

fs.open(

filenames[0], ’r’,
(err, fd) =>
{

var bytesRead = 0
text = ’’

Lecture 11

34 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Implementing a Text Editor
Using System Tray Icons
Using Network Requests

Editor Example (cont.)

Example (index.js, cont.)

do
{

const size = 7;
var buffer = Buffer.alloc(size)

bytesRead = fs.readSync(fd, buffer, 0, size, null)
text += buffer

}
while (bytesRead > 0)
win.webContents.send(’opened’, text)

})

})

// dialog.showSaveDialog()

})

// globalShortcut.register()

}

// createWindow()

Lecture 11

35 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Implementing a Text Editor
Using System Tray Icons
Using Network Requests

Tray Example

Example (index.js)

const {app, Menu, Tray} = require(’electron’)

let tray = null

app.on(

’ready’,
() =>

{

tray = new Tray(’icon.png’)

Lecture 11

36 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Implementing a Text Editor
Using System Tray Icons
Using Network Requests

Tray Example (end)

Example (index.js, end)

const contextMenu = Menu.buildFromTemplate(

[

{label: ’Item1’, type: ’radio’, checked: true},
{label: ’Item2’, type: ’radio’},
{type: ’separator’},
{label: ’Exit’, click() {app.quit()}}

])

tray.setToolTip(’My application’)

tray.setContextMenu(contextMenu)

})

Lecture 11

37 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Implementing a Text Editor
Using System Tray Icons
Using Network Requests

Network Request Example

Example (index.js)

const {app, net} = require(’electron’)

app.on(

’ready’,
() =>
{

var body = ’’

const request = net.request(’https://github.com’)

Lecture 11

38 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Implementing a Text Editor
Using System Tray Icons
Using Network Requests

Network Request Example (cont.)

Example (index.js, cont.)

Example (index.js, cont.)

request.on(

’response’,
(response) =>

{

response.on(
’data’,
(chunk) =>
{

body += chunk

})

response.on(

’end’,
() =>

{

console.log(body)

})

// response.on()

})

// request.on()

Lecture 11

39 / 40

Beginning
Getting started
Developing Applications
Advanced Examples

Implementing a Text Editor
Using System Tray Icons
Using Network Requests

Network Request Example (end)

Example (index.js, cont.)

Example (index.js, end)

request.on(
’error’,
(error) =>

{

console.log(error)

})

// request.on()

request.on(
’close’,
() =>

{

console.log(’closing’)
app.quit()

})

// request.on()

request.end()

})

// app.on()

Lecture 11

40 / 40

