Skip to main content

Getting started with Electron Pt 1.

Electron is a fun and easy way to create desktop application from an mostly web based code. Of course websites aren't the most performance way to create an user-interface ( in terms of technical aspects such as memory, cpu consumption) but it's an extremely powerful experience rich way of doing that.




There are a lot of apps these days that work similarly: slack, visual studio code. These desktop version they are enriched with features that merely a browser cannot fulfill by itself.


1. Checkout the code from github
2. cd directory && npm install && npm start

You should now have a working electron mini browser working. By default it loads a page from the directory itself. You can change this to any type of url, even ones from internet itself. Like below is already enough to load google.com

function createWindow () {
  // Create the browser window.
  mainWindow = new BrowserWindow({width: 800, height: 600, icon: path.join(__dirname, 'favicon.ico'), title: 'Reimburse-It' }) 


mainWindow.loadURL('https://google.com')


 If you want to customize the menu you need to do a little bit more changes. It starts by importing electron.Menu into your namespace. This holds two functions buildFromTemplate and
setApplicationMenu to create and change the menu.


const electron = require('electron')
// Module to control application life.
const app = electron.app
const Menu = electron.Menu 

Now for the actual dirt on actually creating an electron menu see below:

const template = [
  {
    label: 'File',
    submenu: [
      {role: 'minimize'},
      {role: 'close', 'label': 'Quit Application'}
    ]
  },  
  {
    label: 'Edit',
    submenu: [
      {role: 'undo'},
      {role: 'redo'},
      {type: 'separator'},
      {role: 'cut'},
      {role: 'copy'},
      {role: 'paste'},
    ]
  },
  {
    role: 'help',
    submenu: [
      {
        label: 'Learn More',
        click () { require('electron').shell.openExternal('https://google.com') }
      },
      {
        label: 'Articles & Discussion',
        click () { require('electron').shell.openExternal('http://blog.google.com') }
      }        
    ]
  }
]
  
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)

Comments

Popular posts from this blog

How to configure GoDaddy domain to blogspot

1. Sign in to your GoDaddy account. Click on "My Account," and then click on "My Products." Click "DNS" beneath the corresponding domain.   2. Click on "Add Record," and then select "CNAME (Alias)." 3. Type the desired subdomain name in the "Alias" box. In the "Points To" box, type the following: ghs.google.com 5. Click on "Save Zone File." 6. Sign in to your Blogspot account. Click on "Settings." Click on "Basic." 7. Go to the section ‘Publish’, click the link ‘add URL from thirdparty’   then type your custom  subdomain in the "Your Domain" box. 8. Fill out the word verification puzzle, and then click on "Save Settings." Choose whether or not to redirect the primary domain to the subdomain.
Did you know that the std::istream model is pretty cool? In a few lines of code you can create your own streambuf implementation that drives the istream model.