Filters are now applied only when there were actual changes made.

Also disabled the free text filter entry if the system has no games.
This commit is contained in:
Leon Styhre 2021-03-18 20:22:49 +01:00
parent 60ee29f2ea
commit 3e9e592c3b
3 changed files with 49 additions and 16 deletions

View file

@ -18,10 +18,13 @@
GuiGamelistFilter::GuiGamelistFilter( GuiGamelistFilter::GuiGamelistFilter(
Window* window, Window* window,
SystemData* system) SystemData* system,
std::function<void(bool)> filterChangedCallback)
: GuiComponent(window), : GuiComponent(window),
mMenu(window, "FILTER GAMELIST BY"), mMenu(window, "FILTER GAMELIST BY"),
mSystem(system) mSystem(system),
mFiltersChangedCallback(filterChangedCallback),
mFiltersChanged(false)
{ {
initializeMenu(); initializeMenu();
} }
@ -49,6 +52,16 @@ void GuiGamelistFilter::initializeMenu()
mMenu.setPosition((Renderer::getScreenWidth() - mMenu.getSize().x()) / 2, mMenu.setPosition((Renderer::getScreenWidth() - mMenu.getSize().x()) / 2,
Renderer::getScreenHeight() * 0.15f); Renderer::getScreenHeight() * 0.15f);
// Save the initial filter values to be able to check later if any changes were made.
mInitialTextFilter = mTextFilterField->getValue();
for (std::map<FilterIndexType, std::shared_ptr<OptionListComponent<std::string>>>::
const_iterator it = mFilterOptions.cbegin(); it != mFilterOptions.cend(); it++) {
std::shared_ptr<OptionListComponent<std::string>> optionList = it->second;
std::vector<std::string> filters = optionList->getSelectedObjects();
mInitialFilters.push_back(filters);
}
} }
void GuiGamelistFilter::resetAllFilters() void GuiGamelistFilter::resetAllFilters()
@ -62,6 +75,7 @@ void GuiGamelistFilter::resetAllFilters()
mFilterIndex->setTextFilter(""); mFilterIndex->setTextFilter("");
mTextFilterField->setValue(""); mTextFilterField->setValue("");
mFiltersChanged = true;
} }
GuiGamelistFilter::~GuiGamelistFilter() GuiGamelistFilter::~GuiGamelistFilter()
@ -80,6 +94,8 @@ void GuiGamelistFilter::addFiltersToMenu()
mTextFilterField = std::make_shared<TextComponent>(mWindow, "", mTextFilterField = std::make_shared<TextComponent>(mWindow, "",
Font::get(FONT_SIZE_MEDIUM), 0x777777FF, ALIGN_RIGHT); Font::get(FONT_SIZE_MEDIUM), 0x777777FF, ALIGN_RIGHT);
// Don't show the free text filter entry unless there are any games in the system.
if (mSystem->getRootFolder()->getChildren().size() > 0) {
row.addElement(lbl, true); row.addElement(lbl, true);
row.addElement(mTextFilterField, true); row.addElement(mTextFilterField, true);
@ -93,6 +109,7 @@ void GuiGamelistFilter::addFiltersToMenu()
row.addElement(bracket, false); row.addElement(bracket, false);
mTextFilterField->setValue(mFilterIndex->getTextFilter()); mTextFilterField->setValue(mFilterIndex->getTextFilter());
}
// Callback function. // Callback function.
auto updateVal = [this](const std::string& newVal) { auto updateVal = [this](const std::string& newVal) {
@ -136,14 +153,21 @@ void GuiGamelistFilter::addFiltersToMenu()
void GuiGamelistFilter::applyFilters() void GuiGamelistFilter::applyFilters()
{ {
if (mInitialTextFilter != mTextFilterField->getValue())
mFiltersChanged = true;
std::vector<FilterDataDecl> decls = mFilterIndex->getFilterDataDecls(); std::vector<FilterDataDecl> decls = mFilterIndex->getFilterDataDecls();
for (std::map<FilterIndexType, std::shared_ptr<OptionListComponent<std::string>>>:: for (std::map<FilterIndexType, std::shared_ptr<OptionListComponent<std::string>>>::
const_iterator it = mFilterOptions.cbegin(); it != mFilterOptions.cend(); it++) { const_iterator it = mFilterOptions.cbegin(); it != mFilterOptions.cend(); it++) {
std::shared_ptr<OptionListComponent<std::string>> optionList = it->second; std::shared_ptr<OptionListComponent<std::string>> optionList = it->second;
std::vector<std::string> filters = optionList->getSelectedObjects(); std::vector<std::string> filters = optionList->getSelectedObjects();
auto iteratorDistance = std::distance(mFilterOptions.cbegin(), it);
if (mInitialFilters[iteratorDistance] != filters)
mFiltersChanged = true;
mFilterIndex->setFilter(it->first, &filters); mFilterIndex->setFilter(it->first, &filters);
} }
mFiltersChangedCallback(mFiltersChanged);
delete this; delete this;
} }

View file

@ -22,7 +22,8 @@ class SystemData;
class GuiGamelistFilter : public GuiComponent class GuiGamelistFilter : public GuiComponent
{ {
public: public:
GuiGamelistFilter(Window* window, SystemData* system); GuiGamelistFilter(Window* window,
SystemData* system, std::function<void(bool)> filtersChangedCallback);
~GuiGamelistFilter(); ~GuiGamelistFilter();
bool input(InputConfig* config, Input input) override; bool input(InputConfig* config, Input input) override;
@ -37,11 +38,15 @@ private:
void addFiltersToMenu(); void addFiltersToMenu();
std::map<FilterIndexType, std::shared_ptr<OptionListComponent<std::string>>> mFilterOptions; std::map<FilterIndexType, std::shared_ptr<OptionListComponent<std::string>>> mFilterOptions;
std::vector<std::vector<std::string>> mInitialFilters;
std::string mInitialTextFilter;
MenuComponent mMenu; MenuComponent mMenu;
SystemData* mSystem; SystemData* mSystem;
FileFilterIndex* mFilterIndex; FileFilterIndex* mFilterIndex;
std::shared_ptr<TextComponent> mTextFilterField; std::shared_ptr<TextComponent> mTextFilterField;
std::function<void(bool)> mFiltersChangedCallback;
bool mFiltersChanged;
}; };
#endif // ES_APP_GUIS_GUI_GAME_LIST_FILTER_H #endif // ES_APP_GUIS_GUI_GAME_LIST_FILTER_H

View file

@ -277,12 +277,16 @@ GuiGamelistOptions::~GuiGamelistOptions()
void GuiGamelistOptions::openGamelistFilter() void GuiGamelistOptions::openGamelistFilter()
{ {
GuiGamelistFilter* ggf; GuiGamelistFilter* ggf;
mFiltersChanged = true;
auto filtersChangedFunc = [this](bool filtersChanged) {
mFiltersChanged = filtersChanged;
};
if (mIsCustomCollection) if (mIsCustomCollection)
ggf = new GuiGamelistFilter(mWindow, getGamelist()->getCursor()->getSystem()); ggf = new GuiGamelistFilter(mWindow, getGamelist()->getCursor()->getSystem(),
filtersChangedFunc);
else else
ggf = new GuiGamelistFilter(mWindow, mSystem); ggf = new GuiGamelistFilter(mWindow, mSystem, filtersChangedFunc);
mWindow->pushGui(ggf); mWindow->pushGui(ggf);
} }