|
1 | 1 | import {
|
2 |
| - screen, |
3 |
| - BrowserWindow, |
4 |
| - BrowserWindowConstructorOptions, |
5 |
| -} from 'electron'; |
6 |
| -import Store from 'electron-store'; |
| 2 | + screen, |
| 3 | + BrowserWindow, |
| 4 | + BrowserWindowConstructorOptions, |
| 5 | +} from "electron"; |
| 6 | +import Store from "electron-store"; |
| 7 | +import { version } from "../../package.json"; |
7 | 8 |
|
8 |
| -export default (windowName: string, options: BrowserWindowConstructorOptions): BrowserWindow => { |
9 |
| - const key = 'window-state'; |
10 |
| - const name = `window-state-${windowName}`; |
11 |
| - const store = new Store({ name }); |
12 |
| - const defaultSize = { |
13 |
| - width: options.width, |
14 |
| - height: options.height, |
15 |
| - }; |
16 |
| - let state = {}; |
17 |
| - let win: BrowserWindow; |
| 9 | +export default ( |
| 10 | + windowName: string, |
| 11 | + options: BrowserWindowConstructorOptions |
| 12 | +): BrowserWindow => { |
| 13 | + const key = "window-state"; |
| 14 | + const name = `window-state-${windowName}`; |
| 15 | + // @ts-ignore |
| 16 | + const store = new Store({ name, projectVersion: version }); |
| 17 | + const defaultSize = { |
| 18 | + width: options.width, |
| 19 | + height: options.height, |
| 20 | + }; |
| 21 | + let state = {}; |
| 22 | + let win: BrowserWindow; |
18 | 23 |
|
19 |
| - const restore = () => store.get(key, defaultSize); |
| 24 | + const restore = () => store.get(key, defaultSize); |
20 | 25 |
|
21 |
| - const getCurrentPosition = () => { |
22 |
| - const position = win.getPosition(); |
23 |
| - const size = win.getSize(); |
24 |
| - return { |
25 |
| - x: position[0], |
26 |
| - y: position[1], |
27 |
| - width: size[0], |
28 |
| - height: size[1], |
29 |
| - }; |
30 |
| - }; |
| 26 | + const getCurrentPosition = () => { |
| 27 | + const position = win.getPosition(); |
| 28 | + const size = win.getSize(); |
| 29 | + return { |
| 30 | + x: position[0], |
| 31 | + y: position[1], |
| 32 | + width: size[0], |
| 33 | + height: size[1], |
| 34 | + }; |
| 35 | + }; |
31 | 36 |
|
32 |
| - const windowWithinBounds = (windowState: any, bounds: any) => { |
33 |
| - return ( |
34 |
| - windowState.x >= bounds.x && |
35 |
| - windowState.y >= bounds.y && |
36 |
| - windowState.x + windowState.width <= bounds.x + bounds.width && |
37 |
| - windowState.y + windowState.height <= bounds.y + bounds.height |
38 |
| - ); |
39 |
| - }; |
| 37 | + const windowWithinBounds = (windowState: any, bounds: any) => { |
| 38 | + return ( |
| 39 | + windowState.x >= bounds.x && |
| 40 | + windowState.y >= bounds.y && |
| 41 | + windowState.x + windowState.width <= bounds.x + bounds.width && |
| 42 | + windowState.y + windowState.height <= bounds.y + bounds.height |
| 43 | + ); |
| 44 | + }; |
40 | 45 |
|
41 |
| - const resetToDefaults = () => { |
42 |
| - const bounds = screen.getPrimaryDisplay().bounds; |
43 |
| - return Object.assign({}, defaultSize, { |
44 |
| - x: (bounds.width - defaultSize.width) / 2, |
45 |
| - y: (bounds.height - defaultSize.height) / 2, |
46 |
| - }); |
47 |
| - }; |
| 46 | + const resetToDefaults = () => { |
| 47 | + const bounds = screen.getPrimaryDisplay().bounds; |
| 48 | + return Object.assign({}, defaultSize, { |
| 49 | + x: (bounds.width - defaultSize.width) / 2, |
| 50 | + y: (bounds.height - defaultSize.height) / 2, |
| 51 | + }); |
| 52 | + }; |
48 | 53 |
|
49 |
| - const ensureVisibleOnSomeDisplay = (windowState: any) => { |
50 |
| - const visible = screen.getAllDisplays().some(display => { |
51 |
| - return windowWithinBounds(windowState, display.bounds); |
52 |
| - }); |
53 |
| - if (!visible) { |
54 |
| - // Window is partially or fully not visible now. |
55 |
| - // Reset it to safe defaults. |
56 |
| - return resetToDefaults(); |
57 |
| - } |
58 |
| - return windowState; |
59 |
| - }; |
| 54 | + const ensureVisibleOnSomeDisplay = (windowState: any) => { |
| 55 | + const visible = screen.getAllDisplays().some((display) => { |
| 56 | + return windowWithinBounds(windowState, display.bounds); |
| 57 | + }); |
| 58 | + if (!visible) { |
| 59 | + // Window is partially or fully not visible now. |
| 60 | + // Reset it to safe defaults. |
| 61 | + return resetToDefaults(); |
| 62 | + } |
| 63 | + return windowState; |
| 64 | + }; |
60 | 65 |
|
61 |
| - const saveState = () => { |
62 |
| - if (!win.isMinimized() && !win.isMaximized()) { |
63 |
| - Object.assign(state, getCurrentPosition()); |
64 |
| - } |
65 |
| - store.set(key, state); |
66 |
| - }; |
| 66 | + const saveState = () => { |
| 67 | + if (!win.isMinimized() && !win.isMaximized()) { |
| 68 | + Object.assign(state, getCurrentPosition()); |
| 69 | + } |
| 70 | + store.set(key, state); |
| 71 | + }; |
67 | 72 |
|
68 |
| - state = ensureVisibleOnSomeDisplay(restore()); |
| 73 | + state = ensureVisibleOnSomeDisplay(restore()); |
69 | 74 |
|
70 |
| - const browserOptions: BrowserWindowConstructorOptions = { |
71 |
| - ...options, |
72 |
| - ...state, |
73 |
| - webPreferences: { |
74 |
| - nodeIntegration: true, |
75 |
| - ...options.webPreferences, |
76 |
| - }, |
77 |
| - }; |
78 |
| - win = new BrowserWindow(browserOptions); |
| 75 | + const browserOptions: BrowserWindowConstructorOptions = { |
| 76 | + ...options, |
| 77 | + ...state, |
| 78 | + webPreferences: { |
| 79 | + nodeIntegration: true, |
| 80 | + ...options.webPreferences, |
| 81 | + }, |
| 82 | + }; |
| 83 | + win = new BrowserWindow(browserOptions); |
79 | 84 |
|
80 |
| - win.on('close', saveState); |
| 85 | + win.on("close", saveState); |
81 | 86 |
|
82 |
| - return win; |
| 87 | + return win; |
83 | 88 | };
|
0 commit comments