Implement window.alert/confirm/close in main process

This commit is contained in:
Kevin Sawicki 2016-12-01 17:34:14 -08:00
parent f894da13b0
commit 635c909aab
2 changed files with 56 additions and 37 deletions

View file

@ -416,3 +416,34 @@ ipcMain.on('ELECTRON_BROWSER_SEND_TO', function (event, sendToAll, webContentsId
contents.send(channel, ...args) contents.send(channel, ...args)
} }
}) })
// Implements window.alert(message, title)
ipcMain.on('ELECTRON_BROWSER_WINDOW_ALERT', function (event, message, title) {
if (message == null) message = ''
if (title == null) title = ''
event.returnValue = electron.dialog.showMessageBox(event.sender.getOwnerBrowserWindow(), {
message: `${message}`,
title: `${title}`,
buttons: ['OK']
})
})
// Implements window.confirm(message, title)
ipcMain.on('ELECTRON_BROWSER_WINDOW_CONFIRM', function (event, message, title) {
if (message == null) message = ''
if (title == null) title = ''
event.returnValue = !electron.dialog.showMessageBox(event.sender.getOwnerBrowserWindow(), {
message: `${message}`,
title: `${title}`,
buttons: ['OK', 'Cancel'],
cancelId: 1
})
})
// Implements window.close()
ipcMain.on('ELECTRON_BROWSER_WINDOW_CLOSE', function (event) {
event.sender.getOwnerBrowserWindow().close()
event.returnValue = null
})

View file

@ -1,24 +1,29 @@
'use strict' 'use strict'
const ipcRenderer = require('electron').ipcRenderer const {ipcRenderer} = require('electron')
const remote = require('electron').remote
const parseFeaturesString = require('../common/parse-features-string') const parseFeaturesString = require('../common/parse-features-string')
const {captureStackTrace} = Error
const {defineProperty} = Object
// Helper function to resolve relative url. // Helper function to resolve relative url.
var a = window.top.document.createElement('a') const a = window.top.document.createElement('a')
var resolveURL = function (url) { const resolveURL = function (url) {
a.href = url a.href = url
return a.href return a.href
} }
// Window object returned by "window.open". // Window object returned by "window.open".
var BrowserWindowProxy = (function () { const BrowserWindowProxy = (function () {
BrowserWindowProxy.proxies = {} BrowserWindowProxy.proxies = {}
BrowserWindowProxy.getOrCreate = function (guestId) { BrowserWindowProxy.getOrCreate = function (guestId) {
var base = this.proxies let proxy = this.proxies[guestId]
base[guestId] != null ? base[guestId] : base[guestId] = new BrowserWindowProxy(guestId) if (proxy == null) {
return base[guestId] proxy = new BrowserWindowProxy(guestId)
this.proxies[guestId] = proxy
}
return proxy
} }
BrowserWindowProxy.remove = function (guestId) { BrowserWindowProxy.remove = function (guestId) {
@ -26,7 +31,7 @@ var BrowserWindowProxy = (function () {
} }
function BrowserWindowProxy (guestId1) { function BrowserWindowProxy (guestId1) {
Object.defineProperty(this, 'guestId', { defineProperty(this, 'guestId', {
configurable: false, configurable: false,
enumerable: true, enumerable: true,
writeable: false, writeable: false,
@ -56,7 +61,7 @@ var BrowserWindowProxy = (function () {
ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', this.guestId, 'print') ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', this.guestId, 'print')
} }
Object.defineProperty(BrowserWindowProxy.prototype, 'location', { defineProperty(BrowserWindowProxy.prototype, 'location', {
get: function () { get: function () {
return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', this.guestId, 'getURL') return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', this.guestId, 'getURL')
}, },
@ -83,13 +88,13 @@ var BrowserWindowProxy = (function () {
if (process.guestInstanceId == null) { if (process.guestInstanceId == null) {
// Override default window.close. // Override default window.close.
window.close = function () { window.close = function () {
return remote.getCurrentWindow().close() ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_CLOSE')
} }
} }
// Make the browser window or guest view emit "new-window" event. // Make the browser window or guest view emit "new-window" event.
window.open = function (url, frameName, features) { window.open = function (url, frameName, features) {
var guestId, j, len1, name, options, additionalFeatures let guestId, j, len1, name, options, additionalFeatures
if (frameName == null) { if (frameName == null) {
frameName = '' frameName = ''
} }
@ -160,29 +165,12 @@ window.open = function (url, frameName, features) {
} }
} }
// Use the dialog API to implement alert(). window.alert = function (message, title) {
window.alert = function (message = '', title = '') { ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_ALERT', message, title)
remote.dialog.showMessageBox(remote.getCurrentWindow(), {
message: String(message),
title: String(title),
buttons: ['OK']
})
} }
// And the confirm().
window.confirm = function (message, title) { window.confirm = function (message, title) {
var buttons, cancelId return ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_CONFIRM', message, title)
if (title == null) {
title = ''
}
buttons = ['OK', 'Cancel']
cancelId = 1
return !remote.dialog.showMessageBox(remote.getCurrentWindow(), {
message: message,
title: title,
buttons: buttons,
cancelId: cancelId
})
} }
// But we do not support prompt(). // But we do not support prompt().
@ -206,11 +194,11 @@ ipcRenderer.on('ELECTRON_GUEST_WINDOW_POSTMESSAGE', function (event, sourceId, m
}) })
// Forward history operations to browser. // Forward history operations to browser.
var sendHistoryOperation = function (...args) { const sendHistoryOperation = function (...args) {
ipcRenderer.send('ELECTRON_NAVIGATION_CONTROLLER', ...args) ipcRenderer.send('ELECTRON_NAVIGATION_CONTROLLER', ...args)
} }
var getHistoryOperation = function (...args) { const getHistoryOperation = function (...args) {
return ipcRenderer.sendSync('ELECTRON_SYNC_NAVIGATION_CONTROLLER', ...args) return ipcRenderer.sendSync('ELECTRON_SYNC_NAVIGATION_CONTROLLER', ...args)
} }
@ -226,7 +214,7 @@ window.history.go = function (offset) {
sendHistoryOperation('goToOffset', offset) sendHistoryOperation('goToOffset', offset)
} }
Object.defineProperty(window.history, 'length', { defineProperty(window.history, 'length', {
get: function () { get: function () {
return getHistoryOperation('length') return getHistoryOperation('length')
} }
@ -244,13 +232,13 @@ ipcRenderer.on('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', function (event, vi
}) })
// Make document.hidden and document.visibilityState return the correct value. // Make document.hidden and document.visibilityState return the correct value.
Object.defineProperty(document, 'hidden', { defineProperty(document, 'hidden', {
get: function () { get: function () {
return cachedVisibilityState !== 'visible' return cachedVisibilityState !== 'visible'
} }
}) })
Object.defineProperty(document, 'visibilityState', { defineProperty(document, 'visibilityState', {
get: function () { get: function () {
return cachedVisibilityState return cachedVisibilityState
} }