1559101970
In this tutorial we’ll build a cross-platform desktop application with Electron and web technologies such as TypeScript and Angular.
Electron.js is a popular platform for building cross-platform desktop apps for Windows, Linux and macOS with JavaScript, HTML, and CSS. It’s created and maintained by GitHub and it’s available under the MIT permissive license. It was initially created for GitHub’s Atom editor, but has since been used to create applications by companies like Microsoft (Visual Studio Code), Facebook, Slack, and Docker.
Electron makes use of powerful platforms like Google Chromium and Node.js, but also provides its own set of rich APIs for interacting with the underlying operating system.
Electron provides a native container that wraps web apps so they look and feel like desktop apps with access to operating system features (similar to Cordova for mobile apps). This means we can use any JavaScript libraryor framework to build our application. In this tutorial, we’ll be using Angular.
For this tutorial, you will need to have these prerequisites covered:
Let’s get started by installing Angular CLI, which is the official tool for creating and working with Angular projects. Open a new terminal and run the following command:
npm install -g @angular/cli
We’ll install the Angular CLI globally on our system. If the command fails with the EACCESS error, add sudo
before your command in Linux or macOS, or run the command prompt as an administrator in Windows.
If the CLI is installed successfully, navigate to your working directory and create a new Angular project using the following commands:
cd ~
ng new electron-angular-demo
Wait for your project’s files to be generated and dependencies to be installed from npm. Next, navigate to the root of your project and run the following command to install the latest version of Electron from npm as a development dependency:
npm install --save-dev electron@latest
As of this writing, this command will install Electron v4.1.4.
Read Also: Build a Basic CRUD App with Node and React
Next, create a main.js
file and add the following code:
const {app, BrowserWindow} = require('electron')
const url = require("url");
const path = require("path");
let mainWindow
function createWindow () {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, `/dist/index.html`),
protocol: "file:",
slashes: true
})
);
// Open the DevTools.
mainWindow.webContents.openDevTools()
mainWindow.on('closed', function () {
mainWindow = null
})
}
app.on('ready', createWindow)
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', function () {
if (mainWindow === null) createWindow()
})
This code simply creates a GUI window and loads the index.html
file that should be available under the dist
folder after we build our Angular application. This example code is adapted from the official starter repository.
Next, open the package.json
file of your project and add the main
key to set the main.js
file as the main entry point:
{
"name": "electron-angular-demo",
"version": "0.0.0",
"main": "main.js",
// [...]
}
Next, we need to add a script to easily start the Electron app after building the Angular project:
{
"name": "electron-angular-demo",
"version": "0.0.0",
"main": "main.js",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"start:electron": "ng build --base-href ./ && electron ."
},
// [...]
}
We added the start:electron
script which runs the ng build --base-href ./ && electron .
command:
ng build --base-href ./
part of the command builds the Angular app and sets the base href to ./
.electron .
part of the command starts our Electron app from the current directory.Now, in your terminal, run the following command:
npm run start:electron
An Electron GUI window will be opened, but will be blank. In the console, you’ll see the Not allowed to load local resource: /electron-angular-demo/dist/index.html error.
Electron is unable to load the file from the dist
folder because it simply doesn’t exist. If you look in your project’s folder, you’ll see that Angular CLI builds your app in the dist/electron-angular-demo
folder instead of just the dist
folder.
In our main.js
file, we are telling Electron to look for the index.html
file in the dist
folder without a subfolder:
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, `/dist/index.html`),
protocol: "file:",
slashes: true
})
);
__dirname
refers to the current folder from which we’re running Electron.
We use the path.join()
method to join the path of the current folder with the /dist/index.html
path.
You can either change the second part of the path to /dist/electron-angular-demo/index.html
or, better yet, change the Angular configuration to output the files in the dist
folder without using a subfolder.
Open the angular.json
file, locate the projects → architect → build → options → outputPath
key and change its value from dist/electron-angular-demo
to just dist
:
"projects": {
"electron-angular-demo": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist",
Head back to your terminal and again run the following command:
npm run start:electron
The script will call the ng build
command to build the Angular app in the dist
folder, and call electron
from the current folder to start the Electron window with the Angular app loaded.
This is a screenshot of our desktop app running Angular:
Let’s now see how we can call Electron APIs from Angular.
Read Also: Node.js, ExpressJs, MongoDB and Vue.js (MEVN Stack) Application Tutorial
Electron apps make use of a main process running Node.js and a renderer process running the Chromium browser. We can’t directly access all of Electron’s APIs from the Angular app.
We need to make use of IPC or Inter-Process Communication, which is a mechanism provided by operating systems to allow communication between different processes.
Not all Electron APIs need to be accessed from the main process. Some APIs can be accessed from the renderer process, and some APIs can be accessed from both the main and renderer processes.
BrowserWindow, which is used to create and control browser windows, is only available in the main process. The desktopCapturer API (used for capturing audio and video from the desktop using the navigator.mediaDevices.getUserMedia
API) is only available in the renderer process. Meanwhile the clipboard API (for performing copy and paste operations on the system clipboard) is available on both the main and renderer processes.
You can see the complete list of APIs from the official docs.
Let’s see an example of calling the BrowserWindow
API, available only in the main process, from the Angular app.
Open the main.js
file and import ipcMain
:
const {app, BrowserWindow, ipcMain} = require('electron')
Next, define the openModal()
function:
function openModal(){
const { BrowserWindow } = require('electron');
let modal = new BrowserWindow({ parent: mainWindow, modal: true, show: false })
modal.loadURL('https://www.sitepoint.com')
modal.once('ready-to-show', () => {
modal.show()
})
}
This method will create a child modal window, load the [https://www.sitepoint.com](https://www.sitepoint.com)
URL inside it, and display it when it’s ready.
Read also: Node, Express, React.js, Graphql and MongoDB CRUD Web Application
Next, listen for an openModal
message that will be sent from the renderer process and call the openModal()
function when the message is received:
ipcMain.on('openModal', (event, arg) => {
openModal()
})
Now, open the src/app/app.component.ts
file and add the following import:
import { IpcRenderer } from 'electron';
Next, define an ipc
variable and call require('electron').ipcRenderer
to import ipcRenderer
in your Angular component:
private ipc: IpcRenderer
constructor(){
if ((window).require) {
try {
this.ipc = (window).require('electron').ipcRenderer;
} catch (e) {
throw e;
}
} else {
console.warn('App not running inside Electron!');
}
}
The require()
method is injected at runtime in the renderer process by Electron and as such, it will only be available when running your web application in Electron.
Read also: WebSocket + Node.js + Express — Step by step tutorial using Typescript
Finally, add the following openModal()
method:
openModal(){
console.log("Open a modal");
this.ipc.send("openModal");
}
We use the send()
method of ipcRenderer
to send an openModal
message to the main process.
Open the src/app/app.component.html
file and add a button, then bind it to the openModal()
method:
Open Modal
Now, run your desktop app using the following command:
npm run start:electron
This is a screenshot of the main window with a button:
You can find the source code of this demo from this GitHub repository.
In this tutorial, we’ve looked at how to run a web application built with Angular as a desktop application using Electron.js. We hope you’ve learned how easy it can be to get started building desktop apps with your web development toolkit!
#electron #angular #typescript #web-development
1616583098
AppClues Infotech is a top-notch cross platform mobile app development company in USA. With strong mobile app designers & developers team that help to create powerful cross-platform apps using the current market technologies & functionalities.
For more info:
Website: https://www.appcluesinfotech.com/
Email: info@appcluesinfotech.com
Call: +1-978-309-9910
#cross platform mobile app development company #best cross platform mobile app development #cross platform app development services #top cross platform app development company #cross-platform app development usa #hire cross-platform app developer
1615889878
AppClues Infotech offers high-quality cross-platform mobile app development services. Our expert & highly skilled developers build innovative, robust, scalable & interactive mobile apps for your business needs with the most advanced tools & features.
For more info:
Website: https://www.appcluesinfotech.com/
Email: info@appcluesinfotech.com
Call: +1-978-309-9910
#cross platform mobile app development company #best cross platform mobile app development #cross platform app development services #top cross platform app development company #hire cross-platform app developers #hire top cross-platform app developers usa
1611229536
Are you finding the best Cross-Platform App Development Service providing company in USA? We at AppClues Infotech is one of the leading mobile app development company in USA that helps to make a creative & high-quality Cross-Platform mobile app with modern methodology & technology.
Hire our dedicated team of designers & programmers for your app development project.
Cross-Platform App Development Services
• Custom Cross-Platform App Development
• Porting Cross-Platform Apps
• Cross-Platform App Migration
• Cross-Platform UI/UX Designing
For more info:
Website: https://www.appcluesinfotech.com/
Email: info@appcluesinfotech.com
Call: +1-978-309-9910
#best cross-platform app development services company in usa #cross platform mobile app development company #best cross platform mobile app development #cross platform app development services #top cross platform app development company #cross-platform app development
1614600295
Looking for dedicated & highly skilled Cross-Platform mobile app developers for your app development project? We at AppClues Infotech have the best team of Cross-Platform mobile app developers that help to designing & developing powerful cross-platform apps with the latest trends & features.
For more info:
Website: https://www.appcluesinfotech.com/
Email: info@appcluesinfotech.com
Call: +1-978-309-9910
#cross platform mobile app development company #cross platform mobile app development company #cross platform mobile app development company #top cross platform app development company #top cross platform app development company #top cross platform app development company
1623323207
Cross-Platform Development Services
With the development in mobile app technology, a huge time saver as well as the quality maintainer technology is Cross-Platform App development. The development of an app that takes less time to develop as well as uses one technology to develop an app for both android and iOS is game-changing technology in mobile app development.
Want to develop or design a Cross-platform app?
With the successful delivery of more than 950 projects, WebClues Infotech has got the expertise as well as a huge experience of cross-platform app development and design. With global offices in 4 continents and a customer presence in most developed countries, WebClues Infotech has got a huge network around the world.
Want to know more about our cross-platform app designs?
Visit: https://www.webcluesinfotech.com/cross-platform-design/
Share your requirements https://www.webcluesinfotech.com/contact-us/
View Portfolio https://www.webcluesinfotech.com/portfolio/
#cross-platform development services #cross platform mobile app development services #cross-platform mobile app development services #cross platform app development services #hire cross platform app developer #hire cross-platform app developer india usa,