aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-12-14 16:59:30 +0800
committercrupest <crupest@outlook.com>2021-12-14 16:59:30 +0800
commit72abeb9c86d768a5c6a9a2d28dab037b145b7390 (patch)
treef40f2c90c7e466537945519354842382e84d0595
parent13f76127fe0e3371b8748ceb38485b6237f69aeb (diff)
downloadcru-72abeb9c86d768a5c6a9a2d28dab037b145b7390.tar.gz
cru-72abeb9c86d768a5c6a9a2d28dab037b145b7390.tar.bz2
cru-72abeb9c86d768a5c6a9a2d28dab037b145b7390.zip
...
-rw-r--r--include/cru/ui/components/Menu.hpp9
-rw-r--r--src/ui/components/Menu.cpp7
2 files changed, 13 insertions, 3 deletions
diff --git a/include/cru/ui/components/Menu.hpp b/include/cru/ui/components/Menu.hpp
index a72c0313..b0075574 100644
--- a/include/cru/ui/components/Menu.hpp
+++ b/include/cru/ui/components/Menu.hpp
@@ -60,12 +60,17 @@ class Menu : public Component {
void AddTextItem(String text, std::function<void()> on_click) {
AddTextItem(std::move(text), GetItemCount(), std::move(on_click));
}
- void AddTextItem(String text, gsl::index index,
- std::function<void()> on_click);
+ void AddTextItem(String text, Index index, std::function<void()> on_click);
+
+ void SetOnItemClick(std::function<void(Index)> on_item_click) {
+ on_item_click_ = std::move(on_item_click);
+ }
private:
controls::FlexLayout* container_;
std::vector<Component*> items_;
+
+ std::function<void(Index)> on_item_click_;
};
class PopupMenu : public Component {
diff --git a/src/ui/components/Menu.cpp b/src/ui/components/Menu.cpp
index 60364f13..681b44b7 100644
--- a/src/ui/components/Menu.cpp
+++ b/src/ui/components/Menu.cpp
@@ -79,7 +79,10 @@ void Menu::ClearItems() {
void Menu::AddTextItem(String text, gsl::index index,
std::function<void()> on_click) {
MenuItem* item = new MenuItem(std::move(text));
- item->SetOnClick(std::move(on_click));
+ item->SetOnClick([this, index, on_click = std::move(on_click)] {
+ on_click();
+ if (on_item_click_) on_item_click_(index);
+ });
AddItem(item, index);
}
@@ -89,6 +92,8 @@ PopupMenu::PopupMenu(controls::Control* attached_control)
menu_ = new Menu();
+ menu_->SetOnItemClick([this](Index _) { this->Close(); });
+
popup_->AddChild(menu_->GetRootControl(), 0);
}