File: view_stack.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (159 lines) | stat: -rw-r--r-- 4,941 bytes parent folder | download | duplicates (5)
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
152
153
154
155
156
157
158
159
// Copyright 2016 The Chromium Authors
// 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/base/metadata/metadata_impl_macros.h"
#include "ui/compositor/layer.h"
#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() = default;

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->DeprecatedLayoutImmediately();

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

  AddChildView(std::move(view));

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

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

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

  if (animate) {
    gfx::Rect destination = bounds();
    destination.set_origin(gfx::Point(width(), 0));
    slide_out_animator_->AnimateViewTo(stack_.back(), destination);
  } else {
    RemoveChildViewT(stack_.back());
    stack_.pop_back();
  }
}

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

  size_t pre_size = stack_.size();

  // Remove N - 1 child views from the hierarchy now, the last one will be
  // removed when its animation completes
  for (unsigned i = pre_size - n; i < (pre_size - 1); i++) {
    RemoveChildViewT(stack_.at(i));
  }

  // 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(animate);
}

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

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

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::OnBoundsChanged(const gfx::Rect& previous_bounds) {
  UpdateAnimatorBounds(slide_in_animator_.get(), GetLocalBounds());
  UpdateAnimatorBounds(slide_out_animator_.get(), {{width(), 0}, size()});
}

void ViewStack::HideCoveredViews() {
  // Iterate through all but the last (topmost) view.
  for (size_t i = 0; i + 1 < GetSize(); 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 (views::View* view : stack_) {
      if (animator->IsAnimating(view)) {
        animator->SetTargetBounds(view, target);
      }
    }
  }
}

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

BEGIN_METADATA(ViewStack)
ADD_READONLY_PROPERTY_METADATA(size_t, Size)
END_METADATA