diff --git a/include/ui/MenuItem.hpp b/include/ui/MenuItem.hpp index 3a8147fb..bf4ab3d9 100644 --- a/include/ui/MenuItem.hpp +++ b/include/ui/MenuItem.hpp @@ -13,6 +13,7 @@ struct MenuItem : MenuEntry { std::string text; std::string rightText; bool disabled = false; + bool active = false; void draw(const DrawArgs &args) override; void step() override; diff --git a/include/widget/FramebufferWidget.hpp b/include/widget/FramebufferWidget.hpp index 6a3780a6..43b5bdb6 100644 --- a/include/widget/FramebufferWidget.hpp +++ b/include/widget/FramebufferWidget.hpp @@ -7,7 +7,7 @@ namespace widget { /** Caches a widget's draw() result to a framebuffer so it is called less frequently. -When `dirty` is true, its children will be re-rendered on the next call to step() override. +When `dirty` is true, its children will be re-rendered on the next call to step(). Events are not passed to the underlying scene. */ struct FramebufferWidget : Widget { diff --git a/src/app/ModuleBrowser.cpp b/src/app/ModuleBrowser.cpp index 820a54df..a36fa7fd 100644 --- a/src/app/ModuleBrowser.cpp +++ b/src/app/ModuleBrowser.cpp @@ -266,11 +266,13 @@ struct ModelBox : widget::OpaqueWidget { struct AuthorItem : ui::MenuItem { void onAction(const event::Action &e) override; + void step() override; }; struct TagItem : ui::MenuItem { void onAction(const event::Action &e) override; + void step() override; }; @@ -308,6 +310,11 @@ struct BrowserSearchField : ui::TextField { }; +struct ClearButton : ui::Button { + void onAction(const event::Action &e) override; +}; + + struct ShowFavoritesQuantity : Quantity { widget::Widget *widget; std::string getLabel() override { @@ -331,6 +338,7 @@ struct ShowFavoritesButton : ui::RadioButton { struct BrowserSidebar : widget::Widget { BrowserSearchField *searchField; + ClearButton *clearButton; ShowFavoritesButton *favoriteButton; ui::Label *authorLabel; ui::List *authorList; @@ -343,6 +351,10 @@ struct BrowserSidebar : widget::Widget { searchField = new BrowserSearchField; addChild(searchField); + clearButton = new ClearButton; + clearButton->text = "Reset filters"; + addChild(clearButton); + favoriteButton = new ShowFavoritesButton; dynamic_cast(favoriteButton->quantity)->widget = favoriteButton; addChild(favoriteButton); @@ -392,9 +404,10 @@ struct BrowserSidebar : widget::Widget { } void step() override { - searchField->box.size.x = box.size.x; - favoriteButton->box.pos = searchField->box.getBottomLeft(); + clearButton->box.pos = searchField->box.getBottomLeft(); + clearButton->box.size.x = box.size.x; + favoriteButton->box.pos = clearButton->box.getBottomLeft(); favoriteButton->box.size.x = box.size.x; float listHeight = (box.size.y - favoriteButton->box.getBottom()) / 2; @@ -606,6 +619,14 @@ struct ModuleBrowser : widget::OpaqueWidget { } sidebar->tagLabel->text = string::f("Tags (%d)", tagsLen); } + + void clear() { + search = ""; + sidebar->searchField->setText(""); + author = ""; + tag = ""; + refresh(); + } }; @@ -648,6 +669,11 @@ inline void AuthorItem::onAction(const event::Action &e) { browser->refresh(); } +inline void AuthorItem::step() { + MenuItem::step(); + ModuleBrowser *browser = getAncestorOfType(); + active = (browser->author == text); +} inline void TagItem::onAction(const event::Action &e) { ModuleBrowser *browser = getAncestorOfType(); @@ -658,6 +684,11 @@ inline void TagItem::onAction(const event::Action &e) { browser->refresh(); } +inline void TagItem::step() { + MenuItem::step(); + ModuleBrowser *browser = getAncestorOfType(); + active = (browser->tag == text); +} inline void BrowserSearchField::onChange(const event::Change &e) { ModuleBrowser *browser = getAncestorOfType(); @@ -665,6 +696,11 @@ inline void BrowserSearchField::onChange(const event::Change &e) { browser->refresh(); } +inline void ClearButton::onAction(const event::Action &e) { + ModuleBrowser *browser = getAncestorOfType(); + browser->clear(); +} + inline void ShowFavoritesQuantity::setValue(float value) { ModuleBrowser *browser = widget->getAncestorOfType(); browser->favorites = (bool) value; diff --git a/src/ui/Menu.cpp b/src/ui/Menu.cpp index f8dcc85b..b5d443ea 100644 --- a/src/ui/Menu.cpp +++ b/src/ui/Menu.cpp @@ -14,8 +14,7 @@ Menu::~Menu() { void Menu::setChildMenu(Menu *menu) { if (childMenu) { - if (childMenu->parent) - childMenu->parent->removeChild(childMenu); + childMenu->parent->removeChild(childMenu); delete childMenu; childMenu = NULL; } diff --git a/src/ui/MenuItem.cpp b/src/ui/MenuItem.cpp index 43d9e120..ff38a154 100644 --- a/src/ui/MenuItem.cpp +++ b/src/ui/MenuItem.cpp @@ -20,6 +20,9 @@ void MenuItem::draw(const DrawArgs &args) { if (parentMenu && parentMenu->activeEntry == this) state = BND_ACTIVE; + if (active) + state = BND_ACTIVE; + // Main text and background if (!disabled) bndMenuItem(args.vg, 0.0, 0.0, box.size.x, box.size.y, state, -1, text.c_str()); diff --git a/src/ui/TextField.cpp b/src/ui/TextField.cpp index 1793f69d..ffcd1887 100644 --- a/src/ui/TextField.cpp +++ b/src/ui/TextField.cpp @@ -188,14 +188,13 @@ void TextField::insertText(std::string text) { } void TextField::setText(std::string text) { - bool changed = (text != this->text); - this->text = text; - selection = cursor = text.size(); - if (changed) { + if (this->text != text) { + this->text = text; // event::Change event::Change eChange; onChange(eChange); } + selection = cursor = text.size(); } void TextField::selectAll() {