aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/cru/base/Event.h12
-rw-r--r--include/cru/ui/DeleteLater.h61
-rw-r--r--include/cru/ui/events/RoutedEvent.h15
-rw-r--r--src/ThemeBuilder/components/StyleRuleSetEditor.cpp2
-rw-r--r--src/ThemeBuilder/components/conditions/CompoundConditionEditor.cpp8
-rw-r--r--src/ThemeBuilder/components/stylers/CompoundStylerEditor.cpp5
-rw-r--r--src/ui/DeleteLater.cpp8
-rw-r--r--src/ui/controls/TextHostControlService.cpp4
-rw-r--r--src/ui/host/WindowHost.cpp1
9 files changed, 46 insertions, 70 deletions
diff --git a/include/cru/base/Event.h b/include/cru/base/Event.h
index 1f57e100..226c2ea4 100644
--- a/include/cru/base/Event.h
+++ b/include/cru/base/Event.h
@@ -16,7 +16,11 @@ class EventHandlerRevoker;
class EventBase : public Object, public SelfResolvable<EventBase> {
friend EventHandlerRevoker;
+ public:
+ CRU_DELETE_COPY(EventBase)
+
protected:
+ EventBase() = default;
using EventHandlerToken = int;
/**
@@ -33,7 +37,7 @@ class EventHandlerRevoker {
private:
EventHandlerRevoker(ObjectResolver<EventBase>&& resolver,
- EventBase::EventHandlerToken token)
+ EventBase::EventHandlerToken token)
: resolver_(std::move(resolver)), token_(token) {}
public:
@@ -163,13 +167,15 @@ class EventHandlerRevokerGuard {
}
private:
- std::unique_ptr<EventHandlerRevoker, details::EventHandlerRevokerDestroyer> revoker_;
+ std::unique_ptr<EventHandlerRevoker, details::EventHandlerRevokerDestroyer>
+ revoker_;
};
class EventHandlerRevokerListGuard {
public:
void Add(EventHandlerRevoker&& revoker) {
- event_revoker_guard_list_.push_back(EventHandlerRevokerGuard(std::move(revoker)));
+ event_revoker_guard_list_.push_back(
+ EventHandlerRevokerGuard(std::move(revoker)));
}
EventHandlerRevokerListGuard& operator+=(EventHandlerRevoker&& revoker) {
diff --git a/include/cru/ui/DeleteLater.h b/include/cru/ui/DeleteLater.h
index 0f04c73a..d04d1814 100644
--- a/include/cru/ui/DeleteLater.h
+++ b/include/cru/ui/DeleteLater.h
@@ -1,65 +1,42 @@
#pragma once
#include "Base.h"
+#include <cru/base/Guard.h>
#include <memory>
#include <utility>
namespace cru::ui {
class CRU_UI_API DeleteLaterImpl {
public:
+ DeleteLaterImpl();
virtual ~DeleteLaterImpl();
void DeleteLater();
protected:
virtual void OnPrepareDelete();
+
+ private:
+ bool delete_later_scheduled_;
};
+namespace details {
template <typename T>
-class DeleteLaterPtr {
- public:
- DeleteLaterPtr() = default;
-
- DeleteLaterPtr(const DeleteLaterPtr& other) = delete;
- DeleteLaterPtr& operator=(const DeleteLaterPtr& other) = delete;
-
- DeleteLaterPtr(DeleteLaterPtr&& other) noexcept : ptr_(other.ptr_) {
- other.ptr_ = nullptr;
- }
-
- DeleteLaterPtr& operator=(DeleteLaterPtr&& other) noexcept {
- if (this != &other) {
- if (ptr_ != nullptr) {
- ptr_->DeleteLater();
- }
-
- ptr_ = other.ptr_;
- other.ptr_ = nullptr;
- }
- return *this;
- }
-
- ~DeleteLaterPtr() {
- if (ptr_ != nullptr) {
- ptr_->DeleteLater();
- }
- }
-
- explicit DeleteLaterPtr(T* ptr) : ptr_(ptr) {}
- DeleteLaterPtr(std::unique_ptr<T>&& ptr) : ptr_(ptr.release()) {}
-
- T& operator*() const { return *ptr_; }
- T* operator->() const { return ptr_; }
-
- explicit operator bool() const { return ptr_ != nullptr; }
+struct DeleteLaterPtrDeleter {
+ void operator()(T* p) const noexcept { p->DeleteLater(); }
+};
+} // namespace details
- T* get() const { return ptr_; }
+template <typename T>
+using DeleteLaterPtr = std::unique_ptr<T, details::DeleteLaterPtrDeleter<T>>;
- private:
- T* ptr_ = nullptr;
-};
+template <typename T>
+DeleteLaterPtr<T> ToDeleteLaterPtr(std::unique_ptr<T>&& p) {
+ return DeleteLaterPtr<T>(p.release());
+}
template <typename T, typename... Args>
-DeleteLaterPtr<T> MakeDeleteLaterPtr(Args&&... args) {
- return DeleteLaterPtr<T>(new T(std::forward<Args>(args)...));
+DeleteLaterPtr<T> MakeDeleteLater(Args&&... args) {
+ return DeleteLaterPtr<T>(std::forward<Args>(args)...);
}
+
} // namespace cru::ui
diff --git a/include/cru/ui/events/RoutedEvent.h b/include/cru/ui/events/RoutedEvent.h
index 9a07e1e9..aa3331a6 100644
--- a/include/cru/ui/events/RoutedEvent.h
+++ b/include/cru/ui/events/RoutedEvent.h
@@ -1,7 +1,7 @@
#pragma once
#include "UiEventArgs.h"
-#include "cru/base/Event.h"
+#include <cru/base/Event.h>
namespace cru::ui::events {
// TEventArgs must not be a reference type. This class help add reference.
@@ -14,21 +14,10 @@ class CRU_UI_API RoutedEvent {
static_assert(!std::is_reference_v<TEventArgs>,
"TEventArgs must not be reference.");
- using RawEventArgs = TEventArgs;
- using IEventType = IEvent<TEventArgs&>;
- using EventArgs = typename IEventType::Args;
-
- RoutedEvent() = default;
- RoutedEvent(const RoutedEvent& other) = delete;
- RoutedEvent(RoutedEvent&& other) = delete;
- RoutedEvent& operator=(const RoutedEvent& other) = delete;
- RoutedEvent& operator=(RoutedEvent&& other) = delete;
- ~RoutedEvent() = default;
+ using EventArgs = TEventArgs&;
IEvent<TEventArgs&>* Direct() { return &direct_; }
-
IEvent<TEventArgs&>* Bubble() { return &bubble_; }
-
IEvent<TEventArgs&>* Tunnel() { return &tunnel_; }
private:
diff --git a/src/ThemeBuilder/components/StyleRuleSetEditor.cpp b/src/ThemeBuilder/components/StyleRuleSetEditor.cpp
index a2dd3177..f2509f4f 100644
--- a/src/ThemeBuilder/components/StyleRuleSetEditor.cpp
+++ b/src/ThemeBuilder/components/StyleRuleSetEditor.cpp
@@ -66,7 +66,7 @@ void StyleRuleSetEditor::UpdateView(
for (auto i = change->position; i < change->position + change->count;
++i) {
const auto& rule = style_rule_set->GetStyleRule(i);
- auto style_rule_editor = ui::MakeDeleteLaterPtr<StyleRuleEditor>();
+ auto style_rule_editor = ui::MakeDeleteLater<StyleRuleEditor>();
style_rule_editor->SetValue(rule, false);
style_rule_editor->RemoveEvent()->AddSpyOnlyHandler(
[this, editor = style_rule_editor.get()] {
diff --git a/src/ThemeBuilder/components/conditions/CompoundConditionEditor.cpp b/src/ThemeBuilder/components/conditions/CompoundConditionEditor.cpp
index d01ceca0..b9b1fdef 100644
--- a/src/ThemeBuilder/components/conditions/CompoundConditionEditor.cpp
+++ b/src/ThemeBuilder/components/conditions/CompoundConditionEditor.cpp
@@ -7,6 +7,7 @@
#include "cru/base/ClonePtr.h"
#include "cru/platform/Color.h"
#include "cru/ui/Base.h"
+#include "cru/ui/DeleteLater.h"
#include "cru/ui/ThemeManager.h"
#include "cru/ui/controls/FlexLayout.h"
#include "cru/ui/style/Condition.h"
@@ -67,7 +68,7 @@ CompoundConditionEditor::CompoundConditionEditor() {
this->children_container_.RemoveChildAt(index);
RaiseChangeEvent();
});
- children_.push_back(std::move(editor));
+ children_.push_back(ui::ToDeleteLaterPtr(std::move(editor)));
children_container_.AddChild(children_.back()->GetRootControl());
RaiseChangeEvent();
}
@@ -86,8 +87,7 @@ CompoundConditionEditor::GetChildren() {
}
void CompoundConditionEditor::SetChildren(
- std::vector<ClonePtr<ui::style::Condition>> children,
- bool trigger_change) {
+ std::vector<ClonePtr<ui::style::Condition>> children, bool trigger_change) {
children_container_.ClearChildren();
children_.clear();
for (const auto& condition : children) {
@@ -99,7 +99,7 @@ void CompoundConditionEditor::SetChildren(
this->children_container_.RemoveChildAt(index);
RaiseChangeEvent();
});
- children_.push_back(std::move(editor));
+ children_.push_back(ui::ToDeleteLaterPtr(std::move(editor)));
children_container_.AddChild(children_.back()->GetRootControl());
}
if (trigger_change) {
diff --git a/src/ThemeBuilder/components/stylers/CompoundStylerEditor.cpp b/src/ThemeBuilder/components/stylers/CompoundStylerEditor.cpp
index 15f4e912..1a20bf70 100644
--- a/src/ThemeBuilder/components/stylers/CompoundStylerEditor.cpp
+++ b/src/ThemeBuilder/components/stylers/CompoundStylerEditor.cpp
@@ -7,6 +7,7 @@
#include "PaddingStylerEditor.h"
#include "PreferredSizeStylerEditor.h"
#include "cru/base/ClonePtr.h"
+#include "cru/ui/DeleteLater.h"
#include "cru/ui/ThemeManager.h"
#include "cru/ui/style/Styler.h"
@@ -69,7 +70,7 @@ CompoundStylerEditor::CompoundStylerEditor() {
this->children_container_.RemoveChildAt(index);
RaiseChangeEvent();
});
- children_.push_back(std::move(editor));
+ children_.push_back(ui::ToDeleteLaterPtr(std::move(editor)));
children_container_.AddChild(editor->GetRootControl());
RaiseChangeEvent();
}
@@ -98,7 +99,7 @@ void CompoundStylerEditor::SetValue(ui::style::CompoundStyler* value,
this->children_container_.RemoveChildAt(index);
RaiseChangeEvent();
});
- children_.push_back(std::move(editor));
+ children_.push_back(ui::ToDeleteLaterPtr(std::move(editor)));
children_container_.AddChild(children_.back()->GetRootControl());
}
}
diff --git a/src/ui/DeleteLater.cpp b/src/ui/DeleteLater.cpp
index f8911ae1..499b9b34 100644
--- a/src/ui/DeleteLater.cpp
+++ b/src/ui/DeleteLater.cpp
@@ -4,10 +4,16 @@
#include "cru/platform/gui/UiApplication.h"
namespace cru::ui {
+
+DeleteLaterImpl::DeleteLaterImpl() : delete_later_scheduled_(false) {}
+
DeleteLaterImpl::~DeleteLaterImpl() {}
void DeleteLaterImpl::DeleteLater() {
- GetUiApplication()->SetImmediate([this] { delete this; });
+ if (!delete_later_scheduled_) {
+ GetUiApplication()->SetImmediate([this] { delete this; });
+ delete_later_scheduled_ = true;
+ }
}
void DeleteLaterImpl::OnPrepareDelete() {}
diff --git a/src/ui/controls/TextHostControlService.cpp b/src/ui/controls/TextHostControlService.cpp
index 3c38c454..235e6e6a 100644
--- a/src/ui/controls/TextHostControlService.cpp
+++ b/src/ui/controls/TextHostControlService.cpp
@@ -150,8 +150,6 @@ std::vector<TextControlMovePattern> TextControlMovePattern::kDefaultPatterns = {
TextHostControlService::TextHostControlService(Control* control)
: control_(control),
text_host_control_(dynamic_cast<ITextHostControl*>(control)) {
- context_menu_ = MakeDeleteLaterPtr<components::PopupMenu>();
-
SetUpShortcuts();
SetupOneHandler(&Control::MouseMoveEvent,
@@ -701,7 +699,7 @@ void TextHostControlService::SetUpShortcuts() {
void TextHostControlService::OpenContextMenu(const Point& position,
ContextMenuItem items) {
- context_menu_ = MakeDeleteLaterPtr<components::PopupMenu>();
+ context_menu_ = MakeDeleteLater<components::PopupMenu>();
auto menu = context_menu_->GetMenu();
if (items & ContextMenuItem::kSelectAll) {
menu->AddTextItem("Select All", [this] { this->SelectAll(); });
diff --git a/src/ui/host/WindowHost.cpp b/src/ui/host/WindowHost.cpp
index 7417740d..ec956bb4 100644
--- a/src/ui/host/WindowHost.cpp
+++ b/src/ui/host/WindowHost.cpp
@@ -4,7 +4,6 @@
#include "cru/base/Base.h"
#include "cru/base/log/Logger.h"
#include "cru/platform/graphics/Painter.h"
-#include "cru/platform/gui/InputMethod.h"
#include "cru/platform/gui/UiApplication.h"
#include "cru/platform/gui/Window.h"
#include "cru/ui/Base.h"