Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Add pagination option #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
53 changes: 53 additions & 0 deletions src/Interface/Controls/Pagination.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace Controls
{
class Pagination {

int page = 0;
int total = 0;
int pageCount = 0;

int m_limit = 3;

bool isPageRequested = false;
int requestedPageIndex = 0;

void Render() {
isPageRequested = false;
requestedPageIndex = 0;

if (page > 0) {
PageButton(page - 1, "Prev Page");
}

UI::BeginGroup();
for (uint i = Math::Max(0, page - m_limit); i < page; i++) {
PageButton(i);
}

UI::BeginDisabled();
PageButton(page);

UI::EndDisabled();

for (uint i = page + 1; i < Math::Min(pageCount, page + m_limit + 1); i++) {
PageButton(i);
}

if (pageCount > page + 1) {
PageButton(page + 1, "Next Page");
}
UI::EndGroup();
}

void PageButton(int pageIndex, string label = "") {
if (label == "") {
label = "" + (pageIndex + 1);
}
if (UI::Button(label)) {
isPageRequested = true;
requestedPageIndex = pageIndex;
}
UI::SameLine();
}
}
}
43 changes: 34 additions & 9 deletions src/Interface/Tabs/PluginList.as
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class PluginListTab : Tab
int m_pageCount;
uint m_lastPageRequestFinished = 0;

Controls::Pagination m_pagination;

array<PluginInfo@> m_plugins;

string GetLabel() override { return "Plugins"; }
Expand Down Expand Up @@ -59,7 +61,11 @@ class PluginListTab : Tab
void StartRequest()
{
Clear();
StartRequestForPage(0);
int defaultPage = 0;
if (Setting_TabsPagination) {
defaultPage = m_pagination.page;
}
StartRequestForPage(defaultPage);
}

void StartRequestForPage(int page)
Expand Down Expand Up @@ -121,7 +127,17 @@ class PluginListTab : Tab
m_page = js["page"];
m_pageCount = js["pages"];

m_pagination.total = m_total;
m_pagination.page = m_page;
m_pagination.pageCount = m_pageCount;

auto jsItems = js["items"];

if (Setting_TabsPagination) {
UI::SetScrollY(0);
m_plugins.Resize(0);
}

for (uint i = 0; i < jsItems.Length; i++) {
PluginInfo pi(jsItems[i]);
if (Setting_ChangelogTooltips && pi.GetInstalledVersion() < pi.m_version) {
Expand Down Expand Up @@ -188,17 +204,26 @@ class PluginListTab : Tab
if (haveMorePages) {
UI::TableNextRow(UI::TableRowFlags::None, rowHeight);
UI::TableNextColumn();
string infiniteScrollMsg = (m_request is null ? "Scroll to load" : "Loading") + " page " + (m_page + 2);
UI::Dummy(vec2(0, rowHeight / 3.0));
UI::Text(infiniteScrollMsg);
if (!Setting_TabsPagination) {
string infiniteScrollMsg = (m_request is null ? "Scroll to load" : "Loading") + " page " + (m_page + 2);
UI::Dummy(vec2(0, rowHeight / 3.0));
UI::Text(infiniteScrollMsg);
}
}
UI::EndTable();

// after >500ms since the last request, get the next page if there is excess vertical space or when we scroll to just a bit before the final row is in view
bool waitedLongEnough = m_lastPageRequestFinished + 500 < Time::Now;
bool scrolledNearEnd = UI::GetScrollMaxY() == 0 || UI::GetScrollY() > (UI::GetScrollMaxY() - rowHeight);
if (waitedLongEnough && scrolledNearEnd && haveMorePages && m_request is null) {
StartRequestForPage(m_page + 1);
if (Setting_TabsPagination) {
m_pagination.Render();
if (m_pagination.isPageRequested) {
StartRequestForPage(m_pagination.requestedPageIndex);
}
} else {
// after >500ms since the last request, get the next page if there is excess vertical space or when we scroll to just a bit before the final row is in view
bool waitedLongEnough = m_lastPageRequestFinished + 500 < Time::Now;
bool scrolledNearEnd = UI::GetScrollMaxY() == 0 || UI::GetScrollY() > (UI::GetScrollMaxY() - rowHeight);
if (waitedLongEnough && scrolledNearEnd && haveMorePages && m_request is null) {
StartRequestForPage(m_page + 1);
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Settings.as
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ int Setting_PluginsPerRow = 3;
[Setting category="Interface" name="Display changelog when hovering over updatable plugins."]
bool Setting_ChangelogTooltips = true;

[Setting category="Interface" name="Enable tabs pagination"]
bool Setting_TabsPagination = false;

[Setting category="Advanced" name="Base URL" description="Only change if you know what you're doing!"]
string Setting_BaseURL = "https://openplanet.dev/";

Expand Down