OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/views/payments/view_stack.h" |
| 6 |
| 7 #include "ui/views/layout/fill_layout.h" |
| 8 |
| 9 ViewStack::ViewStack() |
| 10 : slide_in_animator_(base::MakeUnique<views::BoundsAnimator>(this)), |
| 11 slide_out_animator_(base::MakeUnique<views::BoundsAnimator>(this)) { |
| 12 SetLayoutManager(new views::FillLayout()); |
| 13 |
| 14 slide_out_animator_->AddObserver(this); |
| 15 } |
| 16 |
| 17 ViewStack::~ViewStack() {} |
| 18 |
| 19 void ViewStack::Push(std::unique_ptr<views::View> view, bool animate) { |
| 20 gfx::Rect destination = bounds(); |
| 21 destination.set_origin(gfx::Point(0, 0)); |
| 22 if (animate) { |
| 23 // First add the new view out of bounds since it'll slide in from right to |
| 24 // left. |
| 25 view->SetBounds(width(), 0, width(), height()); |
| 26 view->Layout(); |
| 27 |
| 28 AddChildView(view.get()); |
| 29 |
| 30 // Animate the new view to be right on top of this one. |
| 31 slide_in_animator_->AnimateViewTo(view.get(), destination); |
| 32 } else { |
| 33 view->SetBoundsRect(destination); |
| 34 view->Layout(); |
| 35 AddChildView(view.get()); |
| 36 } |
| 37 |
| 38 view->set_owned_by_client(); |
| 39 // Add the new view to the stack so it can be popped later when navigating |
| 40 // back to the previous screen. |
| 41 stack_.push_back(std::move(view)); |
| 42 } |
| 43 |
| 44 void ViewStack::Pop() { |
| 45 gfx::Rect destination = bounds(); |
| 46 destination.set_origin(gfx::Point(width(), 0)); |
| 47 |
| 48 slide_out_animator_->AnimateViewTo( |
| 49 stack_.back().get(), destination); |
| 50 } |
| 51 |
| 52 bool ViewStack::CanProcessEventsWithinSubtree() const { |
| 53 return !slide_in_animator_->IsAnimating() && |
| 54 !slide_out_animator_->IsAnimating(); |
| 55 } |
| 56 |
| 57 void ViewStack::Layout() { |
| 58 views::View::Layout(); |
| 59 |
| 60 // If this view's bounds changed since the beginning of an animation, the |
| 61 // animator's targets have to be changed as well. |
| 62 gfx::Rect in_new_destination = bounds(); |
| 63 in_new_destination.set_origin(gfx::Point(0, 0)); |
| 64 UpdateAnimatorBounds(slide_in_animator_.get(), in_new_destination); |
| 65 |
| 66 |
| 67 gfx::Rect out_new_destination = bounds(); |
| 68 out_new_destination.set_origin(gfx::Point(width(), 0)); |
| 69 UpdateAnimatorBounds(slide_out_animator_.get(), out_new_destination); |
| 70 } |
| 71 |
| 72 void ViewStack::UpdateAnimatorBounds( |
| 73 views::BoundsAnimator* animator, const gfx::Rect& target) { |
| 74 // If an animator is currently animating, figure out which views and update |
| 75 // their target bounds. |
| 76 if (animator->IsAnimating()) { |
| 77 for (auto& view: stack_) { |
| 78 if (animator->IsAnimating(view.get())) { |
| 79 animator->SetTargetBounds(view.get(), target); |
| 80 } |
| 81 } |
| 82 } |
| 83 } |
| 84 |
| 85 void ViewStack::OnBoundsAnimatorDone(views::BoundsAnimator* animator) { |
| 86 // This should only be called from slide_out_animator_ when the views going |
| 87 // out are done animating. |
| 88 DCHECK_EQ(animator, slide_out_animator_.get()); |
| 89 |
| 90 stack_.pop_back(); |
| 91 DCHECK(!stack_.empty()) << "State stack should never be empty"; |
| 92 } |
OLD | NEW |