File: magic_boost_disclaimer_view.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (325 lines) | stat: -rw-r--r-- 14,181 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/magic_boost/magic_boost_disclaimer_view.h"

#include <memory>
#include <string>
#include <vector>

#include "ash/public/cpp/resources/grit/ash_public_unscaled_resources.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/typography.h"
#include "ash/system/magic_boost/magic_boost_constants.h"
#include "base/functional/bind.h"
#include "build/branding_buildflags.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/color/color_id.h"
#include "ui/compositor/layer.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/views/background.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/highlight_border.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/widget/unique_widget_ptr.h"
#include "ui/views/widget/widget.h"

namespace ash {

namespace {

constexpr char kWidgetName[] = "MagicBoostDisclaimerViewWidget";

// Paddings, sizes and insets.
constexpr int kImageWidth = 512;
constexpr int kImageHeight = 236;
constexpr int kContainerPadding = 32;
constexpr int kTextContainerBetweenChildSpacing = 16;
constexpr int kContainerBottomPadding = 28;
constexpr int kWidgetWidth = kImageWidth;
constexpr int kWidgetMiniumHeight = 650;
constexpr int kBetweenButtonsSpacing = 8;
constexpr int kButtonHeight = 32;
constexpr int kRadius = 20;

constexpr gfx::Insets kButtonContainerInsets =
    gfx::Insets::TLBR(0,
                      kContainerPadding,
                      kContainerBottomPadding,
                      kContainerPadding);
constexpr gfx::Insets kTextContainerInsets = gfx::Insets(kContainerPadding);
constexpr gfx::Size kImagePreferredSize(/*width=*/kImageWidth,
                                        /*height=*/kImageHeight);

views::StyledLabel::RangeStyleInfo GetBodyTextStyle() {
  views::StyledLabel::RangeStyleInfo style;
  style.custom_font = TypographyProvider::Get()->ResolveTypographyToken(
      TypographyToken::kCrosBody1);
  style.override_color_id = static_cast<ui::ColorId>(ui::kColorSysOnSurface);
  return style;
}

views::StyledLabel::RangeStyleInfo GetLinkTextStyle(
    base::RepeatingClosure press_link_callback) {
  views::StyledLabel::RangeStyleInfo link_style =
      views::StyledLabel::RangeStyleInfo::CreateForLink(
          std::move(press_link_callback));
  link_style.override_color_id =
      static_cast<ui::ColorId>(cros_tokens::kCrosSysOnSurfaceVariant);
  return link_style;
}

views::Builder<views::StyledLabel> GetTextBodyBuilder(
    const std::u16string& text) {
  return views::Builder<views::StyledLabel>()
      .SetText(text)
      .AddStyleRange(gfx::Range(0, text.length()), GetBodyTextStyle())
      .SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT)
      .SetAutoColorReadabilityEnabled(false);
}

views::Builder<views::StyledLabel> GetParagraphOneBuilder() {
  return GetTextBodyBuilder(
             l10n_util::GetStringUTF16(
                 ash::features::IsLobsterEnabled()
                     ? IDS_LOBSTER_MAGIC_BOOST_DISCLAIMER_PARAGRAPH_ONE
                     : IDS_ASH_MAGIC_BOOST_DISCLAMIER_PARAGRAPH_ONE))
      .SetID(magic_boost::ViewId::DisclaimerViewParagraphOne);
}

views::Builder<views::StyledLabel> GetParagraphTwoBuilder(
    base::RepeatingClosure press_link_callback) {
  std::vector<size_t> offsets;
  const std::u16string link_text =
      l10n_util::GetStringUTF16(IDS_ASH_MAGIC_BOOST_DISCLAIMER_TERMS_LINK_TEXT);
  const std::u16string text = l10n_util::GetStringFUTF16(
      ash::features::IsLobsterEnabled()
          ? IDS_LOBSTER_MAGIC_BOOST_DISCLAIMER_PARAGRAPH_TWO
          : IDS_ASH_MAGIC_BOOST_DISCLAIMER_PARAGRAPH_TWO,
      {link_text}, &offsets);

  return views::Builder<views::StyledLabel>()
      .SetText(text)
      .SetID(magic_boost::ViewId::DisclaimerViewParagraphTwo)
      .AddStyleRange(gfx::Range(0, offsets.at(0)), GetBodyTextStyle())
      .AddStyleRange(
          gfx::Range(offsets.at(0), offsets.at(0) + link_text.length()),
          GetLinkTextStyle(std::move(press_link_callback)))
      .AddStyleRange(
          gfx::Range(offsets.at(0) + link_text.length(), text.length()),
          GetBodyTextStyle())
      .SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT)
      .SetAutoColorReadabilityEnabled(false);
}

views::Builder<views::StyledLabel> GetParagraphThreeBuilder() {
  return GetTextBodyBuilder(
             l10n_util::GetStringUTF16(
                 ash::features::IsLobsterEnabled()
                     ? IDS_LOBSTER_MAGIC_BOOST_DISCLAIMER_PARAGRAPH_THREE
                     : IDS_ASH_MAGIC_BOOST_DISCLAIMER_PARAGRAPH_THREE))
      .SetID(magic_boost::ViewId::DisclaimerViewParagraphThree);
}

views::Builder<views::StyledLabel> GetParagraphFourBuilder(
    base::RepeatingClosure press_link_callback) {
  std::vector<size_t> offsets;
  const std::u16string link_text = l10n_util::GetStringUTF16(
      IDS_ASH_MAGIC_BOOST_DISCLAIMER_LEARN_MORE_LINK_TEXT);
  const std::u16string text = l10n_util::GetStringFUTF16(
      ash::features::IsLobsterEnabled()
          ? IDS_LOBSTER_MAGIC_BOOST_DISCLAIMER_PARAGRAPH_FOUR
          : IDS_ASH_MAGIC_BOOST_DISCLAIMER_PARAGRAPH_FOUR,
      {link_text}, &offsets);

  return views::Builder<views::StyledLabel>()
      .SetText(text)
      .SetID(magic_boost::ViewId::DisclaimerViewParagraphFour)
      .AddStyleRange(gfx::Range(0, offsets.at(0)), GetBodyTextStyle())
      .AddStyleRange(
          gfx::Range(offsets.at(0), offsets.at(0) + link_text.length()),
          GetLinkTextStyle(std::move(press_link_callback)))
      .SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT)
      .SetAutoColorReadabilityEnabled(false);
}

}  // namespace

MagicBoostDisclaimerView::MagicBoostDisclaimerView(
    base::RepeatingClosure press_accept_button_callback,
    base::RepeatingClosure press_decline_button_callback,
    base::RepeatingClosure press_terms_of_service_callback,
    base::RepeatingClosure press_learn_more_link_callback) {
  views::View* disclaimer_view;
  std::u16string decline_button_text =
      l10n_util::GetStringUTF16(IDS_ASH_MAGIC_BOOST_DISCLAIMER_DECLINE_BUTTON);
  std::u16string accept_button_text =
      l10n_util::GetStringUTF16(IDS_ASH_MAGIC_BOOST_DISCLAIMER_ACCEPT_BUTTON);
  views::Builder<views::View>(this)
      .CopyAddressTo(&disclaimer_view)
      .SetLayoutManager(std::make_unique<views::BoxLayout>(
          views::BoxLayout::Orientation::kVertical))
      .SetBackground(views::CreateRoundedRectBackground(
          cros_tokens::kCrosSysDialogContainer, kRadius))
      .SetPaintToLayer()
      .AddChildren(
          views::Builder<views::ImageView>()
              .SetImage(ui::ImageModel::FromResourceId(
                  IDR_MAGIC_BOOST_DISCLAIMER_ILLUSTRATION))
              .SetPreferredSize(kImagePreferredSize),
          views::Builder<views::ScrollView>()
              .SetBackgroundColor(std::nullopt)
              .SetDrawOverflowIndicator(false)
              .SetVerticalScrollBarMode(
                  views::ScrollView::ScrollBarMode::kHiddenButEnabled)
              // The content will take all the available space of the widget.
              .SetProperty(views::kBoxLayoutFlexKey,
                           views::BoxLayoutFlexSpecification())
              .ClipHeightTo(0, std::numeric_limits<int>::max())
              .SetContents(
                  views::Builder<views::BoxLayoutView>()
                      .SetOrientation(views::LayoutOrientation::kVertical)
                      .SetBetweenChildSpacing(kTextContainerBetweenChildSpacing)
                      .SetBorder(views::CreateEmptyBorder(kTextContainerInsets))
                      .AddChildren(
                          views::Builder<views::Label>()
                              .SetFontList(
                                  TypographyProvider::Get()
                                      ->ResolveTypographyToken(
                                          TypographyToken::kCrosDisplay7))
                              .SetEnabledColor(cros_tokens::kCrosSysOnSurface)
                              .SetHorizontalAlignment(
                                  gfx::HorizontalAlignment::ALIGN_LEFT)
                              .SetID(magic_boost::ViewId::DisclaimerViewTitle)
                              .CopyAddressTo(&title_)
                              .SetText(l10n_util::GetStringUTF16(
                                  IDS_ASH_MAGIC_BOOST_DISCLAIMER_TITLE)),
                          GetParagraphOneBuilder().CopyAddressTo(
                              &paragraph_one_),
                          GetParagraphTwoBuilder(
                              std::move(press_terms_of_service_callback))
                              .CopyAddressTo(&paragraph_two_),
                          GetParagraphThreeBuilder().CopyAddressTo(
                              &paragraph_three_),
                          GetParagraphFourBuilder(
                              std::move(press_learn_more_link_callback))
                              .CopyAddressTo(&paragraph_four_))),
          views::Builder<views::BoxLayoutView>()
              .SetMainAxisAlignment(views::LayoutAlignment::kEnd)
              .SetBetweenChildSpacing(kBetweenButtonsSpacing)
              .SetBorder(views::CreateEmptyBorder(kButtonContainerInsets))
              .AddChildren(
                  views::Builder<views::MdTextButton>()
                      .SetID(magic_boost::ViewId::DisclaimerViewDeclineButton)
                      .SetText(decline_button_text)
                      .SetAccessibleName(decline_button_text)
                      // Sets the button's height to a customized
                      // `kButtonHeight` instead of using the default
                      // height.
                      .SetMaxSize(gfx::Size(kImageWidth, kButtonHeight))
                      .SetStyle(ui::ButtonStyle::kProminent)
                      .SetCallback(std::move(press_decline_button_callback)),
                  views::Builder<views::MdTextButton>()
                      .CopyAddressTo(&accept_button_)
                      .SetID(magic_boost::ViewId::DisclaimerViewAcceptButton)
                      .SetText(accept_button_text)
                      .SetAccessibleName(accept_button_text)
                      .SetMaxSize(gfx::Size(kImageWidth, kButtonHeight))
                      .SetStyle(ui::ButtonStyle::kProminent)
                      .SetCallback(std::move(press_accept_button_callback))))
      .BuildChildren();

  disclaimer_view->layer()->SetRoundedCornerRadius(
      gfx::RoundedCornersF{kRadius});
}

MagicBoostDisclaimerView::~MagicBoostDisclaimerView() = default;

// static
views::UniqueWidgetPtr MagicBoostDisclaimerView::CreateWidget(
    int64_t display_id,
    base::RepeatingClosure press_accept_button_callback,
    base::RepeatingClosure press_decline_button_callback,
    base::RepeatingClosure press_disclaimer_link_callback,
    base::RepeatingClosure press_learn_more_link_callback) {
  views::Widget::InitParams params(
      views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
      views::Widget::InitParams::TYPE_POPUP);
  params.opacity = views::Widget::InitParams::WindowOpacity::kTranslucent;
  params.activatable = views::Widget::InitParams::Activatable::kYes;
  params.shadow_elevation = 2;
  params.rounded_corners = gfx::RoundedCornersF(kRadius);
  params.shadow_type = views::Widget::InitParams::ShadowType::kDrop;
  params.z_order = ui::ZOrderLevel::kFloatingUIElement;
  params.name = GetWidgetName();

  views::UniqueWidgetPtr widget =
      std::make_unique<views::Widget>(std::move(params));
  auto disclaimer_view = std::make_unique<MagicBoostDisclaimerView>(
      std::move(press_accept_button_callback),
      std::move(press_decline_button_callback),
      std::move(press_disclaimer_link_callback),
      std::move(press_learn_more_link_callback));
  const int widget_height = disclaimer_view->GetPreferredSize().height();
  widget->SetContentsView(std::move(disclaimer_view));

  // Shows the widget in the middle of the screen.
  aura::Window* window = Shell::GetRootWindowForDisplayId(display_id);

  if (!window) {
    window = Shell::GetPrimaryRootWindow();
  }

  auto center = window->GetBoundsInScreen().CenterPoint();
  widget->SetBounds(gfx::Rect(center.x() - kWidgetWidth / 2,
                              center.y() - widget_height / 2, kWidgetWidth,
                              widget_height));

  return widget;
}

// static
const char* MagicBoostDisclaimerView::GetWidgetName() {
  return kWidgetName;
}

void MagicBoostDisclaimerView::RequestFocus() {
  views::View::RequestFocus();

  accept_button_->RequestFocus();
}

gfx::Size MagicBoostDisclaimerView::CalculatePreferredSize(
    const views::SizeBounds& available_size) const {
  const int body_width = kWidgetWidth - 2 * kContainerPadding;
  int height = kImageHeight + paragraph_one_->GetHeightForWidth(body_width) +
               paragraph_two_->GetHeightForWidth(body_width) +
               paragraph_three_->GetHeightForWidth(body_width) +
               paragraph_four_->GetHeightForWidth(body_width) +
               title_->GetHeightForWidth(body_width) +
               accept_button_->GetHeightForWidth(body_width) +
               kContainerPadding * 2 + kContainerBottomPadding +
               kTextContainerBetweenChildSpacing * 4;
  height = std::max(height, kWidgetMiniumHeight);

  return gfx::Size(kWidgetWidth, height);
}

BEGIN_METADATA(MagicBoostDisclaimerView)
END_METADATA

}  // namespace ash