adplus-dvertising
Connect with us

Tech

How to use Electron to build responsive desktop apps – The Next Web

Published

 on


If you are like me, you love JavaScript and its ecosystem, and you have been building amazing web applications using frameworks like React or performant web servers with NodeJS . Now you want to develop a desktop application, and you don’t want to learn a new programming language, or perhaps you want to re-use as much as you can from existing web projects. Here is when Electron enters the picture to save the day.

Electron allows you to build desktop applications using HTML, CSS, and JavaScript. There are many arguments on the internet against Electron, one of them being its performance and many times low-quality apps, but don’t blame the framework; Electron is powerful and can be performant. Today, many popular applications run on top of Electron, such as VS Code, Slack, Skype, Discord, and more.

But why is it then that many people argue so poorly against it? The problem starts with the apps and the way people use Electron. For many, porting a web application to an Electron means taking your existing code as-is and embedding it into an Electron container. Is this a terrible thing to do? Maybe not, but you are not taking advantage of the power of Electron to its fullest. You are merely changing a browser tab for an independent application.

300x250x1

What can we improve? In this article, we will explore the basics of Electron, and we will build a sample application to show some of the Electron ways.

[Read: Neural’s market outlook for artificial intelligence in 2021 and beyond]

How does Electron work?

Electron is built on top of 3 main components:

  • Chromium: responsible for the web content
  • NodeJS: for interacting with the OS
  • Custom APIs: to solve for common issues when dealing with the OS

Each of these components interacts on a different level on the Electron architecture layer, as shown in the architecture diagram.

Electron Architecture

Electron works with two types of processes.

  • Main Process: responsible for window management and all interactions with the OS. It’s where it all starts, and it can create and manage multiple renderer processes
  • Renderer Process: There could be one or more, each of which will host a chromium instance and be responsible for the web contents.

It’s important to note that the renderer processes cannot access OS features directly. Instead, they communicate with the Main Process through IPC to achieve these tasks.

Many typical Electron applications would use the main process to create one renderer process and load their web application. Today we are going to take that a step forward.

Must-have section: Hello World!

Next, we are going to build a “hello world!” application. We will not use any framework or libraries that are not necessary to stay focused on the Electron code.

Let’s get started.

Setting up Electron

The first step into building an application is to create a project and install the electron library, so start with the project creation using NPM:

And set up your application details. As the starting point for the application, I like to use main.js, but you can use any file name you want.

Next, install Electron.

Building the screen

For our micro hello world example, we need two files, main.js and hello-world.html. main.js is our main process; we will create the first renderer process that will load our hello-world.html.

Here is the starter code for main.js

The starter kit will handle the minimum operations to run the application, creating the first renderer with new BrowserWindow and loading main.html on it. It will also address some scenarios to exit the application and to re-launch the main window when needed.

As to our main.html we will use the following:

It is just a simple HTML exposing the current versions of node, chrome, and Electron our app uses.

Finally, we need to run our application; first, you need to change the package.json and add the start script.

If everything worked out well, you should see a window like this:

Hello World app made with Electron

Isn’t there an easier way?

The short answer is YES! Though it comes at a cost. Many boilerplates provide a starting point for building Electron applications, some of them using vanilla JS, others directly integrated with some of the most popular frameworks like React and Vue.

I’m not a big fan of these boilerplates as they often come with many libraries and additions I don’t need. But they are a great place to get you started.

Here are some popular ones:

Building smooth applications

We already saw in the architecture of Electron how things work. And if you are like me, you are probably worried about all those instances of Chromium and Node running, and you should be. We all know how Chromium (or Chrome) can devour our memory and affect our performance, so what can we do to avoid our Chromium-based application to do exactly that? How do we keep them performant?

Here are a few tips:

Never, ever, block the main process

The main process is where it all starts. It is the parent process for all the processes of the application. It is the one that primarily communicates with the operating system, handles all the windows and communications between them, and it runs the UI thread.

Blocking this process means that the application will stop responding until the operation finishes. Under no circumstances run code that is CPU intensive here and takes a long time to complete.

Here are some recommendations:

  • For CPU intensive operations, delegate those functions to either a worker thread, a renderer process, or even spawn a dedicated process to perform that task (though make sure you know what you are doing for this last one).
  • Avoid the use of the remote module as much as possible. It is far too easy to unknowingly block the UI thread using the remote module.
  • Avoid using blocking I/O operations on the main thread; if needed, use the asynchronous equivalents provided by NodeJS.

Is it ok to block the renderer process?

Not really. Perhaps the consequences won’t be as harmful as blocking the main process, but blocking the renderers come at a price. Your windows may become sluggish or unresponsive, and the overall user experience will be terrible.

When we use the web, we are used to some web apps suddenly going slow, not being smooth, and we are ok with it; However, when it comes to desktop applications, our standards are higher. Be aware of this, as user expectations matter.

What can I do to make my apps more responsive? Pretty much the same things we could do on our web apps; after all, on the renderer process, we are just talking about Chromium.

  • requestIdleCallback: API allowing the execution of JavaScript to be queued to run in idle browser time, either at the end of a frame or when the user is inactive.
  • Web workers: the best tool to run expensive computations on web browsers by assigning them to a new thread.

You don’t need cross-browser compatibility

During web development, it is very typical to use polyfills to support different browsers. When building Electron applications, you don’t need any of that. If it runs on Chromium, it runs on Electron, and there’s no need to support any other browser. Reduce your bundles, and make everything faster by not loading these extra modules.

Bundle all of your code

In web development, we sometimes load scripts or pages from servers, like CDNs, which are served separately from our application, and that’s fine. After all, for the web, we always need to download these assets to run the application.

For desktop applications, this is different. Avoid any unnecessary network requests by bundling all your static assets, scripts, and contents in your application. This will enable your app to do two things, work offline, and speed up the load process as reading the disk is cheaper than going to the internet.

Conclusion

Next time you need to build a cross-platform desktop application, I recommend you to try Electron, especially if you are coming from JavaScript, and you already have some code you may be able to re-use.

Just be aware, Electron can be great if used right. Keep in mind that though it looks like a web, it is not precisely a web, and thus you will need to make some special considerations to make it work.

This article was originally published on Live Code Stream by Juan Cruz Martinez (twitter: @bajcmartinez), founder and publisher of Live Code Stream, entrepreneur, developer, author, speaker, and doer of things.

Live Code Stream is also available as a free weekly newsletter. Sign up for updates on everything related to programming, AI, and computer science in general.

Read next:

Enjoy stargazing while you can, SpaceX Starlink is about to ruin it

Let’s block ads! (Why?)

728x90x4

Source link

Continue Reading

Tech

The Ultimate Recap of Sea Otter 2024 – Pinkbike.com

Published

 on



Tech

Vittoria Releases New Peyote & Mezcal XC Race Tires
Maxxis Team Spec Aspen ST Tire
New DT Swiss 240 DEG Hubs
Kali Protectives’ New Full Face Helmets
Industry Nine’s SOLiX M Hubs & Wheelsets
Michelin’s Aggressive New Wild Enduro Tires
Praxis’ New Flat Pedals, Stem, & Carbon Bottle Cage
Transmission Cage Upgrades from Kogel, Ceramicspeed, and Cascade Components
Randoms Round 1 – Sea Otter 2024
Madrone Cycles’ SRAM Eagle Repair Kits & Prototype Derailleur
Vorsprung’s New Telum Coil Shock
EXT’s Vaia Inverted DH Fork & Updated Coil Shocks
Randoms Round 2: New Tools, Goggles, Grips, Racks, & More – Sea Otter 2024
What’s New in Women’s MTB Apparel at Sea Otter 2024
Even More Randoms – Sea Otter 2024
Randoms Round 3: Dario’s Treasures
What’s New for the Kids at Sea Otter 2024
Deity Releases New Stems, Grips, & Pedals
Dario’s Final Sea Otter Randoms
Brian’s Randoms from Sea Otter 2024

300x250x1

Videos

With roots dating back to 1991, the Sea Otter Classic is one of the biggest biking events and tradeshows each year and brings together all sides of the biking industry from athletes to brands, spectators and consumers. Taking place in April in the sunny hills of Monterey, California, that means this event really feels like the official start to the biking season in North America. Christina Chappetta covers why it’s much different to an indoor European biking tradeshow, a World Cup racing weekend or even Crankworx mountain bike festival, in that it encompasses nearly ALL of the biking disciplines, including road cycling, enduro, downhill, dual slalom, XC, trials riding and more.

In the past fortnight, we have seen large amount of new tech releases. However, Sea Otter 2024 represents some of the first opportunities for many riders to see these things in the flesh, as well as take a deeper dive into what the product aims to do.
Welcome to a video summary from Day 2 of the Sea Otter Classic.
There are so many giveaways, interesting new products and colourful characters at Sea Otter Classic that it’s hard to stand out from the crowd. Ben Cathro takes a lap of the venue to find his favourites.



Adblock test (Why?)

728x90x4

Source link

Continue Reading

Tech

Apple iPad Air 2024: Insider Makes Hasty U-Turn On New Feature – Forbes

Published

 on


Well, that was quick. On May 18, a respected industry insider predicted a new display technology for the iPad Air that’s expected in the coming days—Apple just announced its latest special event.

The new 12.9-inch iPad Air, the report claimed, would have the same miniLED backlighting currently found on the larger iPad Pro, using the leftover inventory from the current Pro as that model switches to OLED. That was exciting news.

But now, Ross Young, the analyst who made the claim, has changed his mind. The new prediction, shared with paid subscribers only, is that the miniLED technology won’t be coming to the iPad Air, in either size.

300x250x1

While it made sense that the inventory could be maximized in this way, it now “makes sense” that it won’t.

Young says that while he’d heard from supply chain sources that it would, he’d now had contact from “even more supply chain sources” that it won’t.

And the reason this change of heart now makes sense is that this miniLED technology is expensive, so it would be surprising if it made it to the iPad Air, which is more affordable than the Pro.

That’s not quite all the analyst shared. He also said that there are now reports of a new iPad coming later in the year. This is a 12.9-inch iPad, with miniLED backlighting and it could arrive between October and December this year.

function loadConnatixScript(document)
if (!window.cnxel)
window.cnxel = ;
window.cnxel.cmd = [];
var iframe = document.createElement(‘iframe’);
iframe.style.display = ‘none’;
iframe.onload = function()
var iframeDoc = iframe.contentWindow.document;
var script = iframeDoc.createElement(‘script’);
script.src = ‘//cd.elements.video/player.js’ + ‘?cid=’ + ’62cec241-7d09-4462-afc2-f72f8d8ef40a’;
script.setAttribute(‘defer’, ‘1’);
script.setAttribute(‘type’, ‘text/javascript’);
iframeDoc.body.appendChild(script);
;
document.head.appendChild(iframe);

loadConnatixScript(document);

(function()
function createUniqueId()
return ‘xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx’.replace(/[xy]/g, function(c)
var r = Math.random() * 16 );

const randId = createUniqueId();
document.getElementsByClassName(‘fbs-cnx’)[0].setAttribute(‘id’, randId);
document.getElementById(randId).removeAttribute(‘class’);
(new Image()).src = ‘https://capi.elements.video/tr/si?token=’ + ’44f947fb-a5ce-41f1-a4fc-78dcf31c262a’ + ‘&cid=’ + ’62cec241-7d09-4462-afc2-f72f8d8ef40a’;
cnxel.cmd.push(function ()
cnxel(
playerId: ’44f947fb-a5ce-41f1-a4fc-78dcf31c262a’,
playlistId: ‘aff7f449-8e5d-4c43-8dca-16dfb7dc05b9’,
).render(randId);
);
)();

This is intriguing. What could it be? Assuming that the iPad Pro and iPad Air are released in May, it’s extremely unlikely either will be updated later in the year. And if the iPad Air isn’t pricey enough for miniLED to be included, what tablet could Apple be introducing that is the same size as the bigger Pro, with a pricey screen tech, which would sit between the Air and the Pro, it seems?

Young is highly reliable, but this seems slightly preposterous to me. The only other iPad in the range due a refresh is the regular iPad (at 12.9-inches, the iPad mini is clearly out of the picture) and that doesn’t seem likely either.

It seems to me that any regular iPad will almost certainly have the same screen size as now, 10.9 inches. The regular iPad only grew to this size screen in the current generation, and Apple almost never changes designs after one iteration.

Perhaps things will become clearer as the year goes on.

Adblock test (Why?)

728x90x4

Source link

Continue Reading

Tech

Woman who left beaten dad on floor for 2 days was 'overwhelmed' with his care, judge told – CBC.ca

Published

 on


A Calgary woman who abused her sick, 77-year-old father was “overwhelmed” at the task of caring for him, a judge heard Wednesday at a sentencing hearing. 

In January, Tara Picard, 52, pleaded guilty to charges of assault and failing to provide the necessaries of life after her father (whom CBC News is not naming) was found injured on a basement floor, where he’d been lying for two days. 

On Wednesday, prosecutor Donna Spaner and defence lawyer Shaun Leochko asked the judge to allow Picard to serve her sentence in the community under conditions as part of a conditional sentence order.

300x250x1

Justice Indra Maharaj agreed to a two-year conditional sentence for Picard followed by a year of probation. 

“There is no doubt she became overwhelmed,” said Spaner in her submissions. “There is no question Ms. Picard has remorse.”

Leochko told the judge that caring for her father “was really more than [Picard] could handle.”

Maharaj heard that Picard is Indigenous and was the victim of abuse growing up. She lives in a sober dorm-style facility and is working with a mental health and addictions navigator, according to Leochko.

A ‘willingness to give back’

As part of the sentence, Picard must complete 300 hours of community service. 

Justice Maharaj commended Picard for “taking that on.”

“That shows me Ms. Picard sincerely does recognize what has happened here,” said the judge. 

“What I interpret from that is Ms. Picard’s willingness to give back to her community.”

During Picard’s plea, court heard that in November 2021, Picard and her father fought over his drinking. 

Nurses discover victim

The victim suffers from a number of medical issues, including diabetes, heart disease, dementia and alcoholism.

At the time, home-care registered nurses were assigned to help provide supplementary care.

Nurses found the victim wearing a soiled adult diaper and suffering from two black eyes with blood on his head. 

He told the nurses who discovered him that he’d been there for two days. 

Picard admitted she knew her father had fallen and she had “administered a number of physical blows.”

Adblock test (Why?)

728x90x4

Source link

Continue Reading

Trending