| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/undo/undo_manager.h" | 5 #include "components/undo/undo_manager.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" |
| 11 #include "components/undo/undo_manager_observer.h" | 12 #include "components/undo/undo_manager_observer.h" |
| 12 #include "components/undo/undo_operation.h" | 13 #include "components/undo/undo_operation.h" |
| 13 #include "grit/components_strings.h" | 14 #include "grit/components_strings.h" |
| 14 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // Maximum number of changes that can be undone. | 19 // Maximum number of changes that can be undone. |
| 19 const size_t kMaxUndoGroups = 100; | 20 const size_t kMaxUndoGroups = 100; |
| 20 | 21 |
| 21 } // namespace | 22 } // namespace |
| 22 | 23 |
| 23 // UndoGroup ------------------------------------------------------------------ | 24 // UndoGroup ------------------------------------------------------------------ |
| 24 | 25 |
| 25 UndoGroup::UndoGroup() | 26 UndoGroup::UndoGroup() |
| 26 : undo_label_id_(IDS_BOOKMARK_BAR_UNDO), | 27 : undo_label_id_(IDS_BOOKMARK_BAR_UNDO), |
| 27 redo_label_id_(IDS_BOOKMARK_BAR_REDO) { | 28 redo_label_id_(IDS_BOOKMARK_BAR_REDO) { |
| 28 } | 29 } |
| 29 | 30 |
| 30 UndoGroup::~UndoGroup() { | 31 UndoGroup::~UndoGroup() { |
| 31 } | 32 } |
| 32 | 33 |
| 33 void UndoGroup::AddOperation(std::unique_ptr<UndoOperation> operation) { | 34 void UndoGroup::AddOperation(std::unique_ptr<UndoOperation> operation) { |
| 34 if (operations_.empty()) { | 35 if (operations_.empty()) { |
| 35 set_undo_label_id(operation->GetUndoLabelId()); | 36 set_undo_label_id(operation->GetUndoLabelId()); |
| 36 set_redo_label_id(operation->GetRedoLabelId()); | 37 set_redo_label_id(operation->GetRedoLabelId()); |
| 37 } | 38 } |
| 38 operations_.push_back(operation.release()); | 39 operations_.push_back(std::move(operation)); |
| 39 } | 40 } |
| 40 | 41 |
| 41 void UndoGroup::Undo() { | 42 void UndoGroup::Undo() { |
| 42 for (ScopedVector<UndoOperation>::reverse_iterator ri = operations_.rbegin(); | 43 for (auto ri = operations_.rbegin(); ri != operations_.rend(); ++ri) |
| 43 ri != operations_.rend(); ++ri) { | |
| 44 (*ri)->Undo(); | 44 (*ri)->Undo(); |
| 45 } | |
| 46 } | 45 } |
| 47 | 46 |
| 48 // UndoManager ---------------------------------------------------------------- | 47 // UndoManager ---------------------------------------------------------------- |
| 49 | 48 |
| 50 UndoManager::UndoManager() | 49 UndoManager::UndoManager() |
| 51 : group_actions_count_(0), | 50 : group_actions_count_(0), |
| 52 undo_in_progress_action_(NULL), | 51 undo_in_progress_action_(NULL), |
| 53 undo_suspended_count_(0), | 52 undo_suspended_count_(0), |
| 54 performing_undo_(false), | 53 performing_undo_(false), |
| 55 performing_redo_(false) { | 54 performing_redo_(false) { |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 | 131 |
| 133 void UndoManager::ResumeUndoTracking() { | 132 void UndoManager::ResumeUndoTracking() { |
| 134 DCHECK_GT(undo_suspended_count_, 0); | 133 DCHECK_GT(undo_suspended_count_, 0); |
| 135 --undo_suspended_count_; | 134 --undo_suspended_count_; |
| 136 } | 135 } |
| 137 | 136 |
| 138 bool UndoManager::IsUndoTrakingSuspended() const { | 137 bool UndoManager::IsUndoTrakingSuspended() const { |
| 139 return undo_suspended_count_ > 0; | 138 return undo_suspended_count_ > 0; |
| 140 } | 139 } |
| 141 | 140 |
| 142 std::vector<UndoOperation*> UndoManager::GetAllUndoOperations() const { | |
| 143 std::vector<UndoOperation*> result; | |
| 144 for (size_t i = 0; i < undo_actions_.size(); ++i) { | |
| 145 const std::vector<UndoOperation*>& operations = | |
| 146 undo_actions_[i]->undo_operations(); | |
| 147 result.insert(result.end(), operations.begin(), operations.end()); | |
| 148 } | |
| 149 for (size_t i = 0; i < redo_actions_.size(); ++i) { | |
| 150 const std::vector<UndoOperation*>& operations = | |
| 151 redo_actions_[i]->undo_operations(); | |
| 152 result.insert(result.end(), operations.begin(), operations.end()); | |
| 153 } | |
| 154 // Ensure that if an Undo is in progress the UndoOperations part of that | |
| 155 // UndoGroup are included in the returned set. This will ensure that any | |
| 156 // changes (such as renumbering) will be applied to any potentially | |
| 157 // unprocessed UndoOperations. | |
| 158 if (undo_in_progress_action_) { | |
| 159 const std::vector<UndoOperation*>& operations = | |
| 160 undo_in_progress_action_->undo_operations(); | |
| 161 result.insert(result.end(), operations.begin(), operations.end()); | |
| 162 } | |
| 163 | |
| 164 return result; | |
| 165 } | |
| 166 | |
| 167 void UndoManager::RemoveAllOperations() { | 141 void UndoManager::RemoveAllOperations() { |
| 168 DCHECK(!group_actions_count_); | 142 DCHECK(!group_actions_count_); |
| 169 undo_actions_.clear(); | 143 undo_actions_.clear(); |
| 170 redo_actions_.clear(); | 144 redo_actions_.clear(); |
| 171 | 145 |
| 172 NotifyOnUndoManagerStateChange(); | 146 NotifyOnUndoManagerStateChange(); |
| 173 } | 147 } |
| 174 | 148 |
| 175 void UndoManager::AddObserver(UndoManagerObserver* observer) { | 149 void UndoManager::AddObserver(UndoManagerObserver* observer) { |
| 176 observers_.AddObserver(observer); | 150 observers_.AddObserver(observer); |
| 177 } | 151 } |
| 178 | 152 |
| 179 void UndoManager::RemoveObserver(UndoManagerObserver* observer) { | 153 void UndoManager::RemoveObserver(UndoManagerObserver* observer) { |
| 180 observers_.RemoveObserver(observer); | 154 observers_.RemoveObserver(observer); |
| 181 } | 155 } |
| 182 | 156 |
| 183 void UndoManager::Undo(bool* performing_indicator, | 157 void UndoManager::Undo( |
| 184 ScopedVector<UndoGroup>* active_undo_group) { | 158 bool* performing_indicator, |
| 159 std::vector<std::unique_ptr<UndoGroup>>* active_undo_group) { |
| 185 // Check that action grouping has been correctly ended. | 160 // Check that action grouping has been correctly ended. |
| 186 DCHECK(!group_actions_count_); | 161 DCHECK(!group_actions_count_); |
| 187 | 162 |
| 188 if (active_undo_group->empty()) | 163 if (active_undo_group->empty()) |
| 189 return; | 164 return; |
| 190 | 165 |
| 191 base::AutoReset<bool> incoming_changes(performing_indicator, true); | 166 base::AutoReset<bool> incoming_changes(performing_indicator, true); |
| 192 std::unique_ptr<UndoGroup> action(active_undo_group->back()); | 167 std::unique_ptr<UndoGroup> action = std::move(active_undo_group->back()); |
| 193 base::AutoReset<UndoGroup*> action_context(&undo_in_progress_action_, | 168 base::AutoReset<UndoGroup*> action_context(&undo_in_progress_action_, |
| 194 action.get()); | 169 action.get()); |
| 195 active_undo_group->weak_erase( | 170 active_undo_group->erase(active_undo_group->begin() + |
| 196 active_undo_group->begin() + active_undo_group->size() - 1); | 171 active_undo_group->size() - 1); |
| 197 | 172 |
| 198 StartGroupingActions(); | 173 StartGroupingActions(); |
| 199 action->Undo(); | 174 action->Undo(); |
| 200 EndGroupingActions(); | 175 EndGroupingActions(); |
| 201 | 176 |
| 202 NotifyOnUndoManagerStateChange(); | 177 NotifyOnUndoManagerStateChange(); |
| 203 } | 178 } |
| 204 | 179 |
| 205 void UndoManager::NotifyOnUndoManagerStateChange() { | 180 void UndoManager::NotifyOnUndoManagerStateChange() { |
| 206 for (auto& observer : observers_) | 181 for (auto& observer : observers_) |
| 207 observer.OnUndoManagerStateChange(); | 182 observer.OnUndoManagerStateChange(); |
| 208 } | 183 } |
| 209 | 184 |
| 210 void UndoManager::AddUndoGroup(UndoGroup* new_undo_group) { | 185 void UndoManager::AddUndoGroup(UndoGroup* new_undo_group) { |
| 211 GetActiveUndoGroup()->push_back(new_undo_group); | 186 GetActiveUndoGroup()->push_back(base::WrapUnique<UndoGroup>(new_undo_group)); |
| 212 | 187 |
| 213 // User actions invalidate any available redo actions. | 188 // User actions invalidate any available redo actions. |
| 214 if (is_user_action()) | 189 if (is_user_action()) |
| 215 redo_actions_.clear(); | 190 redo_actions_.clear(); |
| 216 | 191 |
| 217 // Limit the number of undo levels so the undo stack does not grow unbounded. | 192 // Limit the number of undo levels so the undo stack does not grow unbounded. |
| 218 if (GetActiveUndoGroup()->size() > kMaxUndoGroups) | 193 if (GetActiveUndoGroup()->size() > kMaxUndoGroups) |
| 219 GetActiveUndoGroup()->erase(GetActiveUndoGroup()->begin()); | 194 GetActiveUndoGroup()->erase(GetActiveUndoGroup()->begin()); |
| 220 | 195 |
| 221 NotifyOnUndoManagerStateChange(); | 196 NotifyOnUndoManagerStateChange(); |
| 222 } | 197 } |
| 223 | 198 |
| 224 ScopedVector<UndoGroup>* UndoManager::GetActiveUndoGroup() { | 199 std::vector<std::unique_ptr<UndoGroup>>* UndoManager::GetActiveUndoGroup() { |
| 225 return performing_undo_ ? &redo_actions_ : &undo_actions_; | 200 return performing_undo_ ? &redo_actions_ : &undo_actions_; |
| 226 } | 201 } |
| OLD | NEW |