accelerator_configuration.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2021 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. #ifndef ASH_PUBLIC_CPP_ACCELERATOR_CONFIGURATION_H_
  5. #define ASH_PUBLIC_CPP_ACCELERATOR_CONFIGURATION_H_
  6. #include <map>
  7. #include <vector>
  8. #include "ash/public/cpp/ash_public_export.h"
  9. #include "ash/public/mojom/accelerator_info.mojom.h"
  10. #include "base/callback.h"
  11. #include "ui/base/accelerators/accelerator.h"
  12. namespace ash {
  13. // Error codes associated with mutating accelerators.
  14. enum class AcceleratorConfigResult {
  15. kSuccess, // Success
  16. kActionLocked, // Failure, locked actions cannot be modified
  17. kAcceleratorLocked, // Failure, locked accelerators cannot be modified
  18. kConflict, // Transient failure, conflict with existing accelerator
  19. kNotFound, // Failure, accelerator not found
  20. kDuplicate, // Failure, adding a duplicate accelerator to the same
  21. // action
  22. };
  23. struct ASH_PUBLIC_EXPORT AcceleratorInfo {
  24. AcceleratorInfo(ash::mojom::AcceleratorType type,
  25. ui::Accelerator accelerator,
  26. bool locked)
  27. : type(type), accelerator(accelerator), locked(locked) {}
  28. ash::mojom::AcceleratorType type;
  29. ui::Accelerator accelerator;
  30. // Whether the accelerator can be modified.
  31. bool locked = true;
  32. // Accelerators are enabled by default.
  33. ash::mojom::AcceleratorState state = ash::mojom::AcceleratorState::kEnabled;
  34. };
  35. using AcceleratorAction = uint32_t;
  36. // The public-facing interface for shortcut providers, this should be
  37. // implemented by sources, e.g. Browser, Ash, that want their shortcuts to be
  38. // exposed to separate clients.
  39. class ASH_PUBLIC_EXPORT AcceleratorConfiguration {
  40. public:
  41. using AcceleratorsUpdatedCallback = base::RepeatingCallback<void(
  42. ash::mojom::AcceleratorSource,
  43. std::multimap<AcceleratorAction, AcceleratorInfo>)>;
  44. explicit AcceleratorConfiguration(ash::mojom::AcceleratorSource source);
  45. virtual ~AcceleratorConfiguration();
  46. // Callback will fire immediately once after updating.
  47. void AddAcceleratorsUpdatedCallback(AcceleratorsUpdatedCallback callback);
  48. void RemoveAcceleratorsUpdatedCallback(AcceleratorsUpdatedCallback callback);
  49. // Get the accelerators for a single action.
  50. virtual const std::vector<AcceleratorInfo>& GetConfigForAction(
  51. AcceleratorAction actionId) = 0;
  52. // Whether this source of shortcuts can be modified. If this returns false
  53. // then any of the Add/Remove/Replace class will DCHECK. The two Restore
  54. // methods will be no-ops.
  55. virtual bool IsMutable() const = 0;
  56. // Add a new user defined accelerator.
  57. virtual AcceleratorConfigResult AddUserAccelerator(
  58. AcceleratorAction action,
  59. const ui::Accelerator& accelerator) = 0;
  60. // Remove a shortcut. This will delete a user-defined shortcut, or
  61. // mark a default one disabled.
  62. virtual AcceleratorConfigResult RemoveAccelerator(
  63. AcceleratorAction action,
  64. const ui::Accelerator& accelerator) = 0;
  65. // Atomic version of Remove then Add.
  66. virtual AcceleratorConfigResult ReplaceAccelerator(
  67. AcceleratorAction action,
  68. const ui::Accelerator& old_acc,
  69. const ui::Accelerator& new_acc) = 0;
  70. // Restore the defaults for the given action.
  71. virtual AcceleratorConfigResult RestoreDefault(AcceleratorAction action) = 0;
  72. // Restore all defaults.
  73. virtual AcceleratorConfigResult RestoreAllDefaults() = 0;
  74. protected:
  75. void NotifyAcceleratorsUpdated(
  76. const std::multimap<AcceleratorAction, AcceleratorInfo>& accelerators);
  77. private:
  78. // The source of the accelerators. Derived classes are responsible for only
  79. // one source.
  80. const ash::mojom::AcceleratorSource source_;
  81. // Container of all invoked callbacks when the accelerators are updated. Call
  82. // AddAcceleratorsUpdatedCallback or RemoveAcceleratorsUpdatedCallback to
  83. // add/remove callbacks to the container.
  84. std::vector<AcceleratorsUpdatedCallback> callbacks_;
  85. };
  86. } // namespace ash
  87. #endif // ASH_PUBLIC_CPP_ACCELERATOR_CONFIGURATION_H_