2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-09-29 12:49:06 +00:00
nativefier/app/main.js

96 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-07-05 06:08:13 +00:00
/**
* Created by JiaHao on 4/7/15.
*/
var fs = require('fs');
var os = require('os');
var electron = require('electron');
var app = electron.app;
var BrowserWindow = electron.BrowserWindow;
var ipc = electron.ipcMain;
2015-07-05 06:08:13 +00:00
var buildMenu = require('./buildMenu');
const APP_ARGS_FILE_PATH = __dirname + '/nativefier.json';
2015-07-05 06:08:13 +00:00
var mainWindow = null;
var appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8'));
app.on('window-all-closed', function () {
if (!isOSX()) {
2015-07-05 06:08:13 +00:00
app.quit();
}
});
app.on('activate', function (event, hasVisibleWindows) {
if (isOSX()) {
// this is called when the dock is clicked
if (!hasVisibleWindows) {
mainWindow.show();
}
}
});
app.on('before-quit', () => {
// not fired when the close button on the window is clicked
if (isOSX()) {
// need to force a quit as a workaround here to simulate the osx app hiding behaviour
// Somehow sokution at https://github.com/atom/electron/issues/444#issuecomment-76492576 does not work,
// e.prevent default appears to persist
// might cause issues in the future as before-quit and will-quit events are not called
app.exit(0);
}
});
app.on('ready', function () {
2015-07-05 06:08:13 +00:00
mainWindow = new BrowserWindow(
{
width: appArgs.width || 1280,
height: appArgs.height || 800,
2015-07-05 06:08:13 +00:00
'web-preferences': {
javascript: true,
plugins: true,
}
}
);
2016-01-19 12:28:58 +00:00
buildMenu(app, mainWindow);
mainWindow.loadURL('file://' + __dirname + '/index.html');
2015-07-05 06:08:13 +00:00
mainWindow.webContents.on('did-finish-load', function () {
mainWindow.webContents.send('params', JSON.stringify(appArgs));
2015-07-05 06:08:13 +00:00
});
// if the window is focused, clear the badge
mainWindow.on('focus', function () {
if (isOSX()) {
app.dock.setBadge('');
}
});
2015-07-05 06:08:13 +00:00
mainWindow.on('close', (e) => {
if (isOSX()) {
// this is called when exiting from clicking the cross button on the window
e.preventDefault();
mainWindow.hide();
}
});
});
2015-07-05 06:08:13 +00:00
// listen for a notification message
ipc.on('notification-message', function (event, arg) {
if (arg === 'TITLE_CHANGED') {
if (isOSX() && !mainWindow.isFocused()) {
app.dock.setBadge('●');
}
}
2015-07-06 03:46:08 +00:00
});
function isOSX() {
return os.platform() === 'darwin';
}