| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/ui/views/payments/view_stack.h" | 5 #include "chrome/browser/ui/views/payments/view_stack.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "ui/views/layout/fill_layout.h" | 11 #include "ui/views/layout/fill_layout.h" |
| 12 | 12 |
| 13 ViewStack::ViewStack() | 13 ViewStack::ViewStack() |
| 14 : slide_in_animator_(base::MakeUnique<views::BoundsAnimator>(this)), | 14 : slide_in_animator_(base::MakeUnique<views::BoundsAnimator>(this)), |
| 15 slide_out_animator_(base::MakeUnique<views::BoundsAnimator>(this)) { | 15 slide_out_animator_(base::MakeUnique<views::BoundsAnimator>(this)) { |
| 16 SetLayoutManager(new views::FillLayout()); | 16 SetLayoutManager(new views::FillLayout()); |
| 17 | 17 |
| 18 slide_out_animator_->AddObserver(this); | 18 slide_out_animator_->AddObserver(this); |
| 19 slide_in_animator_->AddObserver(this); |
| 20 |
| 19 // Paint to a layer and Mask to Bounds, otherwise descendant views that paint | 21 // Paint to a layer and Mask to Bounds, otherwise descendant views that paint |
| 20 // to a layer themselves will still paint while they're being animated out and | 22 // to a layer themselves will still paint while they're being animated out and |
| 21 // are out of bounds of their parent. | 23 // are out of bounds of their parent. |
| 22 SetPaintToLayer(); | 24 SetPaintToLayer(); |
| 23 layer()->SetMasksToBounds(true); | 25 layer()->SetMasksToBounds(true); |
| 24 | 26 |
| 25 slide_in_animator_->set_tween_type(gfx::Tween::FAST_OUT_SLOW_IN); | 27 slide_in_animator_->set_tween_type(gfx::Tween::FAST_OUT_SLOW_IN); |
| 26 slide_out_animator_->set_tween_type(gfx::Tween::FAST_OUT_SLOW_IN); | 28 slide_out_animator_->set_tween_type(gfx::Tween::FAST_OUT_SLOW_IN); |
| 27 } | 29 } |
| 28 | 30 |
| 29 ViewStack::~ViewStack() {} | 31 ViewStack::~ViewStack() {} |
| 30 | 32 |
| 31 void ViewStack::Push(std::unique_ptr<views::View> view, bool animate) { | 33 void ViewStack::Push(std::unique_ptr<views::View> view, bool animate) { |
| 32 gfx::Rect destination = bounds(); | 34 gfx::Rect destination = bounds(); |
| 33 destination.set_origin(gfx::Point(0, 0)); | 35 destination.set_origin(gfx::Point(0, 0)); |
| 34 | 36 |
| 35 if (animate) { | 37 if (animate) { |
| 36 // First add the new view out of bounds since it'll slide in from right to | 38 // First add the new view out of bounds since it'll slide in from right to |
| 37 // left. | 39 // left. |
| 38 view->SetBounds(width(), 0, width(), height()); | 40 view->SetBounds(width(), 0, width(), height()); |
| 39 view->Layout(); | |
| 40 | |
| 41 AddChildView(view.get()); | |
| 42 | |
| 43 // Animate the new view to be right on top of this one. | |
| 44 slide_in_animator_->AnimateViewTo(view.get(), destination); | |
| 45 } else { | 41 } else { |
| 46 view->SetBoundsRect(destination); | 42 view->SetBoundsRect(destination); |
| 47 view->Layout(); | |
| 48 AddChildView(view.get()); | |
| 49 } | 43 } |
| 44 view->Layout(); |
| 45 AddChildView(view.get()); |
| 50 | 46 |
| 51 view->set_owned_by_client(); | 47 view->set_owned_by_client(); |
| 48 |
| 52 // Add the new view to the stack so it can be popped later when navigating | 49 // Add the new view to the stack so it can be popped later when navigating |
| 53 // back to the previous screen. | 50 // back to the previous screen. |
| 54 stack_.push_back(std::move(view)); | 51 stack_.push_back(std::move(view)); |
| 52 |
| 53 if (animate) { |
| 54 // Animate the new view to be right on top of this one. |
| 55 slide_in_animator_->AnimateViewTo(stack_.back().get(), destination); |
| 56 } else { |
| 57 // This is handled by the post-animation callback in the animated case, so |
| 58 // trigger it synchronously here. |
| 59 HideCoveredViews(); |
| 60 } |
| 61 |
| 55 RequestFocus(); | 62 RequestFocus(); |
| 56 } | 63 } |
| 57 | 64 |
| 58 void ViewStack::Pop() { | 65 void ViewStack::Pop() { |
| 66 DCHECK_LT(1u, size()); // There must be at least one view left after popping. |
| 67 |
| 59 gfx::Rect destination = bounds(); | 68 gfx::Rect destination = bounds(); |
| 60 destination.set_origin(gfx::Point(width(), 0)); | 69 destination.set_origin(gfx::Point(width(), 0)); |
| 61 | 70 |
| 71 // Set the second-to-last view as visible, since it is about to be revealed |
| 72 // when the last view animates out. |
| 73 stack_[size() - 2]->SetVisible(true); |
| 74 |
| 62 slide_out_animator_->AnimateViewTo( | 75 slide_out_animator_->AnimateViewTo( |
| 63 stack_.back().get(), destination); | 76 stack_.back().get(), destination); |
| 64 } | 77 } |
| 65 | 78 |
| 66 void ViewStack::PopMany(int n) { | 79 void ViewStack::PopMany(int n) { |
| 67 DCHECK_LT(static_cast<size_t>(n), size()); // The stack can never be empty. | 80 DCHECK_LT(static_cast<size_t>(n), size()); // The stack can never be empty. |
| 68 | 81 |
| 69 size_t pre_size = stack_.size(); | 82 size_t pre_size = stack_.size(); |
| 70 // Erase N - 1 elements now, the last one will be erased when its animation | 83 // Erase N - 1 elements now, the last one will be erased when its animation |
| 71 // completes | 84 // completes |
| (...skipping 28 matching lines...) Expand all Loading... |
| 100 } | 113 } |
| 101 | 114 |
| 102 void ViewStack::RequestFocus() { | 115 void ViewStack::RequestFocus() { |
| 103 // The view can only be focused if it has a widget already. It's possible that | 116 // The view can only be focused if it has a widget already. It's possible that |
| 104 // this isn't the case if some views are pushed before the stack is added to a | 117 // this isn't the case if some views are pushed before the stack is added to a |
| 105 // hierarchy that has a widget. | 118 // hierarchy that has a widget. |
| 106 if (top()->GetWidget()) | 119 if (top()->GetWidget()) |
| 107 top()->RequestFocus(); | 120 top()->RequestFocus(); |
| 108 } | 121 } |
| 109 | 122 |
| 123 void ViewStack::HideCoveredViews() { |
| 124 // Iterate through all but the last (topmost) view. |
| 125 for (size_t i = 0; i + 1 < size(); i++) { |
| 126 stack_[i]->SetVisible(false); |
| 127 } |
| 128 } |
| 129 |
| 110 void ViewStack::UpdateAnimatorBounds( | 130 void ViewStack::UpdateAnimatorBounds( |
| 111 views::BoundsAnimator* animator, const gfx::Rect& target) { | 131 views::BoundsAnimator* animator, const gfx::Rect& target) { |
| 112 // If an animator is currently animating, figure out which views and update | 132 // If an animator is currently animating, figure out which views and update |
| 113 // their target bounds. | 133 // their target bounds. |
| 114 if (animator->IsAnimating()) { | 134 if (animator->IsAnimating()) { |
| 115 for (auto& view : stack_) { | 135 for (auto& view : stack_) { |
| 116 if (animator->IsAnimating(view.get())) { | 136 if (animator->IsAnimating(view.get())) { |
| 117 animator->SetTargetBounds(view.get(), target); | 137 animator->SetTargetBounds(view.get(), target); |
| 118 } | 138 } |
| 119 } | 139 } |
| 120 } | 140 } |
| 121 } | 141 } |
| 122 | 142 |
| 123 void ViewStack::OnBoundsAnimatorDone(views::BoundsAnimator* animator) { | 143 void ViewStack::OnBoundsAnimatorDone(views::BoundsAnimator* animator) { |
| 124 // This should only be called from slide_out_animator_ when the views going | 144 if (animator == slide_out_animator_.get()) { |
| 125 // out are done animating. | 145 stack_.pop_back(); |
| 126 DCHECK_EQ(animator, slide_out_animator_.get()); | 146 DCHECK(!stack_.empty()) << "State stack should never be empty"; |
| 127 | 147 RequestFocus(); |
| 128 stack_.pop_back(); | 148 } else if (animator == slide_in_animator_.get()) { |
| 129 DCHECK(!stack_.empty()) << "State stack should never be empty"; | 149 HideCoveredViews(); |
| 130 RequestFocus(); | 150 } else { |
| 151 NOTREACHED(); |
| 152 } |
| 131 } | 153 } |
| OLD | NEW |