aboutsummaryrefslogtreecommitdiff
path: root/src/ui/host/RoutedEventDispatch.h
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-11-17 12:54:55 +0800
committerYuqian Yang <crupest@crupest.life>2025-11-17 12:54:55 +0800
commit0f8f98b9005803ab154b43dcad0db1f292072a4d (patch)
tree0a43d5a9c4e3b747ad955fc30a143aa07ab5888d /src/ui/host/RoutedEventDispatch.h
parentb68f9f52a3ecdd8e379dd60ac1c1366e76695464 (diff)
downloadcru-0f8f98b9005803ab154b43dcad0db1f292072a4d.tar.gz
cru-0f8f98b9005803ab154b43dcad0db1f292072a4d.tar.bz2
cru-0f8f98b9005803ab154b43dcad0db1f292072a4d.zip
Refactor window host.
Diffstat (limited to 'src/ui/host/RoutedEventDispatch.h')
-rw-r--r--src/ui/host/RoutedEventDispatch.h122
1 files changed, 0 insertions, 122 deletions
diff --git a/src/ui/host/RoutedEventDispatch.h b/src/ui/host/RoutedEventDispatch.h
deleted file mode 100644
index 6e8b4af1..00000000
--- a/src/ui/host/RoutedEventDispatch.h
+++ /dev/null
@@ -1,122 +0,0 @@
-#pragma once
-#include "cru/base/log/Logger.h"
-#include "cru/ui/DebugFlags.h"
-#include "cru/ui/controls/Control.h"
-#include "cru/ui/host/WindowHost.h"
-
-#include <vector>
-
-namespace cru::ui::host {
-// Dispatch the event.
-//
-// This will raise routed event of the control and its parent and parent's
-// parent ... (until "last_receiver" if it's not nullptr) with appropriate args.
-//
-// First tunnel from top to bottom possibly stopped by "handled" flag in
-// EventArgs. Second bubble from bottom to top possibly stopped by "handled"
-// flag in EventArgs. Last direct to each control.
-//
-// Args is of type "EventArgs". The first init argument is "sender", which is
-// automatically bound to each receiving control. The second init argument is
-// "original_sender", which is unchanged. And "args" will be perfectly forwarded
-// as the rest arguments.
-template <typename EventArgs, typename... Args>
-void DispatchEvent(
- const std::string& event_name, controls::Control* const original_sender,
- events::RoutedEvent<EventArgs>* (controls::Control::*event_ptr)(),
- controls::Control* const last_receiver, Args&&... args) {
- constexpr auto kLogTag = "DispatchEvent";
-
- if (original_sender == nullptr) return;
-
- CRU_UNUSED(event_name)
-
- if (original_sender == last_receiver) {
- if constexpr (debug_flags::routed_event)
- CRU_LOG_TAG_DEBUG(
- "Routed event {} no need to dispatch (original_sender == "
- "last_receiver). Original sender is {}.",
- event_name, original_sender->GetControlType());
- return;
- }
-
- WindowHost::EnterEventHandling();
-
- std::vector<controls::Control*> receive_list;
-
- auto parent = original_sender;
- while (parent != last_receiver) {
- receive_list.push_back(parent);
- auto p = parent->GetParent();
- assert(!(p == nullptr && last_receiver != nullptr));
- parent = p;
- }
-
- if constexpr (debug_flags::routed_event) {
- std::string log = "Dispatch routed event ";
- log += event_name;
- log += ". Path (parent first): ";
- auto i = receive_list.crbegin();
- const auto end = --receive_list.crend();
- for (; i != end; ++i) {
- log += (*i)->GetControlType();
- log += " -> ";
- }
- log += (*i)->GetControlType();
- CRU_LOG_TAG_DEBUG("{}", log);
- }
-
- auto handled = false;
-
- int count = 0;
-
- // tunnel
- for (auto i = receive_list.crbegin(); i != receive_list.crend(); ++i) {
- count++;
- auto control = *i;
- 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;
- if constexpr (debug_flags::routed_event)
- CRU_LOG_TAG_DEBUG(
- "Routed event is short-circuit in TUNNEL at {}-st control (count "
- "from parent).",
- count);
- break;
- }
- }
-
- // bubble
- if (!handled) {
- for (auto control : receive_list) {
- count--;
- 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)
- CRU_LOG_TAG_DEBUG(
- "Routed event is short-circuit in BUBBLE at {}-st control "
- "(count from parent).",
- count);
- break;
- }
- }
- }
-
- // direct
- for (auto control : receive_list) {
- EventArgs event_args(control, original_sender, std::forward<Args>(args)...);
- static_cast<Event<EventArgs&>*>((control->*event_ptr)()->Direct())
- ->Raise(event_args);
- }
-
- if constexpr (debug_flags::routed_event)
- CRU_LOG_TAG_DEBUG("Routed event dispatch finished.");
-
- WindowHost::LeaveEventHandling();
-}
-} // namespace cru::ui::host