Skip to content

Commit

Permalink
Add GenerateCandidatePages method to improve candidate pagination logic
Browse files Browse the repository at this point in the history
  • Loading branch information
doyaGu committed Jan 17, 2025
1 parent be6f3fd commit 130a0d9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
41 changes: 29 additions & 12 deletions src/CommandBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,34 @@ void CommandBar::InvalidateCandidates() {
m_ShowHints = false;
}

void CommandBar::GenerateCandidatePages() {
if (m_Candidates.empty())
return;

const float sep = ImGui::CalcTextSize(" | ").x;
const float pager = ImGui::CalcTextSize("< ").x;
const float max = m_WindowSize.x;
float width = -sep;

m_CandidatePages.clear();
m_CandidatePages.push_back(0); // Start the first page

for (int i = 0; i < (int) m_Candidates.size(); ++i) {
const ImVec2 size = ImGui::CalcTextSize(m_Candidates[i].c_str());
// Add indicator widths if it's not the first page
float effectiveMax = max;
if (m_CandidatePages.size() > 1) {
effectiveMax -= pager * 2; // Reserve space for "< " and " >"
}

width += size.x + sep;
if (width > effectiveMax) {
m_CandidatePages.push_back(i); // Start a new page
width = size.x - sep + pager * 2; // Reset width with indicators
}
}
}

size_t CommandBar::OnCompletion(const char *lineStart, const char *lineEnd) {
const char *wordStart = lineStart;
int wordCount = LastToken(wordStart, lineEnd);
Expand Down Expand Up @@ -369,18 +397,7 @@ size_t CommandBar::OnCompletion(const char *lineStart, const char *lineEnd) {
}
}

const float sep = ImGui::CalcTextSize(" | ").x;
const float max = m_WindowSize.x;
float width = sep;
m_CandidatePages.push_back(0);
for (int i = 0; i < (int) m_Candidates.size(); ++i) {
const ImVec2 size = ImGui::CalcTextSize(m_Candidates[i].c_str());
width += size.x + sep;
if (width > max) {
m_CandidatePages.push_back(i);
width = sep * 3.0f;
}
}
GenerateCandidatePages();
} else {
NextCandidate();
}
Expand Down
1 change: 1 addition & 0 deletions src/CommandBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class CommandBar : public Bui::Window {
void NextPageOfCandidates();
void PrevPageOfCandidates();
void InvalidateCandidates();
void GenerateCandidatePages();

size_t OnCompletion(const char *lineStart, const char *lineEnd);
int OnTextEdit(ImGuiInputTextCallbackData *data);
Expand Down

0 comments on commit 130a0d9

Please sign in to comment.