Skip to content

Commit eaaa8b4

Browse files
Merge pull request #14918 from Snuffleupagus/ProgressBar-modernize
[api-minor] Modernize and simplify the `ProgressBar` class
2 parents b5f2bd8 + 1f3da03 commit eaaa8b4

File tree

4 files changed

+32
-25
lines changed

4 files changed

+32
-25
lines changed

examples/mobile-viewer/viewer.css

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
* limitations under the License.
1414
*/
1515

16+
:root {
17+
--progressBar-percent: 0%;
18+
}
19+
1620
* {
1721
padding: 0;
1822
margin: 0;
@@ -191,13 +195,12 @@ canvas {
191195
height: 0.6rem;
192196
background-color: rgba(51, 51, 51, 1);
193197
border-bottom: 1px solid rgba(51, 51, 51, 1);
194-
margin-top: 5rem;
195198
}
196199

197200
#loadingBar .progress {
198201
position: absolute;
199202
left: 0;
200-
width: 0;
203+
width: var(--progressBar-percent);
201204
height: 100%;
202205
background-color: rgba(221, 221, 221, 1);
203206
overflow: hidden;
@@ -217,6 +220,7 @@ canvas {
217220
}
218221

219222
#loadingBar .progress.indeterminate {
223+
width: 100%;
220224
background-color: rgba(153, 153, 153, 1);
221225
transition: none;
222226
}

examples/mobile-viewer/viewer.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ const PDFViewerApplication = {
8282
self.pdfDocument = pdfDocument;
8383
self.pdfViewer.setDocument(pdfDocument);
8484
self.pdfLinkService.setDocument(pdfDocument);
85-
self.pdfHistory.initialize({ fingerprint: pdfDocument.fingerprint });
85+
self.pdfHistory.initialize({
86+
fingerprint: pdfDocument.fingerprints[0],
87+
});
8688

8789
self.loadingBar.hide();
8890
self.setTitleUsingMetadata(pdfDocument);
@@ -159,7 +161,7 @@ const PDFViewerApplication = {
159161
},
160162

161163
get loadingBar() {
162-
const bar = new pdfjsViewer.ProgressBar("#loadingBar", {});
164+
const bar = new pdfjsViewer.ProgressBar("#loadingBar");
163165

164166
return pdfjsLib.shadow(this, "loadingBar", bar);
165167
},
@@ -187,7 +189,7 @@ const PDFViewerApplication = {
187189
// Provides some basic debug information
188190
console.log(
189191
"PDF " +
190-
pdfDocument.fingerprint +
192+
pdfDocument.fingerprints[0] +
191193
" [" +
192194
info.PDFFormatVersion +
193195
" " +

web/ui_utils.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ const MAX_AUTO_SCALE = 1.25;
2323
const SCROLLBAR_PADDING = 40;
2424
const VERTICAL_PADDING = 5;
2525

26-
const LOADINGBAR_END_OFFSET_VAR = "--loadingBar-end-offset";
27-
2826
const RenderingStates = {
2927
INITIAL: 0,
3028
RUNNING: 1,
@@ -684,34 +682,35 @@ function clamp(v, min, max) {
684682
}
685683

686684
class ProgressBar {
687-
constructor(id, { height, width, units } = {}) {
685+
constructor(id) {
686+
if (
687+
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) &&
688+
arguments.length > 1
689+
) {
690+
throw new Error(
691+
"ProgressBar no longer accepts any additional options, " +
692+
"please use CSS rules to modify its appearance instead."
693+
);
694+
}
688695
this.visible = true;
689696

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

695-
// Get options, with sensible defaults.
696-
this.height = height || 100;
697-
this.width = width || 100;
698-
this.units = units || "%";
699-
700-
// Initialize heights.
701-
this.div.style.height = this.height + this.units;
702702
this.percent = 0;
703703
}
704704

705-
_updateBar() {
705+
#updateBar() {
706706
if (this._indeterminate) {
707707
this.div.classList.add("indeterminate");
708-
this.div.style.width = this.width + this.units;
709708
return;
710709
}
711-
712710
this.div.classList.remove("indeterminate");
713-
const progressSize = (this.width * this._percent) / 100;
714-
this.div.style.width = progressSize + this.units;
711+
712+
const doc = document.documentElement;
713+
doc.style.setProperty("--progressBar-percent", `${this._percent}%`);
715714
}
716715

717716
get percent() {
@@ -721,7 +720,7 @@ class ProgressBar {
721720
set percent(val) {
722721
this._indeterminate = isNaN(val);
723722
this._percent = clamp(val, 0, 100);
724-
this._updateBar();
723+
this.#updateBar();
725724
}
726725

727726
setWidth(viewer) {
@@ -732,7 +731,7 @@ class ProgressBar {
732731
const scrollbarWidth = container.offsetWidth - viewer.offsetWidth;
733732
if (scrollbarWidth > 0) {
734733
const doc = document.documentElement;
735-
doc.style.setProperty(LOADINGBAR_END_OFFSET_VAR, `${scrollbarWidth}px`);
734+
doc.style.setProperty("--progressBar-end-offset", `${scrollbarWidth}px`);
736735
}
737736
}
738737

web/viewer.css

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
--sidebar-transition-timing-function: ease;
2323
--scale-select-container-width: 140px;
2424
--scale-select-overflow: 22px;
25-
--loadingBar-end-offset: 0;
2625

2726
--toolbar-icon-opacity: 0.7;
2827
--doorhanger-icon-opacity: 0.9;
@@ -32,6 +31,8 @@
3231
/*#if !MOZCENTRAL*/
3332
--errorWrapper-bg-color: rgba(255, 110, 110, 1);
3433
/*#endif*/
34+
--progressBar-percent: 0%;
35+
--progressBar-end-offset: 0;
3536
--progressBar-color: rgba(10, 132, 255, 1);
3637
--progressBar-indeterminate-bg-color: rgba(221, 221, 222, 1);
3738
--progressBar-indeterminate-blend-color: rgba(116, 177, 239, 1);
@@ -344,7 +345,7 @@ select {
344345

345346
#loadingBar {
346347
position: absolute;
347-
inset-inline: 0 var(--loadingBar-end-offset);
348+
inset-inline: 0 var(--progressBar-end-offset);
348349
height: 4px;
349350
background-color: var(--body-bg-color);
350351
border-bottom: 1px solid var(--toolbar-border-color);
@@ -361,7 +362,7 @@ select {
361362
position: absolute;
362363
top: 0;
363364
left: 0;
364-
width: 0%;
365+
width: var(--progressBar-percent);
365366
height: 100%;
366367
background-color: var(--progressBar-color);
367368
overflow: hidden;
@@ -378,6 +379,7 @@ select {
378379
}
379380

380381
#loadingBar .progress.indeterminate {
382+
width: 100%;
381383
background-color: var(--progressBar-indeterminate-bg-color);
382384
transition: none;
383385
}

0 commit comments

Comments
 (0)