2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-09-22 17:49:02 +00:00

Fix Window closing behavior

This commit is contained in:
zweicoder 2016-01-30 12:03:40 +08:00
parent 0c9d6312a4
commit a910914aa4

View File

@ -12,7 +12,7 @@ const {isOSX} = helpers;
const APP_ARGS_FILE_PATH = path.join(__dirname, '..', 'nativefier.json'); const APP_ARGS_FILE_PATH = path.join(__dirname, '..', 'nativefier.json');
const appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8')); const appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8'));
const DEFAULT_ICON_PATH = path.join(__dirname, '/icon.png'); const DEFAULT_ICON_PATH = path.join(__dirname, '..', '/icon.png');
const Tray = electron.Tray; const Tray = electron.Tray;
const Menu = electron.Menu; const Menu = electron.Menu;
@ -22,7 +22,7 @@ if (appArgs.insecure) {
app.commandLine.appendSwitch('ignore-certificate-errors'); app.commandLine.appendSwitch('ignore-certificate-errors');
} }
if(!appArgs.icon){ if (!appArgs.icon) {
appArgs.icon = DEFAULT_ICON_PATH; appArgs.icon = DEFAULT_ICON_PATH;
} }
@ -36,7 +36,7 @@ if (isOSX()) {
app.on('window-all-closed', () => { app.on('window-all-closed', () => {
// Need a better place to store user options, unless you intend to dump everything into cli // Need a better place to store user options, unless you intend to dump everything into cli
// determined opts // determined opts
if(appArgs.minimizeToTray){ if (appArgs.minimizeToTray) {
mainWindow.hide(); mainWindow.hide();
return; return;
} }
@ -69,15 +69,31 @@ app.on('before-quit', () => {
let appIcon = null; let appIcon = null;
app.on('ready', () => { app.on('ready', () => {
mainWindow = createMainWindow(appArgs, app.quit, setDockBadge); mainWindow = createMainWindow(appArgs, app.quit, setDockBadge);
mainWindow.on('close', (e)=> {
if (!mainWindow.forceClose && appArgs.minimizeToTray) {
e.preventDefault();
mainWindow.hide();
}
});
appIcon = new Tray(appArgs.icon); appIcon = new Tray(appArgs.icon);
let menu = Menu.buildFromTemplate([ let menu = Menu.buildFromTemplate([
// See https://github.com/atom/electron/blob/master/docs/api/tray.md for why
// there is a shitty option to show
{
label: 'Show',
type: 'normal',
click: function (menuItem) {
console.log(mainWindow);
mainWindow.show();
}
},
{ {
label: 'Minimize to Tray', label: 'Minimize to Tray',
type: 'checkbox', type: 'checkbox',
checked: appArgs.minimizeToTray || true, checked: appArgs.minimizeToTray || true,
click: function (menuItem) { click: function (menuItem) {
appArgs.minimizeToTray = menuItem.checked = !menuItem.checked; appArgs.minimizeToTray = menuItem.checked;
fs.writeFileSync(APP_ARGS_FILE_PATH, JSON.stringify(appArgs)); fs.writeFileSync(APP_ARGS_FILE_PATH, JSON.stringify(appArgs));
} }
}, },
@ -85,6 +101,7 @@ app.on('ready', () => {
label: 'Close', label: 'Close',
type: 'normal', type: 'normal',
click: function (menuItem) { click: function (menuItem) {
mainWindow.forceClose = true;
app.quit(); app.quit();
} }
} }