Skip to content

Commit d22e62e

Browse files
committed
setPreloadMargin: deal with extremes better
1 parent 199e684 commit d22e62e

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

src/Wt/WTableView.C

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "Wt/WTable.h"
2020
#include "Wt/WTheme.h"
2121

22+
#include "WebUtils.h"
23+
2224
#ifndef WT_DEBUG_JS
2325

2426
#include "js/WTableView.min.js"
@@ -687,21 +689,25 @@ void WTableView::renderTable(const int fr, const int lr,
687689
assert(lastRow() == lr && firstRow() == fr);
688690
assert(lastColumn() == lc && firstColumn() == fc);
689691

690-
const int marginTop = static_cast<int>(std::round((preloadMargin(Side::Top).isAuto() ? viewportHeight_ : preloadMargin(Side::Top).toPixels()) / 2));
691-
const int marginBottom = static_cast<int>(std::round((preloadMargin(Side::Bottom).isAuto() ? viewportHeight_ : preloadMargin(Side::Bottom).toPixels()) / 2));
692-
const int marginLeft = static_cast<int>(std::round((preloadMargin(Side::Left).isAuto() ? viewportWidth_ : preloadMargin(Side::Left).toPixels()) / 2));
693-
const int marginRight = static_cast<int>(std::round((preloadMargin(Side::Right).isAuto() ? viewportWidth_ : preloadMargin(Side::Right).toPixels()) / 2));
692+
const double marginTop = (preloadMargin(Side::Top).isAuto() ? viewportHeight_ : preloadMargin(Side::Top).toPixels()) / 2;
693+
const double marginBottom = (preloadMargin(Side::Bottom).isAuto() ? viewportHeight_ : preloadMargin(Side::Bottom).toPixels()) / 2;
694+
const double marginLeft = (preloadMargin(Side::Left).isAuto() ? viewportWidth_ : preloadMargin(Side::Left).toPixels()) / 2;
695+
const double marginRight = (preloadMargin(Side::Right).isAuto() ? viewportWidth_ : preloadMargin(Side::Right).toPixels()) / 2;
694696

695-
int scrollX1 = std::max(0, viewportLeft_ - marginLeft);
696-
int scrollX2 = viewportLeft_ + marginRight;
697-
int scrollY1 = std::max(0, viewportTop_ - marginTop);
698-
int scrollY2 = viewportTop_ + marginBottom;
697+
const double scrollX1 = round(std::max(0.0, viewportLeft_ - marginLeft));
698+
const double scrollX2 = round(viewportLeft_ + marginRight);
699+
const double scrollY1 = round(std::max(0.0, viewportTop_ - marginTop));
700+
const double scrollY2 = round(viewportTop_ + marginBottom);
699701

700702
WStringStream s;
701703

702-
s << "jQuery.data(" << jsRef() << ", 'obj').scrolled("
703-
<< scrollX1 << ", " << scrollX2 << ", " << scrollY1 << ", " << scrollY2
704-
<< ");";
704+
char buf[30];
705+
706+
s << "jQuery.data(" << jsRef() << ", 'obj').scrolled(";
707+
s << Utils::round_js_str(scrollX1, 3, buf) << ", ";
708+
s << Utils::round_js_str(scrollX2, 3, buf) << ", ";
709+
s << Utils::round_js_str(scrollY1, 3, buf) << ", ";
710+
s << Utils::round_js_str(scrollY2, 3, buf) << ");";
705711

706712
doJavaScript(s.str());
707713
}
@@ -1652,22 +1658,20 @@ void WTableView::computeRenderedArea()
16521658
const int height = std::min(viewportHeight_,
16531659
static_cast<int>(canvas_->height().toPixels()));
16541660

1655-
const int renderedRows = static_cast<int>(std::ceil(height / rowHeight().toPixels()));
1661+
const double renderedRows = height / rowHeight().toPixels();
16561662

1657-
const int renderedRowsAbove = preloadMargin(Side::Top).isAuto() ? renderedRows + borderRows :
1658-
static_cast<int>(std::round(preloadMargin(Side::Top).toPixels() / rowHeight().toPixels()));
1663+
const double renderedRowsAbove = preloadMargin(Side::Top).isAuto() ? renderedRows + borderRows :
1664+
preloadMargin(Side::Top).toPixels() / rowHeight().toPixels();
16591665

1660-
const int renderedRowsBelow = preloadMargin(Side::Bottom).isAuto() ? renderedRows + borderRows :
1661-
static_cast<int>(std::round(preloadMargin(Side::Bottom).toPixels() / rowHeight().toPixels()));
1666+
const double renderedRowsBelow = preloadMargin(Side::Bottom).isAuto() ? renderedRows + borderRows :
1667+
preloadMargin(Side::Bottom).toPixels() / rowHeight().toPixels();
16621668

1663-
renderedFirstRow_ = static_cast<int>(top / rowHeight().toPixels());
1669+
renderedFirstRow_ = static_cast<int>(std::floor(top / rowHeight().toPixels()));
16641670

16651671
renderedLastRow_
1666-
= static_cast<int>(std::min(static_cast<long long>(renderedFirstRow_) + renderedRows + renderedRowsBelow,
1667-
static_cast<long long>(modelHeight - 1)));
1672+
= static_cast<int>(std::ceil(std::min(renderedFirstRow_ + renderedRows + renderedRowsBelow, modelHeight - 1.0)));
16681673
renderedFirstRow_
1669-
= static_cast<int>(std::max(static_cast<long long>(renderedFirstRow_) - renderedRowsAbove,
1670-
static_cast<long long>(0)));
1674+
= static_cast<int>(std::floor(std::max(renderedFirstRow_ - renderedRowsAbove, 0.0)));
16711675
} else {
16721676
renderedFirstRow_ = 0;
16731677
renderedLastRow_ = modelHeight - 1;
@@ -1677,21 +1681,20 @@ void WTableView::computeRenderedArea()
16771681
--renderedFirstRow_;
16781682

16791683
const int borderColumnPixels = 200;
1680-
const int marginLeft = static_cast<int>(std::round(preloadMargin(Side::Left).isAuto() ? viewportWidth_ + borderColumnPixels : preloadMargin(Side::Left).toPixels()));
1681-
const int marginRight = static_cast<int>(std::round(preloadMargin(Side::Right).isAuto() ? viewportWidth_ + borderColumnPixels : preloadMargin(Side::Right).toPixels()));
1684+
const double marginLeft = preloadMargin(Side::Left).isAuto() ? viewportWidth_ + borderColumnPixels : preloadMargin(Side::Left).toPixels();
1685+
const double marginRight = preloadMargin(Side::Right).isAuto() ? viewportWidth_ + borderColumnPixels : preloadMargin(Side::Right).toPixels();
16821686

16831687
/* column range */
16841688
int left
1685-
= static_cast<int>(std::max(static_cast<long long>(0),
1686-
static_cast<long long>(viewportLeft_) - marginLeft));
1689+
= static_cast<int>(std::floor(std::max(0.0, viewportLeft_ - marginLeft)));
16871690
int right
1688-
= static_cast<int>(std::min(static_cast<long long>(std::max(static_cast<int>(canvas_->width().toPixels()),
1689-
viewportWidth_)), // When a column was made wider, and the
1690-
// canvas is narrower than the viewport,
1691-
// the size of the canvas will not have
1692-
// been updated yet, so we use the viewport
1693-
// width instead.
1694-
static_cast<long long>(viewportLeft_) + viewportWidth_ + marginRight));
1691+
= static_cast<int>(std::ceil(std::min(std::max(canvas_->width().toPixels(),
1692+
viewportWidth_ * 1.0), // When a column was made wider, and the
1693+
// canvas is narrower than the viewport,
1694+
// the size of the canvas will not have
1695+
// been updated yet, so we use the viewport
1696+
// width instead.
1697+
viewportLeft_ + viewportWidth_ + marginRight)));
16951698

16961699
int total = 0;
16971700
renderedFirstColumn_ = rowHeaderCount();

0 commit comments

Comments
 (0)