Differences between revisions 2 and 7 (spanning 5 versions)
Revision 2 as of 2021-08-09 21:59:10
Size: 2857
Comment:
Revision 7 as of 2023-01-08 17:15:45
Size: 4022
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## page was renamed from NodeJS/Electron
Line 9: Line 10:
== electron-store ==

A simple data persistence solution for Electron apps, using minimal dependencies. It writes to `config.json` in `app.getPath('userData')`.

If the renderer needs access to the store, try:

{{{
const win = new BrowserWindow({ enableRemoteModule: true })
}}}
== Building ==



=== Electron Builder ===

See [[NodeJS/ElectronBuilder|here]] for details on how to use '''Electron Builder''' in a project.



=== Manual Building ===

See [[https://www.electronjs.org/docs/tutorial/application-distribution#manual-distribution|here]] for instructions on manually building an Electron app, optionally with source code obfuscation.
Line 23: Line 28:
== Menu Bar ==

The menu bar is composed of menus and sub-menus. Design a 'template' (see below) and have it built into a menu, then apply the menu to the application.

{{{
== Usage ==

A basic Election application looks like:

{{{
const { app, BrowserWindow } = require("electron");
const path = require("path");

let win;
function init() {
  win = NewBrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      contextIsolation: true,
      nodeIntegration: false,
      preload: path.join(__dirname, "preload.js")
    }
  });

  win.loadFile(path.join(__dirname, "index.html"));

  win.on("closed", () => {
    win = null;
  });
}

app.on("ready", init);

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  if (win === null) {
    init()
  }
});
}}}



=== Menu Bar ===

The '''menu bar''' is composed of menus and sub-menus. Design a 'template' (see below) and have it built into a menu, then apply the menu to the application.

{{{
const { app, BrowserWindow, menu, shell } = require("electron");
Line 61: Line 112:
=== Platform-Specific Menus === ==== Platform-Specific Menus ====
Line 66: Line 117:
const { app, BrowserWindow, menu } = require("electron");
Line 85: Line 137:
----



== Keyboard Shortcuts ==

=== Built-in Method ===


=== Keyboard Shortcuts ===

==== Built-in Method ====
Line 96: Line 146:
const { app, BrowserWindow, menu } = require("electron");
Line 112: Line 164:
=== electron-localshortcut === ==== electron-localshortcut ====
Line 117: Line 169:
const { app, BrowserWindow } = require("electron");
Line 126: Line 179:
=== Platform-Specific Shortcuts === ==== Platform-Specific Shortcuts ====
Line 133: Line 186:
const { app, BrowserWindow, menu } = require("electron");

Electron


Building

Electron Builder

See here for details on how to use Electron Builder in a project.

Manual Building

See here for instructions on manually building an Electron app, optionally with source code obfuscation.


Usage

A basic Election application looks like:

const { app, BrowserWindow } = require("electron");
const path = require("path");

let win;
function init() {
  win = NewBrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      contextIsolation: true,
      nodeIntegration: false,
      preload: path.join(__dirname, "preload.js")
    }
  });

  win.loadFile(path.join(__dirname, "index.html"));

  win.on("closed", () => {
    win = null;
  });
}

app.on("ready", init);

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  if (win === null) {
    init()
  }
});

The menu bar is composed of menus and sub-menus. Design a 'template' (see below) and have it built into a menu, then apply the menu to the application.

const { app, BrowserWindow, menu, shell } = require("electron");

const template = [
  {
    label: 'File',
    submenu: [
      {
        label: 'Exit',
        click() {
          app.quit()
        }
      }
    ]
  },
  {
    label: 'Help',
    submenu: [
      {
        label: 'Learn More',
        click: async () => {
          await shell.openExternal('https://example.org');
        }
      }
    ]
  }
];

const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);

The above example features two ways to define the corresponding action for a menu item, and it also demonstrates how an async function can be used.

Platform-Specific Menus

Using conditional logic and array unpacking, platform-specific menus can be maintained in the same codebase.

const { app, BrowserWindow, menu } = require("electron");
const isMac = process.platform === 'darwin';

const template = [
  ...(isMac ? [{
    label: app.name,
    submenu: [
      { role: 'about' },
      { role: 'quit' }
    ]
  }] : []),
  {
    label: 'File',
    submenu: [
      isMac ? { role: 'close' } : { role: 'quit' }
    ]
  }
];

Keyboard Shortcuts

Built-in Method

Follow the above example for menu items, but add an accelerator option to the corresponding menu item.

const { app, BrowserWindow, menu } = require("electron");

const template = [
  {
    label: 'File',
    submenu: [
      {
        label: 'Save',
        accelerator: 'CmdOrCtrl+S',
        click: saveFile()
      }
    ]
  }
]

electron-localshortcut

A simpler method is to use the electron-localshortcut package.

const { app, BrowserWindow } = require("electron");
const shortcut = require("electron-shortcut");

shortcut.register(win, "CmdOrCtrl+S", () => {
  saveFile();
});

Platform-Specific Shortcuts

Apople computers famously have a different keyboard. Unfortunately, those end users will expect the Command key to work like the Control key for everyone else.

The helper modifier CmdOrCtrl exists to simplify this need as shown in the above examples. Nonetheless, it is possible to re-invent the wheel.

const { app, BrowserWindow, menu } = require("electron");
const isMac = process.platform === 'darwin';

const template = [
  {
    label: 'File',
    submenu: [
      {
        role: 'Save File',
        accelerator: isMac ? 'Cmd+S' : 'Ctrl+S',
        click: saveFile()
      }
    ]
  }
]


CategoryRicottone

Node/Electron (last edited 2023-03-02 17:38:53 by DominicRicottone)