| 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 "chrome/browser/undo/undo_manager.h" | 5 #include "chrome/browser/undo/undo_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> |
| 8 |
| 7 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 9 #include "chrome/browser/undo/undo_operation.h" | 11 #include "chrome/browser/undo/undo_operation.h" |
| 10 | 12 |
| 11 // UndoGroup ------------------------------------------------------------------ | 13 // UndoGroup ------------------------------------------------------------------ |
| 12 | 14 |
| 13 UndoGroup::UndoGroup() { | 15 UndoGroup::UndoGroup() { |
| 14 } | 16 } |
| 15 | 17 |
| 16 UndoGroup::~UndoGroup() { | 18 UndoGroup::~UndoGroup() { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 46 void UndoManager::Undo() { | 48 void UndoManager::Undo() { |
| 47 Undo(&performing_undo_, &undo_actions_); | 49 Undo(&performing_undo_, &undo_actions_); |
| 48 } | 50 } |
| 49 | 51 |
| 50 void UndoManager::Redo() { | 52 void UndoManager::Redo() { |
| 51 Undo(&performing_redo_, &redo_actions_); | 53 Undo(&performing_redo_, &redo_actions_); |
| 52 } | 54 } |
| 53 | 55 |
| 54 void UndoManager::AddUndoOperation(scoped_ptr<UndoOperation> operation) { | 56 void UndoManager::AddUndoOperation(scoped_ptr<UndoOperation> operation) { |
| 55 if (IsUndoTrakingSuspended()) { | 57 if (IsUndoTrakingSuspended()) { |
| 56 RemoveAllActions(); | 58 Reset(); |
| 57 operation.reset(); | 59 operation.reset(); |
| 58 return; | 60 return; |
| 59 } | 61 } |
| 60 | 62 |
| 61 if (group_actions_count_) { | 63 if (group_actions_count_) { |
| 62 pending_grouped_action_->AddOperation(operation.Pass()); | 64 pending_grouped_action_->AddOperation(operation.Pass()); |
| 63 } else { | 65 } else { |
| 64 UndoGroup* new_action = new UndoGroup(); | 66 UndoGroup* new_action = new UndoGroup(); |
| 65 new_action->AddOperation(operation.Pass()); | 67 new_action->AddOperation(operation.Pass()); |
| 66 GetActiveUndoGroup()->insert(GetActiveUndoGroup()->end(), new_action); | 68 GetActiveUndoGroup()->push_back(new_action); |
| 67 | 69 |
| 68 // A new user action invalidates any available redo actions. | 70 // User actions invalidate any available redo actions. |
| 69 RemoveAllRedoActions(); | 71 if (is_user_action()) |
| 72 RemoveAllRedoActions(); |
| 70 } | 73 } |
| 71 } | 74 } |
| 72 | 75 |
| 73 void UndoManager::StartGroupingActions() { | 76 void UndoManager::StartGroupingActions() { |
| 74 if (!group_actions_count_) | 77 if (!group_actions_count_) |
| 75 pending_grouped_action_.reset(new UndoGroup()); | 78 pending_grouped_action_.reset(new UndoGroup()); |
| 76 ++group_actions_count_; | 79 ++group_actions_count_; |
| 77 } | 80 } |
| 78 | 81 |
| 79 void UndoManager::EndGroupingActions() { | 82 void UndoManager::EndGroupingActions() { |
| 80 --group_actions_count_; | 83 --group_actions_count_; |
| 81 if (group_actions_count_ > 0) | 84 if (group_actions_count_ > 0) |
| 82 return; | 85 return; |
| 83 | 86 |
| 84 // Check that StartGroupingActions and EndGroupingActions are paired. | 87 // Check that StartGroupingActions and EndGroupingActions are paired. |
| 85 DCHECK_GE(group_actions_count_, 0); | 88 DCHECK_GE(group_actions_count_, 0); |
| 86 | 89 |
| 87 bool is_user_action = !performing_undo_ && !performing_redo_; | |
| 88 if (pending_grouped_action_->has_operations()) { | 90 if (pending_grouped_action_->has_operations()) { |
| 89 GetActiveUndoGroup()->push_back(pending_grouped_action_.release()); | 91 GetActiveUndoGroup()->push_back(pending_grouped_action_.release()); |
| 92 |
| 90 // User actions invalidate any available redo actions. | 93 // User actions invalidate any available redo actions. |
| 91 if (is_user_action) | 94 if (is_user_action()) |
| 92 RemoveAllRedoActions(); | 95 RemoveAllRedoActions(); |
| 93 } else { | 96 } else { |
| 94 // No changes were executed since we started grouping actions, so the | 97 // No changes were executed since we started grouping actions, so the |
| 95 // pending UndoGroup should be discarded. | 98 // pending UndoGroup should be discarded. |
| 96 pending_grouped_action_.reset(); | 99 pending_grouped_action_.reset(); |
| 97 | 100 |
| 98 // This situation is only expected when it is a user initiated action. | 101 // This situation is only expected when it is a user initiated action. |
| 99 // Undo/Redo should have at least one operation performed. | 102 // Undo/Redo should have at least one operation performed. |
| 100 DCHECK(is_user_action); | 103 DCHECK(is_user_action()); |
| 101 } | 104 } |
| 102 } | 105 } |
| 103 | 106 |
| 104 void UndoManager::SuspendUndoTracking() { | 107 void UndoManager::SuspendUndoTracking() { |
| 105 ++undo_suspended_count_; | 108 ++undo_suspended_count_; |
| 106 } | 109 } |
| 107 | 110 |
| 108 void UndoManager::ResumeUndoTracking() { | 111 void UndoManager::ResumeUndoTracking() { |
| 109 DCHECK_GT(undo_suspended_count_, 0); | 112 DCHECK_GT(undo_suspended_count_, 0); |
| 110 --undo_suspended_count_; | 113 --undo_suspended_count_; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 125 base::AutoReset<bool> incoming_changes(performing_indicator, true); | 128 base::AutoReset<bool> incoming_changes(performing_indicator, true); |
| 126 scoped_ptr<UndoGroup> action(active_undo_group->back()); | 129 scoped_ptr<UndoGroup> action(active_undo_group->back()); |
| 127 active_undo_group->weak_erase( | 130 active_undo_group->weak_erase( |
| 128 active_undo_group->begin() + active_undo_group->size() - 1); | 131 active_undo_group->begin() + active_undo_group->size() - 1); |
| 129 | 132 |
| 130 StartGroupingActions(); | 133 StartGroupingActions(); |
| 131 action->Undo(); | 134 action->Undo(); |
| 132 EndGroupingActions(); | 135 EndGroupingActions(); |
| 133 } | 136 } |
| 134 | 137 |
| 135 void UndoManager::RemoveAllActions() { | 138 void UndoManager::Reset() { |
| 136 undo_actions_.clear(); | 139 undo_actions_.clear(); |
| 137 RemoveAllRedoActions(); | 140 RemoveAllRedoActions(); |
| 138 } | 141 } |
| 139 | 142 |
| 140 void UndoManager::RemoveAllRedoActions() { | 143 void UndoManager::RemoveAllRedoActions() { |
| 141 redo_actions_.clear(); | 144 redo_actions_.clear(); |
| 142 } | 145 } |
| 143 | 146 |
| 144 ScopedVector<UndoGroup>* UndoManager::GetActiveUndoGroup() { | 147 ScopedVector<UndoGroup>* UndoManager::GetActiveUndoGroup() { |
| 145 return performing_undo_ ? &redo_actions_ : &undo_actions_; | 148 return performing_undo_ ? &redo_actions_ : &undo_actions_; |
| 146 } | 149 } |
| OLD | NEW |