Browse Source

Sort ModelBoxes by plugin name and model name when no search query is entered.

tags/v1.0.0
Andrew Belt 5 years ago
parent
commit
74361d4ce2
3 changed files with 39 additions and 24 deletions
  1. +1
    -1
      include/plugin.hpp
  2. +36
    -21
      src/app/ModuleBrowser.cpp
  3. +2
    -2
      src/plugin.cpp

+ 1
- 1
include/plugin.hpp View File

@@ -29,7 +29,7 @@ std::string normalizeTag(const std::string &tag);
bool isSlugValid(const std::string &slug);


extern const std::vector<std::string> allowedTags;
extern const std::set<std::string> allowedTags;
extern std::vector<Plugin*> plugins;
extern bool isDownloading;
extern float downloadProgress;


+ 36
- 21
src/app/ModuleBrowser.cpp View File

@@ -49,7 +49,6 @@ static float modelScore(plugin::Model *model, const std::string &search) {
// s += tag;
// }
float score = string::fuzzyScore(s, search);
DEBUG("%s %f", s.c_str(), score);
return score;
}

@@ -74,7 +73,7 @@ struct BrowserOverlay : widget::OpaqueWidget {
};


static const float MODEL_BOX_ZOOM = 1.0f;
static const float MODEL_BOX_ZOOM = 0.5f;


struct ModelBox : widget::OpaqueWidget {
@@ -277,11 +276,10 @@ struct BrowserSidebar : widget::Widget {
pluginList = new ui::List;
pluginScroll->container->addChild(pluginList);

std::vector<std::string> pluginNames;
std::set<std::string, string::CaseInsensitiveCompare> pluginNames;
for (plugin::Plugin *plugin : plugin::plugins) {
pluginNames.push_back(plugin->name);
pluginNames.insert(plugin->name);
}
std::sort(pluginNames.begin(), pluginNames.end(), string::CaseInsensitiveCompare());

for (const std::string &pluginName : pluginNames) {
ui::MenuItem *item = new ui::MenuItem;
@@ -347,6 +345,8 @@ struct ModuleBrowser : widget::OpaqueWidget {
modelContainer->addChild(moduleBox);
}
}

setSearch("");
}

void step() override {
@@ -370,22 +370,37 @@ struct ModuleBrowser : widget::OpaqueWidget {

void setSearch(const std::string &search) {
std::string searchTrimmed = string::trim(search);
std::map<const Widget*, float> scores;
// Compute scores and set visibility
for (Widget *w : modelContainer->children) {
ModelBox *modelBox = dynamic_cast<ModelBox*>(w);
assert(modelBox);
float score = modelScore(modelBox->model, searchTrimmed);
scores[modelBox] = score;
modelBox->visible = (score > 0);
}
DEBUG("");
// Sort by score
modelContainer->children.sort([&](const Widget *w1, const Widget *w2) {
return scores[w1] > scores[w2];
});
// Reset scroll position
modelScroll->offset = math::Vec();

if (searchTrimmed.empty()) {
for (Widget *w : modelContainer->children) {
w->visible = true;
}
// If no search query, sort by plugin name and module name
modelContainer->children.sort([&](Widget *w1, Widget *w2) {
ModelBox *m1 = dynamic_cast<ModelBox*>(w1);
ModelBox *m2 = dynamic_cast<ModelBox*>(w2);
if (m1->model->plugin->name != m2->model->plugin->name)
return m1->model->plugin->name < m2->model->plugin->name;
return m1->model->name < m2->model->name;
});
}
else {
std::map<Widget*, float> scores;
// Compute scores and set visibility
for (Widget *w : modelContainer->children) {
ModelBox *m = dynamic_cast<ModelBox*>(w);
assert(m);
float score = modelScore(m->model, searchTrimmed);
scores[m] = score;
m->visible = (score > 0);
}
// Sort by score
modelContainer->children.sort([&](Widget *w1, Widget *w2) {
return scores[w1] > scores[w2];
});
}
}
};

@@ -393,7 +408,7 @@ struct ModuleBrowser : widget::OpaqueWidget {
// Implementations to resolve dependencies


void ModelBox::onButton(const event::Button &e) {
inline void ModelBox::onButton(const event::Button &e) {
OpaqueWidget::onButton(e);
if (e.getConsumed() != this)
return;
@@ -419,7 +434,7 @@ void ModelBox::onButton(const event::Button &e) {
}
}

void BrowserSearchField::onChange(const event::Change &e) {
inline void BrowserSearchField::onChange(const event::Change &e) {
ModuleBrowser *browser = getAncestorOfType<ModuleBrowser>();
browser->setSearch(text);
}


+ 2
- 2
src/plugin.cpp View File

@@ -540,7 +540,7 @@ Model *getModel(const std::string &pluginSlug, const std::string &modelSlug) {
All tags here should be in sentence caps for display consistency.
However, tags are case-insensitive in plugin metadata.
*/
const std::vector<std::string> allowedTags = {
const std::set<std::string> allowedTags = {
"Arpeggiator",
"Attenuator", // With a level knob and not much else.
"Blank", // No parameters or ports. Serves no purpose except visual.
@@ -626,7 +626,7 @@ std::string normalizeTag(const std::string &tag) {
if (it != tagAliases.end())
lowercaseTag = it->second;
// Find allowed tag
for (std::string allowedTag : allowedTags) {
for (const std::string &allowedTag : allowedTags) {
if (lowercaseTag == string::lowercase(allowedTag))
return allowedTag;
}


Loading…
Cancel
Save