commit
5bdfa455d8
@ -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, |
catch(e) { |
||||||
role: 'about' |
console.log('Unable to read init.json: ', e) |
||||||
}, |
} |
||||||
{ |
// Create the browser window.
|
||||||
type: 'separator' |
const bounds = (data && data.bounds) ? data.bounds : {width: 1280, height:800 } |
||||||
}, |
mainWindow = new BrowserWindow({ |
||||||
{ |
width: bounds.width, |
||||||
label: 'Services', |
height: bounds.height, |
||||||
role: 'services', |
webPreferences: { |
||||||
submenu: [] |
preload: path.join(__dirname, 'electron-globals.js') |
||||||
}, |
|
||||||
{ |
|
||||||
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' |
|
||||||
} |
|
||||||
); |
|
||||||
} |
} |
||||||
|
}) |
||||||
|
|
||||||
// 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) => { |
||||||
catch(e) { |
if(url != mainWindow.webContents.getURL()) { |
||||||
console.log('Unable to read init.json: ', e); |
e.preventDefault() |
||||||
} |
shell.openExternal(url) |
||||||
const bounds = (data && data.bounds) ? data.bounds : {width: 1280, height:800 }; |
} |
||||||
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) { |
mainWindow.webContents.on('will-navigate', handleLink) |
||||||
bwdata.x = data.bounds.x; |
mainWindow.webContents.on('new-window', handleLink) |
||||||
bwdata.y = data.bounds.y; |
|
||||||
} |
// 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() |
||||||
|
}) |
||||||
|
|
||||||
mainWindow = new BrowserWindow(bwdata); |
|
||||||
mainWindow.loadURL('file://' + __dirname + '/electron-start.html'); |
|
||||||
mainWindow.focus(); |
|
||||||
|
|
||||||
// Listen for badge changes
|
// Listen for badge changes
|
||||||
ipcMain.on('badge', function(event, arg) { |
ipcMain.on('badge', function(event, arg) { |
||||||
if (process.platform === "darwin") { |
if (process.platform === "darwin") { |
||||||
app.dock.setBadge(String(arg)); |
app.dock.setBadge(String(arg)) |
||||||
} |
} |
||||||
else if (process.platform === "win32") { |
else if (process.platform === "win32") { |
||||||
let n = parseInt(arg, 10); |
let n = parseInt(arg, 10) |
||||||
// Only show notifications with number
|
// Only show notifications with number
|
||||||
if (isNaN(n)) { |
if (isNaN(n)) { |
||||||
return; |
return |
||||||
} |
} |
||||||
if (n > 0) { |
if (n > 0) { |
||||||
mainWindow.setOverlayIcon(__dirname + '/assets/img/favicon.ico', String(arg)); |
mainWindow.setOverlayIcon(__dirname + '/assets/img/favicon.ico', String(arg)) |
||||||
} else { |
} else { |
||||||
mainWindow.setOverlayIcon(null, ''); |
mainWindow.setOverlayIcon(null, '') |
||||||
} |
} |
||||||
} |
} |
||||||
}); |
}) |
||||||
|
|
||||||
mainWindow.on('devtools-opened', function() { |
// Quit when all windows are closed.
|
||||||
mainWindow.webContents.executeJavaScript("document.getElementById('glowingbear').openDevTools();"); |
app.on('window-all-closed', function () { |
||||||
}); |
// 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() |
||||||
|
}) |
||||||
|
|
||||||
mainWindow.on('close', function() { |
app.on('activate', function () { |
||||||
// Save window bounds to disk
|
// On macOS it's common to re-create a window in the app when the
|
||||||
var data = { |
// dock icon is clicked and there are no other windows open.
|
||||||
bounds: mainWindow.getBounds() |
if (mainWindow === null) createWindow() |
||||||
}; |
}) |
||||||
fs.writeFileSync(initPath, JSON.stringify(data)); |
|
||||||
}); |
|
||||||
|
|
||||||
mainWindow.on('closed', function() { |
// In this file you can include the rest of your app's specific main process
|
||||||
app.quit(); |
// code. You can also put them in separate files and require them here.
|
||||||
}); |
|
||||||
}); |
|
||||||
})(); |
|
||||||
|
@ -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> |
|
Loading…
Reference in new issue