diff options
| author | crupest <crupest@outlook.com> | 2022-02-08 21:13:01 +0800 |
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2022-02-08 21:13:01 +0800 |
| commit | fa1764d7cb77aa683c6049f915a46b981f9161f7 (patch) | |
| tree | e5b54620d3b84f5391e995ab483647214152ca99 /src/ui | |
| parent | 74bb9cd27242b9320f99ff4d2b50c3051576cc14 (diff) | |
| download | cru-fa1764d7cb77aa683c6049f915a46b981f9161f7.tar.gz cru-fa1764d7cb77aa683c6049f915a46b981f9161f7.tar.bz2 cru-fa1764d7cb77aa683c6049f915a46b981f9161f7.zip | |
...
Diffstat (limited to 'src/ui')
| -rw-r--r-- | src/ui/components/Menu.cpp | 17 | ||||
| -rw-r--r-- | src/ui/controls/Control.cpp | 17 | ||||
| -rw-r--r-- | src/ui/host/WindowHost.cpp | 2 |
3 files changed, 23 insertions, 13 deletions
diff --git a/src/ui/components/Menu.cpp b/src/ui/components/Menu.cpp index 873e7ce0..c6db8e4a 100644 --- a/src/ui/components/Menu.cpp +++ b/src/ui/components/Menu.cpp @@ -25,10 +25,8 @@ MenuItem::MenuItem() { MenuItem::MenuItem(String text) : MenuItem() { SetText(std::move(text)); } MenuItem::~MenuItem() { - if (!container_->GetWindowHost()) { - delete container_; - delete text_; - } + container_->RemoveFromParent(); + delete container_; } void MenuItem::SetText(String text) { text_->SetText(std::move(text)); } @@ -39,13 +37,12 @@ Menu::Menu() { } Menu::~Menu() { - if (!container_->GetWindowHost()) { - delete container_; - } - for (auto item : items_) { delete item; } + + container_->RemoveFromParent(); + delete container_; } void Menu::AddItem(Component* item, gsl::index index) { @@ -100,9 +97,7 @@ PopupMenu::PopupMenu(controls::Control* attached_control) PopupMenu::~PopupMenu() { delete menu_; - if (!popup_->GetWindowHost()) { - delete popup_; - } + delete popup_; } controls::Control* PopupMenu::GetRootControl() { return popup_; } diff --git a/src/ui/controls/Control.cpp b/src/ui/controls/Control.cpp index bda8cb35..36d5cd60 100644 --- a/src/ui/controls/Control.cpp +++ b/src/ui/controls/Control.cpp @@ -32,17 +32,32 @@ Control::Control() { } Control::~Control() { - for (const auto child : children_) delete child; + if (auto_delete_children_) { + for (const auto child : children_) { + delete child; + } + } } host::WindowHost* Control::GetWindowHost() const { return window_host_; } +Index Control::IndexOf(Control* child) const { + for (Index i = 0; i < children_.size(); ++i) { + if (children_[i] == child) return i; + } + return -1; +} + void Control::TraverseDescendants( const std::function<void(Control*)>& predicate) { predicate(this); for (auto c : GetChildren()) c->TraverseDescendants(predicate); } +void Control::RemoveFromParent() { + if (parent_) parent_->RemoveChild(parent_->IndexOf(this)); +} + bool Control::HasFocus() { auto host = GetWindowHost(); if (host == nullptr) return false; diff --git a/src/ui/host/WindowHost.cpp b/src/ui/host/WindowHost.cpp index 26d48f89..2d09a895 100644 --- a/src/ui/host/WindowHost.cpp +++ b/src/ui/host/WindowHost.cpp @@ -118,7 +118,7 @@ WindowHost::WindowHost(controls::Control* root_control) CreateNativeWindow(); } -WindowHost::~WindowHost() {} +WindowHost::~WindowHost() { delete native_window_; } gsl::not_null<platform::gui::INativeWindow*> WindowHost::CreateNativeWindow() { const auto ui_application = IUiApplication::GetInstance(); |
