OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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. |
| 4 |
| 5 #include "ui/base/accelerators/accelerator_history.h" |
| 6 #include "ui/events/event_constants.h" |
| 7 |
| 8 namespace ui { |
| 9 |
| 10 namespace { |
| 11 |
| 12 #if defined(OS_MACOSX) || defined(OS_CHROMEOS) |
| 13 static const int kEventFlagsMask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | |
| 14 ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN; |
| 15 #else |
| 16 static const int kEventFlagsMask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | |
| 17 ui::EF_ALT_DOWN; |
| 18 #endif |
| 19 |
| 20 static inline int CalculateModifiers(const ui::KeyEvent& event) { |
| 21 return event.flags() & kEventFlagsMask; |
| 22 } |
| 23 |
| 24 }; |
| 25 |
| 26 // ---------------------------------------------------------------------- |
| 27 // Public Methods |
| 28 // ---------------------------------------------------------------------- |
| 29 |
| 30 AcceleratorHistory* AcceleratorHistory::GetInstance() { |
| 31 return Singleton<AcceleratorHistory>::get(); |
| 32 } |
| 33 |
| 34 const Accelerator& AcceleratorHistory::GetCurrentAccelerator() const { |
| 35 return current_accelerator_; |
| 36 } |
| 37 |
| 38 const Accelerator& AcceleratorHistory::GetPreviousAccelerator() const { |
| 39 return previous_accelerator_; |
| 40 } |
| 41 |
| 42 void AcceleratorHistory::StoreCurrentAccelerator( |
| 43 const Accelerator& accelerator) { |
| 44 if (accelerator != current_accelerator_) { |
| 45 previous_accelerator_ = current_accelerator_; |
| 46 current_accelerator_ = accelerator; |
| 47 } |
| 48 } |
| 49 |
| 50 // ---------------------------------------------------------------------- |
| 51 // Private Methods |
| 52 // ---------------------------------------------------------------------- |
| 53 |
| 54 AcceleratorHistory::AcceleratorHistory() |
| 55 : current_accelerator_(), |
| 56 previous_accelerator_() { |
| 57 } |
| 58 |
| 59 AcceleratorHistory::~AcceleratorHistory() { |
| 60 } |
| 61 |
| 62 void AcceleratorHistory::OnKeyEvent(KeyEvent* event) { |
| 63 BuildCurrentAccelerator(*event); |
| 64 } |
| 65 |
| 66 void AcceleratorHistory::BuildCurrentAccelerator(const ui::KeyEvent& event) { |
| 67 // Only build accelerators for 'pressed' or 'released' key events. |
| 68 if (event.type() != ui::ET_KEY_PRESSED && event.type() != ui::ET_KEY_RELEASED) |
| 69 return; |
| 70 |
| 71 // Record the new accelerator only if the new key event is different from the |
| 72 // previous one. |
| 73 int modifiers = CalculateModifiers(event); |
| 74 if (ShouldBuildCurrentAccelerator(event, modifiers)) { |
| 75 ui::Accelerator accelerator(event.key_code(), modifiers); |
| 76 accelerator.set_type(event.type()); |
| 77 accelerator.set_is_repeat(event.IsRepeat()); |
| 78 |
| 79 previous_accelerator_ = current_accelerator_; |
| 80 current_accelerator_ = accelerator; |
| 81 } |
| 82 } |
| 83 |
| 84 bool AcceleratorHistory::ShouldBuildCurrentAccelerator( |
| 85 const ui::KeyEvent& event, |
| 86 const int event_modifiers) const { |
| 87 return !(event.type() == current_accelerator_.type() && |
| 88 event.key_code() == current_accelerator_.key_code() && |
| 89 event.IsRepeat() == current_accelerator_.IsRepeat() && |
| 90 event_modifiers == current_accelerator_.modifiers()); |
| 91 } |
| 92 |
| 93 }; // namespace ui |
| 94 |
| 95 |
OLD | NEW |