File: view_stack.cc

package info (click to toggle)
chromium-browser 70.0.3538.110-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,619,476 kB
  • sloc: cpp: 13,024,755; ansic: 1,349,823; python: 916,672; xml: 314,489; java: 280,047; asm: 276,936; perl: 75,771; objc: 66,634; sh: 45,860; cs: 28,354; php: 11,064; makefile: 10,911; yacc: 9,109; tcl: 8,403; ruby: 4,065; lex: 1,779; pascal: 1,411; lisp: 1,055; awk: 41; jsp: 39; sed: 17; sql: 3
file content (151 lines) | stat: -rw-r--r-- 4,684 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// 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 <memory>
#include <utility>

#include "ui/views/layout/fill_layout.h"

ViewStack::ViewStack()
    : slide_in_animator_(std::make_unique<views::BoundsAnimator>(this)),
      slide_out_animator_(std::make_unique<views::BoundsAnimator>(this)) {
  SetLayoutManager(std::make_unique<views::FillLayout>());

  slide_out_animator_->AddObserver(this);
  slide_in_animator_->AddObserver(this);

  // Paint to a layer and Mask to Bounds, otherwise descendant views that paint
  // to a layer themselves will still paint while they're being animated out and
  // are out of bounds of their parent.
  SetPaintToLayer();
  layer()->SetMasksToBounds(true);

  slide_in_animator_->set_tween_type(gfx::Tween::FAST_OUT_SLOW_IN);
  slide_out_animator_->set_tween_type(gfx::Tween::FAST_OUT_SLOW_IN);
}

ViewStack::~ViewStack() {}

void ViewStack::Push(std::unique_ptr<views::View> view, bool animate) {
  gfx::Rect destination = bounds();
  destination.set_origin(gfx::Point(0, 0));

  if (animate) {
    // First add the new view out of bounds since it'll slide in from right to
    // left.
    view->SetBounds(width(), 0, width(), height());
  } else {
    view->SetBoundsRect(destination);
  }
  view->Layout();
  AddChildView(view.get());

  view->set_owned_by_client();

  // Add the new view to the stack so it can be popped later when navigating
  // back to the previous screen.
  stack_.push_back(std::move(view));

  if (animate) {
    // Animate the new view to be right on top of this one.
    slide_in_animator_->AnimateViewTo(stack_.back().get(), destination);
  } else {
    // This is handled by the post-animation callback in the animated case, so
    // trigger it synchronously here.
    HideCoveredViews();
    RequestFocus();
  }
}

void ViewStack::Pop() {
  DCHECK_LT(1u, size());  // There must be at least one view left after popping.

  gfx::Rect destination = bounds();
  destination.set_origin(gfx::Point(width(), 0));

  // Set the second-to-last view as visible, since it is about to be revealed
  // when the last view animates out.
  stack_[size() - 2]->SetVisible(true);

  slide_out_animator_->AnimateViewTo(
      stack_.back().get(), destination);
}

void ViewStack::PopMany(int n) {
  DCHECK_LT(static_cast<size_t>(n), size());  // The stack can never be empty.

  size_t pre_size = stack_.size();
  // Erase N - 1 elements now, the last one will be erased when its animation
  // completes
  stack_.erase(stack_.end() - n, stack_.end() - 1);
  DCHECK_EQ(pre_size - n + 1, stack_.size());

  Pop();
}

size_t ViewStack::size() const {
  return stack_.size();
}

bool ViewStack::CanProcessEventsWithinSubtree() const {
  return !slide_in_animator_->IsAnimating() &&
         !slide_out_animator_->IsAnimating();
}

void ViewStack::Layout() {
  views::View::Layout();

  // If this view's bounds changed since the beginning of an animation, the
  // animator's targets have to be changed as well.
  gfx::Rect in_new_destination = bounds();
  in_new_destination.set_origin(gfx::Point(0, 0));
  UpdateAnimatorBounds(slide_in_animator_.get(), in_new_destination);


  gfx::Rect out_new_destination = bounds();
  out_new_destination.set_origin(gfx::Point(width(), 0));
  UpdateAnimatorBounds(slide_out_animator_.get(), out_new_destination);
}

void ViewStack::RequestFocus() {
  // The view can only be focused if it has a widget already. It's possible that
  // this isn't the case if some views are pushed before the stack is added to a
  // hierarchy that has a widget.
  if (top()->GetWidget())
    top()->RequestFocus();
}

void ViewStack::HideCoveredViews() {
  // Iterate through all but the last (topmost) view.
  for (size_t i = 0; i + 1 < size(); i++) {
    stack_[i]->SetVisible(false);
  }
}

void ViewStack::UpdateAnimatorBounds(
    views::BoundsAnimator* animator, const gfx::Rect& target) {
  // If an animator is currently animating, figure out which views and update
  // their target bounds.
  if (animator->IsAnimating()) {
    for (auto& view : stack_) {
      if (animator->IsAnimating(view.get())) {
        animator->SetTargetBounds(view.get(), target);
      }
    }
  }
}

void ViewStack::OnBoundsAnimatorDone(views::BoundsAnimator* animator) {
  if (animator == slide_out_animator_.get()) {
    stack_.pop_back();
    DCHECK(!stack_.empty()) << "State stack should never be empty";
  } else if (animator == slide_in_animator_.get()) {
    HideCoveredViews();
  } else {
    NOTREACHED();
  }
  RequestFocus();
}