These are the slides from my workshop at ng-conf 2016 on Angular 2 and Electron. Pull down the demo repository and work through the branches. Check out http://onehungrymind.com/ for additional resources.
4. The Demo Application
• An Instagram style application that allows us to select
an image, apply a filter and then save it to our computer
• Feel free to use the existing code as a reference point
• Please explore! Don't be afraid to try new things!
9. The Elevator Pitch
• Use HTML, CSS, and JavaScript with Chromium and
Node.js to build your app.
• Electron is open source; maintained by GitHub and an
active community.
• Electron apps build and run on Mac, Windows, and
Linux.
10. The Elevator Pitch Pt. 2
• Automatic updates
• Crash reporting
• Windows installers
• Debugging & profiling
• Native menus & notifications
16. 'use strict';
const electron = require('electron');
// Module to control application life.
const app = electron.app;
// 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.
let mainWindow;
let createWindow = () => {
// 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');
// Open the DevTools.
// mainWindow.webContents.openDevTools();
// Emitted when the window is closed.
mainWindow.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On OS X 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 (mainWindow === null) createWindow();
});
main.js
20. Adding Angular 2
• Your Angular app generally goes into a sub-folder like
src or app
• If you are using a build system, you may have to tweak
file references to get everything working
• We need target attribute to the webpack config that is
set to electron-renderer. This gave us the ability to
import node and electron modules without breaking.
• Finally, the Angular app is in TypeScript, so we need to
install the electron typings to use electron packages in
our code.
23. Notifications
• All three operating systems provide means for
applications to send notifications to the user.
• Electron conveniently allows developers to send
notifications with the HTML5 Notification API, using the
currently running operating system’s native notification
APIs to display it.
24. let myNotification = new Notification('Title', {
body: 'Lorem Ipsum Dolor Sit Amet'
});
Notification
28. IPC
• Inter-process communication (IPC) is handled by two
different modules
• ipcMain in the main process
• ipcRenderer in the renderer process
• You can also use webContents to send messages to
your renderer process
32. Challenges
• In the main.js file, complete the save and open click
handlers in the setMenu method
• Use app.ts file, add save and open handlers to the App
constructor
34. Packaging Electron
• We used to have to worry about all of packaging for
each OS ourselves
• Fortunately, electron-packager abstracts most of the
tedium away and allows us to run a one script to build
for our OS
35. ➜ npm i -g electron-packager
Install electron-packager