Quasar + Pinia + Electron Desktop App
Recently, I planned to rewrite my “Scrum Daily Standup Picker” Electron application in Vue 3. I wrote the initial release in Angular, but I wanted to refactor the code base and rewrite it in Vue 3.
Why? Because I love Vue and want to have a public showcase that I can reference to potential customers.
Why Quasar?
Quasar is an MIT licensed open-source Vue.js based framework that targets SPA, SSR, PWA, mobile app, desktop app, and browser extension all using one codebase. It handles the build setup and provides a complete collection of Material Design compliant UI components.
Quasar’s motto is:
Write code once and simultaneously deploy it as a website, a Mobile App and/or an Electron App.
Using Quasar drastically saves development time due to these reasons:
It’s based on Vue.js.
It provides many UI components that follow Material Design guidelines.
It has a regular release cycle inclusive of new features.
It provides support for each build mode (SPA, SSR, PWA, Mobile app, Desktop app & Browser Extension).
It has its own CLI that provides a pleasant developer experience. For example, we can build our application as SPA, mobile, or desktop app within the same project folder.
Read more about why Quasar might be a good choice for your next project.
Install Quasar CLI
Let’s start by creating a new project using the Quasar CLI:
We chose SCSS as our CSS preprocessor, ESLint & Typescript as additional features, Vue 3’s Composition API and Prettier for code formatting.
Do not choose Vuex as we will add another state library in the next chapter. If you accidentally added Vuex, remove it manually from your package.json
.
Read the official docs for additional information about the Quasar CLI.
Add Pinia as Vue store library
We’ll use Pinia as Vue store library, which is now the recommended state library for Vue.
First, we need to install Pinia:
To be able to register Pinia at our Vue application instance we need to create a Quasar Boot File:
A common use case for Quasar applications is to run code before the root Vue app instance is instantiated, like injecting and initializing your own dependencies (examples: Vue components, libraries…) or simply configuring some startup code of your app.
Our boot file is called pinia.ts
and is located at src/boot
:
We also need to add this new file to quasar.conf.js
:
Now, we can create a new folder called pinia
in src
.
We cannot name this folder store
as this name is reserved for the official Vuex integration.
A basic store could look like this:
We can use this store in any Vue component:
Now we can run the Vue application using the Quasar CLI:
The Vue application is served at http://localhost:8080
:
Setup Electron
To develop/build a Quasar Electron app, we need to add the Electron mode to our Quasar project:
Every Electron app has two threads: the main thread (deals with the window and initialization code – from the newly created folder /src-electron
) and the renderer thread (which deals with the actual content of your app from /src
).
The new folder has the following structure:
Now we are ready to start our Electron application:
This command will open up an Electron window which will render your app along with Developer Tools opened side by side:
Read the official docs for additional and detailed information about developing Electron apps with Quasar.
Control Electron from Vue code
If we want to use Electron features like opening a file dialog, we need to write some code to be able to access Electron’s API.
For example, if we want to show a dialog to open files, Electron provides the dialog API to display native system dialogs for opening and saving files, alerting, etc.
First, we need to install @electron/remote
:
Then we need to modify src-electron/electron-main.js
and initialize @electron/remote
:
If we want to use Electron API from our Vue code we need to add some code to src-electron/electron-preload.js
:
Next we create src/api/electron-api.ts
to access this code from within our Vue application:
Now we can use this API anywhere in our Vue component:
Clicking on the button should now open the native OS file dialog:
Conclusion
Quasar allows us to quickly develop Electron desktop applications in Vue with high-quality UI components that follow Material Design guidelines.
The most significant advantage against a custom Electron + Vue boilerplate project from GitHub is that Quasar has a regular release cycle and provides upgrade guides for older versions.
Take a look at my “Scrum Daily Standup Picker” GitHub repository to see a more complex “Quasar-Electron-Vue3-Typescript-Pinia” project. The demo source code for the following demo is available at GitHub.
If you liked this article, follow me on Twitter to get notified about new blog posts and more content from me.
Alternatively (or additionally), you can also subscribe to my newsletter.
Last updated