|
1 |
| -import express from 'express' |
2 |
| -import {BrowserWindow, clipboard, NativeImage} from 'electron' |
3 |
| -import state from '../state' |
| 1 | +import express from 'express'; |
| 2 | +import {BrowserWindow, clipboard, NativeImage} from 'electron'; |
| 3 | +import state from '../state'; |
4 | 4 | import {join} from "path";
|
5 | 5 | import {notifyLaravel} from "../utils";
|
6 | 6 | const router = express.Router();
|
7 | 7 | import windowStateKeeper from "electron-window-state";
|
8 | 8 |
|
9 | 9 | router.post('/maximize', (req, res) => {
|
10 |
| - const {id} = req.body |
11 |
| - state.windows[id]?.maximize() |
| 10 | + const {id} = req.body; |
12 | 11 |
|
13 |
| - res.sendStatus(200) |
| 12 | + state.windows[id]?.maximize(); |
| 13 | + |
| 14 | + res.sendStatus(200); |
14 | 15 | });
|
15 | 16 |
|
16 | 17 | router.post('/minimize', (req, res) => {
|
17 |
| - const {id} = req.body |
18 |
| - state.windows[id]?.minimize() |
| 18 | + const {id} = req.body; |
| 19 | + |
| 20 | + state.windows[id]?.minimize(); |
19 | 21 |
|
20 |
| - res.sendStatus(200) |
| 22 | + res.sendStatus(200); |
21 | 23 | });
|
22 | 24 |
|
23 | 25 | router.post('/resize', (req, res) => {
|
24 |
| - const {id, width, height} = req.body |
| 26 | + const {id, width, height} = req.body; |
| 27 | + |
| 28 | + state.windows[id]?.setSize(parseInt(width), parseInt(height)); |
| 29 | + |
| 30 | + res.sendStatus(200); |
| 31 | +}); |
| 32 | + |
| 33 | +router.post('/title', (req, res) => { |
| 34 | + const {id, title} = req.body; |
25 | 35 |
|
26 |
| - state.windows[id]?.setSize(parseInt(width), parseInt(height)) |
| 36 | + state.windows[id]?.setTitle(title); |
27 | 37 |
|
28 |
| - res.sendStatus(200) |
29 |
| -}) |
| 38 | + res.sendStatus(200); |
| 39 | +}); |
| 40 | + |
| 41 | +router.post('/url', (req, res) => { |
| 42 | + const {id, url} = req.body; |
| 43 | + |
| 44 | + state.windows[id]?.loadURL(url); |
30 | 45 |
|
| 46 | + res.sendStatus(200); |
| 47 | +}); |
31 | 48 |
|
32 | 49 | router.post('/position', (req, res) => {
|
33 |
| - const {id, x, y, animate} = req.body |
| 50 | + const {id, x, y, animate} = req.body; |
34 | 51 |
|
35 |
| - state.windows[id]?.setPosition(parseInt(x), parseInt(y), animate) |
| 52 | + state.windows[id]?.setPosition(parseInt(x), parseInt(y), animate); |
36 | 53 |
|
37 |
| - res.sendStatus(200) |
38 |
| -}) |
| 54 | + res.sendStatus(200); |
| 55 | +}); |
39 | 56 |
|
40 | 57 | router.post('/reload', (req, res) => {
|
41 |
| - const {id} = req.body |
42 |
| - state.windows[id]?.reload() |
| 58 | + const {id} = req.body; |
| 59 | + |
| 60 | + state.windows[id]?.reload(); |
43 | 61 |
|
44 |
| - res.sendStatus(200) |
| 62 | + res.sendStatus(200); |
45 | 63 | });
|
46 | 64 |
|
47 | 65 | router.post('/close', (req, res) => {
|
48 |
| - const {id} = req.body |
| 66 | + const {id} = req.body; |
49 | 67 |
|
50 | 68 | if (state.windows[id]) {
|
51 |
| - state.windows[id].close() |
52 |
| - delete state.windows[id] |
| 69 | + state.windows[id].close(); |
| 70 | + delete state.windows[id]; |
53 | 71 | }
|
54 |
| - return res.sendStatus(200) |
55 |
| -}) |
| 72 | + |
| 73 | + return res.sendStatus(200); |
| 74 | +}); |
56 | 75 |
|
57 | 76 | router.post('/hide', (req, res) => {
|
58 |
| - const {id} = req.body |
| 77 | + const {id} = req.body; |
59 | 78 |
|
60 | 79 | if (state.windows[id]) {
|
61 |
| - state.windows[id].hide() |
| 80 | + state.windows[id].hide(); |
62 | 81 | }
|
63 |
| - return res.sendStatus(200) |
64 |
| -}) |
| 82 | + |
| 83 | + return res.sendStatus(200); |
| 84 | +}); |
| 85 | + |
| 86 | +router.post('/always-on-top', (req, res) => { |
| 87 | + const {id, alwaysOnTop} = req.body; |
| 88 | + |
| 89 | + state.windows[id]?.setAlwaysOnTop(alwaysOnTop); |
| 90 | + |
| 91 | + res.sendStatus(200); |
| 92 | +}); |
65 | 93 |
|
66 | 94 | router.get('/current', (req, res) => {
|
67 | 95 | // Find the current window object
|
@@ -118,14 +146,6 @@ function getWindowData(id) {
|
118 | 146 | // trafficLightPosition: currentWindow.getTrafficLightPosition(),
|
119 | 147 | };
|
120 | 148 | }
|
121 |
| -}); |
122 |
| - |
123 |
| -router.post('/always-on-top', (req, res) => { |
124 |
| - const {id, alwaysOnTop} = req.body |
125 |
| - state.windows[id]?.setAlwaysOnTop(alwaysOnTop) |
126 |
| - |
127 |
| - res.sendStatus(200) |
128 |
| -}); |
129 | 149 |
|
130 | 150 | router.post('/open', (req, res) => {
|
131 | 151 | let {
|
@@ -159,24 +179,24 @@ router.post('/open', (req, res) => {
|
159 | 179 | fullscreenable,
|
160 | 180 | kiosk,
|
161 | 181 | autoHideMenuBar,
|
162 |
| - } = req.body |
| 182 | + } = req.body; |
163 | 183 |
|
164 | 184 | if (state.windows[id]) {
|
165 |
| - state.windows[id].show() |
166 |
| - state.windows[id].focus() |
167 |
| - return res.sendStatus(200) |
| 185 | + state.windows[id].show(); |
| 186 | + state.windows[id].focus(); |
| 187 | + return res.sendStatus(200); |
168 | 188 | }
|
169 | 189 |
|
170 |
| - let preloadPath = join(__dirname, '../../preload/index.js') |
| 190 | + let preloadPath = join(__dirname, '../../preload/index.js'); |
171 | 191 |
|
172 |
| - let windowState: windowStateKeeper.State | undefined = undefined |
| 192 | + let windowState: windowStateKeeper.State | undefined = undefined; |
173 | 193 |
|
174 | 194 | if (req.body.rememberState === true) {
|
175 |
| - windowState = windowStateKeeper({ |
176 |
| - file: `window-state-${id}.json`, |
177 |
| - defaultHeight: parseInt(height), |
178 |
| - defaultWidth: parseInt(width), |
179 |
| - }) |
| 195 | + windowState = windowStateKeeper({ |
| 196 | + file: `window-state-${id}.json`, |
| 197 | + defaultHeight: parseInt(height), |
| 198 | + defaultWidth: parseInt(width), |
| 199 | + }); |
180 | 200 | }
|
181 | 201 |
|
182 | 202 | const window = new BrowserWindow({
|
@@ -217,92 +237,94 @@ router.post('/open', (req, res) => {
|
217 | 237 | fullscreen,
|
218 | 238 | fullscreenable,
|
219 | 239 | kiosk,
|
220 |
| - }) |
| 240 | + }); |
221 | 241 |
|
222 | 242 | if ((process.env.NODE_ENV === 'development' || showDevTools === true) && showDevTools !== false) {
|
223 | 243 | window.webContents.openDevTools();
|
224 | 244 | }
|
225 | 245 |
|
226 |
| - require("@electron/remote/main").enable(window.webContents) |
| 246 | + require("@electron/remote/main").enable(window.webContents); |
227 | 247 |
|
228 | 248 | if (req.body.rememberState === true) {
|
229 |
| - windowState.manage(window) |
| 249 | + windowState.manage(window); |
230 | 250 | }
|
231 | 251 |
|
232 | 252 | window.on('blur', () => {
|
233 | 253 | notifyLaravel('events', {
|
234 |
| - event: 'Native\\Laravel\\Events\\Windows\\WindowBlurred', |
235 |
| - payload: [id] |
236 |
| - }) |
| 254 | + event: 'Native\\Laravel\\Events\\Windows\\WindowBlurred', |
| 255 | + payload: [id] |
| 256 | + }); |
237 | 257 | });
|
238 | 258 |
|
239 | 259 | window.on('focus', () => {
|
240 | 260 | notifyLaravel('events', {
|
241 | 261 | event: 'Native\\Laravel\\Events\\Windows\\WindowFocused',
|
242 | 262 | payload: [id]
|
243 |
| - }) |
244 |
| - }) |
| 263 | + }); |
| 264 | + }); |
245 | 265 |
|
246 | 266 | window.on('minimize', () => {
|
247 | 267 | notifyLaravel('events', {
|
248 | 268 | event: 'Native\\Laravel\\Events\\Windows\\WindowMinimized',
|
249 | 269 | payload: [id]
|
250 |
| - }) |
251 |
| - }) |
| 270 | + }); |
| 271 | + }); |
252 | 272 |
|
253 | 273 | window.on('maximize', () => {
|
254 |
| - notifyLaravel('events', { |
255 |
| - event: 'Native\\Laravel\\Events\\Windows\\WindowMaximized', |
256 |
| - payload: [id] |
257 |
| - }) |
258 |
| - }) |
259 |
| - |
260 |
| - window.on('show', () => { |
261 |
| - notifyLaravel('events', { |
262 |
| - event: 'Native\\Laravel\\Events\\Windows\\WindowShown', |
263 |
| - payload: [id] |
264 |
| - }) |
265 |
| - }); |
| 274 | + notifyLaravel('events', { |
| 275 | + event: 'Native\\Laravel\\Events\\Windows\\WindowMaximized', |
| 276 | + payload: [id] |
| 277 | + }); |
| 278 | + }); |
| 279 | + |
| 280 | + window.on('show', () => { |
| 281 | + notifyLaravel('events', { |
| 282 | + event: 'Native\\Laravel\\Events\\Windows\\WindowShown', |
| 283 | + payload: [id] |
| 284 | + }); |
| 285 | + }); |
266 | 286 |
|
267 | 287 | window.on('resized', () => {
|
268 |
| - notifyLaravel('events', { |
269 |
| - event: 'Native\\Laravel\\Events\\Windows\\WindowResized', |
270 |
| - payload: [id, window.getSize()[0], window.getSize()[1]] |
271 |
| - }) |
| 288 | + notifyLaravel('events', { |
| 289 | + event: 'Native\\Laravel\\Events\\Windows\\WindowResized', |
| 290 | + payload: [id, window.getSize()[0], window.getSize()[1]] |
| 291 | + }); |
272 | 292 | });
|
273 | 293 |
|
274 | 294 | window.on('page-title-updated', (evt) => {
|
275 |
| - evt.preventDefault() |
276 |
| - }) |
| 295 | + evt.preventDefault(); |
| 296 | + }); |
277 | 297 |
|
278 | 298 | window.on('close', (evt) => {
|
279 | 299 | if (state.windows[id]) {
|
280 |
| - delete state.windows[id] |
| 300 | + delete state.windows[id]; |
281 | 301 | }
|
| 302 | + |
282 | 303 | notifyLaravel('events', {
|
283 | 304 | event: 'Native\\Laravel\\Events\\Windows\\WindowClosed',
|
284 | 305 | payload: [id]
|
285 |
| - }) |
286 |
| - }) |
| 306 | + }); |
| 307 | + }); |
287 | 308 |
|
288 | 309 | window.on('hide', (evt) => {
|
289 | 310 | notifyLaravel('events', {
|
290 | 311 | event: 'Native\\Laravel\\Events\\Windows\\WindowHidden',
|
291 | 312 | payload: [id]
|
292 |
| - }) |
293 |
| - }) |
| 313 | + }); |
| 314 | + }); |
294 | 315 |
|
295 | 316 | // Append the window id to the url
|
296 |
| - url += (url.indexOf('?') === -1 ? '?' : '&') + '_windowId=' + id |
| 317 | + url += (url.indexOf('?') === -1 ? '?' : '&') + '_windowId=' + id; |
297 | 318 |
|
298 |
| - window.loadURL(url) |
| 319 | + window.loadURL(url); |
299 | 320 |
|
300 | 321 | window.webContents.on('did-finish-load', () => {
|
301 |
| - window.show() |
302 |
| - }) |
303 |
| - state.windows[id] = window |
| 322 | + window.show(); |
| 323 | + }); |
304 | 324 |
|
305 |
| - res.sendStatus(200) |
306 |
| -}) |
| 325 | + state.windows[id] = window; |
| 326 | + |
| 327 | + res.sendStatus(200); |
| 328 | +}); |
307 | 329 |
|
308 | 330 | export default router;
|
0 commit comments