| 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_ACCELERATORS_ACCELERATOR_MANAGER_H_ | 5 #ifndef UI_BASE_ACCELERATORS_ACCELERATOR_MANAGER_H_ |
| 6 #define UI_BASE_ACCELERATORS_ACCELERATOR_MANAGER_H_ | 6 #define UI_BASE_ACCELERATORS_ACCELERATOR_MANAGER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <list> | 9 #include <list> |
| 10 #include <map> | 10 #include <map> |
| 11 #include <utility> |
| 11 | 12 |
| 12 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 13 #include "ui/base/accelerators/accelerator.h" | 14 #include "ui/base/accelerators/accelerator.h" |
| 14 #include "ui/base/events.h" | 15 #include "ui/base/events.h" |
| 15 #include "ui/base/ui_export.h" | 16 #include "ui/base/ui_export.h" |
| 16 | 17 |
| 17 namespace ui { | 18 namespace ui { |
| 18 | 19 |
| 19 // The AcceleratorManger is used to handle keyboard accelerators. | 20 // The AcceleratorManger is used to handle keyboard accelerators. |
| 20 class UI_EXPORT AcceleratorManager { | 21 class UI_EXPORT AcceleratorManager { |
| 21 public: | 22 public: |
| 23 enum HandlerPriority { |
| 24 kNormalPriority, |
| 25 kHighPriority, |
| 26 }; |
| 27 |
| 22 AcceleratorManager(); | 28 AcceleratorManager(); |
| 23 ~AcceleratorManager(); | 29 ~AcceleratorManager(); |
| 24 | 30 |
| 25 // Register a keyboard accelerator for the specified target. If multiple | 31 // Register a keyboard accelerator for the specified target. If multiple |
| 26 // targets are registered for an accelerator, a target registered later has | 32 // targets are registered for an accelerator, a target registered later has |
| 27 // higher priority. | 33 // higher priority. |
| 34 // |accelerator| is the accelerator to register. |
| 35 // |priority| denotes the priority of the handler. |
| 36 // NOTE: In almost all cases, you should specify kPriorityNormal for this |
| 37 // parameter. Setting it to kPriorityHigh prevents Chrome from sending the |
| 38 // shortcut to the webpage if the renderer has focus, which is not desirable |
| 39 // except for very isolated cases. |
| 40 // |target| is the AcceleratorTarget that handles the event once the |
| 41 // accelerator is pressed. |
| 28 // Note that we are currently limited to accelerators that are either: | 42 // Note that we are currently limited to accelerators that are either: |
| 29 // - a key combination including Ctrl or Alt | 43 // - a key combination including Ctrl or Alt |
| 30 // - the escape key | 44 // - the escape key |
| 31 // - the enter key | 45 // - the enter key |
| 32 // - any F key (F1, F2, F3 ...) | 46 // - any F key (F1, F2, F3 ...) |
| 33 // - any browser specific keys (as available on special keyboards) | 47 // - any browser specific keys (as available on special keyboards) |
| 34 void Register(const Accelerator& accelerator, AcceleratorTarget* target); | 48 void Register(const Accelerator& accelerator, |
| 49 HandlerPriority priority, |
| 50 AcceleratorTarget* target); |
| 35 | 51 |
| 36 // Unregister the specified keyboard accelerator for the specified target. | 52 // Unregister the specified keyboard accelerator for the specified target. |
| 37 void Unregister(const Accelerator& accelerator, AcceleratorTarget* target); | 53 void Unregister(const Accelerator& accelerator, AcceleratorTarget* target); |
| 38 | 54 |
| 39 // Unregister all keyboard accelerator for the specified target. | 55 // Unregister all keyboard accelerator for the specified target. |
| 40 void UnregisterAll(AcceleratorTarget* target); | 56 void UnregisterAll(AcceleratorTarget* target); |
| 41 | 57 |
| 42 // Activate the target associated with the specified accelerator. | 58 // Activate the target associated with the specified accelerator. |
| 43 // First, AcceleratorPressed handler of the most recently registered target | 59 // First, AcceleratorPressed handler of the most recently registered target |
| 44 // is called, and if that handler processes the event (i.e. returns true), | 60 // is called, and if that handler processes the event (i.e. returns true), |
| 45 // this method immediately returns. If not, we do the same thing on the next | 61 // this method immediately returns. If not, we do the same thing on the next |
| 46 // target, and so on. | 62 // target, and so on. |
| 47 // Returns true if an accelerator was activated. | 63 // Returns true if an accelerator was activated. |
| 48 bool Process(const Accelerator& accelerator); | 64 bool Process(const Accelerator& accelerator); |
| 49 | 65 |
| 50 // Returns the AcceleratorTarget that should be activated for the specified | 66 // Returns the AcceleratorTarget that should be activated for the specified |
| 51 // keyboard accelerator, or NULL if no view is registered for that keyboard | 67 // keyboard accelerator, or NULL if no view is registered for that keyboard |
| 52 // accelerator. | 68 // accelerator. |
| 53 AcceleratorTarget* GetCurrentTarget(const Accelerator& accelertor) const; | 69 AcceleratorTarget* GetCurrentTarget(const Accelerator& accelertor) const; |
| 54 | 70 |
| 71 // Whether the given |accelerator| has a priority handler associated with it. |
| 72 bool HasPriorityHandler(const Accelerator& accelerator) const; |
| 73 |
| 55 private: | 74 private: |
| 56 bool ShouldHandle(const Accelerator& accelerator) const; | 75 bool ShouldHandle(const Accelerator& accelerator) const; |
| 57 | 76 |
| 58 // The accelerators and associated targets. | 77 // The accelerators and associated targets. |
| 59 typedef std::list<AcceleratorTarget*> AcceleratorTargetList; | 78 typedef std::list<AcceleratorTarget*> AcceleratorTargetList; |
| 60 typedef std::map<Accelerator, AcceleratorTargetList> AcceleratorMap; | 79 // This construct pairs together a |bool| (denoting whether the list contains |
| 80 // a priority_handler at the front) with the list of AcceleratorTargets. |
| 81 typedef std::pair<bool, AcceleratorTargetList> AcceleratorTargets; |
| 82 typedef std::map<Accelerator, AcceleratorTargets> AcceleratorMap; |
| 61 AcceleratorMap accelerators_; | 83 AcceleratorMap accelerators_; |
| 62 | 84 |
| 63 // An event passed to Process() last time. | 85 // An event passed to Process() last time. |
| 64 EventType last_event_type_; | 86 EventType last_event_type_; |
| 65 | 87 |
| 66 DISALLOW_COPY_AND_ASSIGN(AcceleratorManager); | 88 DISALLOW_COPY_AND_ASSIGN(AcceleratorManager); |
| 67 }; | 89 }; |
| 68 | 90 |
| 69 } // namespace ui | 91 } // namespace ui |
| 70 | 92 |
| 71 #endif // UI_BASE_ACCELERATORS_ACCELERATOR_MANAGER_H_ | 93 #endif // UI_BASE_ACCELERATORS_ACCELERATOR_MANAGER_H_ |
| OLD | NEW |