blob: ecc1459855bb4e3cc0fda17bea837c876c359f7c [file] [log] [blame]
[email protected]be5cf822013-07-10 16:36:211// 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
sdefresnec083d1f2015-04-17 21:12:185#ifndef COMPONENTS_UNDO_UNDO_MANAGER_H_
6#define COMPONENTS_UNDO_UNDO_MANAGER_H_
[email protected]be5cf822013-07-10 16:36:217
avi5dd91f82015-12-25 22:30:468#include <stddef.h>
9
dcheng3f767dc32016-04-25 22:54:2210#include <memory>
ke.heda6df4de2017-01-26 00:20:0311#include <vector>
dcheng3f767dc32016-04-25 22:54:2212
avi5dd91f82015-12-25 22:30:4613#include "base/macros.h"
[email protected]0167050e2014-04-15 21:14:2214#include "base/observer_list.h"
[email protected]7d63a262013-12-17 17:15:5515#include "base/strings/string16.h"
[email protected]be5cf822013-07-10 16:36:2116
[email protected]0167050e2014-04-15 21:14:2217class UndoManagerObserver;
[email protected]be5cf822013-07-10 16:36:2118class UndoOperation;
19
20// UndoGroup ------------------------------------------------------------------
21
22// UndoGroup represents a user action and stores all the operations that
23// make that action. Typically there is only one operation per UndoGroup.
24class UndoGroup {
25 public:
26 UndoGroup();
27 ~UndoGroup();
28
dcheng3f767dc32016-04-25 22:54:2229 void AddOperation(std::unique_ptr<UndoOperation> operation);
ke.heda6df4de2017-01-26 00:20:0330 const std::vector<std::unique_ptr<UndoOperation>>& undo_operations() {
31 return operations_;
[email protected]be5cf822013-07-10 16:36:2132 }
[email protected]be5cf822013-07-10 16:36:2133 void Undo();
34
[email protected]7d63a262013-12-17 17:15:5535 // The resource string id describing the undo and redo action.
36 int get_undo_label_id() const { return undo_label_id_; }
37 void set_undo_label_id(int label_id) { undo_label_id_ = label_id; }
38
39 int get_redo_label_id() const { return redo_label_id_; }
40 void set_redo_label_id(int label_id) { redo_label_id_ = label_id; }
41
[email protected]be5cf822013-07-10 16:36:2142 private:
ke.heda6df4de2017-01-26 00:20:0343 std::vector<std::unique_ptr<UndoOperation>> operations_;
[email protected]be5cf822013-07-10 16:36:2144
[email protected]7d63a262013-12-17 17:15:5545 // The resource string id describing the undo and redo action.
46 int undo_label_id_;
47 int redo_label_id_;
48
[email protected]be5cf822013-07-10 16:36:2149 DISALLOW_COPY_AND_ASSIGN(UndoGroup);
50};
51
52// UndoManager ----------------------------------------------------------------
53
54// Maintains user actions as a group of operations that store enough info to
55// undo and redo those operations.
56class UndoManager {
57 public:
58 UndoManager();
59 ~UndoManager();
60
61 // Perform an undo or redo operation.
62 void Undo();
63 void Redo();
64
65 size_t undo_count() const { return undo_actions_.size(); }
66 size_t redo_count() const { return redo_actions_.size(); }
67
[email protected]7d63a262013-12-17 17:15:5568 base::string16 GetUndoLabel() const;
69 base::string16 GetRedoLabel() const;
70
dcheng3f767dc32016-04-25 22:54:2271 void AddUndoOperation(std::unique_ptr<UndoOperation> operation);
[email protected]be5cf822013-07-10 16:36:2172
73 // Group multiple operations into one undoable action.
74 void StartGroupingActions();
75 void EndGroupingActions();
76
77 // Suspend undo tracking while processing non-user initiated changes such as
78 // profile synchonization.
79 void SuspendUndoTracking();
80 void ResumeUndoTracking();
81 bool IsUndoTrakingSuspended() const;
82
[email protected]f0541032013-07-31 07:27:5383 // Remove all undo and redo operations. Note that grouping of actions and
84 // suspension of undo tracking states are left unchanged.
85 void RemoveAllOperations();
86
[email protected]0167050e2014-04-15 21:14:2287 // Observers are notified when the internal state of this class changes.
88 void AddObserver(UndoManagerObserver* observer);
89 void RemoveObserver(UndoManagerObserver* observer);
90
[email protected]be5cf822013-07-10 16:36:2191 private:
ke.heda6df4de2017-01-26 00:20:0392 friend class UndoManagerTestApi;
93
[email protected]be5cf822013-07-10 16:36:2194 void Undo(bool* performing_indicator,
ke.heda6df4de2017-01-26 00:20:0395 std::vector<std::unique_ptr<UndoGroup>>* active_undo_group);
[email protected]f0541032013-07-31 07:27:5396 bool is_user_action() const { return !performing_undo_ && !performing_redo_; }
[email protected]be5cf822013-07-10 16:36:2197
[email protected]0167050e2014-04-15 21:14:2298 // Notifies the observers that the undo manager's state has changed.
99 void NotifyOnUndoManagerStateChange();
100
[email protected]f0541032013-07-31 07:27:53101 // Handle the addition of |new_undo_group| to the active undo group container.
102 void AddUndoGroup(UndoGroup* new_undo_group);
[email protected]be5cf822013-07-10 16:36:21103
[email protected]f0541032013-07-31 07:27:53104 // Returns the undo or redo UndoGroup container that should store the next
105 // change taking into account if an undo or redo is being executed.
ke.heda6df4de2017-01-26 00:20:03106 std::vector<std::unique_ptr<UndoGroup>>* GetActiveUndoGroup();
[email protected]be5cf822013-07-10 16:36:21107
108 // Containers of user actions ready for an undo or redo treated as a stack.
ke.heda6df4de2017-01-26 00:20:03109 std::vector<std::unique_ptr<UndoGroup>> undo_actions_;
110 std::vector<std::unique_ptr<UndoGroup>> redo_actions_;
[email protected]be5cf822013-07-10 16:36:21111
[email protected]0167050e2014-04-15 21:14:22112 // The observers to notify when internal state changes.
Trent Apteda250ec3ab2018-08-19 08:52:19113 base::ObserverList<UndoManagerObserver>::Unchecked observers_;
[email protected]0167050e2014-04-15 21:14:22114
[email protected]be5cf822013-07-10 16:36:21115 // Supports grouping operations into a single undo action.
116 int group_actions_count_;
117
118 // The container that is used when actions are grouped.
dcheng3f767dc32016-04-25 22:54:22119 std::unique_ptr<UndoGroup> pending_grouped_action_;
[email protected]be5cf822013-07-10 16:36:21120
[email protected]cb8f79e2013-12-18 20:08:42121 // The action that is in the process of being undone.
122 UndoGroup* undo_in_progress_action_;
123
[email protected]be5cf822013-07-10 16:36:21124 // Supports the suspension of undo tracking.
125 int undo_suspended_count_;
126
127 // Set when executing Undo or Redo so that incoming changes are correctly
128 // processed.
129 bool performing_undo_;
130 bool performing_redo_;
131
132 DISALLOW_COPY_AND_ASSIGN(UndoManager);
133};
134
sdefresnec083d1f2015-04-17 21:12:18135#endif // COMPONENTS_UNDO_UNDO_MANAGER_H_