event_target.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_EVENTS_EVENT_TARGET_H_
  5. #define UI_EVENTS_EVENT_TARGET_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/memory/raw_ptr.h"
  9. #include "ui/events/event_handler.h"
  10. #include "ui/events/events_export.h"
  11. #include "ui/gfx/geometry/point.h"
  12. #include "ui/gfx/geometry/point_f.h"
  13. namespace ui {
  14. class EventDispatcher;
  15. class EventTargeter;
  16. class EventTargetIterator;
  17. class LocatedEvent;
  18. class EVENTS_EXPORT EventTarget {
  19. public:
  20. class DispatcherApi {
  21. public:
  22. explicit DispatcherApi(EventTarget* target) : target_(target) {}
  23. DispatcherApi(const DispatcherApi&) = delete;
  24. DispatcherApi& operator=(const DispatcherApi&) = delete;
  25. private:
  26. DispatcherApi();
  27. raw_ptr<EventTarget> target_;
  28. };
  29. EventTarget();
  30. EventTarget(const EventTarget&) = delete;
  31. EventTarget& operator=(const EventTarget&) = delete;
  32. virtual ~EventTarget();
  33. virtual bool CanAcceptEvent(const Event& event) = 0;
  34. // Returns the parent EventTarget in the event-target tree.
  35. virtual EventTarget* GetParentTarget() = 0;
  36. // Returns an iterator an EventTargeter can use to iterate over the list of
  37. // child EventTargets.
  38. virtual std::unique_ptr<EventTargetIterator> GetChildIterator() const = 0;
  39. // Returns the EventTargeter that should be used to find the target for an
  40. // event in the subtree rooted at this EventTarget.
  41. virtual EventTargeter* GetEventTargeter() = 0;
  42. // Updates the states in |event| (e.g. location) to be suitable for |target|,
  43. // so that |event| can be dispatched to |target|.
  44. virtual void ConvertEventToTarget(const EventTarget* target,
  45. LocatedEvent* event) const;
  46. // Get |event|'s screen location, using the EventTarget's screen location.
  47. virtual gfx::PointF GetScreenLocationF(const LocatedEvent& event) const;
  48. gfx::Point GetScreenLocation(const LocatedEvent& event) const;
  49. // Priority levels for PreTargetHandlers.
  50. enum class Priority {
  51. // The Accessibility level is the highest, and gets events before
  52. // other priority levels. This allows accessibility features to
  53. // modify events directly from the user.
  54. kAccessibility,
  55. // System priority EventHandlers get events before default level, and
  56. // should be used for drag and drop, menus, etc.
  57. kSystem,
  58. // The default level should be used by most EventHandlers.
  59. kDefault,
  60. };
  61. // Adds a handler to receive events before the target. The handler must be
  62. // explicitly removed from the target before the handler is destroyed. The
  63. // EventTarget does not take ownership of the handler.
  64. void AddPreTargetHandler(EventHandler* handler);
  65. void AddPreTargetHandler(EventHandler* handler, Priority priority);
  66. void RemovePreTargetHandler(EventHandler* handler);
  67. // Adds a handler to receive events after the target. The handler must be
  68. // explicitly removed from the target before the handler is destroyed. The
  69. // EventTarget does not take ownership of the handler.
  70. void AddPostTargetHandler(EventHandler* handler);
  71. void RemovePostTargetHandler(EventHandler* handler);
  72. // Returns true if the event pre target list is empty.
  73. bool IsPreTargetListEmpty() const;
  74. // Sets |target_handler| as |target_handler_| and returns the old handler.
  75. EventHandler* SetTargetHandler(EventHandler* target_handler);
  76. bool HasTargetHandler() const { return target_handler_ != nullptr; }
  77. protected:
  78. EventHandler* target_handler() { return target_handler_; }
  79. private:
  80. friend class EventDispatcher;
  81. friend class EventTargetTestApi;
  82. // A handler with a priority.
  83. struct PrioritizedHandler {
  84. EventHandler* handler = nullptr;
  85. Priority priority = Priority::kDefault;
  86. bool operator<(const PrioritizedHandler& ph) const {
  87. return priority < ph.priority;
  88. }
  89. };
  90. using EventHandlerPriorityList = std::vector<PrioritizedHandler>;
  91. // Returns the list of handlers that should receive the event before the
  92. // target. The handlers from the outermost target are first in the list, and
  93. // the handlers on |this| are the last in the list.
  94. void GetPreTargetHandlers(EventHandlerList* list);
  95. // Returns the list of handlers that should receive the event after the
  96. // target. The handlers from the outermost target are last in the list, and
  97. // the handlers on |this| are the first in the list.
  98. void GetPostTargetHandlers(EventHandlerList* list);
  99. EventHandlerPriorityList pre_target_list_;
  100. EventHandlerList post_target_list_;
  101. // TODO(crbug.com/1298696): Breaks content_unittests.
  102. raw_ptr<EventHandler, DegradeToNoOpWhenMTE> target_handler_ = nullptr;
  103. };
  104. } // namespace ui
  105. #endif // UI_EVENTS_EVENT_TARGET_H_