Simplified some code; use a lambda instead of two identical loops.

This commit is contained in:
Stephen Anthony 2020-11-29 16:25:23 -03:30
parent ad88f26c61
commit 47bab52621
1 changed files with 11 additions and 13 deletions

View File

@ -123,27 +123,25 @@ void ContextMenu::setSelectedIndex(int idx)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ContextMenu::setSelected(const Variant& tag, const Variant& defaultTag) void ContextMenu::setSelected(const Variant& tag, const Variant& defaultTag)
{ {
if(tag != "") // indicates that the defaultTag should be used instead auto SEARCH_AND_SELECT = [&](const Variant& t)
{ {
for(uInt32 item = 0; item < _entries.size(); ++item) for(uInt32 item = 0; item < _entries.size(); ++item)
{ {
if(BSPF::equalsIgnoreCase(_entries[item].second.toString(), tag.toString())) if(BSPF::equalsIgnoreCase(_entries[item].second.toString(), t.toString()))
{ {
setSelectedIndex(item); setSelectedIndex(item);
return; return true;
} }
} }
} return false;
};
// If we get this far, the value wasn't found; use the default value // First try searching for a valid tag
for(uInt32 item = 0; item < _entries.size(); ++item) bool tagSelected = tag != "" && SEARCH_AND_SELECT(tag);
{
if(BSPF::equalsIgnoreCase(_entries[item].second.toString(), defaultTag.toString())) // Otherwise use the default tag
{ if(!tagSelected)
setSelectedIndex(item); SEARCH_AND_SELECT(defaultTag);
return;
}
}
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -