diff options
author | crupest <crupest@outlook.com> | 2022-02-10 21:21:46 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-02-10 21:21:46 +0800 |
commit | a236a2a146bfcc4eb5c93a85cd99ac330e83a7f5 (patch) | |
tree | 32756f8f90df09b37f30a5753cff69207851fc4a | |
parent | 28d7016c32b39656dafa0936cfedb02159f9e7cd (diff) | |
download | cru-a236a2a146bfcc4eb5c93a85cd99ac330e83a7f5.tar.gz cru-a236a2a146bfcc4eb5c93a85cd99ac330e83a7f5.tar.bz2 cru-a236a2a146bfcc4eb5c93a85cd99ac330e83a7f5.zip |
...
-rw-r--r-- | include/cru/ui/components/Component.h | 11 | ||||
-rw-r--r-- | include/cru/ui/components/Menu.h | 10 | ||||
-rw-r--r-- | src/ui/components/Menu.cpp | 13 |
3 files changed, 23 insertions, 11 deletions
diff --git a/include/cru/ui/components/Component.h b/include/cru/ui/components/Component.h index e889ae97..8861f30b 100644 --- a/include/cru/ui/components/Component.h +++ b/include/cru/ui/components/Component.h @@ -17,5 +17,16 @@ class CRU_UI_API Component : public Object { ~Component() = default; virtual controls::Control* GetRootControl() = 0; + + bool IsDeleteByParent() const { return delete_by_parent_; } + void SetDeleteByParent(bool delete_by_parent) { + delete_by_parent_ = delete_by_parent; + } + void DeleteIfDeleteByParent() const { + if (delete_by_parent_) delete this; + } + + private: + bool delete_by_parent_; }; } // namespace cru::ui::components diff --git a/include/cru/ui/components/Menu.h b/include/cru/ui/components/Menu.h index 99668f62..f4e54c9f 100644 --- a/include/cru/ui/components/Menu.h +++ b/include/cru/ui/components/Menu.h @@ -52,15 +52,15 @@ class CRU_UI_API Menu : public Component { return static_cast<gsl::index>(items_.size()); } - void AddItem(Component* component) { AddItem(component, GetItemCount()); } - void AddItem(Component* component, gsl::index index); - Component* RemoveItem(gsl::index index); + void AddItem(Component* component) { AddItemAt(component, GetItemCount()); } + void AddItemAt(Component* component, gsl::index index); + Component* RemoveItemAt(gsl::index index); void ClearItems(); void AddTextItem(String text, std::function<void()> on_click) { - AddTextItem(std::move(text), GetItemCount(), std::move(on_click)); + AddTextItemAt(std::move(text), GetItemCount(), std::move(on_click)); } - void AddTextItem(String text, Index index, std::function<void()> on_click); + void AddTextItemAt(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); diff --git a/src/ui/components/Menu.cpp b/src/ui/components/Menu.cpp index ab897077..74909c85 100644 --- a/src/ui/components/Menu.cpp +++ b/src/ui/components/Menu.cpp @@ -37,14 +37,14 @@ Menu::~Menu() { container_.RemoveFromParent(); } -void Menu::AddItem(Component* item, gsl::index index) { +void Menu::AddItemAt(Component* item, gsl::index index) { Expects(index >= 0 && index <= GetItemCount()); items_.insert(items_.cbegin() + index, item); container_.AddChildAt(item->GetRootControl(), index); } -Component* Menu::RemoveItem(gsl::index index) { +Component* Menu::RemoveItemAt(gsl::index index) { Expects(index >= 0 && index < GetItemCount()); Component* item = items_[index]; @@ -57,7 +57,7 @@ Component* Menu::RemoveItem(gsl::index index) { void Menu::ClearItems() { for (auto item : items_) { - delete item; + item->DeleteIfDeleteByParent(); } items_.clear(); @@ -65,14 +65,15 @@ void Menu::ClearItems() { container_.ClearChildren(); } -void Menu::AddTextItem(String text, gsl::index index, - std::function<void()> on_click) { +void Menu::AddTextItemAt(String text, gsl::index index, + std::function<void()> on_click) { MenuItem* item = new MenuItem(std::move(text)); item->SetOnClick([this, index, on_click = std::move(on_click)] { on_click(); if (on_item_click_) on_item_click_(index); }); - AddItem(item, index); + item->SetDeleteByParent(true); + AddItemAt(item, index); } PopupMenu::PopupMenu(controls::Control* attached_control) |