From 9e4552d7920fcba13b3d74e1e180fc49180f4436 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 3 Oct 2019 12:03:45 +0200 Subject: [PATCH] [PDFSidebarResizer] Refactor the clamping in `_updateWidth` Rather than manually clamping the `width` here, we can just `export` an already existing helper function from `ui_utils.js` instead. (Hopefully it will eventually be possible to replace this helper function with a native `Math.clamp` function, given that there exists a "Stage 1 Proposal" for adding such a thing to the ECMAScript specification.) --- web/pdf_sidebar_resizer.js | 17 ++++++----------- web/ui_utils.js | 1 + 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/web/pdf_sidebar_resizer.js b/web/pdf_sidebar_resizer.js index 388331054..f975c0587 100644 --- a/web/pdf_sidebar_resizer.js +++ b/web/pdf_sidebar_resizer.js @@ -13,7 +13,7 @@ * limitations under the License. */ -import { NullL10n } from './ui_utils'; +import { clamp, NullL10n } from './ui_utils'; const SIDEBAR_WIDTH_VAR = '--sidebar-width'; const SIDEBAR_MIN_WIDTH = 200; // pixels @@ -84,19 +84,14 @@ class PDFSidebarResizer { } // Prevent the sidebar from becoming too narrow, or from occupying more // than half of the available viewer width. - const maxWidth = Math.floor(this.outerContainerWidth / 2); - if (width > maxWidth) { - width = maxWidth; - } - if (width < SIDEBAR_MIN_WIDTH) { - width = SIDEBAR_MIN_WIDTH; - } + const newWidth = clamp(width, SIDEBAR_MIN_WIDTH, + Math.floor(this.outerContainerWidth / 2)); // Only update the UI when the sidebar width did in fact change. - if (width === this._width) { + if (newWidth === this._width) { return false; } - this._width = width; - this.doc.style.setProperty(SIDEBAR_WIDTH_VAR, `${width}px`); + this._width = newWidth; + this.doc.style.setProperty(SIDEBAR_WIDTH_VAR, `${newWidth}px`); return true; } diff --git a/web/ui_utils.js b/web/ui_utils.js index e1235759a..cb7108a75 100644 --- a/web/ui_utils.js +++ b/web/ui_utils.js @@ -898,6 +898,7 @@ export { NullL10n, EventBus, getGlobalEventBus, + clamp, ProgressBar, getPDFFileNameFromURL, noContextMenuHandler,