Skip to content

Commit a2ac595

Browse files
committed
WTableView: preloadMarginRows
1 parent c484002 commit a2ac595

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

src/Wt/WTableView.C

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ WTableView::WTableView()
5858
dropEvent_(impl_, "dropEvent"),
5959
scrolled_(impl_, "scrolled"),
6060
itemTouchSelectEvent_(impl_, "itemTouchSelectEvent"),
61+
preloadMarginRows_(-1),
6162
firstColumn_(-1),
6263
lastColumn_(-1),
6364
viewportLeft_(0),
@@ -685,10 +686,14 @@ void WTableView::renderTable(const int fr, const int lr,
685686
assert(lastRow() == lr && firstRow() == fr);
686687
assert(lastColumn() == lc && firstColumn() == fc);
687688

689+
const int marginHeight = preloadMarginRows_ == -1 ?
690+
viewportHeight_ / 2 :
691+
static_cast<int>(preloadMarginRows_ * rowHeight().toPixels()) / 2;
692+
688693
int scrollX1 = std::max(0, viewportLeft_ - viewportWidth_ / 2);
689694
int scrollX2 = viewportLeft_ + viewportWidth_ / 2;
690-
int scrollY1 = std::max(0, viewportTop_ - viewportHeight_ / 2);
691-
int scrollY2 = viewportTop_ + viewportHeight_ / 2;
695+
int scrollY1 = std::max(0, viewportTop_ - marginHeight);
696+
int scrollY2 = viewportTop_ + marginHeight;
692697

693698
WStringStream s;
694699

@@ -1640,22 +1645,25 @@ void WTableView::computeRenderedArea()
16401645

16411646
if (viewportHeight_ != -1) {
16421647
/* row range */
1643-
int top = std::min(viewportTop_,
1648+
const int top = std::min(viewportTop_,
16441649
static_cast<int>(canvas_->height().toPixels()));
16451650

1646-
int height = std::min(viewportHeight_,
1651+
const int height = std::min(viewportHeight_,
16471652
static_cast<int>(canvas_->height().toPixels()));
16481653

1649-
int renderedRows = static_cast<int>(height / rowHeight().toPixels()
1654+
const int renderedRows = static_cast<int>(height / rowHeight().toPixels()
16501655
+ 0.5);
16511656

16521657
renderedFirstRow_ = static_cast<int>(top / rowHeight().toPixels());
16531658

1659+
const int marginRows = preloadMarginRows_ == -1 ? renderedRows + borderRows : preloadMarginRows_;
1660+
16541661
renderedLastRow_
1655-
= std::min(renderedFirstRow_ + renderedRows * 2 + borderRows,
1656-
modelHeight - 1);
1662+
= static_cast<int>(std::min(static_cast<long long>(renderedFirstRow_) + renderedRows + marginRows,
1663+
static_cast<long long>(modelHeight - 1)));
16571664
renderedFirstRow_
1658-
= std::max(renderedFirstRow_ - renderedRows - borderRows, 0);
1665+
= static_cast<int>(std::max(static_cast<long long>(renderedFirstRow_) - marginRows,
1666+
0LL));
16591667
} else {
16601668
renderedFirstRow_ = 0;
16611669
renderedLastRow_ = modelHeight - 1;
@@ -2110,6 +2118,15 @@ void WTableView::setOverflow(Overflow overflow,
21102118
contentsContainer_->setOverflow(overflow, orientation);
21112119
}
21122120

2121+
void WTableView::setPreloadMarginRows(int rows)
2122+
{
2123+
preloadMarginRows_ = rows;
2124+
2125+
computeRenderedArea();
2126+
2127+
scheduleRerender(RenderState::NeedAdjustViewPort);
2128+
}
2129+
21132130
void WTableView::setRowHeaderCount(int count)
21142131
{
21152132
WAbstractItemView::setRowHeaderCount(count);

src/Wt/WTableView.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,25 @@ class WT_API WTableView : public WAbstractItemView
123123
WFlags<Orientation> orientation
124124
= (Orientation::Horizontal | Orientation::Vertical));
125125

126+
/*! \brief Sets the amount of rows to preload.
127+
*
128+
* Configures the amount of rows before and after the currently visible
129+
* range that should be loaded. If N rows are shown, and C is the margin,
130+
* then N + 2C rows are loaded: N visible, C rows before, and C rows after.
131+
*
132+
* Set to 0 if you don't want to load more rows than are currently visible.
133+
*
134+
* Set to -1 if you want to keep default behaviour. This means that if N rows
135+
* are visible, then 3N rows are loaded: N visible, N rows before, and N rows after.
136+
*/
137+
void setPreloadMarginRows(int rows);
138+
139+
/*! \brief Get the preload margin.
140+
*
141+
* \sa setPreloadMarginRows()
142+
*/
143+
int preloadMarginRows() const { return preloadMarginRows_; }
144+
126145
virtual void setHidden(bool hidden,
127146
const WAnimation& animation = WAnimation()) override;
128147

@@ -176,6 +195,8 @@ class WT_API WTableView : public WAbstractItemView
176195
Signals::connection touchMoveConnection_;
177196
Signals::connection touchEndConnection_;
178197

198+
int preloadMarginRows_;
199+
179200
/* Ajax only: First and last columns rendered (this somewhat
180201
* redundant with the state contained in the widgets, but because
181202
* columns are variable width, we cache these values as well). The

0 commit comments

Comments
 (0)