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: |
22 AcceleratorManager(); | 23 AcceleratorManager(); |
23 ~AcceleratorManager(); | 24 ~AcceleratorManager(); |
24 | 25 |
25 // Register a keyboard accelerator for the specified target. If multiple | 26 // Register a keyboard accelerator for the specified target. If multiple |
26 // targets are registered for an accelerator, a target registered later has | 27 // targets are registered for an accelerator, a target registered later has |
27 // higher priority. | 28 // higher priority. |
29 // |accelerator| is the accelerator to register. | |
30 // |priority| denotes whether the |target| is a priority handler or not. | |
31 // NOTE: In almost all cases, you should specify |false| for this parameter. | |
32 // Setting it to |true| prevents Chrome from sending the shortcut to the | |
33 // webpage if the renderer has focus, which is not desirable except for | |
34 // isolated cases. | |
sadrul
2012/02/21 16:17:28
I have seen other code use enums for different kin
Finnur
2012/02/21 21:58:21
I thought about this and decided that if we have m
sky
2012/02/22 18:02:52
The style guide encourages enums for this case (se
| |
35 // |target| is the AcceleratorTarget that handles the event once the | |
36 // accelerator is pressed. | |
28 // Note that we are currently limited to accelerators that are either: | 37 // Note that we are currently limited to accelerators that are either: |
29 // - a key combination including Ctrl or Alt | 38 // - a key combination including Ctrl or Alt |
30 // - the escape key | 39 // - the escape key |
31 // - the enter key | 40 // - the enter key |
32 // - any F key (F1, F2, F3 ...) | 41 // - any F key (F1, F2, F3 ...) |
33 // - any browser specific keys (as available on special keyboards) | 42 // - any browser specific keys (as available on special keyboards) |
34 void Register(const Accelerator& accelerator, AcceleratorTarget* target); | 43 void Register(const Accelerator& accelerator, |
44 bool priority, | |
45 AcceleratorTarget* target); | |
35 | 46 |
36 // Unregister the specified keyboard accelerator for the specified target. | 47 // Unregister the specified keyboard accelerator for the specified target. |
37 void Unregister(const Accelerator& accelerator, AcceleratorTarget* target); | 48 void Unregister(const Accelerator& accelerator, AcceleratorTarget* target); |
38 | 49 |
39 // Unregister all keyboard accelerator for the specified target. | 50 // Unregister all keyboard accelerator for the specified target. |
40 void UnregisterAll(AcceleratorTarget* target); | 51 void UnregisterAll(AcceleratorTarget* target); |
41 | 52 |
42 // Activate the target associated with the specified accelerator. | 53 // Activate the target associated with the specified accelerator. |
43 // First, AcceleratorPressed handler of the most recently registered target | 54 // First, AcceleratorPressed handler of the most recently registered target |
44 // is called, and if that handler processes the event (i.e. returns true), | 55 // 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 | 56 // this method immediately returns. If not, we do the same thing on the next |
46 // target, and so on. | 57 // target, and so on. |
47 // Returns true if an accelerator was activated. | 58 // Returns true if an accelerator was activated. |
48 bool Process(const Accelerator& accelerator); | 59 bool Process(const Accelerator& accelerator); |
49 | 60 |
50 // Returns the AcceleratorTarget that should be activated for the specified | 61 // Returns the AcceleratorTarget that should be activated for the specified |
51 // keyboard accelerator, or NULL if no view is registered for that keyboard | 62 // keyboard accelerator, or NULL if no view is registered for that keyboard |
52 // accelerator. | 63 // accelerator. |
53 AcceleratorTarget* GetCurrentTarget(const Accelerator& accelertor) const; | 64 AcceleratorTarget* GetCurrentTarget(const Accelerator& accelertor) const; |
54 | 65 |
66 // Whether the given |accelerator| has a priority handler associated with it. | |
67 bool HasPriorityHandler(const Accelerator& accelerator) const; | |
68 | |
55 private: | 69 private: |
56 bool ShouldHandle(const Accelerator& accelerator) const; | 70 bool ShouldHandle(const Accelerator& accelerator) const; |
57 | 71 |
58 // The accelerators and associated targets. | 72 // The accelerators and associated targets. |
59 typedef std::list<AcceleratorTarget*> AcceleratorTargetList; | 73 typedef std::list<AcceleratorTarget*> AcceleratorTargetList; |
60 typedef std::map<Accelerator, AcceleratorTargetList> AcceleratorMap; | 74 // This construct pairs together a |bool| (denoting whether the list contains |
75 // a priority_handler at the front) with the list of AcceleratorTargets. | |
76 typedef std::pair<bool, AcceleratorTargetList> AcceleratorTargets; | |
77 typedef std::map<Accelerator, AcceleratorTargets> AcceleratorMap; | |
61 AcceleratorMap accelerators_; | 78 AcceleratorMap accelerators_; |
62 | 79 |
63 // An event passed to Process() last time. | 80 // An event passed to Process() last time. |
64 EventType last_event_type_; | 81 EventType last_event_type_; |
65 | 82 |
66 DISALLOW_COPY_AND_ASSIGN(AcceleratorManager); | 83 DISALLOW_COPY_AND_ASSIGN(AcceleratorManager); |
67 }; | 84 }; |
68 | 85 |
69 } // namespace ui | 86 } // namespace ui |
70 | 87 |
71 #endif // UI_BASE_ACCELERATORS_ACCELERATOR_MANAGER_H_ | 88 #endif // UI_BASE_ACCELERATORS_ACCELERATOR_MANAGER_H_ |
OLD | NEW |