Easy, minimalist framework for Electron applications
MIT License
Easy, minimalist framework for Electron applications.
const electron = require('electron');
const sweet = require('sweet-electron')(electron);
sweet().url(__dirname, 'index.html').run();
Install sweet-electron: yarn add sweet-electron
.
Copy/paste this code. Run it using electron
, such as: $ electron script.js
.
sweet-electron requires electron to be passed as an argument when importing it. That makes it more like a wrapper. sweet()
returns an instance of SweetElectron
.
SweetElectron
url(String|Array[string]|Function)
: wrapper around loadURL
.url((is) => is.dev() ? 'index_dev.html' : 'index.html')
menu(electron.Menu|Array[Object]|Function)
electron-is
instance, the main window instance and a reference on electron.app. Should return an Array[Object] as it will be passed to electron.Menu.buildFromTemplate
window(Object|Function)
: additional options to be passed during the main window creation
electron-is
instance as its first parameterready(Function)
: function called after Electron's ready
event is fired. Requires a function, which will get electron-is
and mainWindow
parameters.rendererEvents(Object{channel:callback})
: will loop over each channel and register its associated callback. Each callback will get a reference to the current main window as its last parameter (e.g (event, payload, mainWindow) => { ... }
). Wrapper around ipcMain. Should be combined using ipcRenderer on client-sideshortcuts(Object{command:callback}|Function)
:
on(String, Function)
: can listen to all of the available events on electron.app
. The last argument is a reference to the current main window (e.g mainWindow => { ... }
).run()
const electron = require('electron');
const sweet = require('sweet-electron')(electron);
/**
* All the code has been folded inside `sweet-electron` methods for the sake of
* the example.
* For bigger applications, you can surely split your code into different files
* which will help you to keep an easy-to-maintain project.
*/
sweet()
.url(is => [__dirname, is.dev() ? 'index_dev.html' : 'index.html'])
.window({ height: 800, transparent: true })
.menu(app => [{ label: app.getName() }, { type: 'separator' }, { role: 'quit' }])
.rendererEvents({
setOpacity: (event, opacity, mainWindow) => mainWindow.setOpacity(opacity),
})
.run();