Electron: Create Cross platform desktop app from Scratch with Visual Studio Code





Hi all,

I assume you already know what is Electron and why we are using it.
If you would like to know more on Electron please refer there official web site from here.

Prerequisites 

1. Visual Studio Code.
2. Node.js

I assume you have completed above prerequisites. So then lets try this out with Visual Studio Code.

1. Create a folder (I named it as ElectronTestApp)

2. Open it from VS Code

3. Add a new file and name it as package.json. 

4. Then copy following code to it.

{
    "name": "your-app",
    "productName": "Electron tutorial app",
    "version": "0.1.0",
    "main": "main.js",
    "scripts": {
        "start": "electron ."
    },
    "devDependencies": {
        "electron": "~1.6.2",
        "electron-packager": "^9.0.0"
    }
}

5. Add another new file and name it as main.js

6. Copy following code to it.

const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win

function createWindow () {
  // Create the browser window.
  win = new BrowserWindow({width: 800, height: 600})

  // and load the index.html of the app.
  win.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }))

  // Open the DevTools.
  win.webContents.openDevTools()

  // Emitted when the window is closed.
  win.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (win === null) {
    createWindow()
  }
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.


7. Add another file and name it as index.html.

8. Copy following code to it.


<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    We are using node <script>document.write(process.versions.node)</script>,
    Chrome <script>document.write(process.versions.chrome)</script>,
    and Electron <script>document.write(process.versions.electron)</script>.
  </body>
</html>

9. Open the terminal by View à Integrated Terminal.

10. Type npm install in the terminal and hit enter.
      This will install Electron which we defined in the package.json file

11.  Then you will get bellow result



13. Then type npm start in the terminal and hit enter.

14. Then you will get the bellow screen.



Happy Coding...

Comments

Popular posts from this blog

Deploy Angular 8 app to Azure with Azure DevOps

Apache ActiveMQ 5 with .Net Core 3.1 and C#

Firebase with.Net Core and C#