ELECTRON
Build cross platform desktop apps using web technologies
![](https://s3.amazonaws.com/media-p.slid.es/uploads/467099/images/2443545/electron_logo.png)
ELECTRON
History
Electron was called Atom Shell
![](https://s3.amazonaws.com/media-p.slid.es/uploads/467099/images/2443595/atom_logo.png)
Atom project: build an IDE with a browser
Why ?
- Cross Platform
- Using popular, well-known technologies
- Code reusable
- Development speed
Well-known Electron apps
![](https://s3.amazonaws.com/media-p.slid.es/uploads/467099/images/2443627/Slack.jpg)
Slack
Visual Studio Code
![](https://s3.amazonaws.com/media-p.slid.es/uploads/467099/images/2443632/Screen-Shot-2015-04-29-at-6.25.31-pm.png)
How does it work?
you could also see it as a minimal Chromium browser, controlled by JavaScript
![](https://s3.amazonaws.com/media-p.slid.es/uploads/467099/images/2438945/nodejs-logo.png)
+
![](https://s3.amazonaws.com/media-p.slid.es/uploads/467099/images/2443699/1000px-Chromium_11_Logo.svg.png)
How simple could this be?
my-awesome-app/
├── package.json
├── main.js
└── index.html
Project Structure
Define your app in package.json
{
"name" : "my-awesome-app",
"version" : "0.1.0",
"main" : "main.js"
}
'use strict';
const electron = require('electron');
const app = electron.app; // Module to control application life.
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
var mainWindow = null;
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600});
// and load the index.html of the app.
mainWindow.loadURL('file://' + __dirname + '/index.html');
// Emitted when the window is closed.
mainWindow.on('closed', function() {
mainWindow = null;
});
});
main.js
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</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>
index.js
electron .
start your app
![](https://s3.amazonaws.com/media-p.slid.es/uploads/467099/images/2443938/Screen_Shot_2016-04-08_at_03.52.22.png)
Application structure
Main process & renderer processes
![](https://s3.amazonaws.com/media-p.slid.es/uploads/467099/images/2438966/1-EJETq7XOPz5RVY5IfF6NIg_2x.png)
Main process & renderer processes
![](https://s3.amazonaws.com/media-p.slid.es/uploads/467099/images/2438967/remote2x.png)
IPC
// In main process.
var ipc = require('ipc');
ipc.on('asynchronous-message', (event, arg) => {
console.log(arg);
event.sender.send('asynchronous-reply', 'pong');
});
// In renderer process (web page).
var ipc = require('ipc');
ipc.on('asynchronous-reply', (arg) => {
console.log(arg);
});
ipc.send('asynchronous-message', 'ping');
How to package your application
electron-builder
https://github.com/electron-userland/electron-builder
electron-builder produces all required artifacts:
- .dmg: OS X installer
-
.exe and -ia32.exe: Windows installer
-
-amd64.deb and -i386.deb: Linux Debian package.
electron-builder
scripts in package.json
"scripts": {
"postinstall": "install-app-deps",
"start": "electron ./app",
"build": "npm run build:osx && npm run build:win32
&& npm run build:win64 && npm run build:linux64",
"build:osx": "build --platform darwin",
"build:win64": "build --platform win32 --arch x64",
"build:win32": "build --platform win32 --arch ia32",
"build:linux64": "build --platform linux --arch x64",
"dist": "npm run dist:osx && npm run dist:win32 && npm run dist:win64",
"dist:osx": "build --platform darwin -d",
"dist:win64": "build --platform win32 --arch x64 -d",
"dist:win32": "build --platform win32 --arch ia32 -d"
}
electron-builder
build config in package.json
"build": {
"app-bundle-id": "your.id",
"app-category-type": "your.app.category.type",
"iconUrl": "http://Youtube.ico",
"osx" : {
"title": "my-awesome-app",
"background": "build/icon.png",
"icon": "build/icon.icns",
"icon-size": 80,
"contents": [
{ "x": 438, "y": 344, "type": "link", "path": "/Applications" },
{ "x": 192, "y": 344, "type": "file" }
]
},
"win" : {
"title" : "my-awesome-app",
"icon" : "build/icon.ico"
}
},
System integration
Menus
var menu = new Menu();
menu.append(new MenuItem({ label: 'MenuItem1', click: function() {
console.log('item 1 clicked'); } }
));
menu.append(new MenuItem({ type: 'separator' }));
menu.append(new MenuItem({ label: 'MenuItem2', type: 'checkbox'}));
Menu.setApplicationMenu(menu);
persistent app on mac
app.on('window-all-closed', function() {
if (process.platform != 'darwin') {
app.quit();
}
});
Demo
Awesome Electron
Questions ?
Electron presentation
By Vincent Quagliaro
Electron presentation
- 1,222