electron-findbar 0.2.1 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +9 -1
  2. package/index.js +52 -6
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -82,12 +82,20 @@ findbar.setWindowHandler(win => {
82
82
 
83
83
  ### Opening the Findbar
84
84
 
85
- The Findbar is a child window of the `BaseWindow` passed during construction. It will follow the relative movement of the parent window and must be visible for the Findbar to display:
85
+ The Findbar is a child window of the `BaseWindow` passed during construction. To open it use:
86
86
 
87
87
  ```js
88
88
  findbar.open();
89
89
  ```
90
90
 
91
+ On MacOS, the findbar will follow the relative movement of the parent window by default and there is no way to change it. On Windows and Linux, this behavior is not default and is simulated by using the `move` event of the parent and can be disabled by using:
92
+
93
+ ```js
94
+ findbar.followParentWindow(false)
95
+ ```
96
+
97
+ > Enabled by default.
98
+
91
99
  ### Finding Text in the Page
92
100
 
93
101
  Once open, the Findbar appears by default in the top-right corner of the parent window and can be used without additional coding. Alternatively, you can use the following methods to trigger `findInPage` and navigate through matches in the main process:
package/index.js CHANGED
@@ -23,6 +23,15 @@ class Findbar {
23
23
  /** @type {string} */
24
24
  #lastValue = ''
25
25
 
26
+ /** @type {boolean} */
27
+ #followParent = process.platform !== 'darwin'
28
+
29
+ /**
30
+ * Workaround to fix "findInPage" bug - double-click to loop
31
+ * @type {boolean | null}
32
+ */
33
+ #fixMove = null
34
+
26
35
  /**
27
36
  * Prepare the findbar.
28
37
  * @param {BaseWindow} parent Parent window.
@@ -87,6 +96,10 @@ class Findbar {
87
96
  * Select previous match if any.
88
97
  */
89
98
  findPrevious() {
99
+ if (this.#matches.active === 1) {
100
+ this.#fixMove = false
101
+ }
102
+
90
103
  this.#searchableContents.findInPage(this.#lastValue, { forward: false })
91
104
  }
92
105
 
@@ -94,6 +107,10 @@ class Findbar {
94
107
  * Select next match if any.
95
108
  */
96
109
  findNext() {
110
+ if (this.#matches.active === this.#matches.total) {
111
+ this.#fixMove = true
112
+ }
113
+
97
114
  this.#searchableContents.findInPage(this.#lastValue, { forward: true })
98
115
  }
99
116
 
@@ -140,6 +157,18 @@ class Findbar {
140
157
  this.#windowHandler = windowHandler
141
158
  }
142
159
 
160
+ /**
161
+ * Set the findbar to follow the parent window. Default is true.
162
+ *
163
+ * On darwin platform, the findbar follows the parent window by default. This method is set
164
+ * to false to not create a "move" event listener unnescessarily.
165
+ * @platform win32,linux
166
+ * @param {boolean} follow If true, the findbar will follow the parent window movement.
167
+ */
168
+ followParentWindow(follow) {
169
+ this.#followParent = follow
170
+ }
171
+
143
172
  /**
144
173
  * Merge custom, defaults, and fixed options.
145
174
  * @param {Electron.BrowserWindowConstructorOptions} options Custom options.
@@ -169,24 +198,34 @@ class Findbar {
169
198
  * Register all event listeners.
170
199
  */
171
200
  #registerListeners() {
201
+ const followParent = this.#followParent
172
202
  const showCascade = () => this.#window.isVisible() || this.#window.show()
173
203
  const hideCascade = () => this.#window.isVisible() && this.#window.hide()
204
+
205
+ let lastPos = this.#parent.getPosition()
206
+ const moveCascade = () => {
207
+ const newPos = this.#parent.getPosition()
208
+ const diff = { x: newPos[0] - lastPos[0], y: newPos[1] - lastPos[1] }
209
+ lastPos = newPos
210
+
211
+ const { x, y } = this.#window.getBounds()
212
+ this.#window.setPosition(x + diff.x, y + diff.y)
213
+ }
174
214
 
175
215
  this.#parent.prependListener('show', showCascade)
176
216
  this.#parent.prependListener('hide', hideCascade)
217
+ followParent && this.#parent.prependListener('move', moveCascade)
177
218
 
178
- this.#window.once('closed', () => {
219
+ this.#window.once('close', () => {
179
220
  this.#parent.off('show', showCascade)
180
221
  this.#parent.off('hide', hideCascade)
222
+ followParent && this.#parent.off('move', moveCascade)
181
223
  this.#window = null
182
224
  this.stopFind()
183
225
  })
184
226
 
185
227
  this.#searchableContents.prependOnceListener('destroyed', () => { this.close() })
186
- this.#searchableContents.prependListener('found-in-page', (_e, result) => {
187
- this.#matches = result
188
- this.#sendMatchesCount(result.activeMatchOrdinal, result.matches)
189
- })
228
+ this.#searchableContents.prependListener('found-in-page', (_e, result) => { this.#sendMatchesCount(result.activeMatchOrdinal, result.matches) })
190
229
  }
191
230
 
192
231
  /**
@@ -207,7 +246,14 @@ class Findbar {
207
246
  * @param {number} total Total matches.
208
247
  */
209
248
  #sendMatchesCount(active, total) {
210
- this.#window.webContents.send('electron-findbar/matches', { active, total })
249
+ if (this.#fixMove !== null) {
250
+ this.#fixMove ? this.findNext() : this.findPrevious()
251
+ this.#fixMove = null
252
+ }
253
+
254
+ this.#matches = { active, total }
255
+
256
+ this.#window.webContents.send('electron-findbar/matches', this.#matches)
211
257
  }
212
258
 
213
259
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "electron-findbar",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
4
4
  "description": "Chrome-like findbar for your Electron app.",
5
5
  "main": "index.js",
6
6
  "scripts": {