-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
203 lines (194 loc) · 5.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const {
app, BrowserWindow, ipcMain, globalShortcut,
} = require('electron');
const path = require('path');
const isMac = process.platform === 'darwin';
const BAR_WIDTH = 376;
const BAR_HEIGHT = 48;
const DEFAULT_LEFT = 80;
const DEFAULT_RIGHT = isMac ? 20 : 30;
const showWin = (win) => {
if (win.isMinimized()) {
win.restore();
}
win.show();
win.focus();
win.webContents.send('find-show');
};
const getPosition = (win, options) => {
const pos = win.getBounds();
const {
left, right, top, bottom,
} = options || {};
const result = {};
if (left > 0) {
result.x = pos.x + left;
} else {
result.x = pos.x + pos.width - BAR_WIDTH - (right > 0 ? right : DEFAULT_LEFT);
}
if (bottom > 0) {
result.y = pos.y + pos.height - BAR_HEIGHT - bottom;
} else {
result.y = pos.y + (top > 0 ? top : DEFAULT_RIGHT);
}
return result;
};
const createFindBar = (win, options, hideFindBar) => {
const findBar = new BrowserWindow({
parent: win,
width: BAR_WIDTH,
height: BAR_HEIGHT,
title: 'Find',
resizable: false,
movable: false,
autoHideMenuBar: true,
frame: false,
show: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
spellcheck: false,
},
...getPosition(win, options),
});
findBar.once('ready-to-show', () => findBar.show());
const focusInput = () => {
findBar.webContents.send('find-show');
};
const cleanup = () => {
globalShortcut.unregister('CommandOrControl+F', focusInput);
globalShortcut.unregister('ESC', hideFindBar);
};
findBar._isFindBar = true;
findBar.on('focus', () => {
cleanup();
globalShortcut.register('CommandOrControl+F', focusInput);
globalShortcut.register('ESC', hideFindBar);
});
findBar.on('blur', cleanup);
findBar.once('closed', cleanup);
return findBar;
};
app.on('window-all-closed', () => {
globalShortcut.unregister('CommandOrControl+F');
globalShortcut.unregister('ESC');
});
let index = 0;
module.exports = (win, options) => {
if (win._hasFindBar || win._isFindBar) {
return;
}
win._hasFindBar = true;
const hash = `#${Math.floor(Math.random() * 100000)}/${++index}`;
const search = options && options.darkMode === false ? '?disabledDarkMode' : '';
const FIND_CLOSE = `find-close${hash}`;
const FIND_START = `find-start${hash}`;
const FIND_PREV = `find-prev${hash}`;
const FIND_NEXT = `find-next${hash}`;
let findBar;
let timer;
let curReqId;
let curWord = '';
let curSeq = -1;
let preText = '';
const findStop = () => {
preText = '';
win.webContents.stopFindInPage('clearSelection');
if (findBar) {
findBar.webContents.send('find-result');
}
};
const hideFindBar = () => {
if (findBar) {
findBar.hide();
}
findStop();
};
const showFindBar = () => {
if (findBar) {
return showWin(findBar);
}
findBar = createFindBar(win, options, hideFindBar);
const setPosition = () => {
timer = null;
const pos = getPosition(win, options);
findBar.setPosition(pos.x, pos.y);
};
const debounce = () => {
timer = timer || setTimeout(setPosition, 30);
};
setPosition();
win.on('resize', debounce);
if (!isMac) {
win.on('move', debounce);
}
findBar.once('closed', () => {
findBar = null;
});
findBar.loadFile(path.join(__dirname, 'find.html'), { hash, search });
};
const findInPage = (opts) => {
if (curWord) {
curReqId = win.webContents.findInPage(curWord, opts);
} else {
curReqId = null;
findStop();
}
};
const findStart = (_, { value, seq }) => {
if (!(seq > curSeq)) {
return;
}
curSeq = seq;
curWord = value;
if (curWord !== preText) {
findInPage({ forward: true, findNext: true });
}
};
const findNext = () => {
findInPage({ forward: true, findNext: false });
};
const findPrev = () => {
findInPage({ forward: false, findNext: false });
};
const cleanup = () => {
globalShortcut.unregister('CommandOrControl+F', showFindBar);
globalShortcut.unregister('ESC', hideFindBar);
};
const handleFocus = () => {
cleanup();
globalShortcut.register('CommandOrControl+F', showFindBar);
globalShortcut.register('ESC', hideFindBar);
};
if (win.isFocused()) {
handleFocus();
}
win.on('focus', handleFocus);
win.webContents.on('found-in-page', (_, result) => {
if (result.finalUpdate && curReqId === result.requestId) {
preText = result.matches ? curWord : null;
if (findBar) {
findBar.webContents.send('find-result', {
activeMatchOrdinal: result.activeMatchOrdinal,
matches: result.matches,
});
}
}
});
win.on('blur', cleanup);
win.once('closed', () => {
cleanup();
ipcMain.off(FIND_CLOSE, hideFindBar);
ipcMain.off(FIND_START, findStart);
ipcMain.off(FIND_PREV, findPrev);
ipcMain.off(FIND_NEXT, findNext);
try {
findBar.destroy();
} catch (e) {}
findBar = null;
});
ipcMain.on(FIND_CLOSE, hideFindBar);
ipcMain.on(FIND_START, findStart);
ipcMain.on(FIND_PREV, findPrev);
ipcMain.on(FIND_NEXT, findNext);
};