diff options
Diffstat (limited to 'src/ui/controls')
-rw-r--r-- | src/ui/controls/Control.cpp | 15 | ||||
-rw-r--r-- | src/ui/controls/TextHostControlService.cpp | 21 |
2 files changed, 20 insertions, 16 deletions
diff --git a/src/ui/controls/Control.cpp b/src/ui/controls/Control.cpp index f2d4760e..3b0d4be3 100644 --- a/src/ui/controls/Control.cpp +++ b/src/ui/controls/Control.cpp @@ -1,5 +1,6 @@ #include "cru/ui/controls/Control.h" +#include "cru/common/log/Logger.h" #include "cru/platform/gui/Cursor.h" #include "cru/platform/gui/UiApplication.h" #include "cru/ui/host/WindowHost.h" @@ -28,6 +29,11 @@ Control::Control() { } Control::~Control() { + if (host::WindowHost::IsInEventHandling()) { + CRU_LOG_ERROR( + u"Control destroyed during event handling. Please use DeleteLater."); + } + in_destruction_ = true; RemoveFromParent(); } @@ -120,12 +126,7 @@ void Control::OnParentChangedCore(Control* old_parent, Control* new_parent) { void Control::OnWindowHostChangedCore(host::WindowHost* old_host, host::WindowHost* new_host) { if (old_host != nullptr) { - if (old_host->GetMouseCaptureControl() == this) { - old_host->CaptureMouseFor(nullptr); - } - if (old_host->GetMouseHoverControl() == this) { - old_host->mouse_hover_control_ = nullptr; - } + old_host->OnControlDetach(this); } if (!in_destruction_) { @@ -136,4 +137,6 @@ void Control::OnWindowHostChangedCore(host::WindowHost* old_host, OnWindowHostChanged(old_host, new_host); } } + +void Control::OnPrepareDelete() { RemoveFromParent(); } } // namespace cru::ui::controls diff --git a/src/ui/controls/TextHostControlService.cpp b/src/ui/controls/TextHostControlService.cpp index 15d9102c..d6c40a36 100644 --- a/src/ui/controls/TextHostControlService.cpp +++ b/src/ui/controls/TextHostControlService.cpp @@ -2,9 +2,9 @@ #include "../Helper.h" #include "cru/common/Base.h" -#include "cru/common/log/Logger.h" #include "cru/common/String.h" #include "cru/common/StringUtil.h" +#include "cru/common/log/Logger.h" #include "cru/platform/graphics/Font.h" #include "cru/platform/gui/Base.h" #include "cru/platform/gui/Clipboard.h" @@ -15,6 +15,7 @@ #include "cru/platform/gui/Window.h" #include "cru/ui/Base.h" #include "cru/ui/DebugFlags.h" +#include "cru/ui/DeleteLater.h" #include "cru/ui/components/Menu.h" #include "cru/ui/events/UiEvents.h" #include "cru/ui/helper/ShortcutHub.h" @@ -149,7 +150,7 @@ std::vector<TextControlMovePattern> TextControlMovePattern::kDefaultPatterns = { TextHostControlService::TextHostControlService(gsl::not_null<Control*> control) : control_(control), text_host_control_(dynamic_cast<ITextHostControl*>(control.get())) { - context_menu_ = std::make_unique<components::PopupMenu>(); + context_menu_ = MakeDeleteLaterPtr<components::PopupMenu>(); SetUpShortcuts(); @@ -335,11 +336,12 @@ void TextHostControlService::SetCaretBlinkDuration(int milliseconds) { void TextHostControlService::ScrollToCaret() { if (const auto scroll_render_object = this->GetScrollRenderObject()) { - this->control_->GetWindowHost()->RunAfterLayoutStable( - [this, scroll_render_object]() { - const auto caret_rect = this->GetTextRenderObject()->GetCaretRect(); - scroll_render_object->ScrollToContain(caret_rect, Thickness{5.f}); - }); + auto window_host = this->control_->GetWindowHost(); + if (window_host) + window_host->RunAfterLayoutStable([this, scroll_render_object]() { + const auto caret_rect = this->GetTextRenderObject()->GetCaretRect(); + scroll_render_object->ScrollToContain(caret_rect, Thickness{5.f}); + }); } } @@ -465,8 +467,7 @@ void TextHostControlService::UpdateInputMethodPosition() { right_bottom.y += 5; if constexpr (debug_flags::text_service) { - CRU_LOG_DEBUG( - u"Calculate input method candidate window position: {}.", + CRU_LOG_DEBUG(u"Calculate input method candidate window position: {}.", right_bottom); } @@ -693,7 +694,7 @@ void TextHostControlService::SetUpShortcuts() { void TextHostControlService::OpenContextMenu(const Point& position, ContextMenuItem items) { - context_menu_ = std::make_unique<components::PopupMenu>(); + context_menu_ = MakeDeleteLaterPtr<components::PopupMenu>(); auto menu = context_menu_->GetMenu(); if (items & ContextMenuItem::kSelectAll) { menu->AddTextItem(u"Select All", [this] { this->SelectAll(); }); |