Slide 1

Slide 1 text

Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Christian Liebel @christianliebel Consultant

Slide 2

Slide 2 text

Hello, it’s me. Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Christian Liebel Twitter: @christianliebel Email: christian.liebel @thinktecture.com Angular & PWA Slides: thinktecture.com /christian-liebel

Slide 3

Slide 3 text

09:00–10:00 Block 1 10:00–10:15 Break 10:15–11:30 Block 2 11:30–11:45 Break 11:45–13:00 Block 3 Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Timetable

Slide 4

Slide 4 text

Things NOT to expect - Blueprint for PWA development - No AuthN/AuthZ - No hands-on Things To Expect - Extensive/up-to-date insights into PWA and Project Fugu - A productivity app use case that works on your desktop & smartphone - Lots of fun Desktop-Apps als PWA entwickeln Tausche .NET gegen Web

Slide 5

Slide 5 text

https://paint.js.org – Productivity app – Draw images – Lots of actions & tools – Installable – Read/save files – Copy/paste images from/to clipboard – Share files to other apps – Register for file extensions Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Demo Use Case

Slide 6

Slide 6 text

(Workshop Edition) – Productivity app – Draw images – Lots of actions & tools – Installable – Read/save files – Copy/paste images from/to clipboard – Share files to other apps – Register for file extensions Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Demo Use Case

Slide 7

Slide 7 text

Paint PWA Capabilities Progressive Enhancement Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Agenda

Slide 8

Slide 8 text

Paint PWA Capabilities Progressive Enhancement Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Agenda

Slide 9

Slide 9 text

Canvas – Plain bitmap for the web – Cross-platform, hardware- accelerated graphics operations – Supports different contexts (2D, 3D: WebGL, WebGL 2.0) – Supported on all evergreen browsers and on IE 9+ Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Paint

Slide 10

Slide 10 text

Canvas Add a canvas element to index.html (line 11): Query the canvas element in app.js (line 1): const canvas = document.querySelector('#canvas'); Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Paint EX #1

Slide 11

Slide 11 text

Canvas 2D Context fillRect(x, y, width, height) strokeRect(x, y, width, height) beginPath() moveTo(x, y) lineTo(x, y) fill() stroke() Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Paint

Slide 12

Slide 12 text

Canvas 2D Context Get the 2D context and prepare the canvas in app.js: const canvas = document.querySelector('#canvas'); const ctx = canvas.getContext('2d'); ctx.fillStyle = 'white'; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = 'black'; Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Paint EX #2

Slide 13

Slide 13 text

Low-latency Rendering Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Paint Web pages need to synchronize with the DOM on graphics updates causing latencies that can make drawing difficult Low-latency rendering skips syncing with the DOM with can lead to a more natural experience Supported on Chrome (Chrome OS and Windows only)

Slide 14

Slide 14 text

Low-latency Rendering Opt-in to low-latency rendering (app.js): const ctx = canvas.getContext('2d', { desynchronized: true }); Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Paint EX #3

Slide 15

Slide 15 text

Pointer Events Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Paint Users nowadays use different input methods, cross-platform apps should cover all of them Pointer Events offer an abstract interface to catch all input methods (pointerdown, pointermove, pointerup) event.offsetX/event.offsetY contain the pointer’s position relative to the listener

Slide 16

Slide 16 text

Pointer Events Add event listeners (bottom of app.js): canvas.addEventListener('pointerdown', event => {}); canvas.addEventListener('pointermove', event => { // ~~ is the shorter Math.floor() ctx.fillRect(~~event.offsetX, ~~event.offsetY, 2, 2); }); canvas.addEventListener('pointerup', () => {}); Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Paint EX #4

Slide 17

Slide 17 text

Bresenham Line Algorithm The user may move faster than the input can be processed For this case, we need to remember the previous point and interpolate the points in between The Bresenham line algorithm calculates the missing pixels between two given points Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Paint

Slide 18

Slide 18 text

Bresenham Line Algorithm 1. Add the following import to the top of app.js (“.js” is required, otherwise the request will 404): import {bresenhamLine} from "./helpers.js"; 2. Then, adjust the event listeners: a) In the pointerdown event handler, store ~~event.offsetX/~~event.offsetY in a variable called previousPoint (~~ is required, otherwise Bresenham will go into an endless loop) b) In pointermove i. only draw if the pointer is down ii. retrieve the current point (event.offsetX|Y, ~~ required!) iii. In a for…of loop, call bresenhamLine() with previousPoint.x|y & currentPoint.x|y and draw a 2x2 rectangle for each returned point. iv. store current point in previousPoint c) In pointerup, set previousPoint to null again. Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Paint EX #5

Slide 19

Slide 19 text

Bresenham Line Algorithm let previousPoint = null; canvas.addEventListener('pointerdown', event => previousPoint = {x: ~~event.offsetX, y: ~~event.offsetY}); canvas.addEventListener('pointermove', event => { if (previousPoint) { const currentPoint = {x: ~~event.offsetX, y: ~~event.offsetY}; for(let {x, y} of bresenhamLine(previousPoint.x, previousPoint.y, currentPoint.x, currentPoint.y)) { ctx.fillRect(x, y, 2, 2); } previousPoint = currentPoint; } }); canvas.addEventListener('pointerup', () => previousPoint = null); Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Paint

Slide 20

Slide 20 text

Color Selection For this demo, we will only implement the brush tool. However, we want to bring in some color. A simple method to implement a color selection is to use the HTML color-type input element that shows the native color picker: Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Paint

Slide 21

Slide 21 text

Color Selection 1. In index.html, add the following input to the node: 2. In app.js, add the following code at the bottom: const txtColor = document.querySelector('#color'); txtColor.addEventListener('change', () => { ctx.fillStyle = txtColor.value; }); Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Paint EX #6

Slide 22

Slide 22 text

Paint PWA Capabilities Progressive Enhancement Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Agenda

Slide 23

Slide 23 text

Desktop-Apps als PWA entwickeln Tausche .NET gegen Web Responsive Linkable Discoverable Installable App-like Connectivity Independent Fresh Safe Re-engageable Progressive

Slide 24

Slide 24 text

Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Web App Manifest Service Worker

Slide 25

Slide 25 text

Web App Manifest Distinguishes Web Apps from Websites JSON-based file containing metadata for apps only Apps can be identified by search engines, app store providers, etc. Tausche .NET gegen Web Desktop-Apps als PWA entwickeln PWA

Slide 26

Slide 26 text

manifest.webmanifest { "short_name": "Paint", "name": "Paint Workshop", "theme_color": "white", "icons": [{ "src": "icon.png", "sizes": "512x512" }], "start_url": "/index.html", "display": "standalone", "shortcuts": [/* … */] } Tausche .NET gegen Web Desktop-Apps als PWA entwickeln PWA Names Icons Display Mode Shortcuts Start URL Theme color (status bar/window bar)

Slide 27

Slide 27 text

Manifest Display Modes Tausche .NET gegen Web Desktop-Apps als PWA entwickeln PWA browser minimal-ui standalone fullscreen

Slide 28

Slide 28 text

Manifest Icon Purposes (any) any context (e.g. app icon) monochrome different color requirements (at risk) maskable user agent masks icon as required Tausche .NET gegen Web Desktop-Apps als PWA entwickeln PWA Safe Zone Windows iOS Android

Slide 29

Slide 29 text

Web App Manifest 1. In index.html, add the following tag to the document’s node (e.g., after the stylesheet): 2. Create the new file manifest.webmanifest and set: a) name and short_name to values of your choice b) display to standalone c) theme_color to #000080 and background_color to #808080 d) start_url to /index.html and scope to / e) icons to [{ src: "icon.png", sizes: "512x512" }] 3. Test manifest in DevTools: F12 > Application > Manifest Tausche .NET gegen Web Desktop-Apps als PWA entwickeln PWA EX #7

Slide 30

Slide 30 text

Manifest Shortcuts Tausche .NET gegen Web Desktop-Apps als PWA entwickeln PWA Secondary entrypoints for your application (e.g., home screen quick actions, jump lists, …) Static definition in Web App Manifest Dynamic API may follow in the future Supported by Google Chrome for Android

Slide 31

Slide 31 text

Web App Manifest 1. In manifest.webmanifest, add (at least) the following shortcut: "shortcuts": [{ "name": "About Paint", "short_name": "About", "description": "Show information about Paint", "url": "/about.html", "icons": [{ "src": "icon.png", "sizes": "512x512" }] }], 2. Test in DevTools: F12 > Application > Manifest Tausche .NET gegen Web Desktop-Apps als PWA entwickeln PWA EX #8

Slide 32

Slide 32 text

Web App Manifest – Chrome Installability Signals Web App is not already installed Meets a user engagement heuristic (previously: user has interacted with domain for at least 30 seconds) Includes a Web App Manifest that has short_name or name, at least a 192px and 512px icon, a start_url and a display mode of fullscreen, standalone or minimal-ui Served over HTTPS Has a registered service worker with a fetch event handler Tausche .NET gegen Web Desktop-Apps als PWA entwickeln PWA

Slide 33

Slide 33 text

Service Worker JavaScript snippet executed in an own thread, registered by the website Acts as a controller, proxy, or interceptor Has a cache to store responses (for offline availability and performance) Can wake up even when the website is closed and perform background tasks (e.g., push notifications or sync) Tausche .NET gegen Web Desktop-Apps als PWA entwickeln PWA

Slide 34

Slide 34 text

Service Worker Tausche .NET gegen Web Desktop-Apps als PWA entwickeln PWA Service Worker Internet Website HTML/JS Cache fetch

Slide 35

Slide 35 text

Workbox Toolchain by Google Includes a CLI Generates a service worker implementation from directory contents (e.g., build output, or our development directory) Allows you to modify the service worker behavior (maximum flexibility) Tausche .NET gegen Web Desktop-Apps als PWA entwickeln PWA

Slide 36

Slide 36 text

Service Worker 1. In your console, run: a) npm run workbox wizard (use “src” as project root and defaults for the rest) b) npm run workbox generateSW 2. In index.html, add the following script to the bottom of the node: if (navigator.serviceWorker) { navigator.serviceWorker.register('sw.js'); } 3. Test in DevTools: F12 > Application > Service Worker 4. Install the app: Address Bar > ⊕ Install Tausche .NET gegen Web Desktop-Apps als PWA entwickeln PWA EX #9

Slide 37

Slide 37 text

Service Worker Debugging More information on installed service workers can be found on • chrome://serviceworker- internals (Google Chrome) • about:serviceworkers (Mozilla Firefox) Tausche .NET gegen Web Desktop-Apps als PWA entwickeln PWA

Slide 38

Slide 38 text

Service Worker Debugging Tausche .NET gegen Web Desktop-Apps als PWA entwickeln PWA

Slide 39

Slide 39 text

Lighthouse Tausche .NET gegen Web Desktop-Apps als PWA entwickeln PWA Auditing tool by Google, integrated in Chrome DevTools Automatically checks performance metrics, PWA support, accessibility, SEO and other best practices and gives tips on how to improve Can simulate mobile devices

Slide 40

Slide 40 text

Lighthouse 1. Make sure only one tab/window of your PWA is open 2. Open DevTools: F12 > Lighthouse 3. Make sure to select at least the “Progressive Web App” category 4. Click “Generate report” Tausche .NET gegen Web Desktop-Apps als PWA entwickeln PWA EX #10

Slide 41

Slide 41 text

Paint PWA Capabilities Progressive Enhancement Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Agenda

Slide 42

Slide 42 text

Tausche .NET gegen Web Desktop-Apps als PWA entwickeln

Slide 43

Slide 43 text

Project Fugu Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Capabilities »Let’s bring the web back – API by API« Thomas Steiner, Google

Slide 44

Slide 44 text

Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Capabilities Contacts Picker Screen Wake Lock API File System Access API Shape Detection API

Slide 45

Slide 45 text

Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Capabilities https://goo.gle/fugu-api-tracker

Slide 46

Slide 46 text

Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Capabilities navigator.share({ url: 'http://example.com' }); ShareIntent DataTransferManager … NSSharingServicePicker

Slide 47

Slide 47 text

File System Access API Some applications heavily rely on working with files (e.g. Visual Studio Code, Adobe Photoshop, …) File System Access API allows you to open, save and override files and directories Shipped with Google Chrome 86 Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Capabilities

Slide 48

Slide 48 text

File System Access API 1. Add the following buttons to the bar in index.html (before the color input): Open Save Copy Paste Share Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Capabilities EX #11

Slide 49

Slide 49 text

File System Access API 2. At the top of app.js, change the import as follows: import {bresenhamLine, getImage, toBlob} from "./helpers.js"; 3. Add the following lines at the bottom of app.js: const fileOptions = { types: [{ description: 'PNG files', accept: {'image/png': ['.png']} }] }; Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Capabilities EX #11

Slide 50

Slide 50 text

File System Access API 4. Add the following lines at the bottom of app.js: const btnSave = document.querySelector('#save'); btnSave.addEventListener('click', async () => { const blob = await toBlob(canvas); const handle = await window.showSaveFilePicker(fileOptions); const writable = await handle.createWritable(); await writable.write(blob); await writable.close(); }); Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Capabilities EX #11

Slide 51

Slide 51 text

File System Access API Add the following code at the end of app.js: const btnOpen = document.querySelector('#open'); btnOpen.addEventListener('click', async () => { const [handle] = await window.showOpenFilePicker(fileOptions); const file = await handle.getFile(); const image = await getImage(file); ctx.drawImage(image, 0, 0); }); Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Capabilities EX #12

Slide 52

Slide 52 text

Async Clipboard API Allows reading from/writing to the clipboard in an asynchronous manner (UI won’t freeze during long-running operations) Reading from the clipboard requires user consent first (privacy!) Supported by Chrome, Edge and Safari Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Capabilities

Slide 53

Slide 53 text

Async Clipboard API Add the following code at the end of app.js: const btnCopy = document.querySelector('#copy'); btnCopy.addEventListener('click', async () => { const blob = await toBlob(canvas); await navigator.clipboard.write([ new ClipboardItem({[blob.type]: blob}) ]); }); Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Capabilities EX #13

Slide 54

Slide 54 text

Async Clipboard API const btnPaste = document.querySelector('#paste'); btnPaste.addEventListener('click', async () => { const clipboardItems = await navigator.clipboard.read(); for (const clipboardItem of clipboardItems) { for (const type of clipboardItem.types) { if (type === 'image/png') { const blob = await clipboardItem.getType(type); const image = await getImage(blob); ctx.drawImage(image, 0, 0); } } } }); Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Capabilities EX #14

Slide 55

Slide 55 text

Web Share API Allows sharing a title, URL, text, or files API can only be invoked as a result of a user action (i.e. a click or keypress) Files supported in Chrome on Android, Edge on Windows Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Capabilities

Slide 56

Slide 56 text

Web Share API const btnShare = document.querySelector('#share'); btnShare.disabled = !('canShare' in navigator); btnShare.addEventListener('click', async () => { const blob = await toBlob(canvas); const file = new File([blob], 'untitled.png', {type: 'image/png'}); const item = {files: [file], title: 'untitled.png'}; if (await navigator.canShare(item)) { await navigator.share(item); } }); Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Capabilities EX #15

Slide 57

Slide 57 text

File System Handling API Register your PWA as a handler for file extensions Requires installing the application first Declare supported extensions in Web App Manifest and add imperative code to your application logic Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Capabilities

Slide 58

Slide 58 text

File System Handling API 1. Enable the following flag in Chrome Canary and restart the browser: chrome://flags/#file-handling-api 2. Add the following property to your manifest (manifest.webmanifest): "file_handlers": [{ "action": "/index.html", "accept": { "image/png": [".png"] } }] Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Capabilities EX #16

Slide 59

Slide 59 text

File System Handling API 3. Add the following code to the bottom of app.js: if ('launchQueue' in window) { launchQueue.setConsumer(async params => { const [handle] = params.files; if (handle) { const file = await handle.getFile(); const image = await getImage(file); ctx.drawImage(image, 0, 0); } }); } 4. Re-generate Service Worker: npm run workbox generateSW Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Capabilities EX #16

Slide 60

Slide 60 text

Summary - Project Fugu helps the web to become more and more capable - Project Fugu APIs could bring many more app experiences to the web (IDEs, productivity apps, etc.) - But: Web is threatened by alternative approaches and platforms, support for modern Web API varies from platform to platform - You can make a difference! - File bugs in browser engine bugtrackers - File Fugu requests: https://goo.gle/new-fugu-request Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Capabilities

Slide 61

Slide 61 text

Paint PWA Capabilities Progressive Enhancement Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Agenda

Slide 62

Slide 62 text

Overview Use available interfaces and functions of a system (opposite of Graceful Degradation) Users with modern, feature-rich browsers get a better experience Apps are available on older browsers, but with limited functionality Concept: Browser feature support should grow over time—thereby more users can enjoy an increasing number of features Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Progressive Enhancement

Slide 63

Slide 63 text

Overview if ('serviceWorker' in navigator) { navigator.serviceWorker.register(…) .then(() => /* … */); } In JavaScript: check whether an API/feature is available. If yes—use it! Otherwise: 1. Disable the functionality 2. Fall back to an alternative API (if available) Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Progressive Enhancement

Slide 64

Slide 64 text

Disabling unsupported features In app.js, disable the buttons in case the respective API does not exist, e.g.: btnOpen.disabled = !('showOpenFilePicker' in window); 1. Open: window.showOpenFilePicker() 2. Save: window.showSaveFilePicker() 3. Copy: navigator.clipboard + navigator.clipboard.read() 4. Paste: navigator.clipboard + navigator.clipboard.write() 5. Share: navigator.canShare() Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Progressive Enhancement EX #17

Slide 65

Slide 65 text

Providing an alternative implementation 1. In app.js, test for the existence of the showSaveFilePicker() method on the navigator object. 2. If the method does not exist, fall back to the following code for save: const anchor = document.createElement('a'); const url = URL.createObjectURL(blob); anchor.href = url; anchor.download = ''; anchor.click(); URL.revokeObjectURL(url); Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Progressive Enhancement EX #18

Slide 66

Slide 66 text

Deployment Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Electron/Capacitor JS HTML CSS .ipa .exe .app ELF .apk .appx Single-Page Application Capacitor Electron

Slide 67

Slide 67 text

(Simplified) Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Store Deployments https://example.com .appx .apk Server Microsoft Store Play Store Source Files

Slide 68

Slide 68 text

Q&A Tausche .NET gegen Web Desktop-Apps als PWA entwickeln

Slide 69

Slide 69 text

New powerful APIs regularly ship with new releases of Chromium-based browsers Some only add minor finishing touches, others enable whole application categories as productivity apps to finally make the shift to the web Some APIs already made their way into other browsers (e.g., Web Share) Fugu process makes sure that capabilities are implemented in a secure, privacy-preserving manner Let’s make the web a more capable platform! Tausche .NET gegen Web Desktop-Apps als PWA entwickeln Summary

Slide 70

Slide 70 text

Thank you for your kind attention! Christian Liebel @christianliebel [email protected]