undo_manager.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2013 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 COMPONENTS_UNDO_UNDO_MANAGER_H_
  5. #define COMPONENTS_UNDO_UNDO_MANAGER_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/observer_list.h"
  11. class UndoManagerObserver;
  12. class UndoOperation;
  13. // UndoGroup ------------------------------------------------------------------
  14. // UndoGroup represents a user action and stores all the operations that
  15. // make that action. Typically there is only one operation per UndoGroup.
  16. class UndoGroup {
  17. public:
  18. UndoGroup();
  19. UndoGroup(const UndoGroup&) = delete;
  20. UndoGroup& operator=(const UndoGroup&) = delete;
  21. ~UndoGroup();
  22. void AddOperation(std::unique_ptr<UndoOperation> operation);
  23. const std::vector<std::unique_ptr<UndoOperation>>& undo_operations() {
  24. return operations_;
  25. }
  26. void Undo();
  27. // The resource string id describing the undo and redo action.
  28. int get_undo_label_id() const { return undo_label_id_; }
  29. void set_undo_label_id(int label_id) { undo_label_id_ = label_id; }
  30. int get_redo_label_id() const { return redo_label_id_; }
  31. void set_redo_label_id(int label_id) { redo_label_id_ = label_id; }
  32. private:
  33. std::vector<std::unique_ptr<UndoOperation>> operations_;
  34. // The resource string id describing the undo and redo action.
  35. int undo_label_id_;
  36. int redo_label_id_;
  37. };
  38. // UndoManager ----------------------------------------------------------------
  39. // Maintains user actions as a group of operations that store enough info to
  40. // undo and redo those operations.
  41. class UndoManager {
  42. public:
  43. UndoManager();
  44. UndoManager(const UndoManager&) = delete;
  45. UndoManager& operator=(const UndoManager&) = delete;
  46. ~UndoManager();
  47. // Perform an undo or redo operation.
  48. void Undo();
  49. void Redo();
  50. size_t undo_count() const { return undo_actions_.size(); }
  51. size_t redo_count() const { return redo_actions_.size(); }
  52. std::u16string GetUndoLabel() const;
  53. std::u16string GetRedoLabel() const;
  54. void AddUndoOperation(std::unique_ptr<UndoOperation> operation);
  55. // Group multiple operations into one undoable action.
  56. void StartGroupingActions();
  57. void EndGroupingActions();
  58. // Suspend undo tracking while processing non-user initiated changes such as
  59. // profile synchonization.
  60. void SuspendUndoTracking();
  61. void ResumeUndoTracking();
  62. bool IsUndoTrakingSuspended() const;
  63. // Remove all undo and redo operations. Note that grouping of actions and
  64. // suspension of undo tracking states are left unchanged.
  65. void RemoveAllOperations();
  66. // Observers are notified when the internal state of this class changes.
  67. void AddObserver(UndoManagerObserver* observer);
  68. void RemoveObserver(UndoManagerObserver* observer);
  69. private:
  70. friend class UndoManagerTestApi;
  71. void Undo(bool* performing_indicator,
  72. std::vector<std::unique_ptr<UndoGroup>>* active_undo_group);
  73. bool is_user_action() const { return !performing_undo_ && !performing_redo_; }
  74. // Notifies the observers that the undo manager's state has changed.
  75. void NotifyOnUndoManagerStateChange();
  76. // Handle the addition of |new_undo_group| to the active undo group container.
  77. void AddUndoGroup(std::unique_ptr<UndoGroup> new_undo_group);
  78. // Returns the undo or redo UndoGroup container that should store the next
  79. // change taking into account if an undo or redo is being executed.
  80. std::vector<std::unique_ptr<UndoGroup>>* GetActiveUndoGroup();
  81. // Containers of user actions ready for an undo or redo treated as a stack.
  82. std::vector<std::unique_ptr<UndoGroup>> undo_actions_;
  83. std::vector<std::unique_ptr<UndoGroup>> redo_actions_;
  84. // The observers to notify when internal state changes.
  85. base::ObserverList<UndoManagerObserver>::Unchecked observers_;
  86. // Supports grouping operations into a single undo action.
  87. int group_actions_count_;
  88. // The container that is used when actions are grouped.
  89. std::unique_ptr<UndoGroup> pending_grouped_action_;
  90. // The action that is in the process of being undone.
  91. UndoGroup* undo_in_progress_action_;
  92. // Supports the suspension of undo tracking.
  93. int undo_suspended_count_;
  94. // Set when executing Undo or Redo so that incoming changes are correctly
  95. // processed.
  96. bool performing_undo_;
  97. bool performing_redo_;
  98. };
  99. #endif // COMPONENTS_UNDO_UNDO_MANAGER_H_