Forráskód Böngészése

Amélioration main

Loquicom 5 éve
szülő
commit
1d4ca0af56
2 módosított fájl, 72 hozzáadás és 24 törlés
  1. 37 0
      helper/window.js
  2. 35 24
      main.js

+ 37 - 0
helper/window.js

@@ -0,0 +1,37 @@
+const config = require('../config');
+const { BrowserWindow } = require('electron');
+const window = {};
+
+window.new = function (file, width, height) {
+    // Default values
+    width = width ? width : config.window.width;
+    height = height ? height : config.window.height;
+    // New window
+    const win = new BrowserWindow ({
+        width: width,
+        height: height,
+        webPreferences: {
+            nodeIntegration: true
+        }
+    });
+    win.loadFile(file);
+    return win;
+}
+
+window.simple = function (file, width, height) {
+    // Default values
+    width = width ? width : config.window.width;
+    height = height ? height : config.window.height;
+    // New window
+    const win = new BrowserWindow ({
+        width: width,
+        height: height,
+        webPreferences: {
+            nodeIntegration: false
+        }
+    });
+    win.loadFile(file);
+    return win;
+}
+
+module.exports = window;

+ 35 - 24
main.js

@@ -1,37 +1,51 @@
 const { app, BrowserWindow } = require('electron');
+const window = require('./helper/window');
 const path = require('path');
 const { program } = require('commander');
-program.version('0.0.1');
 
+// Handle creating/removing shortcuts on Windows when installing/uninstalling.
+if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
+  app.quit();
+}
+
+/* --- Initialisation --- */
+
+// Global var
+let mainWindow;
+let indexFile = 'src/index.html';
+
+// Add version and option
+program.version('0.0.1');
 program
   .option('-s, --src', 'Load the file from src folder instead of the app folder');
-
 program.parse(process.argv);
 
-const folder = program.src ? 'src' : 'app';
+// Check Option
+const folder = program.src ? './' : './app/';
 
+/* --- Functions --- */
 
-// Handle creating/removing shortcuts on Windows when installing/uninstalling.
-if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
-  app.quit();
+// Main function
+async function main() {
+  createMainWindow(program.src);
 }
 
-const createWindow = () => {
-  // Create the browser window.
-  const mainWindow = new BrowserWindow({
-    width: 1200,
-    height: 720,
+// Create main window function
+async function createMainWindow(simple = false) {
+  if (simple) {
+    mainWindow = window.simple(folder + indexFile);
+  } else {
+    mainWindow = window.new(folder + indexFile);
+  }
+  mainWindow.on('close', () => {
+    mainWindow = null;
   });
+}
 
-  // and load the index.html of the app.
-  mainWindow.loadFile(path.join(__dirname, `/${folder}/index.html`));
-};
-
-// 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);
+/* --- Electron app actions --- */
 
+// This method will be called when Electron is ready
+app.on('ready', main);
 // Quit when all windows are closed.
 app.on('window-all-closed', () => {
   // On OS X it is common for applications and their menu bar
@@ -40,15 +54,12 @@ app.on('window-all-closed', () => {
     app.quit();
   }
 });
-
+// Re-open
 app.on('activate', () => {
   // 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 (BrowserWindow.getAllWindows().length === 0) {
-    createWindow();
+    createMainWindow(program.src);
   }
 });
-
-// In this file you can include the rest of your app's specific main process
-// code. You can also put them in separate files and import them here.