Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PDFSidebarResizer] Add a couple of (small) readability improvements in the code #11198

Merged
merged 3 commits into from
Oct 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 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,14 +151,22 @@ 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) {
if (!evt || evt.source !== window) {
return;
}
// Always reset the cached width when the viewer is resized.
this._outerContainerWidth = null;

if (this._width) {
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) {
if (!this.sidebarOpen) {
this._updateWidth(this._width);
return;
}
this.outerContainer.classList.add(SIDEBAR_RESIZING_CLASS);
let updated = this._updateWidth(this._width);

Expand All @@ -173,11 +178,6 @@ class PDFSidebarResizer {
this.eventBus.dispatch('resize', { source: this, });
}
});
} else {
this._updateWidth(this._width);
}
}
}
});
}
}
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