Merge pull request #1074 from torhve/electron-712
Update to latest electron
This commit is contained in:
commit
5bdfa455d8
354
electron-main.js
Executable file → Normal file
354
electron-main.js
Executable file → Normal file
@ -1,258 +1,122 @@
|
|||||||
(function() {
|
// Modules to control application life and create native browser window
|
||||||
'use strict';
|
const {app, BrowserWindow, shell, ipcMain} = require('electron')
|
||||||
const electron = require('electron');
|
const path = require('path')
|
||||||
const app = electron.app; // Module to control application life.
|
const fs = require('fs')
|
||||||
const BrowserWindow = electron.BrowserWindow; // Module to create native browser window.
|
|
||||||
|
|
||||||
const ipcMain = require('electron').ipcMain;
|
// Keep a global reference of the window object, if you don't, the window will
|
||||||
const nativeImage = require('electron').nativeImage;
|
// be closed automatically when the JavaScript object is garbage collected.
|
||||||
const Menu = require('electron').Menu;
|
let mainWindow
|
||||||
// Node fs module
|
|
||||||
const fs = require("fs");
|
|
||||||
var template;
|
|
||||||
|
|
||||||
template = [
|
// We use this to store some tiny amount of preferences specific to electron
|
||||||
{
|
// things like window bounds and location
|
||||||
label: 'Edit',
|
const initPath = "init.json"
|
||||||
submenu: [
|
|
||||||
{
|
|
||||||
label: 'Undo',
|
|
||||||
accelerator: 'CmdOrCtrl+Z',
|
|
||||||
role: 'undo'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Redo',
|
|
||||||
accelerator: 'Shift+CmdOrCtrl+Z',
|
|
||||||
role: 'redo'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: 'separator'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Cut',
|
|
||||||
accelerator: 'CmdOrCtrl+X',
|
|
||||||
role: 'cut'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Copy',
|
|
||||||
accelerator: 'CmdOrCtrl+C',
|
|
||||||
role: 'copy'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Paste',
|
|
||||||
accelerator: 'CmdOrCtrl+V',
|
|
||||||
role: 'paste'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Select All',
|
|
||||||
accelerator: 'CmdOrCtrl+A',
|
|
||||||
role: 'selectall'
|
|
||||||
},
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'View',
|
|
||||||
submenu: [
|
|
||||||
{
|
|
||||||
label: 'Reload',
|
|
||||||
accelerator: 'CmdOrCtrl+R',
|
|
||||||
click: function(item, focusedWindow) {
|
|
||||||
if (focusedWindow)
|
|
||||||
focusedWindow.reload();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Toggle Full Screen',
|
|
||||||
accelerator: (function() {
|
|
||||||
if (process.platform == 'darwin')
|
|
||||||
return 'Ctrl+Command+F';
|
|
||||||
else
|
|
||||||
return 'F11';
|
|
||||||
})(),
|
|
||||||
click: function(item, focusedWindow) {
|
|
||||||
if (focusedWindow)
|
|
||||||
focusedWindow.setFullScreen(!focusedWindow.isFullScreen());
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Electron Developer Tools',
|
|
||||||
accelerator: (function() {
|
|
||||||
if (process.platform == 'darwin')
|
|
||||||
return 'Alt+Command+E';
|
|
||||||
else
|
|
||||||
return 'Ctrl+Shift+E';
|
|
||||||
})(),
|
|
||||||
click: function(item, focusedWindow) {
|
|
||||||
if (focusedWindow)
|
|
||||||
focusedWindow.toggleDevTools();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Web Developer Tools',
|
|
||||||
accelerator: (function() {
|
|
||||||
if (process.platform == 'darwin')
|
|
||||||
return 'Alt+Command+I';
|
|
||||||
else
|
|
||||||
return 'Ctrl+Shift+I';
|
|
||||||
})(),
|
|
||||||
click: function(item, focusedWindow) {
|
|
||||||
if ( focusedWindow ) {
|
|
||||||
focusedWindow.webContents.send( 'openDevTools' );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Window',
|
|
||||||
role: 'window',
|
|
||||||
submenu: [
|
|
||||||
{
|
|
||||||
label: 'Minimize',
|
|
||||||
accelerator: 'CmdOrCtrl+M',
|
|
||||||
role: 'minimize'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Close',
|
|
||||||
accelerator: 'CmdOrCtrl+Q',
|
|
||||||
role: 'close'
|
|
||||||
},
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Help',
|
|
||||||
role: 'help',
|
|
||||||
submenu: [
|
|
||||||
{
|
|
||||||
label: 'Learn More',
|
|
||||||
click: function() { require('electron').shell.openExternal('https://github.com/glowing-bear/glowing-bear'); }
|
|
||||||
},
|
|
||||||
]
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
if (process.platform == 'darwin') {
|
function createWindow () {
|
||||||
var name = app.getName();
|
let data
|
||||||
template.unshift({
|
// read saved state from file (e.g. window bounds)
|
||||||
label: name,
|
try {
|
||||||
submenu: [
|
data = JSON.parse(fs.readFileSync(initPath, 'utf8'))
|
||||||
{
|
|
||||||
label: 'About ' + name,
|
|
||||||
role: 'about'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: 'separator'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Services',
|
|
||||||
role: 'services',
|
|
||||||
submenu: []
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: 'separator'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Hide ' + name,
|
|
||||||
accelerator: 'Command+H',
|
|
||||||
role: 'hide'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Hide Others',
|
|
||||||
accelerator: 'Command+Alt+H',
|
|
||||||
role: 'hideothers'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Show All',
|
|
||||||
role: 'unhide'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: 'separator'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Quit',
|
|
||||||
accelerator: 'Command+Q',
|
|
||||||
click: function() { app.quit(); }
|
|
||||||
},
|
|
||||||
]
|
|
||||||
});
|
|
||||||
// Window menu.
|
|
||||||
template[3].submenu.push(
|
|
||||||
{
|
|
||||||
type: 'separator'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Bring All to Front',
|
|
||||||
role: 'front'
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
catch(e) {
|
||||||
|
console.log('Unable to read init.json: ', e)
|
||||||
|
}
|
||||||
|
// Create the browser window.
|
||||||
|
const bounds = (data && data.bounds) ? data.bounds : {width: 1280, height:800 }
|
||||||
|
mainWindow = new BrowserWindow({
|
||||||
|
width: bounds.width,
|
||||||
|
height: bounds.height,
|
||||||
|
webPreferences: {
|
||||||
|
preload: path.join(__dirname, 'electron-globals.js')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Keep a global reference of the window object, if you don't, the window will
|
// Remember window position
|
||||||
// be closed automatically when the JavaScript object is garbage collected.
|
if (data && data.bounds.x && data.bounds.y) {
|
||||||
var mainWindow = null;
|
mainWindow.x = data.bounds.x;
|
||||||
|
mainWindow.y = data.bounds.y;
|
||||||
|
}
|
||||||
|
|
||||||
app.on('browser-window-focus', function(e, w) {
|
mainWindow.setMenu(null)
|
||||||
w.webContents.send('browser-window-focus');
|
mainWindow.setMenuBarVisibility(false)
|
||||||
});
|
mainWindow.setAutoHideMenuBar(true)
|
||||||
|
|
||||||
app.on('ready', function() {
|
// and load the index.html of the app.
|
||||||
var menu = Menu.buildFromTemplate(template);
|
mainWindow.loadFile('index.html')
|
||||||
Menu.setApplicationMenu(menu);
|
|
||||||
const initPath = __dirname + "/init.json";
|
|
||||||
var data;
|
|
||||||
|
|
||||||
// read saved state from file (e.g. window bounds)
|
// Open the DevTools.
|
||||||
try {
|
// mainWindow.webContents.openDevTools()
|
||||||
data = JSON.parse(fs.readFileSync(initPath, 'utf8'));
|
|
||||||
|
var handleLink = (e, url) => {
|
||||||
|
if(url != mainWindow.webContents.getURL()) {
|
||||||
|
e.preventDefault()
|
||||||
|
shell.openExternal(url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mainWindow.webContents.on('will-navigate', handleLink)
|
||||||
|
mainWindow.webContents.on('new-window', handleLink)
|
||||||
|
|
||||||
|
// Emitted when the window is closing.
|
||||||
|
mainWindow.on('close', function () {
|
||||||
|
let data = {
|
||||||
|
bounds: mainWindow.getBounds()
|
||||||
|
}
|
||||||
|
fs.writeFileSync(initPath, JSON.stringify(data))
|
||||||
|
})
|
||||||
|
|
||||||
|
// 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
|
||||||
|
})
|
||||||
|
|
||||||
|
app.on('browser-window-focus', function() {
|
||||||
|
setTimeout(function() { mainWindow.webContents.focus() }, 0)
|
||||||
|
setTimeout(function() { mainWindow.webContents.executeJavaScript("document.getElementById(\"sendMessage\").focus()") }, 0)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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', function() {
|
||||||
|
createWindow()
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
// Listen for badge changes
|
||||||
|
ipcMain.on('badge', function(event, arg) {
|
||||||
|
if (process.platform === "darwin") {
|
||||||
|
app.dock.setBadge(String(arg))
|
||||||
|
}
|
||||||
|
else if (process.platform === "win32") {
|
||||||
|
let n = parseInt(arg, 10)
|
||||||
|
// Only show notifications with number
|
||||||
|
if (isNaN(n)) {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
catch(e) {
|
if (n > 0) {
|
||||||
console.log('Unable to read init.json: ', e);
|
mainWindow.setOverlayIcon(__dirname + '/assets/img/favicon.ico', String(arg))
|
||||||
}
|
} else {
|
||||||
const bounds = (data && data.bounds) ? data.bounds : {width: 1280, height:800 };
|
mainWindow.setOverlayIcon(null, '')
|
||||||
var bwdata = {width: bounds.width, height: bounds.height, 'min-width': 1024, 'min-height': 600, 'autoHideMenuBar': true, 'web-security': true, 'java': false, 'accept-first-mouse': true, defaultEncoding: 'UTF-8', 'icon':'file://'+__dirname + '/assets/img/favicon.png'};
|
|
||||||
// Remembe window position
|
|
||||||
if (data && data.bounds.x && data.bounds.y) {
|
|
||||||
bwdata.x = data.bounds.x;
|
|
||||||
bwdata.y = data.bounds.y;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
mainWindow = new BrowserWindow(bwdata);
|
// Quit when all windows are closed.
|
||||||
mainWindow.loadURL('file://' + __dirname + '/electron-start.html');
|
app.on('window-all-closed', function () {
|
||||||
mainWindow.focus();
|
// On macOS it is common for applications and their menu bar
|
||||||
|
// to stay active until the user quits explicitly with Cmd + Q
|
||||||
|
if (process.platform !== 'darwin') app.quit()
|
||||||
|
})
|
||||||
|
|
||||||
// Listen for badge changes
|
app.on('activate', function () {
|
||||||
ipcMain.on('badge', function(event, arg) {
|
// On macOS it's common to re-create a window in the app when the
|
||||||
if (process.platform === "darwin") {
|
// dock icon is clicked and there are no other windows open.
|
||||||
app.dock.setBadge(String(arg));
|
if (mainWindow === null) createWindow()
|
||||||
}
|
})
|
||||||
else if (process.platform === "win32") {
|
|
||||||
let n = parseInt(arg, 10);
|
|
||||||
// Only show notifications with number
|
|
||||||
if (isNaN(n)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (n > 0) {
|
|
||||||
mainWindow.setOverlayIcon(__dirname + '/assets/img/favicon.ico', String(arg));
|
|
||||||
} else {
|
|
||||||
mainWindow.setOverlayIcon(null, '');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
mainWindow.on('devtools-opened', function() {
|
// In this file you can include the rest of your app's specific main process
|
||||||
mainWindow.webContents.executeJavaScript("document.getElementById('glowingbear').openDevTools();");
|
// code. You can also put them in separate files and require them here.
|
||||||
});
|
|
||||||
|
|
||||||
mainWindow.on('close', function() {
|
|
||||||
// Save window bounds to disk
|
|
||||||
var data = {
|
|
||||||
bounds: mainWindow.getBounds()
|
|
||||||
};
|
|
||||||
fs.writeFileSync(initPath, JSON.stringify(data));
|
|
||||||
});
|
|
||||||
|
|
||||||
mainWindow.on('closed', function() {
|
|
||||||
app.quit();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
})();
|
|
||||||
|
@ -1,41 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<script>
|
|
||||||
onload = function() {
|
|
||||||
const ipc= require('electron').ipcRenderer;
|
|
||||||
const remote = require('electron').remote;
|
|
||||||
const nativeImage = require('electron').nativeImage;
|
|
||||||
const shell = require('electron').shell;
|
|
||||||
|
|
||||||
var webview = document.getElementById("glowingbear");
|
|
||||||
|
|
||||||
var handleconsole = function(e) {
|
|
||||||
console.log("webview: " + e.message);
|
|
||||||
}
|
|
||||||
var handlenewwindow = function(e) {
|
|
||||||
shell.openExternal(e.url);
|
|
||||||
}
|
|
||||||
var handletitleset = function(e) {
|
|
||||||
document.title = e.title;
|
|
||||||
}
|
|
||||||
webview.addEventListener("console-message", handleconsole);
|
|
||||||
webview.addEventListener("new-window", handlenewwindow);
|
|
||||||
webview.addEventListener("page-title-set", handletitleset);
|
|
||||||
|
|
||||||
ipc.on('openDevTools', function() {
|
|
||||||
setTimeout(function() { webview.openDevTools(); }, 0 );
|
|
||||||
});
|
|
||||||
|
|
||||||
ipc.on('browser-window-focus', function() {
|
|
||||||
setTimeout(function() { webview.focus(); }, 0);
|
|
||||||
setTimeout(function() { webview.executeJavaScript("document.getElementById(\"sendMessage\").focus();") }, 0);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<webview preload="electron-globals.js" id="glowingbear" src="index.html" style="position:fixed; top:0; left:0; bottom:0; right:0;"></webview>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
@ -18,10 +18,10 @@ uselocal: copylocal
|
|||||||
|
|
||||||
# build the electron app for various platforms
|
# build the electron app for various platforms
|
||||||
build-electron-windows: uselocal
|
build-electron-windows: uselocal
|
||||||
electron-packager ${ELECTRON_COMMON} --platform=win32 --arch=ia32 --electron-version=3.0.6 --icon=assets/img/favicon.ico --asar=true
|
electron-packager ${ELECTRON_COMMON} --platform=win32 --arch=ia32 --electron-version=7.1.2 --icon=assets/img/favicon.ico --asar=true
|
||||||
|
|
||||||
build-electron-darwin: uselocal
|
build-electron-darwin: uselocal
|
||||||
electron-packager ${ELECTRON_COMMON} --platform=darwin --arch=x64 --electron-version=3.0.6 --icon=assets/img/glowing-bear.icns
|
electron-packager ${ELECTRON_COMMON} --platform=darwin --arch=x64 --electron-version=7.1.2 --icon=assets/img/glowing-bear.icns
|
||||||
|
|
||||||
build-electron-linux: uselocal
|
build-electron-linux: uselocal
|
||||||
electron-packager ${ELECTRON_COMMON} --platform=linux --arch=x64 --electron-version=3.0.6 --icon=assets/img/favicon.ico
|
electron-packager ${ELECTRON_COMMON} --platform=linux --arch=x64 --electron-version=7.1.2 --icon=assets/img/favicon.ico
|
||||||
|
Loading…
Reference in New Issue
Block a user