diff options
author | Stefan Malzner <stefan@adlk.io> | 2017-10-13 12:29:40 +0200 |
---|---|---|
committer | Stefan Malzner <stefan@adlk.io> | 2017-10-13 12:29:40 +0200 |
commit | 58cda9cc7fb79ca9df6746de7f9662bc08dc156a (patch) | |
tree | 1211600c2a5d3b5f81c435c6896618111a611720 /src/index.js | |
download | ferdium-app-58cda9cc7fb79ca9df6746de7f9662bc08dc156a.tar.gz ferdium-app-58cda9cc7fb79ca9df6746de7f9662bc08dc156a.tar.zst ferdium-app-58cda9cc7fb79ca9df6746de7f9662bc08dc156a.zip |
initial commit
Diffstat (limited to 'src/index.js')
-rw-r--r-- | src/index.js | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/src/index.js b/src/index.js new file mode 100644 index 000000000..3244c44ad --- /dev/null +++ b/src/index.js | |||
@@ -0,0 +1,147 @@ | |||
1 | import { app, BrowserWindow, shell } from 'electron'; | ||
2 | import fs from 'fs-extra'; | ||
3 | import path from 'path'; | ||
4 | |||
5 | // eslint-disable-next-line | ||
6 | if (require('electron-squirrel-startup')) app.quit(); | ||
7 | |||
8 | import windowStateKeeper from 'electron-window-state'; // eslint-disable-line | ||
9 | |||
10 | import { isDevMode, isWindows } from './environment'; // eslint-disable-line | ||
11 | import ipcApi from './electron/ipc-api'; // eslint-disable-line | ||
12 | import Settings from './electron/Settings'; // eslint-disable-line | ||
13 | import { appId } from './package.json'; // eslint-disable-line | ||
14 | import './electron/exception'; // eslint-disable-line | ||
15 | |||
16 | // Keep a global reference of the window object, if you don't, the window will | ||
17 | // be closed automatically when the JavaScript object is garbage collected. | ||
18 | let mainWindow; | ||
19 | const settings = new Settings(); | ||
20 | let willQuitApp = false; | ||
21 | |||
22 | // Ensure that the recipe directory exists | ||
23 | fs.ensureDir(path.join(app.getPath('userData'), 'recipes')); | ||
24 | |||
25 | // Set App ID for Windows | ||
26 | if (isWindows) { | ||
27 | app.setAppUserModelId(appId); | ||
28 | } | ||
29 | |||
30 | const createWindow = async () => { | ||
31 | // Remember window size | ||
32 | const mainWindowState = windowStateKeeper({ | ||
33 | defaultWidth: 800, | ||
34 | defaultHeight: 600, | ||
35 | }); | ||
36 | |||
37 | // Create the browser window. | ||
38 | mainWindow = new BrowserWindow({ | ||
39 | x: mainWindowState.x, | ||
40 | y: mainWindowState.y, | ||
41 | width: mainWindowState.width, | ||
42 | height: mainWindowState.height, | ||
43 | minWidth: 800, | ||
44 | minHeight: 600, | ||
45 | titleBarStyle: 'hidden', | ||
46 | backgroundColor: '#3498db', | ||
47 | autoHideMenuBar: true, | ||
48 | }); | ||
49 | |||
50 | // Initialize ipcApi | ||
51 | ipcApi({ mainWindow, settings }); | ||
52 | |||
53 | // Manage Window State | ||
54 | mainWindowState.manage(mainWindow); | ||
55 | |||
56 | // and load the index.html of the app. | ||
57 | mainWindow.loadURL(`file://${__dirname}/index.html`); | ||
58 | |||
59 | // Open the DevTools. | ||
60 | if (isDevMode) { | ||
61 | mainWindow.webContents.openDevTools(); | ||
62 | } | ||
63 | |||
64 | // Emitted when the window is closed. | ||
65 | mainWindow.on('close', (e) => { | ||
66 | // Dereference the window object, usually you would store windows | ||
67 | // in an array if your app supports multi windows, this is the time | ||
68 | // when you should delete the corresponding element. | ||
69 | if (!willQuitApp && (settings.get('runInBackground') === undefined || settings.get('runInBackground'))) { | ||
70 | e.preventDefault(); | ||
71 | mainWindow.hide(); | ||
72 | |||
73 | if (process.platform === 'win32') { | ||
74 | mainWindow.setSkipTaskbar(true); | ||
75 | } | ||
76 | } else { | ||
77 | app.quit(); | ||
78 | } | ||
79 | }); | ||
80 | |||
81 | // For Windows we need to store a flag to properly restore the window | ||
82 | // if the window was maximized before minimizing it so system tray | ||
83 | mainWindow.on('minimize', () => { | ||
84 | app.wasMaximized = app.isMaximized; | ||
85 | |||
86 | if (settings.get('minimizeToSystemTray')) { | ||
87 | mainWindow.setSkipTaskbar(true); | ||
88 | } | ||
89 | }); | ||
90 | |||
91 | mainWindow.on('maximize', () => { | ||
92 | app.isMaximized = true; | ||
93 | }); | ||
94 | |||
95 | mainWindow.on('unmaximize', () => { | ||
96 | app.isMaximized = false; | ||
97 | }); | ||
98 | |||
99 | mainWindow.on('restore', () => { | ||
100 | mainWindow.setSkipTaskbar(false); | ||
101 | |||
102 | if (app.wasMaximized) { | ||
103 | mainWindow.maximize(); | ||
104 | } | ||
105 | }); | ||
106 | |||
107 | mainWindow.on('show', () => { | ||
108 | mainWindow.setSkipTaskbar(false); | ||
109 | }); | ||
110 | |||
111 | app.mainWindow = mainWindow; | ||
112 | app.isMaximized = mainWindow.isMaximized(); | ||
113 | |||
114 | mainWindow.webContents.on('new-window', (e, url) => { | ||
115 | e.preventDefault(); | ||
116 | shell.openExternal(url); | ||
117 | }); | ||
118 | }; | ||
119 | |||
120 | // This method will be called when Electron has finished | ||
121 | // initialization and is ready to create browser windows. | ||
122 | // Some APIs can only be used after this event occurs. | ||
123 | app.on('ready', createWindow); | ||
124 | |||
125 | // Quit when all windows are closed. | ||
126 | app.on('window-all-closed', () => { | ||
127 | // On OS X it is common for applications and their menu bar | ||
128 | // to stay active until the user quits explicitly with Cmd + Q | ||
129 | if (settings.get('runInBackground') === undefined | ||
130 | || settings.get('runInBackground')) { | ||
131 | app.quit(); | ||
132 | } | ||
133 | }); | ||
134 | |||
135 | app.on('before-quit', () => { | ||
136 | willQuitApp = true; | ||
137 | }); | ||
138 | |||
139 | app.on('activate', () => { | ||
140 | // On OS X it's common to re-create a window in the app when the | ||
141 | // dock icon is clicked and there are no other windows open. | ||
142 | if (mainWindow === null) { | ||
143 | createWindow(); | ||
144 | } else { | ||
145 | mainWindow.show(); | ||
146 | } | ||
147 | }); | ||