Implement window.alert/confirm/close in main process

This commit is contained in:
Kevin Sawicki 2016-12-01 17:34:14 -08:00
Родитель f894da13b0
Коммит 635c909aab
2 изменённых файлов: 56 добавлений и 37 удалений

Просмотреть файл

@ -416,3 +416,34 @@ ipcMain.on('ELECTRON_BROWSER_SEND_TO', function (event, sendToAll, webContentsId
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
})

Просмотреть файл

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