|
3 | 3 | const { BrowserWindow, nativeImage } = require("electron");
|
4 | 4 |
|
5 | 5 | module.exports = (dirname, storage, util) => {
|
6 |
| - const { touchbar } = util; |
7 |
| - let win; |
| 6 | + const { touchbar } = util; |
| 7 | + let win; |
8 | 8 |
|
9 |
| - /** |
10 |
| - * [init] |
11 |
| - * @param {boolean} force [force launching new window] |
12 |
| - * @return {void} [new Colorpicker] |
13 |
| - */ |
14 |
| - let init = (force, color) => { |
15 |
| - const size = storage.get("size"); |
16 |
| - if (win === null || win === undefined || force) |
17 |
| - createWindow(size.width, size.height); |
18 |
| - else win.show(); |
| 9 | + /** |
| 10 | + * [init] |
| 11 | + * @param {boolean} force [force launching new window] |
| 12 | + * @return {void} [new Colorpicker] |
| 13 | + */ |
| 14 | + let init = (force, color) => { |
| 15 | + const size = storage.get("size"); |
| 16 | + if (win === null || win === undefined || force) |
| 17 | + createWindow(size.width, size.height); |
| 18 | + else win.show(); |
| 19 | + }; |
| 20 | + |
| 21 | + /** |
| 22 | + * [createWindow - create new Window] |
| 23 | + * @param {int} width [width of the window] |
| 24 | + * @param {int} height [height of the window] |
| 25 | + * @return {void} |
| 26 | + */ |
| 27 | + let createWindow = (width, height) => { |
| 28 | + const pos = storage.get("pos"); |
| 29 | + let options = { |
| 30 | + frame: false, |
| 31 | + autoHideMenuBar: true, |
| 32 | + width: width, |
| 33 | + height: height, |
| 34 | + minWidth: 440, |
| 35 | + minHeight: 150, |
| 36 | + transparent: true, |
| 37 | + icon: nativeImage.createFromPath(`${dirname}/build/icon.png`), |
| 38 | + webPreferences: { |
| 39 | + preload: `${dirname}/preload.js`, |
| 40 | + }, |
19 | 41 | };
|
| 42 | + if (pos) { |
| 43 | + options.x = pos.x; |
| 44 | + options.y = pos.y; |
| 45 | + } |
20 | 46 |
|
21 |
| - /** |
22 |
| - * [createWindow - create new Window] |
23 |
| - * @param {int} width [width of the window] |
24 |
| - * @param {int} height [height of the window] |
25 |
| - * @return {void} |
26 |
| - */ |
27 |
| - let createWindow = (width, height) => { |
28 |
| - const pos = storage.get("pos"); |
29 |
| - let options = { |
30 |
| - frame: false, |
31 |
| - autoHideMenuBar: true, |
32 |
| - width: width, |
33 |
| - height: height, |
34 |
| - minWidth: 440, |
35 |
| - minHeight: 150, |
36 |
| - transparent: true, |
37 |
| - icon: nativeImage.createFromPath(`${dirname}/build/icon.png`), |
38 |
| - webPreferences: { |
39 |
| - nodeIntegration: true, |
40 |
| - }, |
41 |
| - }; |
42 |
| - if (pos) { |
43 |
| - options.x = pos.x; |
44 |
| - options.y = pos.y; |
45 |
| - } |
| 47 | + win = new BrowserWindow(options); |
| 48 | + win.loadURL(`file://${dirname}/views/colorpicker.html`); |
46 | 49 |
|
47 |
| - win = new BrowserWindow(options); |
48 |
| - win.loadURL(`file://${dirname}/views/colorpicker.html`); |
| 50 | + if (touchbar) win.setTouchBar(touchbar); |
49 | 51 |
|
50 |
| - if (touchbar) win.setTouchBar(touchbar); |
| 52 | + win.on("closed", () => { |
| 53 | + win = undefined; |
| 54 | + let totalWin = BrowserWindow.getAllWindows(); |
| 55 | + for (let wins of totalWin) wins.close(); |
| 56 | + }); |
51 | 57 |
|
52 |
| - win.on("closed", () => { |
53 |
| - win = undefined; |
54 |
| - let totalWin = BrowserWindow.getAllWindows(); |
55 |
| - for (let wins of totalWin) wins.close(); |
56 |
| - }); |
| 58 | + windowEvents(win); |
| 59 | + }; |
57 | 60 |
|
58 |
| - windowEvents(win); |
59 |
| - }; |
| 61 | + /** |
| 62 | + * [windowEvents - BrowserWindow events] |
| 63 | + * @param {BrowserWindow} win [current window] |
| 64 | + * @return {void} |
| 65 | + */ |
| 66 | + let windowEvents = (win) => { |
| 67 | + let timing; |
60 | 68 |
|
61 |
| - /** |
62 |
| - * [windowEvents - BrowserWindow events] |
63 |
| - * @param {BrowserWindow} win [current window] |
64 |
| - * @return {void} |
65 |
| - */ |
66 |
| - let windowEvents = (win) => { |
67 |
| - let timing; |
| 69 | + win.on("focus", (event) => win.webContents.send("hasLooseFocus", false)); |
| 70 | + win.on("blur", (event) => win.webContents.send("hasLooseFocus", true)); |
68 | 71 |
|
69 |
| - win.on("focus", (event) => |
70 |
| - win.webContents.send("hasLooseFocus", false) |
71 |
| - ); |
72 |
| - win.on("blur", (event) => win.webContents.send("hasLooseFocus", true)); |
| 72 | + win.on("resize", (event) => { |
| 73 | + const size = win.getBounds(); |
| 74 | + clearTimeout(timing); |
| 75 | + timing = setTimeout( |
| 76 | + () => |
| 77 | + storage.add({ |
| 78 | + size: { width: size.width, height: size.height }, |
| 79 | + }), |
| 80 | + 300 |
| 81 | + ); |
| 82 | + }); |
73 | 83 |
|
74 |
| - win.on("resize", (event) => { |
75 |
| - const size = win.getBounds(); |
76 |
| - clearTimeout(timing); |
77 |
| - timing = setTimeout( |
78 |
| - () => |
79 |
| - storage.add({ |
80 |
| - size: { width: size.width, height: size.height }, |
81 |
| - }), |
82 |
| - 300 |
83 |
| - ); |
84 |
| - }); |
| 84 | + win.on("move", (event) => { |
| 85 | + const pos = win.getBounds(); |
| 86 | + clearTimeout(timing); |
| 87 | + timing = setTimeout( |
| 88 | + () => storage.add({ pos: { x: pos.x, y: pos.y } }), |
| 89 | + 300 |
| 90 | + ); |
| 91 | + }); |
| 92 | + }; |
85 | 93 |
|
86 |
| - win.on("move", (event) => { |
87 |
| - const pos = win.getBounds(); |
88 |
| - clearTimeout(timing); |
89 |
| - timing = setTimeout( |
90 |
| - () => storage.add({ pos: { x: pos.x, y: pos.y } }), |
91 |
| - 300 |
92 |
| - ); |
93 |
| - }); |
94 |
| - }; |
| 94 | + let getWindow = () => win; |
95 | 95 |
|
96 |
| - let getWindow = () => win; |
97 |
| - |
98 |
| - return { |
99 |
| - init: init, |
100 |
| - getWindow: getWindow, |
101 |
| - }; |
| 96 | + return { |
| 97 | + init: init, |
| 98 | + getWindow: getWindow, |
| 99 | + }; |
102 | 100 | };
|
0 commit comments