aboutsummaryrefslogtreecommitdiff
path: root/src/ui
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/helper/ClickDetector.cpp10
-rw-r--r--src/ui/host/RoutedEventDispatch.h32
2 files changed, 25 insertions, 17 deletions
diff --git a/src/ui/helper/ClickDetector.cpp b/src/ui/helper/ClickDetector.cpp
index 8ebdfabc..f76f8af4 100644
--- a/src/ui/helper/ClickDetector.cpp
+++ b/src/ui/helper/ClickDetector.cpp
@@ -58,8 +58,7 @@ ClickDetector::ClickDetector(controls::Control* control) {
this->state_ == ClickState::Hover) {
if (!this->control_->CaptureMouse()) {
if constexpr (debug_flags::click_detector) {
- CRU_LOG_DEBUG(
- u"Failed to capture mouse when begin click.");
+ CRU_LOG_DEBUG(u"Failed to capture mouse when begin click.");
}
return;
}
@@ -77,10 +76,12 @@ ClickDetector::ClickDetector(controls::Control* control) {
button == button_) {
if (this->state_ == ClickState::Press) {
this->SetState(ClickState::Hover);
+ auto resolver = this->control_->CreateResolver();
this->event_.Raise(ClickEventArgs{this->control_,
this->down_point_,
args.GetPoint(), button});
- this->control_->ReleaseMouse();
+ auto c = resolver.Resolve();
+ if (c) c->ReleaseMouse();
} else if (this->state_ == ClickState::PressInactive) {
this->SetState(ClickState::None);
this->control_->ReleaseMouse();
@@ -136,8 +137,7 @@ void ClickDetector::SetState(ClickState state) {
UnreachableCode();
}
};
- CRU_LOG_DEBUG(u"Click state changed, new state: {}.",
- to_string(state));
+ CRU_LOG_DEBUG(u"Click state changed, new state: {}.", to_string(state));
}
state_ = state;
diff --git a/src/ui/host/RoutedEventDispatch.h b/src/ui/host/RoutedEventDispatch.h
index 3c2a98d8..2f437b31 100644
--- a/src/ui/host/RoutedEventDispatch.h
+++ b/src/ui/host/RoutedEventDispatch.h
@@ -1,4 +1,5 @@
#pragma once
+#include "cru/common/SelfResolvable.h"
#include "cru/common/log/Logger.h"
#include "cru/ui/DebugFlags.h"
#include "cru/ui/controls/Control.h"
@@ -39,11 +40,11 @@ void DispatchEvent(
return;
}
- std::vector<controls::Control*> receive_list;
+ std::vector<ObjectResolver<controls::Control>> receive_list;
auto parent = original_sender;
while (parent != last_receiver) {
- receive_list.push_back(parent);
+ receive_list.push_back(parent->CreateResolver());
auto p = parent->GetParent();
assert(!(p == nullptr && last_receiver != nullptr));
parent = p;
@@ -56,10 +57,10 @@ void DispatchEvent(
auto i = receive_list.crbegin();
const auto end = --receive_list.crend();
for (; i != end; ++i) {
- log += (*i)->GetControlType();
+ log += i->Resolve()->GetControlType();
log += u" -> ";
}
- log += (*i)->GetControlType();
+ log += i->Resolve()->GetControlType();
CRU_LOG_DEBUG(log);
}
@@ -70,8 +71,10 @@ void DispatchEvent(
// tunnel
for (auto i = receive_list.crbegin(); i != receive_list.crend(); ++i) {
count++;
- EventArgs event_args(*i, original_sender, std::forward<Args>(args)...);
- static_cast<Event<EventArgs&>*>(((*i)->*event_ptr)()->Tunnel())
+ auto control = i->Resolve();
+ if (!control) continue;
+ EventArgs event_args(control, original_sender, std::forward<Args>(args)...);
+ static_cast<Event<EventArgs&>*>((control->*event_ptr)()->Tunnel())
->Raise(event_args);
if (event_args.IsHandled()) {
handled = true;
@@ -86,10 +89,13 @@ void DispatchEvent(
// bubble
if (!handled) {
- for (auto i : receive_list) {
+ for (const auto& resolver : receive_list) {
count--;
- EventArgs event_args(i, original_sender, std::forward<Args>(args)...);
- static_cast<Event<EventArgs&>*>((i->*event_ptr)()->Bubble())
+ auto control = resolver.Resolve();
+ if (!control) continue;
+ EventArgs event_args(control, original_sender,
+ std::forward<Args>(args)...);
+ static_cast<Event<EventArgs&>*>((control->*event_ptr)()->Bubble())
->Raise(event_args);
if (event_args.IsHandled()) {
if constexpr (debug_flags::routed_event)
@@ -103,9 +109,11 @@ void DispatchEvent(
}
// direct
- for (auto i : receive_list) {
- EventArgs event_args(i, original_sender, std::forward<Args>(args)...);
- static_cast<Event<EventArgs&>*>((i->*event_ptr)()->Direct())
+ for (auto resolver : receive_list) {
+ auto control = resolver.Resolve();
+ if (!control) continue;
+ EventArgs event_args(control, original_sender, std::forward<Args>(args)...);
+ static_cast<Event<EventArgs&>*>((control->*event_ptr)()->Direct())
->Raise(event_args);
}