Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

electron.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. "use strict";
  2. const electron = require("electron");
  3. const core = require("./app.js");
  4. const Log = require("logger");
  5. // Config
  6. let config = process.env.config ? JSON.parse(process.env.config) : {};
  7. // Module to control application life.
  8. const app = electron.app;
  9. // Module to create native browser window.
  10. const BrowserWindow = electron.BrowserWindow;
  11. // Keep a global reference of the window object, if you don't, the window will
  12. // be closed automatically when the JavaScript object is garbage collected.
  13. let mainWindow;
  14. /**
  15. *
  16. */
  17. function createWindow() {
  18. let electronSwitchesDefaults = ["autoplay-policy", "no-user-gesture-required"];
  19. app.commandLine.appendSwitch(...new Set(electronSwitchesDefaults, config.electronSwitches));
  20. let electronOptionsDefaults = {
  21. width: 800,
  22. height: 600,
  23. x: 0,
  24. y: 0,
  25. darkTheme: true,
  26. webPreferences: {
  27. contextIsolation: true,
  28. nodeIntegration: false,
  29. zoomFactor: config.zoom
  30. },
  31. backgroundColor: "#000000"
  32. };
  33. // DEPRECATED: "kioskmode" backwards compatibility, to be removed
  34. // settings these options directly instead provides cleaner interface
  35. if (config.kioskmode) {
  36. electronOptionsDefaults.kiosk = true;
  37. } else {
  38. electronOptionsDefaults.fullscreen = true;
  39. electronOptionsDefaults.autoHideMenuBar = true;
  40. }
  41. const electronOptions = Object.assign({}, electronOptionsDefaults, config.electronOptions);
  42. // Create the browser window.
  43. mainWindow = new BrowserWindow(electronOptions);
  44. // and load the index.html of the app.
  45. // If config.address is not defined or is an empty string (listening on all interfaces), connect to localhost
  46. let prefix;
  47. if (config["tls"] !== null && config["tls"]) {
  48. prefix = "https://";
  49. } else {
  50. prefix = "http://";
  51. }
  52. let address = (config.address === void 0) | (config.address === "") ? (config.address = "localhost") : config.address;
  53. mainWindow.loadURL(`${prefix}${address}:${config.port}`);
  54. // Open the DevTools if run with "npm start dev"
  55. if (process.argv.includes("dev")) {
  56. if (process.env.JEST_WORKER_ID !== undefined) {
  57. // if we are running with jest
  58. const devtools = new BrowserWindow(electronOptions);
  59. mainWindow.webContents.setDevToolsWebContents(devtools.webContents);
  60. }
  61. mainWindow.webContents.openDevTools();
  62. }
  63. // simulate mouse move to hide black cursor on start
  64. mainWindow.webContents.on("dom-ready", (event) => {
  65. mainWindow.webContents.sendInputEvent({ type: "mouseMove", x: 0, y: 0 });
  66. });
  67. // Set responders for window events.
  68. mainWindow.on("closed", function () {
  69. mainWindow = null;
  70. });
  71. if (config.kioskmode) {
  72. mainWindow.on("blur", function () {
  73. mainWindow.focus();
  74. });
  75. mainWindow.on("leave-full-screen", function () {
  76. mainWindow.setFullScreen(true);
  77. });
  78. mainWindow.on("resize", function () {
  79. setTimeout(function () {
  80. mainWindow.reload();
  81. }, 1000);
  82. });
  83. }
  84. }
  85. // This method will be called when Electron has finished
  86. // initialization and is ready to create browser windows.
  87. app.on("ready", function () {
  88. Log.log("Launching application.");
  89. createWindow();
  90. });
  91. // Quit when all windows are closed.
  92. app.on("window-all-closed", function () {
  93. if (process.env.JEST_WORKER_ID !== undefined) {
  94. // if we are running with jest
  95. app.quit();
  96. } else {
  97. createWindow();
  98. }
  99. });
  100. app.on("activate", function () {
  101. // On OS X it's common to re-create a window in the app when the
  102. // dock icon is clicked and there are no other windows open.
  103. if (mainWindow === null) {
  104. createWindow();
  105. }
  106. });
  107. /* This method will be called when SIGINT is received and will call
  108. * each node_helper's stop function if it exists. Added to fix #1056
  109. *
  110. * Note: this is only used if running Electron. Otherwise
  111. * core.stop() is called by process.on("SIGINT"... in `app.js`
  112. */
  113. app.on("before-quit", (event) => {
  114. Log.log("Shutting down server...");
  115. event.preventDefault();
  116. setTimeout(() => {
  117. process.exit(0);
  118. }, 3000); // Force-quit after 3 seconds.
  119. core.stop();
  120. process.exit(0);
  121. });
  122. // Start the core application if server is run on localhost
  123. // This starts all node helpers and starts the webserver.
  124. if (["localhost", "127.0.0.1", "::1", "::ffff:127.0.0.1", undefined].includes(config.address)) {
  125. core.start(function (c) {
  126. config = c;
  127. });
  128. }