Index: chrome/browser/ui/views/payments/view_stack.cc |
diff --git a/chrome/browser/ui/views/payments/view_stack.cc b/chrome/browser/ui/views/payments/view_stack.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4bb890f334a05a4e240175a3722d24bc8b0bc2c3 |
--- /dev/null |
+++ b/chrome/browser/ui/views/payments/view_stack.cc |
@@ -0,0 +1,98 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/ui/views/payments/view_stack.h" |
+ |
+#include "chrome/browser/ui/views/payments/view_stack_state.h" |
+#include "ui/views/layout/fill_layout.h" |
+ |
+// Observes the Slide Out state animation and removes the view when it's done |
+// being animated, then deletes itself. This takes ownership of the state so |
+// it's garanteed to be properly disposed of when the animation is over. |
+class SlideOutObserver : public views::BoundsAnimatorObserver { |
sky
2016/12/12 01:54:32
Move to anonymous namespace?
anthonyvd
2016/12/12 17:01:56
Done.
|
+ public: |
+ explicit SlideOutObserver(std::unique_ptr<ViewStackState> state_animating_out) |
+ : state_animating_out_(std::move(state_animating_out)) {} |
+ |
+ views::View* GetAnimatedView() { |
+ return state_animating_out_->GetView(); |
+ } |
+ |
+ private: |
+ // views::BoundsAnimatorObserver: |
+ void OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) override {} |
+ |
+ void OnBoundsAnimatorDone(views::BoundsAnimator* animator) override { |
+ animator->RemoveObserver(this); |
+ delete this; |
+ } |
+ |
+ std::unique_ptr<ViewStackState> state_animating_out_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(SlideOutObserver); |
+}; |
+ |
+ViewStack::ViewStack(std::unique_ptr<ViewStackState> initial_state) |
+ : slide_in_animator_(new views::BoundsAnimator(this)), |
+ slide_out_animator_(new views::BoundsAnimator(this)), |
+ can_process_events_within_subtree_(true) { |
+ SetLayoutManager(new views::FillLayout()); |
+ |
+ state_stack_.push(std::move(initial_state)); |
+ AddChildView(state_stack_.top()->GetView()); |
+ |
+ slide_in_animator_->AddObserver(this); |
+ slide_out_animator_->AddObserver(this); |
+} |
+ |
+ViewStack::~ViewStack() { |
+ // Cancel animations to make sure their observer's OnBoundsAnimatorDone is |
+ // called. |
+ slide_in_animator_->Cancel(); |
+ slide_out_animator_->Cancel(); |
+} |
+ |
+void ViewStack::PushState(std::unique_ptr<ViewStackState> state) { |
+ can_process_events_within_subtree_ = false; |
+ |
+ // First add the new view out of bounds since it'll slide in from right to |
+ // left. |
+ state->GetView()->SetBounds(width(), 0, width(), height()); |
sky
2016/12/12 01:54:32
You should call Layout() on the view after changin
anthonyvd
2016/12/12 17:01:56
Done.
|
+ |
+ AddChildView(state->GetView()); |
+ // Animate the new view to be right on top of this one. |
+ gfx::Rect destination = bounds(); |
+ destination.set_origin(gfx::Point(0, 0)); |
+ slide_in_animator_->AnimateViewTo(state->GetView(), destination); |
+ |
+ // Add the new state to the stack so it can be popped later when navigating |
+ // back to the previous screen. |
+ state_stack_.push(std::move(state)); |
+} |
+ |
+void ViewStack::PopState() { |
+ can_process_events_within_subtree_ = false; |
+ |
+ gfx::Rect destination = bounds(); |
+ destination.set_origin(gfx::Point(width(), 0)); |
+ |
+ // This deletes itself. |
+ SlideOutObserver* observer = |
+ new SlideOutObserver(std::move(state_stack_.top())); |
+ slide_out_animator_->AddObserver(observer); |
+ |
+ state_stack_.pop(); |
+ DCHECK(!state_stack_.empty()) << "State stack should never be empty"; |
+ |
+ slide_out_animator_->AnimateViewTo(observer->GetAnimatedView(), destination); |
+} |
+ |
+bool ViewStack::CanProcessEventsWithinSubtree() const { |
+ return can_process_events_within_subtree_; |
+} |
+ |
+void ViewStack::OnBoundsAnimatorDone( |
+ views::BoundsAnimator* animator) { |
+ can_process_events_within_subtree_ = true; |
+} |