2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-11-11 15:51:06 +00:00

Refactor menu arguments into options

This commit is contained in:
Jia Hao 2016-02-25 10:37:06 +08:00
parent 875149bcf7
commit 6faba7da42
2 changed files with 67 additions and 39 deletions

View File

@ -5,7 +5,7 @@ import helpers from './../../helpers/helpers';
import createMenu from './../menu/menu'; import createMenu from './../menu/menu';
import initContextMenu from './../contextMenu/contextMenu'; import initContextMenu from './../contextMenu/contextMenu';
const {BrowserWindow, shell, ipcMain} = electron; const {BrowserWindow, shell, ipcMain, dialog} = electron;
const {isOSX, linkIsInternal} = helpers; const {isOSX, linkIsInternal} = helpers;
const ZOOM_INTERVAL = 0.1; const ZOOM_INTERVAL = 0.1;
@ -55,7 +55,49 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
mainWindow.webContents.send('change-zoom', currentZoom); mainWindow.webContents.send('change-zoom', currentZoom);
}; };
createMenu(options.nativefierVersion, onAppQuit, onZoomIn, onZoomOut, mainWindow, options); const clearAppData = () => {
dialog.showMessageBox(mainWindow, {
type: 'warning',
buttons: ['Yes', 'Cancel'],
defaultId: 1,
title: 'Clear cache confirmation',
message: 'This will clear all data (cookies, local storage etc) from this app. Are you sure you wish to proceed?'
}, response => {
if (response === 0) {
const session = mainWindow.webContents.session;
session.clearStorageData(() => {
session.clearCache(() => {
mainWindow.loadURL(options.targetUrl);
});
});
}
});
};
const onGoBack = () => {
mainWindow.webContents.goBack();
};
const onGoForward = () => {
mainWindow.webContents.goForward();
};
const getCurrentUrl = () => {
return mainWindow.webContents.getURL();
};
const menuOptions = {
nativefierVersion: options.nativefierVersion,
appQuit: onAppQuit,
zoomIn: onZoomIn,
zoomOut: onZoomOut,
goBack: onGoBack,
goForward: onGoForward,
getCurrentUrl: getCurrentUrl,
clearAppData: clearAppData
};
createMenu(menuOptions);
initContextMenu(mainWindow); initContextMenu(mainWindow);
if (options.userAgent) { if (options.userAgent) {

View File

@ -1,15 +1,16 @@
import {Menu, shell, clipboard, dialog} from 'electron'; import {Menu, shell, clipboard} from 'electron';
/** /**
* * @param nativefierVersion
* @param {string} nativefierVersion * @param appQuit
* @param {function} onQuit should be from app.quit * @param zoomIn
* @param {function} onZoomIn * @param zoomOut
* @param {function} onZoomOut * @param goBack
* @param {{}}} mainWindow * @param goForward
* @param {{}}} options * @param getCurrentUrl
* @param clearAppData
*/ */
function createMenu(nativefierVersion, onQuit, onZoomIn, onZoomOut, mainWindow, options) { function createMenu({nativefierVersion, appQuit, zoomIn, zoomOut, goBack, goForward, getCurrentUrl, clearAppData}) {
if (Menu.getApplicationMenu()) { if (Menu.getApplicationMenu()) {
return; return;
} }
@ -45,7 +46,7 @@ function createMenu(nativefierVersion, onQuit, onZoomIn, onZoomOut, mainWindow,
label: 'Copy Current URL', label: 'Copy Current URL',
accelerator: 'CmdOrCtrl+C', accelerator: 'CmdOrCtrl+C',
click: () => { click: () => {
const currentURL = mainWindow.webContents.getURL(); const currentURL = getCurrentUrl();
clipboard.writeText(currentURL); clipboard.writeText(currentURL);
} }
}, },
@ -58,6 +59,12 @@ function createMenu(nativefierVersion, onQuit, onZoomIn, onZoomOut, mainWindow,
label: 'Select All', label: 'Select All',
accelerator: 'CmdOrCtrl+A', accelerator: 'CmdOrCtrl+A',
role: 'selectall' role: 'selectall'
},
{
label: 'Clear App Data',
click: () => {
clearAppData();
}
} }
] ]
}, },
@ -68,14 +75,14 @@ function createMenu(nativefierVersion, onQuit, onZoomIn, onZoomOut, mainWindow,
label: 'Back', label: 'Back',
accelerator: 'CmdOrCtrl+[', accelerator: 'CmdOrCtrl+[',
click: () => { click: () => {
mainWindow.webContents.goBack(); goBack();
} }
}, },
{ {
label: 'Forward', label: 'Forward',
accelerator: 'CmdOrCtrl+]', accelerator: 'CmdOrCtrl+]',
click: () => { click: () => {
mainWindow.webContents.goForward(); goForward();
} }
}, },
{ {
@ -113,7 +120,7 @@ function createMenu(nativefierVersion, onQuit, onZoomIn, onZoomOut, mainWindow,
return 'Ctrl+='; return 'Ctrl+=';
})(), })(),
click: () => { click: () => {
onZoomIn(); zoomIn();
} }
}, },
{ {
@ -125,32 +132,11 @@ function createMenu(nativefierVersion, onQuit, onZoomIn, onZoomOut, mainWindow,
return 'Ctrl+-'; return 'Ctrl+-';
})(), })(),
click: () => { click: () => {
onZoomOut(); zoomOut();
} }
}, },
{ {
label: 'Clear App Data', label: 'Toggle Developer Tools',
click: () => {
dialog.showMessageBox(mainWindow, {
type: 'warning',
buttons: ['Yes', 'Cancel'],
defaultId: 1,
title: 'Clear cache confirmation',
message: 'This will clear all data (cookies, local storage etc) from this app. Are you sure you wish to proceed?'
}, response => {
if (response === 0) {
mainWindow.webContents.session.clearStorageData({},
() => {
mainWindow.webContents.session.clearCache(() => {
mainWindow.loadURL(options.targetUrl);
});
});
}
});
}
},
{
label: 'Toggle Window Developer Tools',
accelerator: (() => { accelerator: (() => {
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
return 'Alt+Command+I'; return 'Alt+Command+I';
@ -234,7 +220,7 @@ function createMenu(nativefierVersion, onQuit, onZoomIn, onZoomOut, mainWindow,
label: 'Quit', label: 'Quit',
accelerator: 'Command+Q', accelerator: 'Command+Q',
click: () => { click: () => {
onQuit(); appQuit();
} }
} }
] ]