Implement support for clicking buttons so that users can control where links open as referenced in #73

This commit is contained in:
Jia Hao 2016-01-25 16:49:11 +08:00
parent 949fce5e3c
commit d8fb87ccb5
3 changed files with 28 additions and 4 deletions

View File

@ -10,19 +10,23 @@ function initContextMenu(mainWindow, sendMessage) {
{
label: 'Open in default browser',
click: function() {
if (!targetHref) {
if (targetHref) {
shell.openExternal(targetHref);
return;
}
shell.openExternal(targetHref);
}
},
{
label: 'Open in new window',
click: function() {
if (!targetHref) {
if (targetHref) {
new BrowserWindow().loadURL(targetHref);
return;
}
new BrowserWindow().loadURL(targetHref);
mainWindow.useDefaultWindowBehaviour = true;
mainWindow.webContents.send('contextMenuClosed');
}
}
];

View File

@ -5,6 +5,7 @@ var helpers = require('./../../helpers/helpers');
var createMenu = require('./../menu/menu');
var BrowserWindow = electron.BrowserWindow;
var shell = electron.shell;
const ipcMain = electron.ipcMain;
var isOSX = helpers.isOSX;
var linkIsInternal = helpers.linkIsInternal;
@ -86,6 +87,11 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
}
mainWindow.webContents.on('new-window', function(event, urlToGo) {
if (mainWindow.useDefaultWindowBehaviour) {
mainWindow.useDefaultWindowBehaviour = false;
return;
}
if (linkIsInternal(options.targetUrl, urlToGo)) {
return;
}
@ -111,6 +117,13 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
return mainWindow;
}
ipcMain.on('cancelNewWindowOverride', () => {
const allWindows = BrowserWindow.getAllWindows();
allWindows.forEach(window => {
window.useDefaultWindowBehaviour = false;
});
});
function maybeHideWindow(window, event) {
if (isOSX()) {
// this is called when exiting from clicking the cross button on the window

View File

@ -17,6 +17,13 @@ document.addEventListener('DOMContentLoaded', function(event) {
const targetElement = event.srcElement;
const targetHref = targetElement.href;
if (!targetHref) {
ipc.once('contextMenuClosed', () => {
clickSelector(event.target);
ipc.send('cancelNewWindowOverride');
});
}
ipc.send('contextMenuOpened', targetHref);
}, false);