Linux Format

Electron apps

Mats Tage Axelsson dips his toes in the waters of desktop applicatio­ns using Javascript and the Electron.js framework.

- Mats Tage Axelsson keeps fighting the tide, showing you what even he can do using Linux to make the world a better place and improve his own life in the process.

Mats Tage Axelsson dips your toes in the waters of desktop applicatio­ns using Javascript and the Electron.js framework.

Electron is actually a browser packaged with node.js and a few APIS. Because it’s built on top of the Chromium browser, you have everything available from there to add to your applicatio­n. Github developed it as part of the Atom editor; it was open-sourced in 2014 and is now popular with many open source developers. Electron is practical for building small (ish–ed), simple desktop applicatio­ns since it uses web technologi­es. This makes it very easy for web developers to make a desktop applicatio­n.

It has already been used for many productivi­ty and developmen­t applicatio­ns. The obvious use case is the

Atom editor which Github developed in parallel. Other applicatio­ns lift out web pages and social media and drop them into their own applicatio­n.

In this tutorial, you will learn how the parts hang together and how to create simple applicatio­ns in Electron. You will end up with your own ‘To Do’ applicatio­n that you can tweak to your heart’s content.

To follow this tutorial, all you need is your favourite editor prepared to handle Javascript and HTML. You should not need a browser, though the Electron system uses Chromium and Node.js. To make sure you get at least a glimpse of the ecosystem that is Electron, download https://github.com/electron/electronqu­ick-start from Github. This repository is a very basic applicatio­n that contains code to open a window. It is not useful on its own, but it contains many comments to help you get started and get your head around some principles used.

To make any Electron applicatio­n, you need a Javascript developmen­t environmen­t. In this case, you can start with the quick-start applicatio­n for Electron and install node.js with npm. Other packages you need are related to the purpose of the applicatio­n. Apart from for this, you just need to choose your favourite text editor or IDE for programmin­g Javascript.

When you install Electron using npm, most libraries are included, but you need to look for the ones that are used specifical­ly in your applicatio­n. Installing Electron is simple if you have installed packages in npm before. Just run the following in your new directory:

$ npm install electron

There are several ways to install, but this way suits a local filesystem on an ordinary laptop. For this tutorial, you will need electron and spectre.css.

Processes: Main and Renderer

In Electron applicatio­ns, there is one main process and only one, determined from the package.json file. The main process creates other web pages, and each of these is its own process. To create a new window, the main process uses the Browserwin­dow call, which creates a new renderer instance – one for each window. The renderer process is controlled by the Browserwin­dow API. The whole applicatio­n is controlled by events inside the ‘app’ object. The full documentat­ion for this ‘app’ object is at https:// electronjs.org/docs/api/app.

Before you start your project, it is a good idea to consider hosting it on the web: Github comes to mind. The easiest way to do this is to create the repo on Github first and then pull the almost empty project to your local disc. This also creates a comprehens­ive .gitignore file, which makes it a little easier to versioncon­trol your work.

When that’s done, it is time to initiate the project; this actually creates your package.json file. You could write this yourself but it’s useful to have the basic info filled in for you.

$ npm init

This command is a script that will ask you for basic informatio­n about the project. Fill it in as you want – you can change it afterwards. The file contains the applicatio­n’s name, version, descriptio­n and how to run the project. Inside the automatica­lly created file, there will be a scripts section. This is where you set how to test and run your applicatio­n. You can also use this file to define how you version-control your file. To make this an Electron applicatio­n, you should change, or add to, the start section of package.json.

While developing, you will want to start the applicatio­n to see if your latest changes work. As standard, you run npm start in the project directory. What ‘start’ means is defined in the start section.

{

“name”: “taskgame”,

“version”: “1.0.0”,

“descriptio­n”: “This is a task app that uses gamificati­on to encourage you in your endeavours.”, “main”: “main.js”,

“scripts”: {

“start” : “electron .”,

“test”: “echo \”Error: no test specified\” && exit 1” },

As you can see in the script, test procedures are also defined, but that is for another time. Further down in this file, you will soon see the packages that you install to make the project. To install your dependenci­es, use

npm, like this:

$ npm install electron spectre.css

Note that you can install Electron globally but that may cause problems if you have several projects running and you need to keep different versions for each project. You also do not need root to install in your project directory.

Now it is time to write your first file with code in it. For anything to work, you must at least include the parts you need, in our case Electron. In this applicatio­n, you will also use ‘path’, to make sure we can find our files locally.

The main purpose of this applicatio­n is to have a window where you can enter your goals and a second window to see how you are doing. For this to show up nicely, you include Browserwin­dow; the windows need a Menu; and ipcmain and ipcrendere­r are there to communicat­e between instances. ‘app’ is your main process, as mentioned.

In this applicatio­n, you want to be able to add and remove tasks. The task list will be in the main window – you will have an add window and finally you can remove tasks. In this tutorial, you will create an HTML file with a correspond­ing Javascript file.

Piecing together main.js

This file creates the app instance that keeps the whole applicatio­n running. In here, we define as many events as we can for a small applicatio­n like this.

Things we need to cover are the creation and destructio­n of windows, and also catching events from forms, like this:

// The main electron Modules const {app, ipcmain} = require(‘electron’) const path = require(‘path’) const Window = require(’./window.js’) const Datastore = require(’./datastore.js’) require(‘electron-reload’);

// create the Store for my tasks const taskdata =new Datastore({name: ‘Task List’})

The top of this file initiates the first few classes, including your own class. It also creates the data store you will use. The rest of the file creates and runs the main process.

In this code, the way to store the data is defined in a separate file ./Datastore/ and the windows code is wrapped in ./Window.js/. In the main.js file, you define the main process. You need to add the event handling code inside the main process since that is the controllin­g one.

...Earlier code... function main () {

// Create Task list window in memory let mainwindow = new Window({

file: path.join(’./renderer’, ‘mainwindow.html’) });

// Add task Window in memory let addtaskwin­dow

...Code to be added later… }

// End of function main

// Listen for applicatio­n to be ready app.on(‘ready’, main)

// Close all when closing main Window mainwindow.on(‘closed’, function(){ app.quit();

});

The main function is the process that handles your applicatio­n and the other processes. In the code above, you only add the process and the handlers for opening and closing routines. Later, you will also add the code that opens other processes and reacts to events from those processes.

Before you write the software, you need to plan how to tie in the HTML with the Javascript code. You also will need to know what events you are handling and where the code goes to handle them.

The file Window.js creates classes that you can use in the rest of the applicatio­n. With this approach, you can re-use the code more easily. If you are changing anything about window handling, you should change it here.

In Datastorag­e.js we define the storage; this ensures that you can change storage libraries when the code evolves and your needs change. A good example is if you want to switch to a new database.

mainwindow.html

The code here is a connection to the Javascript and an unordered list. You will populate the list when you create a new task. You will also see the list in here, including progress, failures and rewards.

task List

In the code above you define your stylesheet framework. This framework adds a lot of styles, which is great. However, you can override the settings in your own file – hence the style.css file inserted after the framework. Note that you can override styles, but it is not as straightfo­rward as that, as only the last definition will actually have an effect. There are specificit­y rules, but this article is not about CSS.

The code that adds functions is below; this page calls the Javascript at the bottom of the body text. It is usually a good idea to load the Javascript last as it may slow the loading of the site. Make sure that websites can do something even without Javascript.

task Game List

Newspapers in English

Newspapers from Australia