|
1 | 1 | const electron = require("electron"), |
2 | | - app = electron.app, |
3 | | - BrowserWindow = electron.BrowserWindow; |
| 2 | +app = electron.app, |
| 3 | +BrowserWindow = electron.BrowserWindow; |
4 | 4 |
|
5 | | -require('electron-debug')({ enabled: true}); |
| 5 | +require('electron-debug')({ |
| 6 | + enabled: true |
| 7 | +}); |
6 | 8 | let mainWindow; |
| 9 | +const fs = require('fs'); |
| 10 | +let url = `${__dirname}/dist/index.html`; |
| 11 | +const globalShortcut = electron.globalShortcut; |
7 | 12 |
|
8 | 13 | function createWindow() { |
9 | | - mainWindow = new BrowserWindow({ |
10 | | - autoHideMenuBar: true, |
11 | | - width: 640, |
12 | | - height: 480, |
13 | | - webPreferences: { |
14 | | - nodeIntegration: false } |
15 | | - }); |
16 | | - mainWindow.loadURL(`file://${__dirname}/dist/index.html`); |
17 | | - //mainWindow.webContents.openDevTools(); |
18 | | - mainWindow.on("closed", function() { |
19 | | - mainWindow = null; |
20 | | - }); |
| 14 | + mainWindow = new BrowserWindow({ |
| 15 | + autoHideMenuBar: true, |
| 16 | + width: 640, |
| 17 | + height: 480, |
| 18 | + webPreferences: { |
| 19 | + nodeIntegration: false |
| 20 | + } |
| 21 | + }); |
| 22 | + mainWindow.loadURL(url); |
| 23 | + //mainWindow.webContents.openDevTools(); |
| 24 | + mainWindow.on("closed", function () { |
| 25 | + mainWindow = null; |
| 26 | + }); |
| 27 | + |
| 28 | + globalShortcut.register('f5', function () { |
| 29 | + reloadPage(); |
| 30 | + }); |
| 31 | + globalShortcut.register('CommandOrControl+R', function () { |
| 32 | + reloadPage(); |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +function reloadPage() { |
| 37 | + var currentURL = mainWindow.webContents.getURL(); |
| 38 | + let urlWithoutFileProtocol = currentURL.split('file:///'); |
| 39 | + currentURL = urlWithoutFileProtocol[1]; |
| 40 | + try { |
| 41 | + if (fs.statSync(currentURL).isFile()) { |
| 42 | + mainWindow.reload(); |
| 43 | + } else { |
| 44 | + mainWindow.loadURL(url); |
| 45 | + } |
| 46 | + } catch (err) { |
| 47 | + mainWindow.loadURL(url); |
| 48 | + } |
21 | 49 | } |
22 | 50 |
|
23 | 51 | app.on("ready", createWindow); |
24 | | -app.on("browser-window-created", function(e, window) { |
25 | | - window.setMenu(null); |
| 52 | +app.on("browser-window-created", function (e, window) { |
| 53 | + window.setMenu(null); |
26 | 54 | }); |
27 | 55 |
|
28 | | -app.on("window-all-closed", function() { |
29 | | - if (process.platform !== "darwin") { |
30 | | - app.quit(); |
31 | | - } |
| 56 | +app.on("window-all-closed", function () { |
| 57 | + if (process.platform !== "darwin") { |
| 58 | + app.quit(); |
| 59 | + } |
32 | 60 | }); |
33 | 61 |
|
34 | | -app.on("activate", function() { |
35 | | - if (mainWindow === null) { |
36 | | - createWindow(); |
37 | | - } |
| 62 | +app.on("activate", function () { |
| 63 | + if (mainWindow === null) { |
| 64 | + createWindow(); |
| 65 | + } |
38 | 66 | }); |
0 commit comments