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.
5 #ifndef ASH_ACCELERATORS_ACCELERATOR_CONTROLLER_H_
6 #define ASH_ACCELERATORS_ACCELERATOR_CONTROLLER_H_
11 #include "ash/ash_export.h"
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "ui/base/accelerators/accelerator.h"
19 class AcceleratorManager
;
24 struct AcceleratorData
;
25 class BrightnessControlDelegate
;
26 class ImeControlDelegate
;
27 class KeyboardBrightnessControlDelegate
;
28 class ScreenshotDelegate
;
29 class VolumeControlDelegate
;
31 // Stores information about accelerator context, eg. previous accelerator
32 // or if the current accelerator is repeated or not.
33 class ASH_EXPORT AcceleratorControllerContext
{
35 AcceleratorControllerContext();
36 ~AcceleratorControllerContext() {}
38 // Updates context - determines if the accelerator is repeated, as well as
39 // event type of the previous accelerator.
40 void UpdateContext(const ui::Accelerator
& accelerator
);
42 const ui::Accelerator
& previous_accelerator() const {
43 return previous_accelerator_
;
45 bool repeated() const {
46 return current_accelerator_
== previous_accelerator_
&&
47 current_accelerator_
.type() != ui::ET_UNKNOWN
;
51 ui::Accelerator current_accelerator_
;
52 // Used for NEXT_IME and DISABLE_CAPS_LOCK accelerator actions.
53 ui::Accelerator previous_accelerator_
;
55 DISALLOW_COPY_AND_ASSIGN(AcceleratorControllerContext
);
58 // AcceleratorController provides functions for registering or unregistering
59 // global keyboard accelerators, which are handled earlier than any windows. It
60 // also implements several handlers as an accelerator target.
61 class ASH_EXPORT AcceleratorController
: public ui::AcceleratorTarget
{
63 AcceleratorController();
64 virtual ~AcceleratorController();
66 // Registers a global keyboard accelerator for the specified target. If
67 // multiple targets are registered for an accelerator, a target registered
68 // later has higher priority.
69 void Register(const ui::Accelerator
& accelerator
,
70 ui::AcceleratorTarget
* target
);
72 // Unregisters the specified keyboard accelerator for the specified target.
73 void Unregister(const ui::Accelerator
& accelerator
,
74 ui::AcceleratorTarget
* target
);
76 // Unregisters all keyboard accelerators for the specified target.
77 void UnregisterAll(ui::AcceleratorTarget
* target
);
79 // Activates the target associated with the specified accelerator.
80 // First, AcceleratorPressed handler of the most recently registered target
81 // is called, and if that handler processes the event (i.e. returns true),
82 // this method immediately returns. If not, we do the same thing on the next
84 // Returns true if an accelerator was activated.
85 bool Process(const ui::Accelerator
& accelerator
);
87 // Returns true if the |accelerator| is registered.
88 bool IsRegistered(const ui::Accelerator
& accelerator
) const;
90 // Returns true if the |accelerator| is one of the |reserved_actions_|.
91 bool IsReservedAccelerator(const ui::Accelerator
& accelerator
) const;
93 // Performs the specified action. The |accelerator| may provide additional
94 // data the action needs. Returns whether an action was performed
96 bool PerformAction(int action
,
97 const ui::Accelerator
& accelerator
);
99 // Overridden from ui::AcceleratorTarget:
100 virtual bool AcceleratorPressed(const ui::Accelerator
& accelerator
) OVERRIDE
;
101 virtual bool CanHandleAccelerators() const OVERRIDE
;
103 void SetBrightnessControlDelegate(
104 scoped_ptr
<BrightnessControlDelegate
> brightness_control_delegate
);
105 void SetImeControlDelegate(
106 scoped_ptr
<ImeControlDelegate
> ime_control_delegate
);
107 void SetScreenshotDelegate(
108 scoped_ptr
<ScreenshotDelegate
> screenshot_delegate
);
109 BrightnessControlDelegate
* brightness_control_delegate() const {
110 return brightness_control_delegate_
.get();
113 // Provides access to an object holding contextual information.
114 AcceleratorControllerContext
* context() {
119 FRIEND_TEST_ALL_PREFIXES(AcceleratorControllerTest
, GlobalAccelerators
);
121 // Initializes the accelerators this class handles as a target.
124 // Registers the specified accelerators.
125 void RegisterAccelerators(const AcceleratorData accelerators
[],
126 size_t accelerators_length
);
128 void SetKeyboardBrightnessControlDelegate(
129 scoped_ptr
<KeyboardBrightnessControlDelegate
>
130 keyboard_brightness_control_delegate
);
132 scoped_ptr
<ui::AcceleratorManager
> accelerator_manager_
;
134 // TODO(derat): BrightnessControlDelegate is also used by the system tray;
135 // move it outside of this class.
136 scoped_ptr
<BrightnessControlDelegate
> brightness_control_delegate_
;
137 scoped_ptr
<ImeControlDelegate
> ime_control_delegate_
;
138 scoped_ptr
<KeyboardBrightnessControlDelegate
>
139 keyboard_brightness_control_delegate_
;
140 scoped_ptr
<ScreenshotDelegate
> screenshot_delegate_
;
142 // Contextual information, eg. if the current accelerator is repeated.
143 AcceleratorControllerContext context_
;
145 // A map from accelerators to the AcceleratorAction values, which are used in
146 // the implementation.
147 std::map
<ui::Accelerator
, int> accelerators_
;
149 // Actions allowed when the user is not signed in.
150 std::set
<int> actions_allowed_at_login_screen_
;
151 // Actions allowed when the screen is locked.
152 std::set
<int> actions_allowed_at_lock_screen_
;
153 // Actions allowed when a modal window is up.
154 std::set
<int> actions_allowed_at_modal_window_
;
155 // Reserved actions. See accelerator_table.h for details.
156 std::set
<int> reserved_actions_
;
157 // Actions which will not be repeated while holding the accelerator key.
158 std::set
<int> nonrepeatable_actions_
;
160 DISALLOW_COPY_AND_ASSIGN(AcceleratorController
);
165 #endif // ASH_ACCELERATORS_ACCELERATOR_CONTROLLER_H_