electron-findbar 3.1.0 → 3.2.0

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 (4) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +7 -6
  3. package/index.js +42 -13
  4. package/package.json +1 -1
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025 Emerson Capuchi Romaneli
3
+ Copyright (c) 2026 Emerson Capuchi Romaneli
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -13,7 +13,7 @@
13
13
 
14
14
  ## Installation
15
15
 
16
- Install the `electron-findbar` package via npm:
16
+ Install the `electron-findbar` package via [npm](https://www.npmjs.com/package/electron-findbar):
17
17
 
18
18
  ```sh
19
19
  npm install electron-findbar
@@ -21,7 +21,7 @@ npm install electron-findbar
21
21
 
22
22
  ## Overview
23
23
 
24
- The `electron-findbar` is a `BrowserWindow` component designed to emulate the Chrome findbar layout, leveraging the `webContents.findInPage` method to navigate through matches. Inter-process communication (IPC) is used for interaction between the `main` and `renderer` processes.
24
+ The `electron-findbar` package creates a `BrowserWindow`-based component designed to emulate the Chrome findbar layout, leveraging the `webContents.findInPage` method to navigate through matches. Inter-process communication (IPC) is used for interaction between the `main` and `renderer` processes.
25
25
 
26
26
  ### Memory Usage
27
27
 
@@ -55,14 +55,14 @@ Alternatively, you can provide a custom `WebContents` as the second parameter. I
55
55
  const findbar = Findbar.from(baseWindow, webContents)
56
56
  ```
57
57
 
58
- Is also possible to create a findbar providing only the web contents. The BaseWindow.getAllWindows() will be used to query for the parent window:
58
+ It is also possible to create a findbar providing only the web contents. The BaseWindow.getAllWindows() will be used to query for the parent window:
59
59
 
60
60
  ```js
61
61
  // Create or retrieve the findbar associated to the webContents.
62
62
  const findbar = Findbar.from(webContents)
63
63
  ```
64
64
 
65
- **Note:** The findbar is ALWAYS linked to the webContents not the window. The parent is only the window to connect the events and stay on top. If the `.from(webContents)` is used to retrieve an existing findbar previously created with a parent, the findbar will stay connected to the parent.
65
+ **Note:** The findbar is ALWAYS linked to the webContents, not the window. The parent is only the window to connect the events and stay on top. If the `.from(webContents)` is used to retrieve an existing findbar previously created with a parent, the findbar will stay connected to the parent. If a different parent is used, the parent window is updated automatically.
66
66
 
67
67
  #### Retrieve if exists
68
68
 
@@ -305,10 +305,11 @@ If the `contextIsolation` is enabled, the `electron-findbar/remote` will not be
305
305
  ```js
306
306
  const $remote = (ipc => ({
307
307
  getLastState: async () => ipc.invoke('electron-findbar/last-state'),
308
- inputChange: (value) => { ipc.send('electron-findbar/input-change', value) },
308
+ inputChange: (text) => { ipc.send('electron-findbar/input-change', text) },
309
309
  matchCase: (value) => { ipc.send('electron-findbar/match-case', value) },
310
310
  previous: () => { ipc.send('electron-findbar/previous') },
311
311
  next: () => { ipc.send('electron-findbar/next') },
312
+ open: () => { ipc.send('electron-findbar/open') },
312
313
  close: () => { ipc.send('electron-findbar/close') }
313
314
  })) (require('electron').ipcRenderer)
314
315
 
@@ -337,7 +338,7 @@ The `updateParentWindow` method allows you to change the parent window while pre
337
338
 
338
339
  ```javascript
339
340
  // Create a findbar for the initial window
340
- const findbar = Findbar.from(oldWindow, webContents)
341
+ const findbar = Findbar.from([oldWindow, ]webContents)
341
342
 
342
343
  // Later, when you need to change the parent:
343
344
  findbar.updateParentWindow(newWindow)
package/index.js CHANGED
@@ -13,8 +13,11 @@ class Findbar {
13
13
  /** @type {WebContents} */
14
14
  #findableContents
15
15
 
16
+ /** @type {boolean} */
17
+ #propagateVisibilityEvents = true
18
+
16
19
  /** @type { { active: number, total: number } } */
17
- #matches
20
+ #matches = { active: 0, total: 0 }
18
21
 
19
22
  /** @type {(findbarWindow: BrowserWindow) => void} */
20
23
  #windowHandler
@@ -63,9 +66,8 @@ class Findbar {
63
66
  constructor (parent, webContents) {
64
67
  if (isFindable(parent)) {
65
68
  this.#findableContents = parent
66
- this.#parent = Findbar.#getBaseWindowFromWebContents(this.#findableContents)
69
+ parent = Findbar.#getBaseWindowFromWebContents(this.#findableContents)
67
70
  } else {
68
- this.#parent = parent
69
71
  this.#findableContents = webContents ?? Findbar.#retrieveWebContents(parent)
70
72
  }
71
73
 
@@ -76,6 +78,7 @@ class Findbar {
76
78
  this.#findableContents._findbar = this
77
79
 
78
80
  this.#findableContents.once('destroyed', () => { this.detach() })
81
+ this.updateParentWindow(parent)
79
82
  }
80
83
 
81
84
  /**
@@ -87,6 +90,7 @@ class Findbar {
87
90
  this.#focusWindowAndHighlightInput()
88
91
  return
89
92
  }
93
+ if (!this.#parent) { this.updateParentWindow() }
90
94
  const options = Findbar.#mergeStandardOptions(this.#customOptions, this.#parent)
91
95
  this.#isMovable = options.movable
92
96
  this.#window = new BrowserWindow(options)
@@ -127,6 +131,9 @@ class Findbar {
127
131
  if (this.#parent === newParent) { return }
128
132
  this.close()
129
133
  this.#parent = newParent ?? Findbar.#getBaseWindowFromWebContents(this.#findableContents)
134
+ if (this.#parent && !this.#parent.isDestroyed()) {
135
+ this.#parent.once('closed', () => { this.#removeParent() })
136
+ }
130
137
  }
131
138
 
132
139
  /**
@@ -171,6 +178,7 @@ class Findbar {
171
178
  * @returns {void}
172
179
  */
173
180
  findPrevious() {
181
+ if (this.#matches.total < 2) { return }
174
182
  this.#matches.active === 1 && (this.#fixMove = false)
175
183
  this.isOpen() && this.#findInContent({ forward: false })
176
184
  }
@@ -180,6 +188,7 @@ class Findbar {
180
188
  * @returns {void}
181
189
  */
182
190
  findNext() {
191
+ if (this.#matches.total < 2) { return }
183
192
  this.#matches.active === this.#matches.total && (this.#fixMove = true)
184
193
  this.isOpen() && this.#findInContent({ forward: true })
185
194
  }
@@ -257,6 +266,15 @@ class Findbar {
257
266
  this.#boundsHandler = boundsHandler
258
267
  }
259
268
 
269
+ /**
270
+ * Set whether to propagate visibility events to the parent window. If true, when the parent window is shown/hidden, the findbar will also be shown/hidden.
271
+ * @param {boolean} shouldPropagate - Whether to propagate visibility events. Default is true.
272
+ * @returns {void}
273
+ */
274
+ propagateVisibilityEvents(shouldPropagate) {
275
+ this.#propagateVisibilityEvents = shouldPropagate
276
+ }
277
+
260
278
  #registerKeyboardShortcuts(event, input) {
261
279
  if (input.meta || input.control || input.alt) { return }
262
280
 
@@ -283,6 +301,12 @@ class Findbar {
283
301
  }
284
302
  }
285
303
  }
304
+
305
+ #removeParent() {
306
+ this.close()
307
+ this.#parent = null
308
+ }
309
+
286
310
  /**
287
311
  * @param {Electron.FindInPageOptions} options
288
312
  */
@@ -311,16 +335,20 @@ class Findbar {
311
335
 
312
336
  if (this.#parent && !this.#parent.isDestroyed()) {
313
337
  boundsHandler()
314
- this.#parent.prependListener('show', showCascade)
315
- this.#parent.prependListener('hide', hideCascade)
338
+ if (this.#propagateVisibilityEvents) {
339
+ this.#parent.prependListener('show', showCascade)
340
+ this.#parent.prependListener('hide', hideCascade)
341
+ }
316
342
  this.#parent.prependListener('resize', boundsHandler)
317
343
  this.#parent.prependListener('move', boundsHandler)
318
344
  }
319
345
 
320
346
  this.#window.once('closed', () => {
321
347
  if (this.#parent && !this.#parent.isDestroyed()) {
322
- this.#parent.off('show', showCascade)
323
- this.#parent.off('hide', hideCascade)
348
+ if (this.#propagateVisibilityEvents) {
349
+ this.#parent.off('show', showCascade)
350
+ this.#parent.off('hide', hideCascade)
351
+ }
324
352
  this.#parent.off('resize', boundsHandler)
325
353
  this.#parent.off('move', boundsHandler)
326
354
  }
@@ -346,7 +374,8 @@ class Findbar {
346
374
  this.#fixMove = null
347
375
  }
348
376
 
349
- this.#matches = { active, total }
377
+ this.#matches.active = active
378
+ this.#matches.total = total
350
379
 
351
380
  this.#window.webContents.send('electron-findbar/matches', this.#matches)
352
381
  }
@@ -365,16 +394,16 @@ class Findbar {
365
394
  * @returns {WebContents | undefined} The web contents if any.
366
395
  */
367
396
  static #retrieveWebContents(window) {
368
- return window.webContents ?? window.contentView?.children[0]
397
+ return window.webContents ?? window.contentView?.children[0]?.webContents
369
398
  }
370
399
 
371
400
  /**
372
401
  * Get the parent window from web contents.
373
- * @param {WebContents} cont
402
+ * @param {WebContents} w
374
403
  * @returns {BaseWindow | undefined} Parent window if any.
375
404
  */
376
- static #getBaseWindowFromWebContents(cont) {
377
- return BaseWindow.getAllWindows().find(win => win.webContents === cont || win.contentView.children.some(child => child.webContents === cont))
405
+ static #getBaseWindowFromWebContents(w) {
406
+ return BaseWindow.getAllWindows().find(win => win.webContents === w || win.contentView.children.some(child => child.webContents === w))
378
407
  }
379
408
 
380
409
  /**
@@ -448,7 +477,7 @@ class Findbar {
448
477
  }
449
478
 
450
479
  /**
451
- * Get the findbar instance for a given BrowserWindow or WebContents.
480
+ * Get the findbar instance for a given BrowserWindow or WebContents.
452
481
  * @param {BrowserWindow | WebContents} windowOrWebContents
453
482
  * @returns {Findbar | undefined} The findbar instance if it exists, otherwise undefined.
454
483
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "electron-findbar",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
4
4
  "description": "Chrome-like findbar for your Electron app.",
5
5
  "main": "index.js",
6
6
  "files": [