MagicMirror/js/electron.js

130 lines
3.5 KiB
JavaScript
Raw Normal View History

2016-04-05 14:35:11 -04:00
"use strict";
2016-03-27 20:40:07 +02:00
2016-04-05 14:35:11 -04:00
const electron = require("electron");
2020-05-11 07:25:42 +02:00
const core = require("./app.js");
const Log = require("./logger.js");
// Config
var config = process.env.config ? JSON.parse(process.env.config) : {};
2016-03-27 20:40:07 +02:00
// Module to control application life.
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;
// 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;
2016-04-05 14:35:11 -04:00
function createWindow() {
2019-05-13 23:24:59 +02:00
app.commandLine.appendSwitch("autoplay-policy", "no-user-gesture-required");
var electronOptionsDefaults = {
width: 800,
height: 600,
x: 0,
y: 0,
darkTheme: true,
webPreferences: {
nodeIntegration: false,
zoomFactor: config.zoom
},
backgroundColor: "#000000"
};
// DEPRECATED: "kioskmode" backwards compatibility, to be removed
// settings these options directly instead provides cleaner interface
2016-07-27 12:50:59 +02:00
if (config.kioskmode) {
electronOptionsDefaults.kiosk = true;
2016-07-27 12:50:59 +02:00
} else {
electronOptionsDefaults.fullscreen = true;
electronOptionsDefaults.autoHideMenuBar = true;
2016-07-27 12:50:59 +02:00
}
var electronOptions = Object.assign({}, electronOptionsDefaults, config.electronOptions);
// Create the browser window.
mainWindow = new BrowserWindow(electronOptions);
// and load the index.html of the app.
// If config.address is not defined or is an empty string (listening on all interfaces), connect to localhost
2020-03-28 12:37:50 +01:00
var prefix;
if (config["tls"] !== null && config["tls"]) {
2020-04-03 12:37:33 +02:00
prefix = "https://";
2020-03-28 12:37:50 +01:00
} else {
2020-04-03 12:37:33 +02:00
prefix = "http://";
2020-03-28 12:37:50 +01:00
}
var address = (config.address === void 0) | (config.address === "") ? (config.address = "localhost") : config.address;
2020-03-28 12:37:50 +01:00
mainWindow.loadURL(`${prefix}${address}:${config.port}`);
2016-07-30 09:42:31 -04:00
// Open the DevTools if run with "npm start dev"
if (process.argv.includes("dev")) {
2016-07-30 15:58:51 +02:00
mainWindow.webContents.openDevTools();
}
// Set responders for window events.
mainWindow.on("closed", function () {
mainWindow = null;
});
if (config.kioskmode) {
mainWindow.on("blur", function () {
mainWindow.focus();
});
mainWindow.on("leave-full-screen", function () {
mainWindow.setFullScreen(true);
});
mainWindow.on("resize", function () {
setTimeout(function () {
mainWindow.reload();
}, 1000);
});
}
2016-03-27 20:40:07 +02:00
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
2020-05-25 18:57:15 +02:00
app.on("ready", function () {
2020-05-11 07:25:42 +02:00
Log.log("Launching application.");
createWindow();
});
2016-03-27 20:40:07 +02:00
// Quit when all windows are closed.
app.on("window-all-closed", function () {
createWindow();
2016-03-27 20:40:07 +02:00
});
app.on("activate", function () {
// 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) {
createWindow();
}
2016-03-27 20:40:07 +02:00
});
2016-04-08 22:16:22 +02:00
/* This method will be called when SIGINT is received and will call
* each node_helper's stop function if it exists. Added to fix #1056
*
* Note: this is only used if running Electron. Otherwise
* core.stop() is called by process.on("SIGINT"... in `app.js`
*/
app.on("before-quit", (event) => {
2020-05-11 07:25:42 +02:00
Log.log("Shutting down server...");
event.preventDefault();
setTimeout(() => {
process.exit(0);
}, 3000); // Force-quit after 3 seconds.
core.stop();
process.exit(0);
});
// Start the core application if server is run on localhost
2016-04-08 22:16:22 +02:00
// This starts all node helpers and starts the webserver.
if (["localhost", "127.0.0.1", "::1", "::ffff:127.0.0.1", undefined].indexOf(config.address) > -1) {
core.start(function (c) {
config = c;
});
}