Skip to content

Commit

Permalink
Completing the new paging control (should be extracted into a juce-wi…
Browse files Browse the repository at this point in the history
…dgets widget!) with nice ellipsis and shortening. Maybe 12 is a bit much for pages to show? should be rather 7 I guess? This continues #37
  • Loading branch information
christofmuc committed Jan 19, 2021
1 parent d77020d commit 44ef054
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 32 deletions.
104 changes: 72 additions & 32 deletions The-Orm/PatchButtonPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ PatchButtonPanel::PatchButtonPanel(std::function<void(midikraft::PatchHolder &)>
pageDown_.setButtonText("<");
pageDown_.addListener(this);

for (int i = 0; i < 2; i++) {
auto e = new Label();
e->setText("...", dontSendNotification);
addAndMakeVisible(e);
ellipsis_.add(std::move(e));
}

maxPageButtons_ = 16; // For now, hard-coded value.Should probably calculate this from the width available.
pageNumbers_.clear();
for (int i = 0; i < maxPageButtons_; i++) {
TextButton *b = new TextButton();
b->setClickingTogglesState(true);
b->setRadioGroupId(1357);
b->setConnectedEdges(TextButton::ConnectedOnLeft | TextButton::ConnectedOnRight);
b->setColour(ComboBox::outlineColourId, ColourHelpers::getUIColour(this, LookAndFeel_V4::ColourScheme::windowBackground));
addAndMakeVisible(b);
b->setVisible(false);
pageNumbers_.add(std::move(b));
}

UIModel::instance()->thumbnails_.addChangeListener(this);
}

Expand All @@ -46,20 +66,44 @@ void PatchButtonPanel::setTotalCount(int totalCount)
totalSize_ = totalCount;
numPages_ = totalCount / pageSize_;
if (totalCount % pageSize_ != 0) numPages_++;
}

// Create the page buttons
pageNumbers_.clear();
for (int i = 0; i < numPages_; i++) {
TextButton *b = new TextButton();
b->setButtonText(String(i + 1));
b->setClickingTogglesState(true);
b->setRadioGroupId(1357);
b->setConnectedEdges(TextButton::ConnectedOnLeft | TextButton::ConnectedOnRight);
b->setColour(ComboBox::outlineColourId, ColourHelpers::getUIColour(this, LookAndFeel_V4::ColourScheme::windowBackground));
b->onClick = [this, i, b]() { if (b->getToggleState()) jumpToPage(i); };
addAndMakeVisible(b);
if (i == 0) b->setToggleState(true, dontSendNotification);
pageNumbers_.add(std::move(b));
void PatchButtonPanel::setupPageButtons() {
pageButtonMap_.clear();
if (numPages_ <= maxPageButtons_) {
// Easy, just make on page button for each page available
for (int i = 0; i < numPages_; i++) {
pageButtonMap_[i] = i;
}
}
else {
// Need first page, and then 5 more centered around the current page, and the last one to also show the count
int button = 0;
pageButtonMap_[button++] = 0;
int blockStart = std::max(1, std::min(numPages_ - 6, pageNumber_ - 2));
for (int page = blockStart; page < blockStart + 5; page++) {
if (page > 0 && page < numPages_) {
pageButtonMap_[button++] = page;
}
}
pageButtonMap_[button++] = numPages_ - 1;
}

// Now relabel!
for (auto button : pageButtonMap_) {
Button *b = pageNumbers_[button.first];
b->onClick = [this, b, button]() { if (b->getToggleState()) jumpToPage(button.second); };
b->setButtonText(String(button.second + 1));
b->setVisible(true);
if (button.second == pageNumber_) {
b->setToggleState(true, dontSendNotification);
}
}
// Any more buttons unused?
for (int i = 0; i < maxPageButtons_; i++) {
if (i >= pageButtonMap_.size()) {
pageNumbers_[i]->setVisible(false);
}
}
resized();
}
Expand All @@ -76,6 +120,7 @@ void PatchButtonPanel::setPatches(std::vector<midikraft::PatchHolder> const &pat
buttonClicked(((int) patches_.size()) - 1);
}
}
setupPageButtons();
}

String PatchButtonPanel::createNameOfThubnailCacheFile(midikraft::PatchHolder const &patch) {
Expand Down Expand Up @@ -148,21 +193,6 @@ void PatchButtonPanel::refresh(bool async, int autoSelectTarget /* = -1 */) {
}
}
}

/*// Also, set the page number stripe
std::string pages;
size_t numberOfPages = (totalSize_ / pageSize_) + 1;
for (size_t i = 0; i < numberOfPages; i++) {
if ((i == numberOfPages - 1) && (totalSize_ % pageSize_ == 0)) continue;
if (!pages.empty()) pages.append(" ");
if (i == pageNumber_) {
pages.append((boost::format("<%d>") % (i + 1)).str());
}
else {
pages.append((boost::format("%d") % (i + 1)).str());
}
}
pageNumbers_.setText(pages, dontSendNotification);*/
}

void PatchButtonPanel::resized()
Expand All @@ -173,8 +203,17 @@ void PatchButtonPanel::resized()
pageNumberBox.flexDirection = FlexBox::Direction::row;
pageNumberBox.justifyContent = FlexBox::JustifyContent::center;
pageNumberBox.alignContent = FlexBox::AlignContent::center;
for (auto page : pageNumbers_) {
pageNumberBox.items.add(FlexItem(*page).withHeight(32).withWidth((float) page->getBestWidthForHeight(32)));
int ecounter = 0;
for (int i = 0; i < ellipsis_.size(); i++) ellipsis_[i]->setVisible(false);
for (int i = 0; i < pageNumbers_.size(); i++) {
auto page = pageNumbers_[i];
if (page->isVisible()) {
if (i > 0 && pageButtonMap_[i] != pageButtonMap_[i - 1] + 1) {
pageNumberBox.items.add(FlexItem(*ellipsis_[ecounter]).withHeight(32).withWidth(32));
ellipsis_[ecounter++]->setVisible(true);
}
pageNumberBox.items.add(FlexItem(*page).withHeight(32).withWidth((float)page->getBestWidthForHeight(32)));
}
}
pageNumberBox.performLayout(pageNumberStrip);

Expand Down Expand Up @@ -211,7 +250,7 @@ void PatchButtonPanel::pageUp(bool selectNext) {
if (pageBase_ + pageSize_ < totalSize_) {
pageBase_ += pageSize_;
pageNumber_++;
if (pageNumber_ < pageNumbers_.size()) pageNumbers_[pageNumber_]->setToggleState(true, dontSendNotification);
setupPageButtons();
refresh(true, selectNext ? 0 : -1);
}
}
Expand All @@ -220,7 +259,7 @@ void PatchButtonPanel::pageDown(bool selectLast) {
if (pageBase_ - pageSize_ >= 0) {
pageBase_ -= pageSize_;
pageNumber_--;
if (pageNumber_ >= 0 && pageNumber_ < pageNumbers_.size()) pageNumbers_[pageNumber_]->setToggleState(true, dontSendNotification);
setupPageButtons();
refresh(true, selectLast ? 1 : -1);
}
}
Expand All @@ -229,6 +268,7 @@ void PatchButtonPanel::jumpToPage(int pagenumber) {
if (pagenumber >= 0 && pagenumber < numPages_) {
pageBase_ = pagenumber * pageSize_;
pageNumber_ = pagenumber;
setupPageButtons();
refresh(true);
}
}
Expand Down
5 changes: 5 additions & 0 deletions The-Orm/PatchButtonPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class PatchButtonPanel : public Component,
File findPrehearFile(midikraft::PatchHolder const &patch);
void refreshThumbnail(int i);
int indexOfActive() const;
void resizePageNumbers();
void setupPageButtons();

std::vector<midikraft::PatchHolder> patches_;
std::unique_ptr<PatchButtonGrid<PatchHolderButton>> patchButtons_;
Expand All @@ -60,11 +62,14 @@ class PatchButtonPanel : public Component,

TextButton pageUp_, pageDown_;
OwnedArray<TextButton> pageNumbers_;
OwnedArray<Label> ellipsis_;
int pageBase_;
int pageNumber_;
int pageSize_;
int totalSize_;
int numPages_;
int maxPageButtons_;
std::map<int, int> pageButtonMap_;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PatchButtonPanel)
};
Expand Down

0 comments on commit 44ef054

Please sign in to comment.