Chromium Code Reviews| 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 namespace { | |
| 10 | |
| 11 // Observes the Slide Out view animation and removes the view when it's done | |
| 12 // being animated, then deletes itself. This takes ownership of the view so | |
| 13 // it's garanteed to be properly disposed of when the animation is over. | |
| 14 class SlideOutObserver : public views::BoundsAnimatorObserver { | |
|
sky
2016/12/12 17:38:07
Is this class needed? If so, what for? It seems it
anthonyvd
2016/12/12 18:47:57
Its initial purpose was to remove the view from th
| |
| 15 public: | |
| 16 explicit SlideOutObserver(std::unique_ptr<views::View> view_animating_out) | |
| 17 : view_animating_out_(std::move(view_animating_out)) {} | |
| 18 | |
| 19 views::View* view_animating_out() { | |
| 20 return view_animating_out_.get(); | |
| 21 } | |
| 22 | |
| 23 private: | |
| 24 // views::BoundsAnimatorObserver: | |
| 25 void OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) override {} | |
| 26 | |
| 27 void OnBoundsAnimatorDone(views::BoundsAnimator* animator) override { | |
| 28 animator->RemoveObserver(this); | |
| 29 delete this; | |
| 30 } | |
| 31 | |
| 32 std::unique_ptr<views::View> view_animating_out_; | |
| 33 | |
| 34 DISALLOW_COPY_AND_ASSIGN(SlideOutObserver); | |
| 35 }; | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 39 ViewStack::ViewStack(std::unique_ptr<views::View> initial_view) | |
| 40 : slide_in_animator_(new views::BoundsAnimator(this)), | |
|
sky
2016/12/12 17:38:07
MakeUnique where possible.
anthonyvd
2016/12/12 18:47:57
Done.
| |
| 41 slide_out_animator_(new views::BoundsAnimator(this)), | |
| 42 can_process_events_within_subtree_(true) { | |
| 43 SetLayoutManager(new views::FillLayout()); | |
| 44 | |
| 45 stack_.push(std::move(initial_view)); | |
| 46 AddChildView(stack_.top().get()); | |
| 47 | |
| 48 slide_in_animator_->AddObserver(this); | |
| 49 slide_out_animator_->AddObserver(this); | |
| 50 } | |
| 51 | |
| 52 ViewStack::~ViewStack() { | |
| 53 // Cancel animations to make sure their observer's OnBoundsAnimatorDone is | |
| 54 // called. | |
| 55 slide_in_animator_->Cancel(); | |
| 56 slide_out_animator_->Cancel(); | |
| 57 } | |
| 58 | |
|
sky
2016/12/12 17:38:07
You should override Layout to resize the views. Th
anthonyvd
2016/12/12 18:47:57
I was under the impression that propagating Layout
sky
2016/12/12 20:30:56
You are absolutely right. The only wrinkle here is
anthonyvd
2016/12/12 22:20:48
A yeah, makes sense. I added code that re-targets
| |
| 59 void ViewStack::Push(std::unique_ptr<views::View> view) { | |
| 60 can_process_events_within_subtree_ = false; | |
| 61 | |
| 62 // First add the new view out of bounds since it'll slide in from right to | |
| 63 // left. | |
| 64 view->SetBounds(width(), 0, width(), height()); | |
| 65 view->Layout(); | |
| 66 | |
| 67 AddChildView(view.get()); | |
| 68 // Animate the new view to be right on top of this one. | |
| 69 gfx::Rect destination = bounds(); | |
| 70 destination.set_origin(gfx::Point(0, 0)); | |
| 71 slide_in_animator_->AnimateViewTo(view.get(), destination); | |
| 72 | |
| 73 view->set_owned_by_client(); | |
| 74 // Add the new view to the stack so it can be popped later when navigating | |
| 75 // back to the previous screen. | |
| 76 stack_.push(std::move(view)); | |
| 77 } | |
| 78 | |
| 79 void ViewStack::Pop() { | |
| 80 can_process_events_within_subtree_ = false; | |
| 81 | |
| 82 gfx::Rect destination = bounds(); | |
| 83 destination.set_origin(gfx::Point(width(), 0)); | |
| 84 | |
| 85 // This deletes itself. | |
| 86 SlideOutObserver* observer = | |
| 87 new SlideOutObserver(std::move(stack_.top())); | |
| 88 slide_out_animator_->AddObserver(observer); | |
| 89 | |
| 90 stack_.pop(); | |
| 91 DCHECK(!stack_.empty()) << "State stack should never be empty"; | |
| 92 | |
| 93 slide_out_animator_->AnimateViewTo( | |
| 94 observer->view_animating_out(), destination); | |
| 95 } | |
| 96 | |
| 97 bool ViewStack::CanProcessEventsWithinSubtree() const { | |
| 98 return can_process_events_within_subtree_; | |
| 99 } | |
| 100 | |
| 101 void ViewStack::OnBoundsAnimatorDone( | |
| 102 views::BoundsAnimator* animator) { | |
| 103 can_process_events_within_subtree_ = true; | |
| 104 } | |
| OLD | NEW |