Chromium Code Reviews| 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" |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 // Allows the subclass to add additional event handlers to the dispatch | 26 // Allows the subclass to add additional event handlers to the dispatch |
| 27 // sequence. | 27 // sequence. |
| 28 virtual void ProcessPreTargetList(EventHandlerList* list) = 0; | 28 virtual void ProcessPreTargetList(EventHandlerList* list) = 0; |
| 29 virtual void ProcessPostTargetList(EventHandlerList* list) = 0; | 29 virtual void ProcessPostTargetList(EventHandlerList* list) = 0; |
| 30 | 30 |
| 31 template<class T> | 31 template<class T> |
| 32 int ProcessEvent(EventTarget* target, T* event) { | 32 int ProcessEvent(EventTarget* target, T* event) { |
| 33 if (!target || !target->CanAcceptEvents()) | 33 if (!target || !target->CanAcceptEvents()) |
| 34 return ER_UNHANDLED; | 34 return ER_UNHANDLED; |
| 35 | 35 |
| 36 Event::DispatcherApi dispatcher(event); | 36 Event::DispatcherApi dispatcher(event); |
|
Ben Goodger (Google)
2012/09/10 18:07:14
this variable name is short, but I find it a littl
sadrul
2012/09/10 18:35:24
Renamed to dispatch_helper
| |
| 37 dispatcher.set_target(target); | 37 dispatcher.set_target(target); |
| 38 | 38 |
| 39 EventHandlerList list; | 39 EventHandlerList list; |
| 40 target->GetPreTargetHandlers(&list); | 40 target->GetPreTargetHandlers(&list); |
| 41 ProcessPreTargetList(&list); | 41 ProcessPreTargetList(&list); |
| 42 dispatcher.set_phase(EP_PRETARGET); | |
| 42 int result = DispatchEventToEventHandlers(list, event); | 43 int result = DispatchEventToEventHandlers(list, event); |
| 43 if (result & ER_CONSUMED) | 44 if (result & ER_CONSUMED) { |
| 45 dispatcher.set_phase(EP_COMPLETE); | |
|
Ben Goodger (Google)
2012/09/10 18:07:14
it would be nice to find a way to do this with a s
sadrul
2012/09/10 18:35:24
Done.
| |
| 44 return result; | 46 return result; |
| 47 } | |
| 45 | 48 |
| 46 // If the event hasn't been consumed, trigger the default handler. Note that | 49 // If the event hasn't been consumed, trigger the default handler. Note that |
| 47 // even if the event has already been handled (i.e. return result has | 50 // even if the event has already been handled (i.e. return result has |
| 48 // ER_HANDLED set), that means that the event should still be processed at | 51 // ER_HANDLED set), that means that the event should still be processed at |
| 49 // this layer, however it should not be processed in the next layer of | 52 // this layer, however it should not be processed in the next layer of |
| 50 // abstraction. | 53 // abstraction. |
| 51 if (CanDispatchToTarget(target)) { | 54 if (CanDispatchToTarget(target)) { |
| 55 dispatcher.set_phase(EP_TARGET); | |
| 52 result |= DispatchEventToSingleHandler(target, event); | 56 result |= DispatchEventToSingleHandler(target, event); |
| 53 if (result & ER_CONSUMED) | 57 dispatcher.set_result(event->result() | result); |
| 58 if (result & ER_CONSUMED) { | |
| 59 dispatcher.set_phase(EP_COMPLETE); | |
| 54 return result; | 60 return result; |
| 61 } | |
| 55 } | 62 } |
| 56 | 63 |
| 57 if (!CanDispatchToTarget(target)) | 64 if (!CanDispatchToTarget(target)) { |
| 65 dispatcher.set_phase(EP_COMPLETE); | |
| 58 return result; | 66 return result; |
| 67 } | |
| 59 | 68 |
| 60 list.clear(); | 69 list.clear(); |
| 61 target->GetPostTargetHandlers(&list); | 70 target->GetPostTargetHandlers(&list); |
| 62 ProcessPostTargetList(&list); | 71 ProcessPostTargetList(&list); |
| 72 dispatcher.set_phase(EP_POSTTARGET); | |
| 63 result |= DispatchEventToEventHandlers(list, event); | 73 result |= DispatchEventToEventHandlers(list, event); |
| 74 dispatcher.set_phase(EP_COMPLETE); | |
| 64 return result; | 75 return result; |
| 65 } | 76 } |
| 66 | 77 |
| 67 private: | 78 private: |
| 68 template<class T> | 79 template<class T> |
| 69 int DispatchEventToEventHandlers(EventHandlerList& list, T* event) { | 80 int DispatchEventToEventHandlers(EventHandlerList& list, T* event) { |
| 70 int result = ER_UNHANDLED; | 81 int result = ER_UNHANDLED; |
| 82 Event::DispatcherApi dispatcher(event); | |
| 71 for (EventHandlerList::const_iterator it = list.begin(), | 83 for (EventHandlerList::const_iterator it = list.begin(), |
| 72 end = list.end(); it != end; ++it) { | 84 end = list.end(); it != end; ++it) { |
| 73 result |= DispatchEventToSingleHandler((*it), event); | 85 result |= DispatchEventToSingleHandler((*it), event); |
| 86 dispatcher.set_result(event->result() | result); | |
| 74 if (result & ER_CONSUMED) | 87 if (result & ER_CONSUMED) |
| 75 return result; | 88 return result; |
| 76 } | 89 } |
| 77 return result; | 90 return result; |
| 78 } | 91 } |
| 79 | 92 |
| 80 EventResult DispatchEventToSingleHandler(EventHandler* handler, | 93 EventResult DispatchEventToSingleHandler(EventHandler* handler, |
| 81 KeyEvent* event); | 94 KeyEvent* event); |
| 82 EventResult DispatchEventToSingleHandler(EventHandler* handler, | 95 EventResult DispatchEventToSingleHandler(EventHandler* handler, |
| 83 MouseEvent* event); | 96 MouseEvent* event); |
| 84 EventResult DispatchEventToSingleHandler(EventHandler* handler, | 97 EventResult DispatchEventToSingleHandler(EventHandler* handler, |
| 85 ScrollEvent* event); | 98 ScrollEvent* event); |
| 86 EventResult DispatchEventToSingleHandler(EventHandler* handler, | 99 EventResult DispatchEventToSingleHandler(EventHandler* handler, |
| 87 TouchEvent* event); | 100 TouchEvent* event); |
| 88 EventResult DispatchEventToSingleHandler(EventHandler* handler, | 101 EventResult DispatchEventToSingleHandler(EventHandler* handler, |
| 89 GestureEvent* event); | 102 GestureEvent* event); |
| 90 | 103 |
| 91 DISALLOW_COPY_AND_ASSIGN(EventDispatcher); | 104 DISALLOW_COPY_AND_ASSIGN(EventDispatcher); |
| 92 }; | 105 }; |
| 93 | 106 |
| 94 } // namespace ui | 107 } // namespace ui |
| 95 | 108 |
| 96 #endif // UI_BASE_EVENTS_EVENT_DISPATCHER_H_ | 109 #endif // UI_BASE_EVENTS_EVENT_DISPATCHER_H_ |
| OLD | NEW |