diff --git a/app/src/components/mainWindow/mainWindow.js b/app/src/components/mainWindow/mainWindow.js index aaa09e8..562eeb4 100644 --- a/app/src/components/mainWindow/mainWindow.js +++ b/app/src/components/mainWindow/mainWindow.js @@ -5,7 +5,7 @@ import helpers from './../../helpers/helpers'; import createMenu from './../menu/menu'; import initContextMenu from './../contextMenu/contextMenu'; -const {BrowserWindow, shell, ipcMain} = electron; +const {BrowserWindow, shell, ipcMain, dialog} = electron; const {isOSX, linkIsInternal} = helpers; const ZOOM_INTERVAL = 0.1; @@ -55,7 +55,49 @@ function createMainWindow(options, onAppQuit, setDockBadge) { 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); if (options.userAgent) { diff --git a/app/src/components/menu/menu.js b/app/src/components/menu/menu.js index 81b8c3e..2302d2a 100644 --- a/app/src/components/menu/menu.js +++ b/app/src/components/menu/menu.js @@ -1,15 +1,16 @@ -import {Menu, shell, clipboard, dialog} from 'electron'; +import {Menu, shell, clipboard} from 'electron'; /** - * - * @param {string} nativefierVersion - * @param {function} onQuit should be from app.quit - * @param {function} onZoomIn - * @param {function} onZoomOut - * @param {{}}} mainWindow - * @param {{}}} options + * @param nativefierVersion + * @param appQuit + * @param zoomIn + * @param zoomOut + * @param goBack + * @param goForward + * @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()) { return; } @@ -45,7 +46,7 @@ function createMenu(nativefierVersion, onQuit, onZoomIn, onZoomOut, mainWindow, label: 'Copy Current URL', accelerator: 'CmdOrCtrl+C', click: () => { - const currentURL = mainWindow.webContents.getURL(); + const currentURL = getCurrentUrl(); clipboard.writeText(currentURL); } }, @@ -58,6 +59,12 @@ function createMenu(nativefierVersion, onQuit, onZoomIn, onZoomOut, mainWindow, label: 'Select All', accelerator: 'CmdOrCtrl+A', role: 'selectall' + }, + { + label: 'Clear App Data', + click: () => { + clearAppData(); + } } ] }, @@ -68,14 +75,14 @@ function createMenu(nativefierVersion, onQuit, onZoomIn, onZoomOut, mainWindow, label: 'Back', accelerator: 'CmdOrCtrl+[', click: () => { - mainWindow.webContents.goBack(); + goBack(); } }, { label: 'Forward', accelerator: 'CmdOrCtrl+]', click: () => { - mainWindow.webContents.goForward(); + goForward(); } }, { @@ -113,7 +120,7 @@ function createMenu(nativefierVersion, onQuit, onZoomIn, onZoomOut, mainWindow, return 'Ctrl+='; })(), click: () => { - onZoomIn(); + zoomIn(); } }, { @@ -125,32 +132,11 @@ function createMenu(nativefierVersion, onQuit, onZoomIn, onZoomOut, mainWindow, return 'Ctrl+-'; })(), click: () => { - onZoomOut(); + zoomOut(); } }, { - label: 'Clear App Data', - 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', + label: 'Toggle Developer Tools', accelerator: (() => { if (process.platform === 'darwin') { return 'Alt+Command+I'; @@ -234,7 +220,7 @@ function createMenu(nativefierVersion, onQuit, onZoomIn, onZoomOut, mainWindow, label: 'Quit', accelerator: 'Command+Q', click: () => { - onQuit(); + appQuit(); } } ]