get that corner office with angular 2 and electron

39
Get that Corner Office with Angular 2 and Electron

Upload: lukas-ruebbelke

Post on 09-Jan-2017

16.974 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Get that Corner Office with Angular 2 and Electron

Get that Corner Officewith Angular 2 and Electron

Page 2: Get that Corner Office with Angular 2 and Electron

Get acquainted with how to build native, desktop applications using Electron and Angular 2

Page 3: Get that Corner Office with Angular 2 and Electron

Agenda

Running Electron

Adding Angular 2

Notifications

IPC

Packaging Electron

Page 4: Get that Corner Office with Angular 2 and Electron

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!

Page 5: Get that Corner Office with Angular 2 and Electron
Page 6: Get that Corner Office with Angular 2 and Electron
Page 7: Get that Corner Office with Angular 2 and Electron

http://bit.ly/ng-electrogram

Page 8: Get that Corner Office with Angular 2 and Electron

Hello Electron

Page 9: Get that Corner Office with Angular 2 and Electron

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.

Page 10: Get that Corner Office with Angular 2 and Electron

The Elevator Pitch Pt. 2• Automatic updates • Crash reporting • Windows installers • Debugging & profiling • Native menus & notifications

Page 11: Get that Corner Office with Angular 2 and Electron
Page 12: Get that Corner Office with Angular 2 and Electron
Page 13: Get that Corner Office with Angular 2 and Electron

http://electron.atom.io/docs/latest/tutorial/quick-start/

Page 14: Get that Corner Office with Angular 2 and Electron

➜ npm install electron-prebuilt --save-dev

Install Electron

Page 15: Get that Corner Office with Angular 2 and Electron

"scripts": { "start": "electron main.js" },

package.json

Page 16: Get that Corner Office with Angular 2 and Electron

'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

Page 17: Get that Corner Office with Angular 2 and Electron

01-hello-electron-start

Page 18: Get that Corner Office with Angular 2 and Electron

Challenges• Install electron-prebuilt • Create a main.js file • Run electron • In the package.json file, create a start task to run electron

Page 19: Get that Corner Office with Angular 2 and Electron

Adding Angular 2

Page 20: Get that Corner Office with Angular 2 and Electron

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.

Page 21: Get that Corner Office with Angular 2 and Electron

02-adding-angular

Page 22: Get that Corner Office with Angular 2 and Electron

Notifications

Page 23: Get that Corner Office with Angular 2 and Electron

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.

Page 24: Get that Corner Office with Angular 2 and Electron

let myNotification = new Notification('Title', { body: 'Lorem Ipsum Dolor Sit Amet' });

Notification

Page 25: Get that Corner Office with Angular 2 and Electron

03-notifications-start

Page 26: Get that Corner Office with Angular 2 and Electron

Challenges• In the saveFileCallback method in the app.ts file, create an error notification and a success notification

Page 27: Get that Corner Office with Angular 2 and Electron

IPC

Page 28: Get that Corner Office with Angular 2 and Electron

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

Page 29: Get that Corner Office with Angular 2 and Electron

const BrowserWindow = electron.BrowserWindow; mainWindow = new BrowserWindow({width: 800, height: 600}); fileMenu.submenu .find(item => item.label === 'Open') .click = () => mainWindow.webContents.send('open-file')

webContents.send

Page 30: Get that Corner Office with Angular 2 and Electron

import { remote, ipcRenderer } from 'electron';

ipcRenderer.on('open-file', this.open.bind(this)); ipcRenderer.on('save-file', this.save.bind(this));

ipcRenderer.on

Page 31: Get that Corner Office with Angular 2 and Electron

04-ipc-start

Page 32: Get that Corner Office with Angular 2 and Electron

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

Page 33: Get that Corner Office with Angular 2 and Electron

Packaging Electron

Page 34: Get that Corner Office with Angular 2 and Electron

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

Page 35: Get that Corner Office with Angular 2 and Electron

➜ npm i -g electron-packager

Install electron-packager

Page 36: Get that Corner Office with Angular 2 and Electron

➜ electron-packager . <appName> --platform=<platform> --arch=<architecture> --out=<outputDirectory> --overwrite --icon=path/to/custom/icon --asar

Run electron-packager

Page 37: Get that Corner Office with Angular 2 and Electron

05-packaging-start

Page 38: Get that Corner Office with Angular 2 and Electron

Challenges• In the package.json file, create a distribute task that uses electron-packager to package the app for your OS

Page 39: Get that Corner Office with Angular 2 and Electron

Thanks!