Home | History | Annotate | Download | only in accelerators
      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.
      4 
      5 #ifndef UI_BASE_ACCELERATORS_ACCELERATOR_MANAGER_H_
      6 #define UI_BASE_ACCELERATORS_ACCELERATOR_MANAGER_H_
      7 
      8 #include <list>
      9 #include <map>
     10 #include <utility>
     11 
     12 #include "base/basictypes.h"
     13 #include "ui/base/accelerators/accelerator.h"
     14 #include "ui/base/ui_base_export.h"
     15 #include "ui/events/event_constants.h"
     16 
     17 namespace ui {
     18 
     19 // The AcceleratorManger is used to handle keyboard accelerators.
     20 class UI_BASE_EXPORT AcceleratorManager {
     21  public:
     22   enum HandlerPriority {
     23     kNormalPriority,
     24     kHighPriority,
     25   };
     26 
     27   AcceleratorManager();
     28   ~AcceleratorManager();
     29 
     30   // Register a keyboard accelerator for the specified target. If multiple
     31   // targets are registered for an accelerator, a target registered later has
     32   // higher priority.
     33   // |accelerator| is the accelerator to register.
     34   // |priority| denotes the priority of the handler.
     35   // NOTE: In almost all cases, you should specify kNormalPriority for this
     36   // parameter. Setting it to kHighPriority prevents Chrome from sending the
     37   // shortcut to the webpage if the renderer has focus, which is not desirable
     38   // except for very isolated cases.
     39   // |target| is the AcceleratorTarget that handles the event once the
     40   // accelerator is pressed.
     41   // Note that we are currently limited to accelerators that are either:
     42   // - a key combination including Ctrl or Alt
     43   // - the escape key
     44   // - the enter key
     45   // - any F key (F1, F2, F3 ...)
     46   // - any browser specific keys (as available on special keyboards)
     47   void Register(const Accelerator& accelerator,
     48                 HandlerPriority priority,
     49                 AcceleratorTarget* target);
     50 
     51   // Unregister the specified keyboard accelerator for the specified target.
     52   void Unregister(const Accelerator& accelerator, AcceleratorTarget* target);
     53 
     54   // Unregister all keyboard accelerator for the specified target.
     55   void UnregisterAll(AcceleratorTarget* target);
     56 
     57   // Activate the target associated with the specified accelerator.
     58   // First, AcceleratorPressed handler of the most recently registered target
     59   // is called, and if that handler processes the event (i.e. returns true),
     60   // this method immediately returns. If not, we do the same thing on the next
     61   // target, and so on.
     62   // Returns true if an accelerator was activated.
     63   bool Process(const Accelerator& accelerator);
     64 
     65   // Returns the AcceleratorTarget that should be activated for the specified
     66   // keyboard accelerator, or NULL if no view is registered for that keyboard
     67   // accelerator.
     68   AcceleratorTarget* GetCurrentTarget(const Accelerator& accelertor) const;
     69 
     70   // Whether the given |accelerator| has a priority handler associated with it.
     71   bool HasPriorityHandler(const Accelerator& accelerator) const;
     72 
     73  private:
     74   // The accelerators and associated targets.
     75   typedef std::list<AcceleratorTarget*> AcceleratorTargetList;
     76   // This construct pairs together a |bool| (denoting whether the list contains
     77   // a priority_handler at the front) with the list of AcceleratorTargets.
     78   typedef std::pair<bool, AcceleratorTargetList> AcceleratorTargets;
     79   typedef std::map<Accelerator, AcceleratorTargets> AcceleratorMap;
     80   AcceleratorMap accelerators_;
     81 
     82   DISALLOW_COPY_AND_ASSIGN(AcceleratorManager);
     83 };
     84 
     85 }  // namespace ui
     86 
     87 #endif  // UI_BASE_ACCELERATORS_ACCELERATOR_MANAGER_H_
     88