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

[api-minor] Further modernize the ProgressBar class (PR 14918 follow-up) #15121

Merged
merged 1 commit into from
Jul 2, 2022
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
4 changes: 2 additions & 2 deletions examples/mobile-viewer/viewer.css
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,13 @@ canvas {
}
}

#loadingBar .progress.indeterminate {
#loadingBar.indeterminate .progress {
transform: none;
background-color: rgba(153, 153, 153, 1);
transition: none;
}

#loadingBar .indeterminate .glimmer {
#loadingBar.indeterminate .progress .glimmer {
position: absolute;
top: 0;
left: 0;
Expand Down
2 changes: 1 addition & 1 deletion examples/mobile-viewer/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ const PDFViewerApplication = {
},

get loadingBar() {
const bar = new pdfjsViewer.ProgressBar("#loadingBar");
const bar = new pdfjsViewer.ProgressBar("loadingBar");

return pdfjsLib.shadow(this, "loadingBar", bar);
},
Expand Down
50 changes: 26 additions & 24 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ const PDFViewerApplication = {
},

get loadingBar() {
const bar = new ProgressBar("#loadingBar");
const bar = new ProgressBar("loadingBar");
return shadow(this, "loadingBar", bar);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated to this patch, does it make sense to shadow the value ?
I suppose that once the document is loaded we don't need it anymore.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated to this patch, does it make sense to shadow the value ?

Doing that simplifies the code overall, and also allows us to initialize it lazily, since we're accessing it from multiple spots throughout the viewer code; see e.g.

I suppose that once the document is loaded we don't need it anymore.

Correct; however if we'd remove it at the end of loading that'd (as far as I can tell) either mean having to initialize it eagerly or possibly require adding more code.

},

Expand Down Expand Up @@ -1160,31 +1160,33 @@ const PDFViewerApplication = {
// that we discard some of the loaded data. This can cause the loading
// bar to move backwards. So prevent this by only updating the bar if it
// increases.
if (percent > this.loadingBar.percent || isNaN(percent)) {
this.loadingBar.percent = percent;

// When disableAutoFetch is enabled, it's not uncommon for the entire file
// to never be fetched (depends on e.g. the file structure). In this case
// the loading bar will not be completely filled, nor will it be hidden.
// To prevent displaying a partially filled loading bar permanently, we
// hide it when no data has been loaded during a certain amount of time.
const disableAutoFetch = this.pdfDocument
? this.pdfDocument.loadingParams.disableAutoFetch
: AppOptions.get("disableAutoFetch");

if (disableAutoFetch && percent) {
if (this.disableAutoFetchLoadingBarTimeout) {
clearTimeout(this.disableAutoFetchLoadingBarTimeout);
this.disableAutoFetchLoadingBarTimeout = null;
}
this.loadingBar.show();
if (percent <= this.loadingBar.percent) {
return;
}
this.loadingBar.percent = percent;

this.disableAutoFetchLoadingBarTimeout = setTimeout(() => {
this.loadingBar.hide();
this.disableAutoFetchLoadingBarTimeout = null;
}, DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT);
}
// When disableAutoFetch is enabled, it's not uncommon for the entire file
// to never be fetched (depends on e.g. the file structure). In this case
// the loading bar will not be completely filled, nor will it be hidden.
// To prevent displaying a partially filled loading bar permanently, we
// hide it when no data has been loaded during a certain amount of time.
const disableAutoFetch =
this.pdfDocument?.loadingParams.disableAutoFetch ??
AppOptions.get("disableAutoFetch");

if (!disableAutoFetch || isNaN(percent)) {
return;
}
if (this.disableAutoFetchLoadingBarTimeout) {
clearTimeout(this.disableAutoFetchLoadingBarTimeout);
this.disableAutoFetchLoadingBarTimeout = null;
}
this.loadingBar.show();

this.disableAutoFetchLoadingBarTimeout = setTimeout(() => {
this.loadingBar.hide();
this.disableAutoFetchLoadingBarTimeout = null;
}, DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT);
},

load(pdfDocument) {
Expand Down
52 changes: 24 additions & 28 deletions web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,12 @@ function clamp(v, min, max) {
}

class ProgressBar {
#classList = null;

#percent = 0;

#visible = true;

constructor(id) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) &&
Expand All @@ -699,34 +705,24 @@ class ProgressBar {
"please use CSS rules to modify its appearance instead."
);
}
this.visible = true;

// Fetch the sub-elements for later.
this.div = document.querySelector(id + " .progress");
// Get the loading bar element, so it can be resized to fit the viewer.
this.bar = this.div.parentNode;

this.percent = 0;
}

#updateBar() {
if (this._indeterminate) {
this.div.classList.add("indeterminate");
return;
}
this.div.classList.remove("indeterminate");

docStyle.setProperty("--progressBar-percent", `${this._percent}%`);
const bar = document.getElementById(id);
this.#classList = bar.classList;
}

get percent() {
return this._percent;
return this.#percent;
}

set percent(val) {
this._indeterminate = isNaN(val);
this._percent = clamp(val, 0, 100);
this.#updateBar();
this.#percent = clamp(val, 0, 100);

if (isNaN(val)) {
this.#classList.add("indeterminate");
return;
}
this.#classList.remove("indeterminate");

docStyle.setProperty("--progressBar-percent", `${this.#percent}%`);
}

setWidth(viewer) {
Expand All @@ -741,19 +737,19 @@ class ProgressBar {
}

hide() {
if (!this.visible) {
if (!this.#visible) {
return;
}
this.visible = false;
this.bar.classList.add("hidden");
this.#visible = false;
this.#classList.add("hidden");
}

show() {
if (this.visible) {
if (this.#visible) {
return;
}
this.visible = true;
this.bar.classList.remove("hidden");
this.#visible = true;
this.#classList.remove("hidden");
}
}

Expand Down
4 changes: 2 additions & 2 deletions web/viewer.css
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,13 @@ select {
}
}

#loadingBar .progress.indeterminate {
#loadingBar.indeterminate .progress {
transform: none;
background-color: var(--progressBar-indeterminate-bg-color);
transition: none;
}

#loadingBar .progress.indeterminate .glimmer {
#loadingBar.indeterminate .progress .glimmer {
position: absolute;
top: 0;
left: 0;
Expand Down