1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_UI_COCOA_ACCELERATORS_COCOA_H_ 6 #define CHROME_BROWSER_UI_COCOA_ACCELERATORS_COCOA_H_ 7 8 #import <Cocoa/Cocoa.h> 9 10 #include <map> 11 #include <vector> 12 13 #include "base/gtest_prod_util.h" 14 #include "ui/base/accelerators/accelerator.h" 15 16 template <typename T> struct DefaultSingletonTraits; 17 18 // This class maintains a map of command_ids to Accelerator objects (see 19 // chrome/app/chrome_command_ids.h). Currently, this only lists the commands 20 // that are used in the Wrench menu. 21 // 22 // It is recommended that this class be used as a singleton so that the key map 23 // isn't created multiple places. 24 // 25 // #import "base/memory/singleton.h" 26 // ... 27 // AcceleratorsCocoa* keymap = AcceleratorsCocoa::GetInstance(); 28 // return keymap->GetAcceleratorForCommand(IDC_COPY); 29 // 30 class AcceleratorsCocoa { 31 public: 32 typedef std::map<int, ui::Accelerator> AcceleratorMap; 33 typedef std::vector<ui::Accelerator> AcceleratorVector; 34 typedef AcceleratorMap::const_iterator const_iterator; 35 begin()36 const_iterator const begin() { return accelerators_.begin(); } end()37 const_iterator const end() { return accelerators_.end(); } 38 39 // Returns NULL if there is no accelerator for the command. 40 const ui::Accelerator* GetAcceleratorForCommand(int command_id); 41 // Searches the list of accelerators without a command_id for an accelerator 42 // that matches the given |key_equivalent| and |modifiers|. 43 const ui::Accelerator* GetAcceleratorForHotKey(NSString* key_equivalent, 44 NSUInteger modifiers) const; 45 46 // Returns the singleton instance. 47 static AcceleratorsCocoa* GetInstance(); 48 49 private: 50 friend struct DefaultSingletonTraits<AcceleratorsCocoa>; 51 FRIEND_TEST_ALL_PREFIXES(AcceleratorsCocoaBrowserTest, 52 MappingAcceleratorsInMainMenu); 53 54 AcceleratorsCocoa(); 55 ~AcceleratorsCocoa(); 56 57 // A map from command_id to Accelerator. The accelerator is fully filled out, 58 // and its platform_accelerator is also fully filled out. 59 // Contains accelerators from both the wrench menu and the main menu. 60 AcceleratorMap accelerators_; 61 // A list of accelerators used in the main menu that have no associated 62 // command_id. The accelerator is fully filled out, and its 63 // platform_accelerator is also fully filled out. 64 AcceleratorVector accelerator_vector_; 65 66 DISALLOW_COPY_AND_ASSIGN(AcceleratorsCocoa); 67 }; 68 69 #endif // CHROME_BROWSER_UI_COCOA_ACCELERATORS_COCOA_H_ 70