CarBar / ElectronWIIGF

Electron demonstration on initializing a project.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Electron What Is It Good For

Prerequisites

  1. Node.js is installed

Initialize the Project

  1. Create a folder for your project.
  2. Run npm init -y
    • The -y inits the package.json file with defaults rather than the wizard.
  3. Create a src folder and a main.js inside of it.
  4. Edit package.json
    • Change the main property to be src/main.js
    • Add start to the scripts property with a value of electron .
  5. Install the electron-prebuilt package to our dev dependencies by running npm install electron --save-dev
  6. Create a folder src and a main.js file in it.
  7. Add the following code to main.js
    const electron = require('electron');
    const app = electron.app;
    
    app.on('ready', _ => {
        console.log('I\'m ready for anything!');
    });
  8. Now make sure it works by running npm start.
  9. Verify that the app starts and prints I'm ready for anything!

Add Window

  1. Open src\main.js and edit it as follows:
    const electron = require('electron');
    const { app, BrowserWindow } = electron;
    
    let mainWindow = null;
    
    app.on('ready', _ => {
        // Initialize our main browser window.
        mainWindow = new BrowserWindow({});
    
        // Load your root html file into the browser window.
        mainWindow.loadURL(`file://${__dirname}/wiigf.html`);
    
        // Cleanup after mainWindow is closed.
        mainWindow.on('closed', _ => {
            mainWindow = null;
        });
    });
  2. Create src\wiigf.html and add the desired html.
  3. Create src\wiigf.css and add the desired styles.

Add Tray

  1. Add Menu and Tray to the list of constants from electron.
  2. Add path dependency using require const path = require('path');
  3. Place a .png to be used as the icon in the tray in your src folder.
  4. Instantiate with const tray = new Tray(path.join('src', '<yourIconName>.png'));
  5. Use Menu.buildFromTemplate to build menu options.
  6. Add menu to the tray using tray.setContextMenu(contextMenu);
  7. Add tool tip to tray tray.setTooltip('<something whitty>');

Building the menu into a function could look something like this.

function buildTray()
{
    const tray = new Tray(path.join('src', 'SingleCupLogo.png'));
    const contextMenu = Menu.buildFromTemplate([
        {
            label: 'Thing 1',
            click: _ => {
                console.log('Clicked Thing 1');
            }
        },
        {
            label: 'Thing 2',
            click: _ => {
                console.log('Clicked Thing 2');
            }
        }
    ])
    tray.setContextMenu(contextMenu);
    tray.setToolTip('Electron WIIGF');
}

Gobal Shorcuts

Sometimes it would be nice to have some global shortcuts to access your program even when your application is not in the foreground.

  1. Add globalShortcut to the list of constants from electron.
  2. Write a little function to call with out shortcut as follows:
    function getFocus() {
        console.log('Doing Show Main Window');
        mainWindow.show();
    }
  3. Register the shortcut as follows: I built a function to handle this with the signature function registerShortcuts(globalShortcut). Then I called it from inside the app.on('ready' handler.
    globalShortcut.unregisterAll();
    globalShortcut.register('CmdOrCtrl+Alt+C', _ => {
        getFocus();
    });
  4. Make sure to follow best practices and cleanup when the application quits.
    app.on('will-quit', _ => {
        globalShortcut.unregisterAll();
    });

Things To Know

Great way to convert images into icons. https://iconverticons.com/online/

About

Electron demonstration on initializing a project.


Languages

Language:JavaScript 78.3%Language:HTML 15.4%Language:CSS 6.3%