Merge pull request #17152 from Snuffleupagus/findbar-resizeObserver

Use a `ResizeObserver` to update the layout of `PDFFindBar`
This commit is contained in:
Jonas Jenwald 2023-10-21 17:46:30 +02:00 committed by GitHub
commit 4c4676e5a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -25,6 +25,8 @@ const MATCHES_COUNT_LIMIT = 1000;
* is done by PDFFindController. * is done by PDFFindController.
*/ */
class PDFFindBar { class PDFFindBar {
#resizeObserver = new ResizeObserver(this.#resizeObserverCallback.bind(this));
constructor(options, eventBus, l10n) { constructor(options, eventBus, l10n) {
this.opened = false; this.opened = false;
@ -87,8 +89,6 @@ class PDFFindBar {
this.matchDiacritics.addEventListener("click", () => { this.matchDiacritics.addEventListener("click", () => {
this.dispatchEvent("diacriticmatchingchange"); this.dispatchEvent("diacriticmatchingchange");
}); });
this.eventBus._on("resize", this.#adjustWidth.bind(this));
} }
reset() { reset() {
@ -134,7 +134,6 @@ class PDFFindBar {
findMsg.then(msg => { findMsg.then(msg => {
this.findMsg.setAttribute("data-status", status); this.findMsg.setAttribute("data-status", status);
this.findMsg.textContent = msg; this.findMsg.textContent = msg;
this.#adjustWidth();
}); });
this.updateResultsCount(matchesCount); this.updateResultsCount(matchesCount);
@ -152,27 +151,31 @@ class PDFFindBar {
} }
matchCountMsg.then(msg => { matchCountMsg.then(msg => {
this.findResultsCount.textContent = msg; this.findResultsCount.textContent = msg;
// Since `updateResultsCount` may be called from `PDFFindController`,
// ensure that the width of the findbar is always updated correctly.
this.#adjustWidth();
}); });
} }
open() { open() {
if (!this.opened) { if (!this.opened) {
// Potentially update the findbar layout, row vs column, when:
// - The width of the viewer itself changes.
// - The width of the findbar changes, by toggling the visibility
// (or localization) of find count/status messages.
this.#resizeObserver.observe(this.bar.parentNode);
this.#resizeObserver.observe(this.bar);
this.opened = true; this.opened = true;
toggleExpandedBtn(this.toggleButton, true, this.bar); toggleExpandedBtn(this.toggleButton, true, this.bar);
} }
this.findField.select(); this.findField.select();
this.findField.focus(); this.findField.focus();
this.#adjustWidth();
} }
close() { close() {
if (!this.opened) { if (!this.opened) {
return; return;
} }
this.#resizeObserver.disconnect();
this.opened = false; this.opened = false;
toggleExpandedBtn(this.toggleButton, false, this.bar); toggleExpandedBtn(this.toggleButton, false, this.bar);
@ -187,25 +190,22 @@ class PDFFindBar {
} }
} }
#adjustWidth() { #resizeObserverCallback(entries) {
if (!this.opened) { const { bar } = this;
return;
}
// The find bar has an absolute position and thus the browser extends // The find bar has an absolute position and thus the browser extends
// its width to the maximum possible width once the find bar does not fit // its width to the maximum possible width once the find bar does not fit
// entirely within the window anymore (and its elements are automatically // entirely within the window anymore (and its elements are automatically
// wrapped). Here we detect and fix that. // wrapped). Here we detect and fix that.
this.bar.classList.remove("wrapContainers"); bar.classList.remove("wrapContainers");
const findbarHeight = this.bar.clientHeight; const findbarHeight = bar.clientHeight;
const inputContainerHeight = this.bar.firstElementChild.clientHeight; const inputContainerHeight = bar.firstElementChild.clientHeight;
if (findbarHeight > inputContainerHeight) { if (findbarHeight > inputContainerHeight) {
// The findbar is taller than the input container, which means that // The findbar is taller than the input container, which means that
// the browser wrapped some of the elements. For a consistent look, // the browser wrapped some of the elements. For a consistent look,
// wrap all of them to adjust the width of the find bar. // wrap all of them to adjust the width of the find bar.
this.bar.classList.add("wrapContainers"); bar.classList.add("wrapContainers");
} }
} }
} }