| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef UI_BASE_EVENTS_EVENT_DISPATCHER_H_ | 5 #ifndef UI_BASE_EVENTS_EVENT_DISPATCHER_H_ |
| 6 #define UI_BASE_EVENTS_EVENT_DISPATCHER_H_ | 6 #define UI_BASE_EVENTS_EVENT_DISPATCHER_H_ |
| 7 | 7 |
| 8 #include "ui/base/events/event.h" | 8 #include "ui/base/events/event.h" |
| 9 #include "ui/base/events/event_constants.h" | 9 #include "ui/base/events/event_constants.h" |
| 10 #include "ui/base/events/event_target.h" | 10 #include "ui/base/events/event_target.h" |
| 11 #include "ui/base/ui_export.h" | 11 #include "ui/base/ui_export.h" |
| 12 | 12 |
| 13 namespace ui { | 13 namespace ui { |
| 14 | 14 |
| 15 // Dispatches events to appropriate targets. | 15 // Dispatches events to appropriate targets. |
| 16 class UI_EXPORT EventDispatcher { | 16 class UI_EXPORT EventDispatcher { |
| 17 public: | 17 public: |
| 18 EventDispatcher(); | 18 EventDispatcher(); |
| 19 virtual ~EventDispatcher(); | 19 virtual ~EventDispatcher(); |
| 20 | 20 |
| 21 // Returns whether an event can still be dispatched to a target. (e.g. during | 21 // Returns whether an event can still be dispatched to a target. (e.g. during |
| 22 // event dispatch, one of the handlers may have destroyed the target, in which | 22 // event dispatch, one of the handlers may have destroyed the target, in which |
| 23 // case the event can no longer be dispatched to the target). | 23 // case the event can no longer be dispatched to the target). |
| 24 virtual bool CanDispatchToTarget(EventTarget* target) = 0; | 24 virtual bool CanDispatchToTarget(EventTarget* target) = 0; |
| 25 | 25 |
| 26 // Allows the subclass to add additional event handlers to the dispatch | |
| 27 // sequence. | |
| 28 virtual void ProcessPreTargetList(EventHandlerList* list) = 0; | |
| 29 virtual void ProcessPostTargetList(EventHandlerList* list) = 0; | |
| 30 | |
| 31 template<class T> | 26 template<class T> |
| 32 int ProcessEvent(EventTarget* target, T* event) { | 27 int ProcessEvent(EventTarget* target, T* event) { |
| 33 if (!target || !target->CanAcceptEvents()) | 28 if (!target || !target->CanAcceptEvents()) |
| 34 return ER_UNHANDLED; | 29 return ER_UNHANDLED; |
| 35 | 30 |
| 36 ScopedDispatchHelper dispatch_helper(event); | 31 ScopedDispatchHelper dispatch_helper(event); |
| 37 dispatch_helper.set_target(target); | 32 dispatch_helper.set_target(target); |
| 38 | 33 |
| 39 EventHandlerList list; | 34 EventHandlerList list; |
| 40 target->GetPreTargetHandlers(&list); | 35 target->GetPreTargetHandlers(&list); |
| 41 ProcessPreTargetList(&list); | |
| 42 dispatch_helper.set_phase(EP_PRETARGET); | 36 dispatch_helper.set_phase(EP_PRETARGET); |
| 43 int result = DispatchEventToEventHandlers(list, event); | 37 int result = DispatchEventToEventHandlers(list, event); |
| 44 if (result & ER_CONSUMED) | 38 if (result & ER_CONSUMED) |
| 45 return result; | 39 return result; |
| 46 | 40 |
| 47 // If the event hasn't been consumed, trigger the default handler. Note that | 41 // If the event hasn't been consumed, trigger the default handler. Note that |
| 48 // even if the event has already been handled (i.e. return result has | 42 // even if the event has already been handled (i.e. return result has |
| 49 // ER_HANDLED set), that means that the event should still be processed at | 43 // ER_HANDLED set), that means that the event should still be processed at |
| 50 // this layer, however it should not be processed in the next layer of | 44 // this layer, however it should not be processed in the next layer of |
| 51 // abstraction. | 45 // abstraction. |
| 52 if (CanDispatchToTarget(target)) { | 46 if (CanDispatchToTarget(target)) { |
| 53 dispatch_helper.set_phase(EP_TARGET); | 47 dispatch_helper.set_phase(EP_TARGET); |
| 54 result |= DispatchEventToSingleHandler(target, event); | 48 result |= DispatchEventToSingleHandler(target, event); |
| 55 dispatch_helper.set_result(event->result() | result); | 49 dispatch_helper.set_result(event->result() | result); |
| 56 if (result & ER_CONSUMED) | 50 if (result & ER_CONSUMED) |
| 57 return result; | 51 return result; |
| 58 } | 52 } |
| 59 | 53 |
| 60 if (!CanDispatchToTarget(target)) | 54 if (!CanDispatchToTarget(target)) |
| 61 return result; | 55 return result; |
| 62 | 56 |
| 63 list.clear(); | 57 list.clear(); |
| 64 target->GetPostTargetHandlers(&list); | 58 target->GetPostTargetHandlers(&list); |
| 65 ProcessPostTargetList(&list); | |
| 66 dispatch_helper.set_phase(EP_POSTTARGET); | 59 dispatch_helper.set_phase(EP_POSTTARGET); |
| 67 result |= DispatchEventToEventHandlers(list, event); | 60 result |= DispatchEventToEventHandlers(list, event); |
| 68 return result; | 61 return result; |
| 69 } | 62 } |
| 70 | 63 |
| 71 private: | 64 private: |
| 72 class UI_EXPORT ScopedDispatchHelper : public NON_EXPORTED_BASE( | 65 class UI_EXPORT ScopedDispatchHelper : public NON_EXPORTED_BASE( |
| 73 Event::DispatcherApi) { | 66 Event::DispatcherApi) { |
| 74 public: | 67 public: |
| 75 explicit ScopedDispatchHelper(Event* event); | 68 explicit ScopedDispatchHelper(Event* event); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 102 TouchEvent* event); | 95 TouchEvent* event); |
| 103 EventResult DispatchEventToSingleHandler(EventHandler* handler, | 96 EventResult DispatchEventToSingleHandler(EventHandler* handler, |
| 104 GestureEvent* event); | 97 GestureEvent* event); |
| 105 | 98 |
| 106 DISALLOW_COPY_AND_ASSIGN(EventDispatcher); | 99 DISALLOW_COPY_AND_ASSIGN(EventDispatcher); |
| 107 }; | 100 }; |
| 108 | 101 |
| 109 } // namespace ui | 102 } // namespace ui |
| 110 | 103 |
| 111 #endif // UI_BASE_EVENTS_EVENT_DISPATCHER_H_ | 104 #endif // UI_BASE_EVENTS_EVENT_DISPATCHER_H_ |
| OLD | NEW |