Merge pull request #71 from jgraph/electron

managing windows in main thread

Former-commit-id: ce34b70605
This commit is contained in:
David Benson 2017-02-17 16:51:03 +00:00 committed by GitHub
commit c8ea839e47
2 changed files with 92 additions and 51 deletions

View file

@ -1,61 +1,107 @@
const electron = require('electron');
const remote = electron.remote;
const dialog = electron.dialog;
var fs = require('fs');
var path = require('path');
const fs = require('fs')
const path = require('path')
const electron = require('electron')
const ipcMain = electron.ipcMain
const dialog = electron.dialog
const app = electron.app
const BrowserWindow = electron.BrowserWindow
// Module to control application life.
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;
let windowsRegistry = []
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function createWindow (opt = {}) {
let options = Object.assign({
width: 1600,
height: 1200,
'web-security': false,
}, opt)
function createWindow()
{
// Create the browser window.
mainWindow = new BrowserWindow({width: 1600, height: 1200, "web-security" : false});
let mainWindow = new BrowserWindow(options)
windowsRegistry.push(mainWindow)
console.log('createWindow', opt)
// and load the index.html of the app.
mainWindow.loadURL(`file://${__dirname}/index.html?dev=1&test=1&db=0&gapi=0&od=0&analytics=0&picker=0&mode=device&browser=0&p=electron`);
mainWindow.loadURL(
`file://${__dirname}/index.html?dev=1&test=1&db=0&gapi=0&od=0&analytics=0&picker=0&mode=device&browser=0&p=electron`)
// Open the DevTools.
mainWindow.webContents.openDevTools();
mainWindow.webContents.openDevTools()
mainWindow.on('close', (event/*:WindowEvent*/) => {
const win = event.sender
const index = windowsRegistry.indexOf(win)
console.log('Window on close idx:%d', index)
const contents = win.webContents
if (contents != null) {
contents.executeJavaScript(`global.__emt_isModified()`, true,
isModified => {
console.log('__emt_isModified', isModified)
if (isModified) {
var choice = dialog.showMessageBox(
win,
{
type: 'question',
buttons: ['Yes', 'No'],
title: 'Confirm',
message: 'All Changes will be lost' //mxResources.get('allChangesLost')
})
if (choice === 0) {
win.destroy()
}
} else {
win.destroy()
}
})
event.preventDefault()
}
})
// Emitted when the window is closed.
mainWindow.on('closed', function()
{
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
mainWindow.on('closed', (event/*:WindowEvent*/) => {
const index = windowsRegistry.indexOf(event.sender)
console.log('Window closed idx:%d', index)
windowsRegistry.splice(index, 1)
})
return mainWindow.id
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
app.on('ready', e => {
//asynchronous
ipcMain.on('asynchronous-message', (event, arg) => {
console.log(arg) // prints "ping"
event.sender.send('asynchronous-reply', 'pong')
})
//synchronous
ipcMain.on('winman', (event, arg) => {
console.log('ipcMain.on winman', arg)
if (arg.action === 'newfile') {
event.returnValue = createWindow(arg.opt)
return
}
event.returnValue = 'pong'
})
createWindow()
})
// Quit when all windows are closed.
app.on('window-all-closed', function()
{
app.on('window-all-closed', function () {
console.log('window-all-closed', windowsRegistry.length)
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin')
{
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function()
{
app.on('activate', function () {
console.log('app on activate', windowsRegistry.length)
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null)
{
if (windowsRegistry.length === 0) {
createWindow()
}
})

View file

@ -62,6 +62,15 @@ FeedbackDialog.feedbackUrl = 'https://log.draw.io/email';
var editorUi = this;
var graph = this.editor.graph;
global.__emt_isModified = e => {
if (this.getCurrentFile())
return this.getCurrentFile().isModified()
return false
}
// global.__emt_getCurrentFile = e => {
// return this.getCurrentFile()
// }
// Adds support for libraries
this.actions.addAction('newLibrary...', mxUtils.bind(this, function()
{
@ -167,24 +176,10 @@ FeedbackDialog.feedbackUrl = 'https://log.draw.io/email';
{
oldNew();
}
else
{
const electron = require('electron');
const remote = electron.remote;
const BrowserWindow = remote.BrowserWindow;
mainWindow = new BrowserWindow({width: 1600, height: 1200, "web-security" : false});
else {
const ipc = require('electron').ipcRenderer
ipc.sendSync('winman', {action: 'newfile', opt: {width: 1600}})
// and load the index.html of the app.
mainWindow.loadURL(`file://${__dirname}/index.html?dev=1&test=1&db=0&gapi=0&od=0&analytics=0&picker=0&mode=device&browser=0&p=electron`);
// Emitted when the window is closed.
mainWindow.on('closed', function()
{
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
}
}), null, null, 'Ctrl+N');