Building an Electron File Explorer with Quasar

(and Vue)

Jeff Galbraith
Nov 21, 2018 · 15 min read
Image for post
Image for post
Electron File Explorer


What is Electron?

If you can build a website, you can build a desktop app. Electron is a framework for creating native applications with web technologies like JavaScript, HTML and CSS. It takes care of the hard parts so you can focus on the core of your application.

With Electron, you get two processes: the main process and the renderer process. You can think of the main process as your back-end code and the renderer process as your front-end code.

Electron Homepage

What is Quasar?

With Quasar, you can build:

1. SPAs (Single Page App)
2. SSR (Server-side Rendered App) (+ optional PWA client takeover)
3. PWAs (Progressive Web App)
4. Mobile Apps (Android, iOS, …) through Apache Cordova
5. Multi-platform Desktop Apps (using Electron)

It is basically an all-in-one solution for web developers with a rich supply of components derived from innovative ideas and concepts that take away the pain (also known as The Quasar Method to Quasarians) of managing a project with eslint, webpack, babel, etc., so you can get on with developing your app (web, desktop or mobile).

Quasar Framework Homepage

What is a File Explorer

Why a tutorial?

Sarcasm aside, let me say just a few things. I work with Vue on a day-to-day basis and use Quasar Framework. We made this decision collaboratively back in September 2017. It was a gamble to use Vue and even more so for Quasar. Since then, as of this writing, Vue (120k) has more stars on Github than React (116k) or Angular (42k) and Quasar (7.7k) is growing rapidly in popularity with it’s all-in-one solution. So far, we feel our gamble has paid off in spades as we gear up to release 5.0 of our product (IntelliView Technologies Homepage) after converting from Angular.

OK, back to why a tutorial? Our product provides an intelligent camera that creates alert videos that Users can export to their computers, as well as archive videos. With that video, we have a custom SVG that runs as an overlay on top of the video so the User can see the analytics. Because the SVG is custom, we needed to write an offline viewer for these exported videos that can also play the associated SVG as an overlay. Thus, we used Electron and Quasar to develop a custom desktop application that fits our needs. So, yes, I am not that experienced with Electron, but I did learn things along the way, that, in this tutorial I will share with you.

About the Tutorial

What I will be discussing is design decisions, code decisions, electron security and highlight various coding issues and tasks that I had to overcome in order to create this project. You might already be an experienced coder familiar with everything that I have to say, or you just might learn something you didn’t know about. Either way, it’s basically a history of events that allowed this project to be done.

Design Decisions

End Goal

One thing I needed to keep in mind is that with Linux, the file system is under one hierarchy. All files and folders are under root. Windows is different in that each hard drive is basically a root of the files and folders that the hard drive contains. This will present a challenge as I’ll need to get the hard drives being used by Windows and then treat each like a root.

Existing File Explorers

Image for post
Image for post
Windows Explorer — List Mode
Image for post
Image for post
Windows Explorer — Grid Mode

Let’s take a look at Ubuntu Files:

Image for post
Image for post
Ubuntu Files — List Mode
Image for post
Image for post
Ubuntu Files — Grid Mode

Both file explorers offer a grid and list mode. They also have an (optional) address bar at the top. There is navigation of various sorts on the left side. Then there is the content area in the middle that shows both files and folders.


Here are the things I’ll do:

  1. Have a read-only address bar that shows the current path. I’ll call this the breadcrumb bar. For each part of the path, the User will be able to click on it in order to make back-traversal easy.
  2. Show shortcuts on the left side. This will show links to a number of pre-defined paths. Both Windows and Linux share this concept. Like, the home, documents, and downloads folder. I don’t want to hard-code this, so I’ll have to research if there is a way of getting them via Electron.
  3. Under the shortcuts I want to show a traversal-tree that expands each folder to show additional folders. I’ll be able to use the Quasar QTree component for this. It provides for lazy loading of each tree node, so this will be perfect as I want to load data on-demand to keep things running quickly.
  4. In the main content area I want to show the contents of a folder. The contents may contain other folders as well as files. I want to display the file name and I want to display an icon that represents the file mime type for the most commonly used files. In the content area, I also want the User to be able to switch between grid and list views. And, finally, if the User double-clicks on a folder in the content area, the UI will navigate to the selected folder.
  5. In the content area, if additional files are added, removed, modified, while viewing, then they’ll automatically be updated. There is a great package called Chokidar that I can use to watch files and give me notifications.
  6. Finally, if a file is an image, instead of a generic icon, I want to show a thumbnail of the actual image.

Here are things I won’t do:

  1. Double-clicking a file won’t open it. Although this would be nice, this is not one of my goals at this time.
  2. The User will be unable to create, delete or modify files. This will be a read-only app only to provide proof-of-concept abilities only.
  3. The User will be unable to get information (properties) about a selected file.

In general, the User won’t be able to do much except traverse the file system.

These items are likely something I’d do when if I expand out this project with additional functionality.

Security Concerns

This is actually the default setting for this option. But, I did want to highlight it here.

When false, it will disable the same-origin policy (usually using testing websites by people), and set allowRunningInsecureContent to true if this options has not been set by user. Default is true.

You can read up on more options here.

This presents an additional challenge as you can no longer use the file:// protocol from within the renderer process. In this case, I want to be able to show thumbnails of files that are image-based. So, I’ll have to feed them from the main process to the renderer process via a web server instance, like express.


Image for post
Image for post

To begin a project, from the command-line:

quasar init my-project-name

This will create a new folder, based on your current folder, and start the scaffolding process. But, first, you will be asked several questions about your project:

Image for post
Image for post
Quasar Project Initialization

To start the web application, you can now type quasar dev or your can build a production-ready web site using quasar build.

At this point, I have not told Quasar to add Electron. So, I type:

quasar dev -m electron -t mat

Quasar will detect that Electron needs to be installed, does that, then continues on with the development build.

The folder system that Quasar creates is logical and easy to understand:

Image for post
Image for post
Quasar Folder Scaffolding

A very nice feature of Quasar is layouts. This is basically a controller for a page layout and you create pages and components that will be loaded into your layout. For most of my work, I use just one layout, but you can have more than one that does different takes based on your needs.

I will want to have Quasar create the page for use using quasar new <something> command:

$ quasar new

Image for post
Image for post
quasar new

This is what I will be creating:

  1. A main Layout: created automatically for you by Quasar when you init the project.
  2. A contents page using this Quasar command: quasar new p contents
  3. And, then, our components: breadcrumbs, shortcuts, folderTree, gridItem and gridItemImage. These can be created all at once with this Quasar command: quasar new p breadcrumbs, shortcuts, folderTree, gridItem, gridItemImage

Just in case there is any confusion, my gridItem component will contain an image and text and will be displayed on the contents area for both grid and list modes of the file explorer. The gridItemImage component will be a child of the gridItem component and be responsible for image display. This could be a generic icon of sorts, based on mime-type, or if an image file, then the actual image. It will also be responsible for centering images that may be smaller than the display area.

Task #1: Modifications to Scaffolding

Image for post
Image for post
run scripts in package.json

The first one with the lint-fix command is because I am lazy. When I forget to do something that that eslint complains about, I issue yarn lint-fix (or npm run lint-fix) and if the issue can be fixed, it goes away.

The dev command is for concurrent development while running. Changes to your code cause an HMR (Hot Module Reload).

Finally, the last two commands build-linux and build-win32 are to build the production ready applications. build-linux should be called on a Linux system and build-win32 should be called on a Windows system. (Note: For windows I will actually be building a 64-bit application. Electron uses “win32” to denote a Windows system, not it’s “arch” type.)

Finally, I add the following in the generated eslintrc.js file:

‘brace-style’: [2, ‘stroustrup’, { ‘allowSingleLine’: false }],

Only because this is the style I like to use.

Task #2: Files and Folders

This utility function walkFolders is a JavaScript Generator function.

Generator functions provide a powerful alternative [to custom iterators]: they allow you to define an iterative algorithm by writing a single function whose execution is not continuous. Generator functions are written using the function* syntax. When called initially, generator functions do not execute any of their code, instead returning a type of iterator called a Generator. When a value is consumed by calling the generator’s next method, the Generator function executes until it encounters the yield keyword.

Now that I have a function that will give us what I want, I can call it like this:

The first function is responsible for getting the data and filtering it for folders only. The second function gets all content (files and folders). These could have been combined into one function taking a parameter to decide if you wanted all returned or just folders, but depending on the skill-level of the reader, I didn’t want to add too much confusion.

Initially when I wrote this code, I would just console.log() the output. I didn’t have a createNode function yet.

Task #3: QTree

You may have noticed that I am using path.sep which contains the path separator based on the current operating system. This is very important when making cross-platform software. To get access to this, you will need to const path = require(‘path’) in the script area of your code.

Task #4: Get Windows Drives

I did try a couple of packages, that worked, but then when I went to compile my project for Linux, it was broken. The packages themselves were restricted to Windows builds and I needed a way of saying it was optional. I tried the npm route of optionalPackages in the package.json but it did not work for me at all. Really, I could not be bothered trying to figure that out, so I decided to roll my own. I would only ever call it if the application was being run under Windows anyway.

Thus, my getWindowsDrives function was created:

And happily, it allows for me to build for Linux and Windows with no problems.

And calling it, works like this:

Note: process.platform is available while in code written for Electron (as it comes from Node) without having to require/import it.

The process.platform property returns a string identifying the operating system platform on which the Node.js process is running.

For possible values, click here.

Task #5: Spoofing QTree Selected and Lazy Load

In order to do this, you would have to write some funky code. Basically, starting at the root node level and working your way to the desired child node. So, almost recursively, but you’d have to wait until QTree called it’s lazy load function on any nodes that had children that weren’t yet loaded.

To do this will take some creative thinking, but I did come up with a solution right away using a combination of the Vue nextTick function and Vue’s global event bus messaging.

Note: Don’t be confused by articles for creating a global event bus in Vue. These are old articles. The latest Vue supports this directly.

In my folderTree component, I want to access the global event bus to send messages to itself. So, when the component has the message available, I want the expandTree function to be called.

Whenever you use the global event bus, make sure you turn it off when the component that it’s it is unloaded. It can have unintended consequences if your component is reloaded multiple times.

The HTML for my QTree looks like this:

and the method to recursively expand the tree looks like this:

You will notice that if you give an HTML component a ref=”name” you will be able to directly access that component using this.$refs[‘name’] or this.$ It just so happens that QTree has some functions that are accessible. getNodeByKey returns the node with the specified key, isExpanded will return true if the node is expanded, and setExpanded will call the lazy load function and expand the node once the data has been received. I will note right now that setExpanded is an “undocumented” function and I had to find it by actually looking in the code for QTree, but I would be unable to have this sort of functionality without it.

The trick in the code above is to get each node, working down towards the node I want. If a node is not expanded, then call the setExpanded button. If I am at the node I want, then I’m done, otherwise emit the expand-tree in the this->$nextTick function. This allows the lazy load to occur before the next iteration of node traversal occurs.

I wouldn’t say this is good behavior. As programmers, we should not use “undocumented” functions. However, as programmers, we have all written something that was used other than the way intended. When you do something like this (using any 3rd-party code), you should write up an issue (or two) so the author can make adjustments. Better yet, if you have the time, create a PR (Pull Request) with the functionality you need.

Task #6: Displaying Images from Files

At one point, I was using the file:// protocol to access images on the file system so they could be displayed instead of a generic icon. Well, the file:// protocol is no longer available in the renderer process. The best way now, would be the same way as if you were using a web page and a web server.

In electron-main.js I have the following code (at the top):

and at the bottom:

Here, you will see I create a web server (express) to feed the image files to the renderer process. Also notice the ipcMain.on code. Each time the User changes to a different folder in the renderer process, I send an IPC (Inter-Process Communications) message to the main process. Then, when a file is requested from the renderer process, it’s all put together in the router.get function.

The code that calls this is in the gridItemImage component in the getImage function. The fragment looks like this:

Now, if you really wanted to be secure, you could have all kinds of things going on here. 1) You could pick a random port number to use and the renderer process could request it from the main process to use in it’s HTTP GET calls, 2) You could generate a unique key (or guid), that the renderer process could request it from the main process to use in it’s HTTP GET calls.

Task #7: Watching Files and Folders

Each time the selected folder changes, I call this function:

I make sure to remove any “watch” on an existing folder and create a new “watch” on the new folder. Whenever something changes, I emit the message “rescan-current-folder”, which is eventually picked up by this function:


Quasar Framework

Build high-performance cross-device VueJS user interfaces…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store