Skip to content

Commit

Permalink
Display the index of the currently active search result in the matche…
Browse files Browse the repository at this point in the history
…s counter of the findbar (issue 6993, bug 1062025)

For the `PDFFindBar` implementation, similar to the native Firefox findbar, the matches count displayed is now limited to a (hopefully) reasonable value.

*Please note:* In order to enable this feature in the `MOZCENTRAL` version, a follow-up patch will be required once this has landed in `mozilla-central`.
  • Loading branch information
Snuffleupagus committed Sep 8, 2018
1 parent 1fe35b9 commit 4c3acf2
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 29 deletions.
7 changes: 7 additions & 0 deletions l10n/en-US/viewer.properties
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ find_highlight=Highlight all
find_match_case_label=Match case
find_reached_top=Reached top of document, continued from bottom
find_reached_bottom=Reached end of document, continued from top
# LOCALIZATION NOTE (find_matches_count): "{{current}}" and "{{total}}" will be
# replaced by a number representing the index of the currently active find result,
# respectively a number representing the total number of matches in the document.
find_matches_count={{current}} of {{total}} matches
# LOCALIZATION NOTE (find_matches_count_limit): "{{limit}}" will be replaced by
# a numerical value.
find_matches_count_limit=More than {{limit}} matches
find_not_found=Phrase not found

# Error panel labels
Expand Down
7 changes: 7 additions & 0 deletions l10n/sv-SE/viewer.properties
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ find_highlight=Markera alla
find_match_case_label=Matcha versal/gemen
find_reached_top=Nådde början av dokumentet, började från slutet
find_reached_bottom=Nådde slutet på dokumentet, började från början
# LOCALIZATION NOTE (find_matches_count): "{{current}}" and "{{total}}" will be
# replaced by a number representing the index of the currently active find result,
# respectively a number representing the total number of matches in the document.
find_matches_count={{current}} av {{total}} matchande
# LOCALIZATION NOTE (find_matches_count_limit): "{{limit}}" will be replaced by
# a numerical value.
find_matches_count_limit=Fler än {{limit}} matchningar
find_not_found=Frasen hittades inte

# Error panel labels
Expand Down
13 changes: 8 additions & 5 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const FORCE_PAGES_LOADED_TIMEOUT = 10000; // ms

const DefaultExternalServices = {
updateFindControlState(data) {},
updateFindMatchesCount(data) {},
initPassiveLoading(callbacks) {},
fallback(data, callback) {},
reportTelemetry(data) {},
Expand Down Expand Up @@ -349,20 +350,22 @@ let PDFViewerApplication = {
pdfViewer: this.pdfViewer,
eventBus,
});
this.findController.onUpdateResultsCount = (matchCount) => {
this.findController.onUpdateResultsCount = (matchesCount) => {
if (this.supportsIntegratedFind) {
return;
this.externalServices.updateFindMatchesCount(matchesCount);
} else {
this.findBar.updateResultsCount(matchesCount);
}
this.findBar.updateResultsCount(matchCount);
};
this.findController.onUpdateState = (state, previous, matchCount) => {
this.findController.onUpdateState = (state, previous, matchesCount) => {
if (this.supportsIntegratedFind) {
this.externalServices.updateFindControlState({
result: state,
findPrevious: previous,
matchesCount,
});
} else {
this.findBar.updateUIState(state, previous, matchCount);
this.findBar.updateUIState(state, previous, matchesCount);
}
};

Expand Down
4 changes: 4 additions & 0 deletions web/firefoxcom.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ PDFViewerApplication.externalServices = {
FirefoxCom.request('updateFindControlState', data);
},

updateFindMatchesCount(data) {
// FirefoxCom.request('updateFindMatchesCount', data);
},

initPassiveLoading(callbacks) {
let pdfDataRangeTransport;

Expand Down
40 changes: 25 additions & 15 deletions web/pdf_find_bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import { FindState } from './pdf_find_controller';
import { NullL10n } from './ui_utils';

const MATCHES_COUNT_LIMIT = 1000;

/**
* Creates a "search bar" given a set of DOM elements that act as controls
* for searching or for setting search preferences in the UI. This object
Expand Down Expand Up @@ -102,7 +104,7 @@ class PDFFindBar {
});
}

updateUIState(state, previous, matchCount) {
updateUIState(state, previous, matchesCount) {
let notFound = false;
let findMsg = '';
let status = '';
Expand Down Expand Up @@ -143,26 +145,34 @@ class PDFFindBar {
this._adjustWidth();
});

this.updateResultsCount(matchCount);
this.updateResultsCount(matchesCount);
}

updateResultsCount(matchCount) {
updateResultsCount({ current, total, }) {
if (!this.findResultsCount) {
return; // No UI control is provided.
}

if (!matchCount) {
// If there are no matches, hide and reset the counter.
this.findResultsCount.classList.add('hidden');
this.findResultsCount.textContent = '';
} else {
// Update and show the match counter.
this.findResultsCount.textContent = matchCount.toLocaleString();
this.findResultsCount.classList.remove('hidden');
let matchesCountMsg = '';

if (total) {
if (total > MATCHES_COUNT_LIMIT) {
matchesCountMsg = this.l10n.get('find_matches_count_limit', {
limit: MATCHES_COUNT_LIMIT.toLocaleString(),
}, 'More than {{limit}} matches');
} else {
matchesCountMsg = this.l10n.get('find_matches_count', {
current: current.toLocaleString(),
total: total.toLocaleString(),
}, '{{current}} of {{total}} matches');
}
}
// Since `updateResultsCount` may be called from `PDFFindController`,
// ensure that the width of the findbar is always updated correctly.
this._adjustWidth();
Promise.resolve(matchesCountMsg).then((msg) => {
this.findResultsCount.textContent = msg;
this.findResultsCount.classList[!total ? 'add' : 'remove']('hidden');
// Since `updateResultsCount` may be called from `PDFFindController`,
// ensure that the width of the findbar is always updated correctly.
this._adjustWidth();
});
}

open() {
Expand Down
33 changes: 25 additions & 8 deletions web/pdf_find_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class PDFFindController {
this.pageContents = []; // Stores the text for each page.
this.pageMatches = [];
this.pageMatchesLength = null;
this.matchCount = 0;
this.matchesCountTotal = 0;
this.selected = { // Currently selected match.
pageIdx: -1,
matchIdx: -1,
Expand Down Expand Up @@ -269,8 +269,9 @@ class PDFFindController {
}

// Update the match count.
if (this.pageMatches[pageIndex].length > 0) {
this.matchCount += this.pageMatches[pageIndex].length;
const pageMatchesCount = this.pageMatches[pageIndex].length;
if (pageMatchesCount > 0) {
this.matchesCountTotal += pageMatchesCount;
this._updateUIResultsCount();
}
}
Expand Down Expand Up @@ -338,7 +339,7 @@ class PDFFindController {
this.hadMatch = false;
this.resumePageIdx = null;
this.pageMatches = [];
this.matchCount = 0;
this.matchesCountTotal = 0;
this.pageMatchesLength = null;

for (let i = 0; i < numPages; i++) {
Expand Down Expand Up @@ -475,16 +476,32 @@ class PDFFindController {
}
}

_requestMatchesCount() {
const { pageIdx, matchIdx, } = this.selected;
let current = 0;
if (matchIdx !== -1) {
for (let i = 0; i < pageIdx; i++) {
current += (this.pageMatches[i] && this.pageMatches[i].length) || 0;
}
current += matchIdx + 1;
}
return { current, total: this.matchesCountTotal, };
}

_updateUIResultsCount() {
if (this.onUpdateResultsCount) {
this.onUpdateResultsCount(this.matchCount);
if (!this.onUpdateResultsCount) {
return;
}
const matchesCount = this._requestMatchesCount();
this.onUpdateResultsCount(matchesCount);
}

_updateUIState(state, previous) {
if (this.onUpdateState) {
this.onUpdateState(state, previous, this.matchCount);
if (!this.onUpdateState) {
return;
}
const matchesCount = this._requestMatchesCount();
this.onUpdateState(state, previous, matchesCount);
}
}

Expand Down
2 changes: 1 addition & 1 deletion web/viewer.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@
<label for="findHighlightAll" class="toolbarLabel" data-l10n-id="find_highlight">Highlight all</label>
<input type="checkbox" id="findMatchCase" class="toolbarField" tabindex="95">
<label for="findMatchCase" class="toolbarLabel" data-l10n-id="find_match_case_label">Match case</label>
<span id="findResultsCount" class="toolbarLabel hidden"></span>
</div>

<div id="findbarMessageContainer">
<span id="findResultsCount" class="toolbarLabel hidden"></span>
<span id="findMsg" class="toolbarLabel"></span>
</div>
</div> <!-- findbar -->
Expand Down

0 comments on commit 4c3acf2

Please sign in to comment.