Chromium Code Reviews| 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) { |
|
sky
2017/01/20 16:57:05
no {}
ke.he
2017/01/22 07:44:08
Done.
| |
| 43 ri != operations_.rend(); ++ri) { | |
| 44 (*ri)->Undo(); | 44 (*ri)->Undo(); |
| 45 } | 45 } |
| 46 } | 46 } |
| 47 | 47 |
| 48 // UndoManager ---------------------------------------------------------------- | 48 // UndoManager ---------------------------------------------------------------- |
| 49 | 49 |
| 50 UndoManager::UndoManager() | 50 UndoManager::UndoManager() |
| 51 : group_actions_count_(0), | 51 : group_actions_count_(0), |
| 52 undo_in_progress_action_(NULL), | 52 undo_in_progress_action_(NULL), |
| 53 undo_suspended_count_(0), | 53 undo_suspended_count_(0), |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 --undo_suspended_count_; | 135 --undo_suspended_count_; |
| 136 } | 136 } |
| 137 | 137 |
| 138 bool UndoManager::IsUndoTrakingSuspended() const { | 138 bool UndoManager::IsUndoTrakingSuspended() const { |
| 139 return undo_suspended_count_ > 0; | 139 return undo_suspended_count_ > 0; |
| 140 } | 140 } |
| 141 | 141 |
| 142 std::vector<UndoOperation*> UndoManager::GetAllUndoOperations() const { | 142 std::vector<UndoOperation*> UndoManager::GetAllUndoOperations() const { |
| 143 std::vector<UndoOperation*> result; | 143 std::vector<UndoOperation*> result; |
| 144 for (size_t i = 0; i < undo_actions_.size(); ++i) { | 144 for (size_t i = 0; i < undo_actions_.size(); ++i) { |
| 145 const std::vector<UndoOperation*>& operations = | 145 const std::vector<UndoOperation*> operations = |
| 146 undo_actions_[i]->undo_operations(); | 146 ConvertToRawPtrVector(undo_actions_[i]->undo_operations()); |
| 147 result.insert(result.end(), operations.begin(), operations.end()); | 147 result.insert(result.end(), operations.begin(), operations.end()); |
| 148 } | 148 } |
| 149 for (size_t i = 0; i < redo_actions_.size(); ++i) { | 149 for (size_t i = 0; i < redo_actions_.size(); ++i) { |
| 150 const std::vector<UndoOperation*>& operations = | 150 const std::vector<UndoOperation*> operations = |
| 151 redo_actions_[i]->undo_operations(); | 151 ConvertToRawPtrVector(redo_actions_[i]->undo_operations()); |
| 152 result.insert(result.end(), operations.begin(), operations.end()); | 152 result.insert(result.end(), operations.begin(), operations.end()); |
| 153 } | 153 } |
| 154 // Ensure that if an Undo is in progress the UndoOperations part of that | 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 | 155 // UndoGroup are included in the returned set. This will ensure that any |
| 156 // changes (such as renumbering) will be applied to any potentially | 156 // changes (such as renumbering) will be applied to any potentially |
| 157 // unprocessed UndoOperations. | 157 // unprocessed UndoOperations. |
| 158 if (undo_in_progress_action_) { | 158 if (undo_in_progress_action_) { |
| 159 const std::vector<UndoOperation*>& operations = | 159 const std::vector<UndoOperation*> operations = |
| 160 undo_in_progress_action_->undo_operations(); | 160 ConvertToRawPtrVector(undo_in_progress_action_->undo_operations()); |
| 161 result.insert(result.end(), operations.begin(), operations.end()); | 161 result.insert(result.end(), operations.begin(), operations.end()); |
| 162 } | 162 } |
| 163 | 163 |
| 164 return result; | 164 return result; |
| 165 } | 165 } |
| 166 | 166 |
| 167 void UndoManager::RemoveAllOperations() { | 167 void UndoManager::RemoveAllOperations() { |
| 168 DCHECK(!group_actions_count_); | 168 DCHECK(!group_actions_count_); |
| 169 undo_actions_.clear(); | 169 undo_actions_.clear(); |
| 170 redo_actions_.clear(); | 170 redo_actions_.clear(); |
| 171 | 171 |
| 172 NotifyOnUndoManagerStateChange(); | 172 NotifyOnUndoManagerStateChange(); |
| 173 } | 173 } |
| 174 | 174 |
| 175 void UndoManager::AddObserver(UndoManagerObserver* observer) { | 175 void UndoManager::AddObserver(UndoManagerObserver* observer) { |
| 176 observers_.AddObserver(observer); | 176 observers_.AddObserver(observer); |
| 177 } | 177 } |
| 178 | 178 |
| 179 void UndoManager::RemoveObserver(UndoManagerObserver* observer) { | 179 void UndoManager::RemoveObserver(UndoManagerObserver* observer) { |
| 180 observers_.RemoveObserver(observer); | 180 observers_.RemoveObserver(observer); |
| 181 } | 181 } |
| 182 | 182 |
| 183 void UndoManager::Undo(bool* performing_indicator, | 183 void UndoManager::Undo( |
| 184 ScopedVector<UndoGroup>* active_undo_group) { | 184 bool* performing_indicator, |
| 185 std::vector<std::unique_ptr<UndoGroup>>* active_undo_group) { | |
| 185 // Check that action grouping has been correctly ended. | 186 // Check that action grouping has been correctly ended. |
| 186 DCHECK(!group_actions_count_); | 187 DCHECK(!group_actions_count_); |
| 187 | 188 |
| 188 if (active_undo_group->empty()) | 189 if (active_undo_group->empty()) |
| 189 return; | 190 return; |
| 190 | 191 |
| 191 base::AutoReset<bool> incoming_changes(performing_indicator, true); | 192 base::AutoReset<bool> incoming_changes(performing_indicator, true); |
| 192 std::unique_ptr<UndoGroup> action(active_undo_group->back()); | 193 std::unique_ptr<UndoGroup> action = std::move(active_undo_group->back()); |
| 193 base::AutoReset<UndoGroup*> action_context(&undo_in_progress_action_, | 194 base::AutoReset<UndoGroup*> action_context(&undo_in_progress_action_, |
| 194 action.get()); | 195 action.get()); |
| 195 active_undo_group->weak_erase( | 196 active_undo_group->erase(active_undo_group->begin() + |
| 196 active_undo_group->begin() + active_undo_group->size() - 1); | 197 active_undo_group->size() - 1); |
| 197 | 198 |
| 198 StartGroupingActions(); | 199 StartGroupingActions(); |
| 199 action->Undo(); | 200 action->Undo(); |
| 200 EndGroupingActions(); | 201 EndGroupingActions(); |
| 201 | 202 |
| 202 NotifyOnUndoManagerStateChange(); | 203 NotifyOnUndoManagerStateChange(); |
| 203 } | 204 } |
| 204 | 205 |
| 206 std::vector<UndoOperation*> UndoManager::ConvertToRawPtrVector( | |
|
sky
2017/01/20 16:57:05
Move this into anonymous namespace at top of file.
ke.he
2017/01/22 07:44:08
Done.
| |
| 207 const std::vector<std::unique_ptr<UndoOperation>>& args) const { | |
| 208 std::vector<UndoOperation*> args_rawptrs(args.size()); | |
| 209 std::transform( | |
|
sky
2017/01/20 16:57:05
Use a for loop, same amount of code and more reada
ke.he
2017/01/22 07:44:08
Done.
| |
| 210 args.begin(), args.end(), args_rawptrs.begin(), | |
| 211 [](const std::unique_ptr<UndoOperation>& arg) { return arg.get(); }); | |
| 212 return args_rawptrs; | |
| 213 } | |
| 214 | |
| 205 void UndoManager::NotifyOnUndoManagerStateChange() { | 215 void UndoManager::NotifyOnUndoManagerStateChange() { |
| 206 for (auto& observer : observers_) | 216 for (auto& observer : observers_) |
| 207 observer.OnUndoManagerStateChange(); | 217 observer.OnUndoManagerStateChange(); |
| 208 } | 218 } |
| 209 | 219 |
| 210 void UndoManager::AddUndoGroup(UndoGroup* new_undo_group) { | 220 void UndoManager::AddUndoGroup(UndoGroup* new_undo_group) { |
| 211 GetActiveUndoGroup()->push_back(new_undo_group); | 221 GetActiveUndoGroup()->push_back(base::WrapUnique<UndoGroup>(new_undo_group)); |
| 212 | 222 |
| 213 // User actions invalidate any available redo actions. | 223 // User actions invalidate any available redo actions. |
| 214 if (is_user_action()) | 224 if (is_user_action()) |
| 215 redo_actions_.clear(); | 225 redo_actions_.clear(); |
| 216 | 226 |
| 217 // Limit the number of undo levels so the undo stack does not grow unbounded. | 227 // Limit the number of undo levels so the undo stack does not grow unbounded. |
| 218 if (GetActiveUndoGroup()->size() > kMaxUndoGroups) | 228 if (GetActiveUndoGroup()->size() > kMaxUndoGroups) |
| 219 GetActiveUndoGroup()->erase(GetActiveUndoGroup()->begin()); | 229 GetActiveUndoGroup()->erase(GetActiveUndoGroup()->begin()); |
| 220 | 230 |
| 221 NotifyOnUndoManagerStateChange(); | 231 NotifyOnUndoManagerStateChange(); |
| 222 } | 232 } |
| 223 | 233 |
| 224 ScopedVector<UndoGroup>* UndoManager::GetActiveUndoGroup() { | 234 std::vector<std::unique_ptr<UndoGroup>>* UndoManager::GetActiveUndoGroup() { |
| 225 return performing_undo_ ? &redo_actions_ : &undo_actions_; | 235 return performing_undo_ ? &redo_actions_ : &undo_actions_; |
| 226 } | 236 } |
| OLD | NEW |