Bug 1657763 - Choose the closest page to the center of the scroll port as the "current" page. r=emilio

Unfortunately there is no particular way to write automated tests without
exposing a bunch of metrics in the print preview window, such as each
PrintedSheetFrame height, the gap length between the sheets, print preview
scale, the scroll port rect, the current scroll position, etc. etc. So I just
tested this change with the frontend change to show the current page number
there by :mstriemer. [1]

[1] https://phabricator.services.mozilla.com/D86427

Differential Revision: https://phabricator.services.mozilla.com/D87203
This commit is contained in:
Hiroyuki Ikezoe 2020-08-17 20:54:06 +00:00
Родитель cd681f2862
Коммит 1bfd4639dc
2 изменённых файлов: 34 добавлений и 5 удалений

Просмотреть файл

@ -3341,17 +3341,40 @@ nsDocumentViewer::GetPrintPreviewCurrentPageNumber(int32_t* aNumber) {
}
nsPoint currentScrollPosition = sf->GetScrollPosition();
float halfwayPoint =
currentScrollPosition.y + float(sf->GetScrollPortRect().height) / 2.0f;
float lastDistanceFromHalfwayPoint = std::numeric_limits<float>::max();
*aNumber = 0;
float previewScale = seqFrame->GetPrintPreviewScale();
for (const nsIFrame* sheetFrame : seqFrame->PrincipalChildList()) {
nsRect sheetRect = sheetFrame->GetRect();
(*aNumber)++;
nsRect pageRect = sheetFrame->GetRect();
if (pageRect.YMost() * previewScale > currentScrollPosition.y) {
// This is the first visible page even if the visible rect is just 1px
// height.
// TODO: We should have a reasonable threshold.
float bottomOfSheet = sheetRect.YMost() * previewScale;
if (bottomOfSheet < halfwayPoint) {
// If the bottom of the page is not yet over the halfway point, iterate
// the next frame to see if the next frame is over the halfway point and
// compare the distance from the halfway point.
lastDistanceFromHalfwayPoint = halfwayPoint - bottomOfSheet;
continue;
}
float topOfSheet = sheetRect.Y() * previewScale;
if (topOfSheet <= halfwayPoint) {
// If the top of the page is not yet over the halfway point or on the
// point, it's the current page.
break;
}
// Now the page rect is completely over the halfway point, compare the
// distances from the halfway point.
if ((topOfSheet - halfwayPoint) >= lastDistanceFromHalfwayPoint) {
// If the previous page distance is less than or equal to the current page
// distance, choose the previous one as the current.
(*aNumber)--;
MOZ_ASSERT(*aNumber > 0);
}
break;
}
MOZ_ASSERT(*aNumber <= pageCount);

Просмотреть файл

@ -543,6 +543,12 @@ async function runTest20() {
is(gWbp.printPreviewCurrentPageNumber, 1,
"The initial current page number should be 1");
// Scroll to the second page.
gWbp.printPreviewScrollToPage(Ci.nsIWebBrowserPrint.PRINTPREVIEW_GOTO_PAGENUM, 2);
is(gWbp.printPreviewCurrentPageNumber, 2,
"The current page number should be 2");
// Scroll to the last page.
gWbp.printPreviewScrollToPage(Ci.nsIWebBrowserPrint.PRINTPREVIEW_END, 0);