Index: ui/base/accelerators/accelerator_history.cc |
diff --git a/ui/base/accelerators/accelerator_history.cc b/ui/base/accelerators/accelerator_history.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1cda0c75fbcf0b038a5bad8d33f716945c13f11a |
--- /dev/null |
+++ b/ui/base/accelerators/accelerator_history.cc |
@@ -0,0 +1,95 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ui/base/accelerators/accelerator_history.h" |
+#include "ui/events/event_constants.h" |
+ |
+namespace ui { |
+ |
+namespace { |
+ |
+#if defined(OS_MACOSX) || defined(OS_CHROMEOS) |
+static const int kEventFlagsMask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | |
+ ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN; |
+#else |
+static const int kEventFlagsMask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | |
+ ui::EF_ALT_DOWN; |
+#endif |
+ |
+static inline int CalculateModifiers(const ui::KeyEvent& event) { |
+ return event.flags() & kEventFlagsMask; |
+} |
+ |
+}; |
+ |
+// ---------------------------------------------------------------------- |
+// Public Methods |
+// ---------------------------------------------------------------------- |
+ |
+AcceleratorHistory* AcceleratorHistory::GetInstance() { |
+ return Singleton<AcceleratorHistory>::get(); |
+} |
+ |
+const Accelerator& AcceleratorHistory::GetCurrentAccelerator() const { |
+ return current_accelerator_; |
+} |
+ |
+const Accelerator& AcceleratorHistory::GetPreviousAccelerator() const { |
+ return previous_accelerator_; |
+} |
+ |
+void AcceleratorHistory::StoreCurrentAccelerator( |
+ const Accelerator& accelerator) { |
+ if (accelerator != current_accelerator_) { |
+ previous_accelerator_ = current_accelerator_; |
+ current_accelerator_ = accelerator; |
+ } |
+} |
+ |
+// ---------------------------------------------------------------------- |
+// Private Methods |
+// ---------------------------------------------------------------------- |
+ |
+AcceleratorHistory::AcceleratorHistory() |
+ : current_accelerator_(), |
+ previous_accelerator_() { |
+} |
+ |
+AcceleratorHistory::~AcceleratorHistory() { |
+} |
+ |
+void AcceleratorHistory::OnKeyEvent(KeyEvent* event) { |
+ BuildCurrentAccelerator(*event); |
+} |
+ |
+void AcceleratorHistory::BuildCurrentAccelerator(const ui::KeyEvent& event) { |
+ // Only build accelerators for 'pressed' or 'released' key events. |
+ if (event.type() != ui::ET_KEY_PRESSED && event.type() != ui::ET_KEY_RELEASED) |
+ return; |
+ |
+ // Record the new accelerator only if the new key event is different from the |
+ // previous one. |
+ int modifiers = CalculateModifiers(event); |
+ if (ShouldBuildCurrentAccelerator(event, modifiers)) { |
+ ui::Accelerator accelerator(event.key_code(), modifiers); |
+ accelerator.set_type(event.type()); |
+ accelerator.set_is_repeat(event.IsRepeat()); |
+ |
+ previous_accelerator_ = current_accelerator_; |
+ current_accelerator_ = accelerator; |
+ } |
+} |
+ |
+bool AcceleratorHistory::ShouldBuildCurrentAccelerator( |
+ const ui::KeyEvent& event, |
+ const int event_modifiers) const { |
+ return !(event.type() == current_accelerator_.type() && |
+ event.key_code() == current_accelerator_.key_code() && |
+ event.IsRepeat() == current_accelerator_.IsRepeat() && |
+ event_modifiers == current_accelerator_.modifiers()); |
+} |
+ |
+}; // namespace ui |
+ |
+ |