event_dispatcher.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_DISPATCHER_H_
  5. #define UI_EVENTS_EVENT_DISPATCHER_H_
  6. #include "base/auto_reset.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "ui/events/event.h"
  9. #include "ui/events/event_constants.h"
  10. #include "ui/events/event_handler.h"
  11. #include "ui/events/events_export.h"
  12. namespace ui {
  13. class EventDispatcher;
  14. class EventTarget;
  15. struct EventDispatchDetails {
  16. bool dispatcher_destroyed = false;
  17. bool target_destroyed = false;
  18. // Set to true if an EventRewriter discards the event.
  19. bool event_discarded = false;
  20. };
  21. class EVENTS_EXPORT EventDispatcherDelegate {
  22. public:
  23. EventDispatcherDelegate();
  24. EventDispatcherDelegate(const EventDispatcherDelegate&) = delete;
  25. EventDispatcherDelegate& operator=(const EventDispatcherDelegate&) = delete;
  26. virtual ~EventDispatcherDelegate();
  27. // Returns whether an event can still be dispatched to a target. (e.g. during
  28. // event dispatch, one of the handlers may have destroyed the target, in which
  29. // case the event can no longer be dispatched to the target).
  30. virtual bool CanDispatchToTarget(EventTarget* target) = 0;
  31. // Returns the event being dispatched (or NULL if no event is being
  32. // dispatched).
  33. Event* current_event();
  34. // Dispatches |event| to |target|. This calls |PreDispatchEvent()| before
  35. // dispatching the event, and |PostDispatchEvent()| after the event has been
  36. // dispatched.
  37. [[nodiscard]] EventDispatchDetails DispatchEvent(EventTarget* target,
  38. Event* event);
  39. protected:
  40. // This is called once a target has been determined for an event, right before
  41. // the event is dispatched to the target. This function may modify |event| to
  42. // prepare it for dispatch (e.g. update event flags, location etc.).
  43. [[nodiscard]] virtual EventDispatchDetails PreDispatchEvent(
  44. EventTarget* target,
  45. Event* event);
  46. // This is called right after the event dispatch is completed.
  47. // |target| is NULL if the target was deleted during dispatch.
  48. [[nodiscard]] virtual EventDispatchDetails PostDispatchEvent(
  49. EventTarget* target,
  50. const Event& event);
  51. private:
  52. // Dispatches the event to the target.
  53. [[nodiscard]] EventDispatchDetails DispatchEventToTarget(EventTarget* target,
  54. Event* event);
  55. raw_ptr<EventDispatcher> dispatcher_;
  56. };
  57. // Dispatches events to appropriate targets.
  58. class EVENTS_EXPORT EventDispatcher {
  59. public:
  60. explicit EventDispatcher(EventDispatcherDelegate* delegate);
  61. EventDispatcher(const EventDispatcher&) = delete;
  62. EventDispatcher& operator=(const EventDispatcher&) = delete;
  63. virtual ~EventDispatcher();
  64. void ProcessEvent(EventTarget* target, Event* event);
  65. const Event* current_event() const { return current_event_; }
  66. Event* current_event() { return current_event_; }
  67. bool delegate_destroyed() const { return !delegate_; }
  68. void OnHandlerDestroyed(EventHandler* handler);
  69. void OnDispatcherDelegateDestroyed();
  70. private:
  71. void DispatchEventToEventHandlers(EventHandlerList* list, Event* event);
  72. // Dispatches an event, and makes sure it sets ER_CONSUMED on the
  73. // event-handling result if the dispatcher itself has been destroyed during
  74. // dispatching the event to the event handler.
  75. void DispatchEvent(EventHandler* handler, Event* event);
  76. raw_ptr<EventDispatcherDelegate> delegate_;
  77. Event* current_event_ = nullptr;
  78. EventHandlerList handler_list_;
  79. };
  80. } // namespace ui
  81. #endif // UI_EVENTS_EVENT_DISPATCHER_H_