diff options
| -rw-r--r-- | include/cru/ui/components/Component.h | 4 | ||||
| -rw-r--r-- | include/cru/ui/controls/TextHostControlService.h | 4 | ||||
| -rw-r--r-- | src/ui/components/Component.cpp | 10 | ||||
| -rw-r--r-- | src/ui/components/Menu.cpp | 2 | ||||
| -rw-r--r-- | src/ui/controls/Control.cpp | 5 | ||||
| -rw-r--r-- | src/ui/controls/TextHostControlService.cpp | 4 |
6 files changed, 21 insertions, 8 deletions
diff --git a/include/cru/ui/components/Component.h b/include/cru/ui/components/Component.h index 627e2d3c..6d31ae79 100644 --- a/include/cru/ui/components/Component.h +++ b/include/cru/ui/components/Component.h @@ -19,9 +19,7 @@ class CRU_UI_API Component : public Object, public DeleteLaterImpl { void SetDeleteByParent(bool delete_by_parent) { delete_by_parent_ = delete_by_parent; } - void DeleteIfDeleteByParent() const { - if (delete_by_parent_) delete this; - } + void DeleteIfDeleteByParent(bool delete_later = true); protected: void OnPrepareDelete() override; diff --git a/include/cru/ui/controls/TextHostControlService.h b/include/cru/ui/controls/TextHostControlService.h index e1586e66..454be09b 100644 --- a/include/cru/ui/controls/TextHostControlService.h +++ b/include/cru/ui/controls/TextHostControlService.h @@ -2,6 +2,7 @@ #include "../render/TextRenderObject.h" #include "cru/platform/gui/InputMethod.h" #include "cru/platform/gui/UiApplication.h" +#include "cru/ui/DeleteLater.h" #include "cru/ui/controls/Control.h" #include "cru/ui/helper/ShortcutHub.h" @@ -205,7 +206,6 @@ class CRU_UI_API TextHostControlService : public Object { // true if left mouse is down and selecting bool mouse_move_selecting_ = false; - bool context_menu_dirty_ = true; - std::unique_ptr<components::PopupMenu> context_menu_; + DeleteLaterPtr<components::PopupMenu> context_menu_; }; } // namespace cru::ui::controls diff --git a/src/ui/components/Component.cpp b/src/ui/components/Component.cpp index 01db8710..d0525a1c 100644 --- a/src/ui/components/Component.cpp +++ b/src/ui/components/Component.cpp @@ -4,4 +4,14 @@ namespace cru::ui::components { void Component::OnPrepareDelete() { GetRootControl()->RemoveFromParent(); } + +void Component::DeleteIfDeleteByParent(bool delete_later) { + if (delete_by_parent_) { + if (delete_later) { + DeleteLater(); + } else { + delete this; + } + } +} } // namespace cru::ui::components diff --git a/src/ui/components/Menu.cpp b/src/ui/components/Menu.cpp index 65c1e810..04227d66 100644 --- a/src/ui/components/Menu.cpp +++ b/src/ui/components/Menu.cpp @@ -33,7 +33,7 @@ Menu::Menu() { Menu::~Menu() { for (auto item : items_) { - delete item; + item->DeleteIfDeleteByParent(false); } } diff --git a/src/ui/controls/Control.cpp b/src/ui/controls/Control.cpp index cf0cc11f..93213ecf 100644 --- a/src/ui/controls/Control.cpp +++ b/src/ui/controls/Control.cpp @@ -27,6 +27,11 @@ Control::Control() { } Control::~Control() { + if (host::WindowHost::IsInEventHandling()) { + // Don't delete control during event handling. Use DeleteLater. + std::terminate(); + } + in_destruction_ = true; RemoveFromParent(); } diff --git a/src/ui/controls/TextHostControlService.cpp b/src/ui/controls/TextHostControlService.cpp index 06c1651f..99090951 100644 --- a/src/ui/controls/TextHostControlService.cpp +++ b/src/ui/controls/TextHostControlService.cpp @@ -148,8 +148,7 @@ std::vector<TextControlMovePattern> TextControlMovePattern::kDefaultPatterns = { TextHostControlService::TextHostControlService(Control* control) : control_(control), - text_host_control_(dynamic_cast<ITextHostControl*>(control)), - context_menu_(new components::PopupMenu()) { + text_host_control_(dynamic_cast<ITextHostControl*>(control)) { SetUpShortcuts(); SetupOneHandler(&Control::MouseMoveEvent, @@ -701,6 +700,7 @@ void TextHostControlService::SetUpShortcuts() { void TextHostControlService::OpenContextMenu(const Point& position, ContextMenuItem items) { CRU_LOG_TAG_DEBUG("Open context menu."); + context_menu_.reset(new components::PopupMenu()); auto menu = context_menu_->GetMenu(); menu->ClearItems(); if (items & ContextMenuItem::kSelectAll) { |
