Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cpp/adapter/ScrollTargets.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <functional>
#include <optional>

#include "adapter/ListLayout.h"
#include "core/ListLayout.h"

namespace flume {

Expand Down
68 changes: 34 additions & 34 deletions cpp/adapter/ListLayout.cpp → cpp/core/ListLayout.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "adapter/ListLayout.h"
#include "core/ListLayout.h"

#include <algorithm>

Expand Down Expand Up @@ -82,8 +82,8 @@ LengthChange ListLayout::setLength(int index, float length, int anchor, float vi
// Justified short content moves with its slack, which the offset must not chase. Stacking from the end wins.
const bool recenters = justified() && !config_.stackFromEnd;
const auto edge = [&] { return anchorEdge(rowOf_[anchor], viewportLength) - (recenters ? slack(viewportLength) : 0); };
const float before = hasAnchor ? edge() : 0;
const float contentBefore = contentLength(viewportLength);
const double before = hasAnchor ? edge() : 0;
const double contentBefore = contentLength(viewportLength);
Item& item = items_[index];
Mean& mean = means_[item.type];
if (item.measured) {
Expand All @@ -95,7 +95,7 @@ LengthChange ListLayout::setLength(int index, float length, int anchor, float vi
item.length = length;
item.measured = true;
updateRow(rowOf_[index]);
const float moved = hasAnchor ? edge() - before : 0;
const double moved = hasAnchor ? edge() - before : 0;
return LengthChange{moved, contentLength(viewportLength) - contentBefore};
}

Expand All @@ -118,15 +118,15 @@ float ListLayout::estimate(int type) const {
return std::max(config_.estimateFloor, static_cast<float>(it->second.sum / it->second.count));
}

float ListLayout::contentLength(float viewportLength) const {
const float length = config_.paddingStart + rowStart(static_cast<int>(rows_.size())) + config_.paddingEnd;
return bottomAnchored() || justified() ? std::max(viewportLength, length) : length;
double ListLayout::contentLength(float viewportLength) const {
const double length = config_.paddingStart + rowStart(static_cast<int>(rows_.size())) + config_.paddingEnd;
return bottomAnchored() || justified() ? std::max(static_cast<double>(viewportLength), length) : length;
}

std::pair<float, float> ListLayout::contentSpan(float viewportLength) const {
const float length = config_.paddingStart + rowStart(static_cast<int>(rows_.size())) + config_.paddingEnd;
std::pair<double, double> ListLayout::contentSpan(float viewportLength) const {
const double length = config_.paddingStart + rowStart(static_cast<int>(rows_.size())) + config_.paddingEnd;
if (!config_.inverted) return {slack(viewportLength), slack(viewportLength) + length};
const float end = contentLength(viewportLength);
const double end = contentLength(viewportLength);
return {end - length, end};
}

Expand All @@ -145,7 +145,7 @@ float ListLayout::laneLength(float listCrossLength) const {
ItemRect ListLayout::rectOf(int index, float listCrossLength, float viewportLength) const {
if (index < 0 || index >= count()) return ItemRect{0, 0, 0, 0};
const int row = rowOf_[index];
const float main = startOf(row, viewportLength);
const double main = startOf(row, viewportLength);
const float cross = crossLength(index, listCrossLength);
const float lane = static_cast<float>(index - rows_[row].first);
const float crossAt =
Expand All @@ -155,17 +155,17 @@ ItemRect ListLayout::rectOf(int index, float listCrossLength, float viewportLeng
return ItemRect{crossAt, main, cross, length};
}

int ListLayout::indexAt(float offset, float viewportLength) const {
int ListLayout::indexAt(double offset, float viewportLength) const {
if (rows_.empty()) return -1;
return rows_[rowAt(offset, viewportLength)].first;
}

// The first row that starts in the viewport, or the last that ends in it when bottom-anchored; a cut row grows unseen.
int ListLayout::anchorAt(float viewportStart, float viewportLength) const {
int ListLayout::anchorAt(double viewportStart, float viewportLength) const {
if (rows_.empty()) return -1;
const int last = static_cast<int>(rows_.size()) - 1;
if (bottomAnchored()) {
const float end = viewportStart + viewportLength;
const double end = viewportStart + viewportLength;
const int row = rowAt(end - kEdgeSlop, viewportLength);
if (anchorEdge(row, viewportLength) <= end + kEdgeSlop) return rows_[row].first;
// The row above it: the next one inverted, the previous one upright.
Expand All @@ -176,26 +176,26 @@ int ListLayout::anchorAt(float viewportStart, float viewportLength) const {
return rows_[cut && row < last ? row + 1 : row].first;
}

float ListLayout::anchorEdgeOf(int index, float viewportLength) const {
double ListLayout::anchorEdgeOf(int index, float viewportLength) const {
if (index < 0 || index >= count()) return 0;
return anchorEdge(rowOf_[index], viewportLength);
}

std::vector<int> ListLayout::itemsIn(float start, float end, float viewportLength) const {
std::vector<int> ListLayout::itemsIn(double start, double end, float viewportLength) const {
std::vector<int> out;
if (rows_.empty()) return out;
const int a = rowAt(start, viewportLength);
const int b = rowAt(end, viewportLength);
for (int row = std::min(a, b); row <= std::max(a, b); row++) {
// An inverted row's separator sits above it; either way a row whose separator shows is listed.
const float rowFrom = startOf(row, viewportLength) - (config_.inverted ? rowStride(row) - rows_[row].length : 0);
const double rowFrom = startOf(row, viewportLength) - (config_.inverted ? rowStride(row) - rows_[row].length : 0);
if (rowFrom >= end || rowFrom + rowStride(row) <= start) continue;
for (int i = 0; i < rows_[row].count; i++) out.push_back(rows_[row].first + i);
}
return out;
}

std::optional<Pinned> ListLayout::pinned(const std::vector<int>& sticky, float viewportStart,
std::optional<Pinned> ListLayout::pinned(const std::vector<int>& sticky, double viewportStart,
float viewportLength) const {
const int top = indexAt(viewportStart, viewportLength);
if (top < 0) return std::nullopt;
Expand All @@ -210,7 +210,7 @@ std::optional<Pinned> ListLayout::pinned(const std::vector<int>& sticky, float v
if (s < 0 || s >= count()) continue;
if (config_.inverted ? s < *index && (!pusher || s > *pusher) : s > *index && (!pusher || s < *pusher)) pusher = s;
}
float start = viewportStart;
double start = viewportStart;
if (pusher) {
// The pinned row ends at the pusher with the line between them, which is its own upright and the pusher's inverted.
const float line = lineAfter(rowOf_[config_.inverted ? *pusher : *index]);
Expand All @@ -224,7 +224,7 @@ std::optional<ItemRect> ListLayout::separatorRect(int index, float listCrossLeng
const int row = rowOf_[index];
if (!gapAfter(row)) return std::nullopt;
const ItemRect item = rectOf(index, listCrossLength, 0);
const float crossAt = config_.horizontal ? item.y : item.x;
const float crossAt = static_cast<float>(config_.horizontal ? item.y : item.x);
// Each cell draws its share of the padded width, so the row's line runs across the column gaps and lane insets.
const float padded = std::max(0.f, listCrossLength - config_.paddingCrossStart - config_.paddingCrossEnd);
const bool alone = fullSpan(index);
Expand All @@ -239,17 +239,17 @@ std::optional<ItemRect> ListLayout::separatorRect(int index, float listCrossLeng
return ItemRect{from, main, to - from, config_.separator};
}

float ListLayout::emptyMinLength(float viewportLength) const {
double ListLayout::emptyMinLength(float viewportLength) const {
return config_.emptyIndex >= 0 && config_.emptyIndex < count() ? growShare(viewportLength) : 0;
}

float ListLayout::growMinLength(float viewportLength) const {
double ListLayout::growMinLength(float viewportLength) const {
return growsFooter() ? growShare(viewportLength) : 0;
}

bool ListLayout::growsFooter() const { return config_.growFooter && config_.afterData > 0 && count() > 0; }

float ListLayout::growShare(float viewportLength) const {
double ListLayout::growShare(float viewportLength) const {
int growers = 0;
float grown = 0;
for (int index : {config_.emptyIndex, growsFooter() ? count() - 1 : -1}) {
Expand All @@ -258,8 +258,8 @@ float ListLayout::growShare(float viewportLength) const {
grown += rows_[rowOf_[index]].length;
}
if (growers == 0) return 0;
const float used = config_.paddingStart + config_.paddingEnd + rowStart(static_cast<int>(rows_.size())) - grown;
return std::max(0.f, viewportLength - used) / static_cast<float>(growers);
const double used = config_.paddingStart + config_.paddingEnd + rowStart(static_cast<int>(rows_.size())) - grown;
return std::max(0.0, viewportLength - used) / static_cast<double>(growers);
}

bool ListLayout::fullSpan(int index) const { return std::binary_search(fullSpan_.begin(), fullSpan_.end(), index); }
Expand Down Expand Up @@ -310,29 +310,29 @@ float ListLayout::rowStride(int row) const {
return rows_[row].length + data + main;
}

float ListLayout::rowStart(int row) const {
double ListLayout::rowStart(int row) const {
for (; dirtyFrom_ < row; dirtyFrom_++) starts_[dirtyFrom_ + 1] = starts_[dirtyFrom_] + rowStride(dirtyFrom_);
return starts_[row];
}

float ListLayout::slack(float viewportLength) const {
double ListLayout::slack(float viewportLength) const {
if (!config_.stackFromEnd && !justified()) return 0;
const float length = config_.paddingStart + rowStart(static_cast<int>(rows_.size())) + config_.paddingEnd;
const float free = std::max(0.f, viewportLength - length);
const double length = config_.paddingStart + rowStart(static_cast<int>(rows_.size())) + config_.paddingEnd;
const double free = std::max(0.0, viewportLength - length);
return config_.stackFromEnd || config_.justify == ListLayoutConfig::Justify::End ? free : free / 2;
}

float ListLayout::startOf(int row, float viewportLength) const {
double ListLayout::startOf(int row, float viewportLength) const {
if (!config_.inverted) return config_.paddingStart + slack(viewportLength) + rowStart(row);
return contentLength(viewportLength) - config_.paddingEnd - rowStart(row) - rows_[row].length;
}

float ListLayout::anchorEdge(int row, float viewportLength) const {
const float start = startOf(row, viewportLength);
double ListLayout::anchorEdge(int row, float viewportLength) const {
const double start = startOf(row, viewportLength);
return bottomAnchored() ? start + rows_[row].length : start;
}

int ListLayout::rowAt(float offset, float viewportLength) const {
int ListLayout::rowAt(double offset, float viewportLength) const {
const int rows = static_cast<int>(rows_.size());
rowStart(rows);
if (!config_.inverted) {
Expand All @@ -341,7 +341,7 @@ int ListLayout::rowAt(float offset, float viewportLength) const {
return std::max(0, static_cast<int>(it - starts_.begin()) - 1);
}
// Inverted rows run up from the bottom: the row whose end, measured from item 0's bottom, reaches past the offset.
const float fromBottom = contentLength(viewportLength) - config_.paddingEnd - offset;
const double fromBottom = contentLength(viewportLength) - config_.paddingEnd - offset;
auto it = std::lower_bound(starts_.begin() + 1, starts_.begin() + rows + 1, fromBottom);
return std::min(rows - 1, static_cast<int>(it - (starts_.begin() + 1)));
}
Expand Down
42 changes: 21 additions & 21 deletions cpp/adapter/ListLayout.h → cpp/core/ListLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ inline float paddingOf(float points, float fraction, float listWidth) {
}

struct ItemRect {
float x, y, width, height;
double x, y, width, height;
bool operator==(const ItemRect&) const = default;
};

struct Pinned {
int index;
float start; // along the scroll axis, in content coordinates
double start; // along the scroll axis, in content coordinates
};

struct IndexRange {
Expand All @@ -58,11 +58,11 @@ struct IndexRange {
};

struct LengthChange {
float anchorMoved; // how far the anchor's edge moved along the scroll axis
float contentMoved; // how far the content length moved
double anchorMoved; // how far the anchor's edge moved along the scroll axis
double contentMoved; // how far the content length moved
};

// Item positions along the scroll axis from measured lengths and per-type estimates; units are points.
// Item positions along the scroll axis from measured lengths and per-type estimates, in the platform units: points on iOS, px on Android.
class ListLayout {
public:
using TypeOf = std::function<int(int index)>;
Expand All @@ -83,21 +83,21 @@ class ListLayout {
IndexRange itemsOfRows(int firstRow, int lastRow) const;
bool measured(int index) const;
float estimate(int type) const;
float contentLength(float viewportLength) const;
double contentLength(float viewportLength) const;
// From the start of the padding to the end of it, at the bottom of a short list that sits there.
std::pair<float, float> contentSpan(float viewportLength) const;
std::pair<double, double> contentSpan(float viewportLength) const;
float crossLength(int index, float listCrossLength) const; // the size a cell binds at across the scroll axis
float laneLength(float listCrossLength) const; // one column of a grid, the whole width with one lane
ItemRect rectOf(int index, float listCrossLength, float viewportLength) const;
int indexAt(float offset, float viewportLength) const;
int indexAt(double offset, float viewportLength) const;
// The item whose edge stays put while rows measure, for setLength.
int anchorAt(float viewportStart, float viewportLength) const;
int anchorAt(double viewportStart, float viewportLength) const;
// The edge that stays in place, its bottom when inverted or stacking from the end, in content coordinates.
float anchorEdgeOf(int index, float viewportLength) const;
std::vector<int> itemsIn(float start, float end, float viewportLength) const;
float emptyMinLength(float viewportLength) const; // the viewport the padding and the other rows leave
float growMinLength(float viewportLength) const; // the same for a growing footer
std::optional<Pinned> pinned(const std::vector<int>& sticky, float viewportStart, float viewportLength) const;
double anchorEdgeOf(int index, float viewportLength) const;
std::vector<int> itemsIn(double start, double end, float viewportLength) const;
double emptyMinLength(float viewportLength) const; // the viewport the padding and the other rows leave
double growMinLength(float viewportLength) const; // the same for a growing footer
std::optional<Pinned> pinned(const std::vector<int>& sticky, double viewportStart, float viewportLength) const;
// Relative to the item's origin: the gap after its row in content order, none after the last row.
std::optional<ItemRect> separatorRect(int index, float listCrossLength) const;
// Inverted or stacked from the end: the list keeps its end where it was.
Expand Down Expand Up @@ -127,22 +127,22 @@ class ListLayout {
bool gapAfter(int row) const;
bool isData(int index) const; // not the header, the footer or the empty row
float lineAfter(int row) const; // the separator after the row, 0 when it has none
float rowStart(int row) const; // from the start of item 0, inside the padding
float slack(float viewportLength) const; // the space short content leaves above its rows: all of it at the end, half centered
double rowStart(int row) const; // from the start of item 0, inside the padding
double slack(float viewportLength) const; // the space short content leaves above its rows: all of it at the end, half centered
bool growsFooter() const;
// What the other rows leave of the viewport, split evenly between the empty row and a growing footer, as two flex: 1 rows.
float growShare(float viewportLength) const;
float startOf(int row, float viewportLength) const; // in content coordinates
float anchorEdge(int row, float viewportLength) const;
int rowAt(float offset, float viewportLength) const; // offset in content coordinates
double growShare(float viewportLength) const;
double startOf(int row, float viewportLength) const; // in content coordinates
double anchorEdge(int row, float viewportLength) const;
int rowAt(double offset, float viewportLength) const; // offset in content coordinates

ListLayoutConfig config_;
std::vector<int> fullSpan_;
std::vector<Item> items_;
std::unordered_map<int, Mean> means_;
std::vector<Row> rows_;
std::vector<int> rowOf_;
mutable std::vector<float> starts_; // rows_.size() + 1 prefix sums, valid up to dirtyFrom_
mutable std::vector<double> starts_; // rows_.size() + 1 prefix sums, valid up to dirtyFrom_
mutable int dirtyFrom_ = 0;
};

Expand Down
File renamed without changes.
File renamed without changes.
13 changes: 12 additions & 1 deletion cpp/tests/list_layout_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "doctest.h"

#include "adapter/ListLayout.h"
#include "core/ListLayout.h"
#include "core/KeyDiff.h"

using namespace flume;
Expand Down Expand Up @@ -699,3 +699,14 @@ TEST_CASE("stacked from the end, centered content keeps its anchor's bottom wher
CHECK(layout.setLength(0, 200, 2, 600).anchorMoved == 0);
CHECK(layout.rectOf(2, 400, 600).y == 500);
}

TEST_CASE("positions stay exact past 2^24") {
ListLayout layout;
ListLayoutConfig config;
config.estimatedItemLength = 101;
layout.configure(config, {});
layout.reset(200000, [](int) { return 0; });
CHECK(layout.rectOf(166113, 400, 800).y == 16777413.0);
CHECK(layout.contentLength(800) == 20200000.0);
CHECK(layout.indexAt(16777413.0 + 50, 800) == 166113);
}
2 changes: 1 addition & 1 deletion cpp/tests/list_rows_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "doctest.h"

#include "adapter/ListRows.h"
#include "core/ListRows.h"

using namespace flume;

Expand Down
2 changes: 1 addition & 1 deletion ios/FLMLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <vector>

#include "adapter/ContentAnchor.h"
#include "adapter/ListLayout.h"
#include "core/ListLayout.h"
#include "adapter/ScrollTargets.h"
#include "adapter/Viewability.h"

Expand Down
2 changes: 1 addition & 1 deletion ios/FLMView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "adapter/BatchUpdates.h"
#include "adapter/EdgeTracker.h"
#include "adapter/JumpGate.h"
#include "adapter/ListRows.h"
#include "core/ListRows.h"
#include "adapter/ScrollCommands.h"
#include "adapter/ScrollTargets.h"
#include "adapter/Viewability.h"
Expand Down
Loading