some tips by Kilian Valkhof
In my spare time, I like to make tools for other people, mostly developers.
Just pay attention to the details :)
Doesn't work
...without adding an App Menu that has them.
if (process.platform === 'darwin') {
var template = [{
label: 'FromScratch',
submenu: [{
label: 'Quit',
accelerator: 'CmdOrCtrl+Q',
click: function() { app.quit(); }
}]
}, {
label: 'Edit',
submenu: [{
label: 'Undo',
accelerator: 'CmdOrCtrl+Z',
selector: 'undo:'
}, {
label: 'Redo',
accelerator: 'Shift+CmdOrCtrl+Z',
selector: 'redo:'
}, {
type: 'separator'
}, {
label: 'Cut',
accelerator: 'CmdOrCtrl+X',
selector: 'cut:'
}, {
label: 'Copy',
accelerator: 'CmdOrCtrl+C',
selector: 'copy:'
}, {
label: 'Paste',
accelerator: 'CmdOrCtrl+V',
selector: 'paste:'
}, {
label: 'Select All',
accelerator: 'CmdOrCtrl+A',
selector: 'selectAll:'
}]
}];
var osxMenu = menu.buildFromTemplate(template);
menu.setApplicationMenu(osxMenu);
}
...just copy and paste this into your main.js
Or your app will look like this on Ubuntu:
Many apps get this wrong.
mainWindow = new BrowserWindow({
title: 'ElectronApp',
icon: __dirname + '/app/assets/img/icon.png',
};
This also gives you a nice little icon in the topleft of your windows app chrome.
So prevent selection to make your app feel more native:
.my-ui-text {
-webkit-user-select:none;
}
Ctrl+A selects all selectable text in your app.
Windows: .ICO
OS X: .ICNS
Linux: .PNG
Linux, check!
to get an .ico, use icotools:
icotool -c icon.png > icon.ico
to get an .icns, use png2icns:
png2icns icon.icns icon.png
Or use a GUI tool such as img2icns (mac) or iconverticons (web, Windows, OS X)
$ electron-packager . MyApp --icon=img/icon --platform=all --arch=all --version=0.36.0 --out=../dist/ --asar
Hide your app until your page has loaded:
var mainWindow = new BrowserWindow({
title: 'ElectronApp',
show: false,
};
mainWindow.webContents.on('did-finish-load', function() {
mainWindow.show();
mainWindow.focus();
});
There is a special circle of hell for apps that don't do this.
electron-window-state and electron-window-state-manager
Both work, have good documentation and take care of edge cases.
var JSONStorage = require('node-localstorage').JSONStorage;
var storageLocation = process.env[(process.platform === 'win32') ?
'USERPROFILE' : 'HOME'] + '/.fromscratch';
global.nodeStorage = new JSONStorage(storageLocation);
var windowState = {};
try {
windowState = global.nodeStorage.getItem('windowstate');
} catch (err) {
// the file is there, but corrupt. Handle appropriately.
}
var mainWindow = new BrowserWindow({
title: 'ElectronApp',
x: windowState.bounds && windowState.bounds.x || undefined,
y: windowState.bounds && windowState.bounds.y || undefined,
width: windowState.bounds && windowState.bounds.width || 550,
height: windowState.bounds && windowState.bounds.height || 450,
});
// Restore maximised state if it is set.
// not possible via options so we do it here
if (windowState.isMaximized) {
mainWindow.maximize();
}
['resize', 'move', 'close'].forEach(function(e) {
mainWindow.on(e, function() {
storeWindowState();
});
});
var storeWindowState = function() {
windowState.isMaximized = mainWindow.isMaximized();
if (!windowState.isMaximized) {
// only update bounds if the window isn't currently maximized
windowState.bounds = mainWindow.getBounds();
}
global.nodeStorage.setItem('windowstate', windowState);
};
For Accelerators (shortcuts) don't use Cmd
or Ctrl
, just use CmdOrCtrl
.
Want to use the system font San Francisco?
body {
font: caption;
}
For an extra native look and feel, use system colors.
Check out Grid Stylesheets for a contraint based layout system (like GTK, Qt or Autolayout)
...Though you can get very far by using calc() in your css.
I would love to get feedback, you can find me here:
[email protected],
https://kilianvalkhof.com
/kilianvalkhof,
/kilian
Slides will end up somewhere on my blog or on Twitter. Or both!