The Overview address list built all N rows on every frame (icons, labels, balances, buttons, an InvisibleButton and a context-menu check per address) and allocated a vector of N row positions, however few rows fit on screen; the edge mask then walked all their vertices. An ImGuiListClipper now submits only the rows that intersect the list's clip rect, plus: - one guard row on each side, so dividers and anti-aliased edges at the clip boundary are drawn as before; - the row being dragged and the row whose context menu is open, so drag-to-reorder and the menu keep working when they scroll away; - the rows keyboard navigation targets (kept by the clipper itself). Before each row the cursor is put back exactly where the unclipped loop left it, so the density-scaled spacing, which depends on the cursor position, and every row's geometry are unchanged. Row tops are computed from the index, so the per-frame vector is gone, and the scroll extent is the same. The row model (sort, filter) is still rebuilt over all addresses each frame. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
133 lines
5.4 KiB
C++
133 lines
5.4 KiB
C++
#include "balance_address_list.h"
|
|
|
|
#include <algorithm>
|
|
#include <cctype>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
|
|
namespace dragonx {
|
|
namespace ui {
|
|
namespace {
|
|
|
|
bool containsIgnoreCase(const std::string& haystack, const std::string& needle)
|
|
{
|
|
if (needle.empty()) return true;
|
|
auto it = std::search(haystack.begin(), haystack.end(),
|
|
needle.begin(), needle.end(),
|
|
[](char a, char b) {
|
|
return std::tolower(static_cast<unsigned char>(a)) ==
|
|
std::tolower(static_cast<unsigned char>(b));
|
|
});
|
|
return it != haystack.end();
|
|
}
|
|
|
|
} // namespace
|
|
|
|
bool AddressListMatchesFilter(const AddressListInput& input, const std::string& filter)
|
|
{
|
|
if (!input.info) return false;
|
|
return containsIgnoreCase(input.info->address, filter) ||
|
|
containsIgnoreCase(input.info->label, filter) ||
|
|
containsIgnoreCase(input.label, filter);
|
|
}
|
|
|
|
std::vector<AddressListRow> BuildAddressListRows(const std::vector<AddressListInput>& inputs,
|
|
const std::string& filter,
|
|
bool hideZeroBalances,
|
|
bool showHidden)
|
|
{
|
|
std::vector<AddressListRow> rows;
|
|
rows.reserve(inputs.size());
|
|
for (const auto& input : inputs) {
|
|
if (!input.info) continue;
|
|
if (!AddressListMatchesFilter(input, filter)) continue;
|
|
if (input.hidden && !showHidden) continue;
|
|
// Keep mining-flagged addresses visible even at 0 balance (they receive payouts).
|
|
if (hideZeroBalances && input.info->balance < 1e-9 && !input.hidden && !input.favorite && !input.mining) continue;
|
|
rows.push_back({input.info, input.isZ, input.hidden, input.favorite, input.mining,
|
|
input.label, input.icon, input.sortOrder});
|
|
}
|
|
|
|
std::sort(rows.begin(), rows.end(),
|
|
[](const AddressListRow& a, const AddressListRow& b) {
|
|
bool aHasOrder = a.sortOrder >= 0;
|
|
bool bHasOrder = b.sortOrder >= 0;
|
|
if (aHasOrder && bHasOrder) return a.sortOrder < b.sortOrder;
|
|
if (aHasOrder != bHasOrder) return aHasOrder > bHasOrder;
|
|
if (a.favorite != b.favorite) return a.favorite > b.favorite;
|
|
if (a.isZ != b.isZ) return a.isZ > b.isZ;
|
|
return a.info->balance > b.info->balance;
|
|
});
|
|
return rows;
|
|
}
|
|
|
|
AddressRowLayout ComputeAddressRowLayout(float rowX,
|
|
float rowY,
|
|
float rowWidth,
|
|
float rowHeight,
|
|
float rowPadLeft,
|
|
float rowIconSize,
|
|
float spacingMd,
|
|
float spacingSm,
|
|
float spacingXs)
|
|
{
|
|
(void)spacingXs; // trailing button now insets by rowPadLeft (mirrors the left margin)
|
|
AddressRowLayout layout;
|
|
layout.contentStartX = rowX + rowPadLeft;
|
|
layout.contentStartY = rowY + spacingMd;
|
|
layout.buttonSize = rowHeight - spacingMd * 2.0f;
|
|
|
|
const float buttonY = rowY + (rowHeight - layout.buttonSize) * 0.5f;
|
|
const float rightEdge = rowX + rowWidth;
|
|
// Inset the trailing (favorite/star) button by the card's inner padding — the same margin the
|
|
// left content uses (rowPadLeft) — so it mirrors the left edge instead of hugging the card edge.
|
|
const float favoriteX = rightEdge - layout.buttonSize - rowPadLeft;
|
|
const float visibilityX = favoriteX - spacingSm - layout.buttonSize;
|
|
|
|
layout.favoriteButton = {favoriteX, buttonY, layout.buttonSize, layout.buttonSize};
|
|
layout.visibilityButton = {visibilityX, buttonY, layout.buttonSize, layout.buttonSize};
|
|
layout.contentRight = visibilityX - spacingSm;
|
|
layout.labelX = layout.contentStartX + rowIconSize * 2.0f + spacingMd;
|
|
return layout;
|
|
}
|
|
|
|
std::string FormatAddressUsdValue(double balance, double priceUsd)
|
|
{
|
|
if (priceUsd <= 0.0 || balance <= 0.0) return {};
|
|
|
|
char buffer[32];
|
|
const double usdValue = balance * priceUsd;
|
|
if (usdValue >= 1.0) {
|
|
std::snprintf(buffer, sizeof(buffer), "$%.2f", usdValue);
|
|
} else if (usdValue >= 0.01) {
|
|
std::snprintf(buffer, sizeof(buffer), "$%.4f", usdValue);
|
|
} else {
|
|
std::snprintf(buffer, sizeof(buffer), "$%.6f", usdValue);
|
|
}
|
|
return buffer;
|
|
}
|
|
|
|
AddressRowWindow ComputeAddressRowWindow(float clipMinY, float clipMaxY, float startY, float rowH, int rowCount)
|
|
{
|
|
AddressRowWindow w;
|
|
if (rowCount <= 0) return w;
|
|
w.end = rowCount; // fail open (every row) on degenerate input
|
|
if (!(rowH > 0.0f)) return w;
|
|
const double first = std::floor(((double)clipMinY - startY) / rowH) - 1.0;
|
|
const double last = std::ceil(((double)clipMaxY - startY) / rowH) + 1.0; // exclusive
|
|
if (!std::isfinite(first) || !std::isfinite(last)) return w;
|
|
w.begin = (int)std::clamp(first, 0.0, (double)rowCount);
|
|
w.end = (int)std::clamp(last, (double)w.begin, (double)rowCount);
|
|
return w;
|
|
}
|
|
|
|
float AddressRowEntryCursorY(int i, float startY, float rowH, float itemSpacingY)
|
|
{
|
|
if (i <= 0) return startY;
|
|
const float prevTop = startY + (i - 1) * rowH; // the row loop's row-top expression
|
|
return (float)(int)(prevTop + rowH + itemSpacingY); // ImGui::ItemSize: IM_TRUNC(line_y1 + line_height + ItemSpacing.y)
|
|
}
|
|
|
|
} // namespace ui
|
|
} // namespace dragonx
|