Skip to content

Commit

Permalink
Merge pull request #11198 from Snuffleupagus/PDFSidebarResizer-fixes
Browse files Browse the repository at this point in the history
[PDFSidebarResizer] Add a couple of (small) readability improvements in the code
  • Loading branch information
timvandermeij authored Oct 5, 2019
2 parents 4a1fc62 + 32d16ab commit 0786363
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
72 changes: 36 additions & 36 deletions web/pdf_sidebar_resizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,8 +47,10 @@ class PDFSidebarResizer {
this.eventBus = eventBus;
this.l10n = l10n;

if (typeof CSS === 'undefined' || typeof CSS.supports !== 'function' ||
!CSS.supports(SIDEBAR_WIDTH_VAR, `calc(-1 * ${SIDEBAR_MIN_WIDTH}px)`)) {
if ((typeof PDFJSDev === 'undefined' || !PDFJSDev.test('MOZCENTRAL')) &&
(typeof CSS === 'undefined' || typeof CSS.supports !== 'function' ||
!CSS.supports(SIDEBAR_WIDTH_VAR,
`calc(-1 * ${SIDEBAR_MIN_WIDTH}px)`))) {
console.warn('PDFSidebarResizer: ' +
'The browser does not support resizing of the sidebar.');
return;
Expand Down Expand Up @@ -82,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;
}

Expand Down Expand Up @@ -154,30 +151,33 @@ class PDFSidebarResizer {
this.eventBus.on('resize', (evt) => {
// When the *entire* viewer is resized, such that it becomes narrower,
// ensure that the sidebar doesn't end up being too wide.
if (evt && evt.source === window) {
// Always reset the cached width when the viewer is resized.
this._outerContainerWidth = null;

if (this._width) {
// NOTE: If the sidebar is closed, we don't need to worry about
// visual glitches nor ensure that rendering is triggered.
if (this.sidebarOpen) {
this.outerContainer.classList.add(SIDEBAR_RESIZING_CLASS);
let updated = this._updateWidth(this._width);

Promise.resolve().then(() => {
this.outerContainer.classList.remove(SIDEBAR_RESIZING_CLASS);
// Trigger rendering if the sidebar width changed, to avoid
// depending on the order in which 'resize' events are handled.
if (updated) {
this.eventBus.dispatch('resize', { source: this, });
}
});
} else {
this._updateWidth(this._width);
}
}
if (!evt || evt.source !== window) {
return;
}
// Always reset the cached width when the viewer is resized.
this._outerContainerWidth = null;

if (!this._width) {
// The sidebar hasn't been resized, hence no need to adjust its width.
return;
}
// NOTE: If the sidebar is closed, we don't need to worry about
// visual glitches nor ensure that rendering is triggered.
if (!this.sidebarOpen) {
this._updateWidth(this._width);
return;
}
this.outerContainer.classList.add(SIDEBAR_RESIZING_CLASS);
let updated = this._updateWidth(this._width);

Promise.resolve().then(() => {
this.outerContainer.classList.remove(SIDEBAR_RESIZING_CLASS);
// Trigger rendering if the sidebar width changed, to avoid
// depending on the order in which 'resize' events are handled.
if (updated) {
this.eventBus.dispatch('resize', { source: this, });
}
});
});
}
}
Expand Down
1 change: 1 addition & 0 deletions web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,7 @@ export {
NullL10n,
EventBus,
getGlobalEventBus,
clamp,
ProgressBar,
getPDFFileNameFromURL,
noContextMenuHandler,
Expand Down

0 comments on commit 0786363

Please sign in to comment.