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 // This class describe a keyboard accelerator (or keyboard shortcut). | 5 // This class describe a keyboard accelerator (or keyboard shortcut). |
6 // Keyboard accelerators are registered with the FocusManager. | 6 // Keyboard accelerators are registered with the FocusManager. |
7 // It has a copy constructor and assignment operator so that it can be copied. | 7 // It has a copy constructor and assignment operator so that it can be copied. |
8 // It also defines the < operator so that it can be used as a key in a std::map. | 8 // It also defines the < operator so that it can be used as a key in a std::map. |
9 // | 9 // |
10 | 10 |
(...skipping 17 matching lines...) Expand all Loading... | |
28 // This is a cross-platform class for accelerator keys used in menus. | 28 // This is a cross-platform class for accelerator keys used in menus. |
29 // |platform_accelerator| should be used to store platform specific data. | 29 // |platform_accelerator| should be used to store platform specific data. |
30 // | 30 // |
31 // While |modifiers| may include EF_IS_REPEAT, EF_IS_REPEAT is not considered | 31 // While |modifiers| may include EF_IS_REPEAT, EF_IS_REPEAT is not considered |
32 // an intrinsic part of an Accelerator. This is done so that an accelerator | 32 // an intrinsic part of an Accelerator. This is done so that an accelerator |
33 // for a particular KeyEvent matches an accelerator with or without the repeat | 33 // for a particular KeyEvent matches an accelerator with or without the repeat |
34 // flag. A side effect of this is that == (and <) does not consider the | 34 // flag. A side effect of this is that == (and <) does not consider the |
35 // repeat flag in its comparison. | 35 // repeat flag in its comparison. |
36 class UI_BASE_EXPORT Accelerator { | 36 class UI_BASE_EXPORT Accelerator { |
37 public: | 37 public: |
38 enum class KeyState { | |
msw
2017/03/16 16:57:22
very optional nit: If this weren't an enum class,
sky
2017/03/16 17:06:40
If I went down that route I would use a more verbo
| |
39 PRESSED, | |
40 RELEASED, | |
41 }; | |
42 | |
38 Accelerator(); | 43 Accelerator(); |
39 // NOTE: this constructor strips out non key related flags. | 44 // NOTE: this constructor strips out non key related flags. |
40 Accelerator(ui::KeyboardCode keycode, int modifiers); | 45 Accelerator(KeyboardCode key_code, int modifiers); |
41 explicit Accelerator(const KeyEvent& key_event); | 46 explicit Accelerator(const KeyEvent& key_event); |
42 Accelerator(const Accelerator& accelerator); | 47 Accelerator(const Accelerator& accelerator); |
43 ~Accelerator(); | 48 ~Accelerator(); |
44 | 49 |
45 // Masks out all the non-modifiers KeyEvent |flags| and returns only the | 50 // Masks out all the non-modifiers KeyEvent |flags| and returns only the |
46 // available modifier ones. This does not include EF_IS_REPEAT. | 51 // available modifier ones. This does not include EF_IS_REPEAT. |
47 static int MaskOutKeyEventFlags(int flags); | 52 static int MaskOutKeyEventFlags(int flags); |
48 | 53 |
49 Accelerator& operator=(const Accelerator& accelerator); | 54 Accelerator& operator=(const Accelerator& accelerator); |
50 | 55 |
51 // Define the < operator so that the KeyboardShortcut can be used as a key in | 56 // Define the < operator so that the KeyboardShortcut can be used as a key in |
52 // a std::map. | 57 // a std::map. |
53 bool operator <(const Accelerator& rhs) const; | 58 bool operator <(const Accelerator& rhs) const; |
54 | 59 |
55 bool operator ==(const Accelerator& rhs) const; | 60 bool operator ==(const Accelerator& rhs) const; |
56 | 61 |
57 bool operator !=(const Accelerator& rhs) const; | 62 bool operator !=(const Accelerator& rhs) const; |
58 | 63 |
59 ui::KeyboardCode key_code() const { return key_code_; } | 64 KeyboardCode key_code() const { return key_code_; } |
60 | 65 |
61 // Sets the event type if the accelerator should be processed on an event | 66 // Sets the key state that triggers the accelerator. Default is PRESSED. |
62 // other than ui::ET_KEY_PRESSED. | 67 void set_key_state(KeyState state) { key_state_ = state; } |
63 void set_type(ui::EventType type) { type_ = type; } | 68 KeyState key_state() const { return key_state_; } |
64 ui::EventType type() const { return type_; } | |
65 | 69 |
66 int modifiers() const { return modifiers_; } | 70 int modifiers() const { return modifiers_; } |
67 | 71 |
68 bool IsShiftDown() const; | 72 bool IsShiftDown() const; |
69 bool IsCtrlDown() const; | 73 bool IsCtrlDown() const; |
70 bool IsAltDown() const; | 74 bool IsAltDown() const; |
71 bool IsCmdDown() const; | 75 bool IsCmdDown() const; |
72 bool IsRepeat() const; | 76 bool IsRepeat() const; |
73 | 77 |
74 // Returns a string with the localized shortcut if any. | 78 // Returns a string with the localized shortcut if any. |
75 base::string16 GetShortcutText() const; | 79 base::string16 GetShortcutText() const; |
76 | 80 |
77 void set_platform_accelerator(std::unique_ptr<PlatformAccelerator> p) { | 81 void set_platform_accelerator(std::unique_ptr<PlatformAccelerator> p) { |
78 platform_accelerator_ = std::move(p); | 82 platform_accelerator_ = std::move(p); |
79 } | 83 } |
80 | 84 |
81 // This class keeps ownership of the returned object. | 85 // This class keeps ownership of the returned object. |
82 const PlatformAccelerator* platform_accelerator() const { | 86 const PlatformAccelerator* platform_accelerator() const { |
83 return platform_accelerator_.get(); | 87 return platform_accelerator_.get(); |
84 } | 88 } |
85 | 89 |
86 private: | 90 private: |
87 // The keycode (VK_...). | 91 // The keycode (VK_...). |
88 KeyboardCode key_code_; | 92 KeyboardCode key_code_; |
89 | 93 |
90 // The event type (usually ui::ET_KEY_PRESSED). | 94 KeyState key_state_; |
91 EventType type_; | |
92 | 95 |
93 // The state of the Shift/Ctrl/Alt keys. This corresponds to Event::flags(). | 96 // The state of the Shift/Ctrl/Alt keys. This corresponds to Event::flags(). |
94 int modifiers_; | 97 int modifiers_; |
95 | 98 |
96 // Stores platform specific data. May be NULL. | 99 // Stores platform specific data. May be NULL. |
97 std::unique_ptr<PlatformAccelerator> platform_accelerator_; | 100 std::unique_ptr<PlatformAccelerator> platform_accelerator_; |
98 }; | 101 }; |
99 | 102 |
100 // An interface that classes that want to register for keyboard accelerators | 103 // An interface that classes that want to register for keyboard accelerators |
101 // should implement. | 104 // should implement. |
(...skipping 11 matching lines...) Expand all Loading... | |
113 virtual ~AcceleratorTarget() {} | 116 virtual ~AcceleratorTarget() {} |
114 }; | 117 }; |
115 | 118 |
116 // Since accelerator code is one of the few things that can't be cross platform | 119 // Since accelerator code is one of the few things that can't be cross platform |
117 // in the chrome UI, separate out just the GetAcceleratorForCommandId() from | 120 // in the chrome UI, separate out just the GetAcceleratorForCommandId() from |
118 // the menu delegates. | 121 // the menu delegates. |
119 class AcceleratorProvider { | 122 class AcceleratorProvider { |
120 public: | 123 public: |
121 // Gets the accelerator for the specified command id. Returns true if the | 124 // Gets the accelerator for the specified command id. Returns true if the |
122 // command id has a valid accelerator, false otherwise. | 125 // command id has a valid accelerator, false otherwise. |
123 virtual bool GetAcceleratorForCommandId( | 126 virtual bool GetAcceleratorForCommandId(int command_id, |
124 int command_id, | 127 Accelerator* accelerator) const = 0; |
125 ui::Accelerator* accelerator) const = 0; | |
126 | 128 |
127 protected: | 129 protected: |
128 virtual ~AcceleratorProvider() {} | 130 virtual ~AcceleratorProvider() {} |
129 }; | 131 }; |
130 | 132 |
131 } // namespace ui | 133 } // namespace ui |
132 | 134 |
133 #endif // UI_BASE_ACCELERATORS_ACCELERATOR_H_ | 135 #endif // UI_BASE_ACCELERATORS_ACCELERATOR_H_ |
OLD | NEW |