File: contents_container_view.cc

package info (click to toggle)
chromium 140.0.7339.185-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,193,740 kB
  • sloc: cpp: 35,093,945; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,515; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (271 lines) | stat: -rw-r--r-- 10,642 bytes parent folder | download | duplicates (3)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2025 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/frame/contents_container_view.h"

#include <memory>

#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/view_ids.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/contents_separator.h"
#include "chrome/browser/ui/views/frame/contents_web_view.h"
#include "chrome/browser/ui/views/frame/multi_contents_view_mini_toolbar.h"
#include "chrome/browser/ui/views/frame/scrim_view.h"
#include "chrome/browser/ui/views/new_tab_footer/footer_web_view.h"
#include "chrome/common/chrome_features.h"
#include "components/search/ntp_features.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/color/color_provider.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/border.h"
#include "ui/views/layout/delegating_layout_manager.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/proposed_layout.h"
#include "ui/views/view_class_properties.h"

#if BUILDFLAG(ENABLE_GLIC)
#include "chrome/browser/glic/browser_ui/glic_border_view.h"
#include "chrome/browser/glic/glic_enabling.h"
#endif

namespace {
constexpr float kContentCornerRadius = 6;
constexpr gfx::RoundedCornersF kContentRoundedCorners{kContentCornerRadius};

constexpr int kContentOutlineCornerRadius = 8;
constexpr int kContentOutlineThickness = 1;
constexpr int kSplitViewContentPadding = 4;

constexpr int kNewTabFooterSeparatorHeight = 1;
constexpr int kNewTabFooterHeight = 56;
}  // namespace

ContentsContainerView::ContentsContainerView(BrowserView* browser_view) {
  SetLayoutManager(std::make_unique<views::DelegatingLayoutManager>(this));

  // The default z-order is the order in which children were added to the
  // parent view. So first added the content view and new tab page footer.
  // This should be followed by scrims, borders and lastly mini-toolbar.

  contents_view_ = AddChildView(
      std::make_unique<ContentsWebView>(browser_view->GetProfile()));
  contents_view_->SetID(VIEW_ID_TAB_CONTAINER);

  if (base::FeatureList::IsEnabled(ntp_features::kNtpFooter)) {
    new_tab_footer_view_separator_ =
        AddChildView(std::make_unique<ContentsSeparator>());
    new_tab_footer_view_separator_->SetProperty(
        views::kElementIdentifierKey, kFooterWebViewSeparatorElementId);

    new_tab_footer_view_ =
        AddChildView(std::make_unique<new_tab_footer::NewTabFooterWebView>(
            browser_view->browser()));
    new_tab_footer_view_->SetVisible(false);
  }

  contents_scrim_view_ = AddChildView(std::make_unique<ScrimView>());
  contents_scrim_view_->layer()->SetName("ContentsScrimView");

  if (base::FeatureList::IsEnabled(features::kSideBySide)) {
    inactive_split_scrim_view_ =
        AddChildView(std::make_unique<ScrimView>(kColorSplitViewScrim));
    inactive_split_scrim_view_->SetRoundedCorners(kContentRoundedCorners);
  }

  if (features::kGlicActorUiOverlay.Get()) {
    auto actor_overlay_view = std::make_unique<views::View>();
    actor_overlay_view->SetID(VIEW_ID_ACTOR_OVERLAY);
    actor_overlay_view->SetVisible(false);
    actor_overlay_view->SetLayoutManager(std::make_unique<views::FillLayout>());
    actor_overlay_view_ = AddChildView(std::move(actor_overlay_view));
  }

#if BUILDFLAG(ENABLE_GLIC)
  if (glic::GlicEnabling::IsProfileEligible(browser_view->GetProfile())) {
    glic_border_ =
        AddChildView(views::Builder<glic::GlicBorderView>(
                         glic::GlicBorderView::Factory::Create(
                             browser_view->browser(), contents_view_))
                         .SetVisible(false)
                         .SetCanProcessEventsWithinSubtree(false)
                         .Build());
  }
#endif

  if (base::FeatureList::IsEnabled(features::kSideBySide)) {
    mini_toolbar_ = AddChildView(std::make_unique<MultiContentsViewMiniToolbar>(
        browser_view, contents_view_));
  }
}

void ContentsContainerView::UpdateBorderAndOverlay(bool is_in_split,
                                                   bool is_active,
                                                   bool show_scrim) {
  is_in_split_ = is_in_split;
  // The border, mini toolbar, and scrim should not be visible if not in a
  // split.
  if (!is_in_split) {
    SetBorder(nullptr);
    ClearBorderRoundedCorners();
    mini_toolbar_->SetVisible(false);
    inactive_split_scrim_view_->SetVisible(false);
    return;
  }

  // Draw active/inactive outlines around the contents areas and updates mini
  // toolbar visibility.
  const SkColor color =
      is_active ? GetColorProvider()->GetColor(
                      kColorMulitContentsViewActiveContentOutline)
                : GetColorProvider()->GetColor(
                      kColorMulitContentsViewInactiveContentOutline);
  SetBorder(views::CreatePaddedBorder(
      views::CreateRoundedRectBorder(kContentOutlineThickness,
                                     kContentOutlineCornerRadius, color),
      gfx::Insets(kSplitViewContentPadding)));

  UpdateBorderRoundedCorners();

  // Mini toolbar should only be visible for the inactive contents
  // container view or both depending on configuration.
  mini_toolbar_->UpdateState(is_active);
  // Scrim should only be allowed to show the scrim for inactive contents
  // container view.
  inactive_split_scrim_view_->SetVisible(!is_active && show_scrim);
}

void ContentsContainerView::UpdateBorderRoundedCorners() {
  constexpr gfx::RoundedCornersF kContentUpperRoundedCorners =
      gfx::RoundedCornersF{kContentCornerRadius, kContentCornerRadius, 0, 0};
  constexpr gfx::RoundedCornersF kContentLowerRoundedCorners =
      gfx::RoundedCornersF{0, 0, kContentCornerRadius, kContentCornerRadius};

  auto radii = new_tab_footer_view_ && new_tab_footer_view_->GetVisible()
                   ? kContentUpperRoundedCorners
                   : kContentRoundedCorners;

  contents_view_->holder()->SetCornerRadii(radii);

  if (new_tab_footer_view_) {
    new_tab_footer_view_->holder()->SetCornerRadii(kContentLowerRoundedCorners);
  }

  if (contents_scrim_view_->layer()->rounded_corner_radii() !=
      kContentRoundedCorners) {
    contents_scrim_view_->SetRoundedCorners(kContentRoundedCorners);
  }
}

void ContentsContainerView::ClearBorderRoundedCorners() {
  constexpr gfx::RoundedCornersF kNoRoundedCorners = gfx::RoundedCornersF{0};

  contents_view_->holder()->SetCornerRadii(kNoRoundedCorners);

  if (new_tab_footer_view_) {
    new_tab_footer_view_->holder()->SetCornerRadii(kNoRoundedCorners);
  }

  contents_scrim_view_->SetRoundedCorners(kNoRoundedCorners);
}

void ContentsContainerView::ChildVisibilityChanged(View* child) {
  if (child == new_tab_footer_view_ && is_in_split_) {
    UpdateBorderRoundedCorners();
  }
}

views::ProposedLayout ContentsContainerView::CalculateProposedLayout(
    const views::SizeBounds& size_bounds) const {
  views::ProposedLayout layouts;
  if (!size_bounds.is_fully_bounded()) {
    return layouts;
  }

  int height = size_bounds.height().value();
  int width = size_bounds.width().value();

  // |contents_view_| and |new_tab_footer_view_| (if it exists) should fill the
  // contents bounds.
  gfx::Rect contents_bounds = GetContentsBounds();
  gfx::Rect contents_rect = contents_bounds;

  if (new_tab_footer_view_ && new_tab_footer_view_->GetVisible()) {
    // Shrink contents rect if the ntp footer is visible.
    contents_rect.set_height(contents_rect.height() - kNewTabFooterHeight -
                             kNewTabFooterSeparatorHeight);

    gfx::Rect footer_separator_rect =
        gfx::Rect(contents_bounds.x(), contents_rect.bottom(),
                  contents_bounds.width(), kNewTabFooterSeparatorHeight);
    gfx::Rect footer_rect =
        gfx::Rect(contents_bounds.x(), footer_separator_rect.bottom(),
                  contents_bounds.width(), kNewTabFooterHeight);

    layouts.child_layouts.emplace_back(
        new_tab_footer_view_separator_.get(),
        new_tab_footer_view_separator_->GetVisible(), footer_separator_rect);

    layouts.child_layouts.emplace_back(new_tab_footer_view_.get(),
                                       new_tab_footer_view_->GetVisible(),
                                       footer_rect);
  }

  layouts.child_layouts.emplace_back(
      contents_view_.get(), contents_view_->GetVisible(), contents_rect);

#if BUILDFLAG(ENABLE_GLIC)
  if (glic_border_) {
    layouts.child_layouts.emplace_back(
        glic_border_.get(), glic_border_->GetVisible(), contents_bounds);
  }
#endif

  // The scrim view should cover the entire contents bounds.
  CHECK(contents_scrim_view_);
  layouts.child_layouts.emplace_back(contents_scrim_view_.get(),
                                     contents_scrim_view_->GetVisible(),
                                     contents_bounds);

  // The scrim view should cover the entire contents bounds.
  if (inactive_split_scrim_view_) {
    layouts.child_layouts.emplace_back(inactive_split_scrim_view_.get(),
                                       inactive_split_scrim_view_->GetVisible(),
                                       contents_bounds);
  }

  // Actor Overlay view bounds are the same as the contents view.
  if (actor_overlay_view_) {
    layouts.child_layouts.emplace_back(actor_overlay_view_.get(),
                                       actor_overlay_view_->GetVisible(),
                                       contents_rect, size_bounds);
  }

  if (mini_toolbar_) {
    // |mini_toolbar_| should be offset in the bottom right corner, overlapping
    // the outline.
    gfx::Size mini_toolbar_size = mini_toolbar_->GetPreferredSize(
        views::SizeBounds(width - kContentOutlineCornerRadius, height));
    const int offset_x =
        width - mini_toolbar_size.width() + (kContentOutlineThickness / 2.0f);
    const int offset_y =
        height - mini_toolbar_size.height() + (kContentOutlineThickness / 2.0f);
    const gfx::Rect mini_toolbar_rect =
        gfx::Rect(offset_x, offset_y, mini_toolbar_size.width(),
                  mini_toolbar_size.height());
    layouts.child_layouts.emplace_back(
        mini_toolbar_.get(), mini_toolbar_->GetVisible(), mini_toolbar_rect);
  }

  layouts.host_size = gfx::Size(width, height);
  return layouts;
}

BEGIN_METADATA(ContentsContainerView)
END_METADATA