File: refresh_banner_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 (285 lines) | stat: -rw-r--r-- 10,436 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
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
// 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/mahi/refresh_banner_view.h"

#include <string>

#include "ash/strings/grit/ash_strings.h"
#include "ash/style/icon_button.h"
#include "ash/style/typography.h"
#include "ash/system/mahi/mahi_constants.h"
#include "base/check_is_test.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "chromeos/components/mahi/public/cpp/mahi_manager.h"
#include "components/vector_icons/vector_icons.h"
#include "third_party/skia/include/core/SkPath.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/gfx/text_constants.h"
#include "ui/views/animation/animation_builder.h"
#include "ui/views/background.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/flex_layout_types.h"
#include "ui/views/layout/flex_layout_view.h"
#include "ui/views/layout/layout_types.h"
#include "ui/views/view_class_properties.h"

namespace ash {

namespace {

constexpr int kRefreshBannerCornerRadius = 20;
constexpr base::TimeDelta kRefreshBannerSlideAnimationDurationMs =
    base::Milliseconds(200);
constexpr base::TimeDelta kRefreshBannerOpacityAnimationDurationMs =
    base::Milliseconds(100);
constexpr gfx::Insets kRefreshBannerInteriorMargin =
    gfx::Insets::TLBR(4, 20, mahi_constants::kRefreshBannerStackDepth + 4, 20);
constexpr gfx::Insets kTitleLabelMargin = gfx::Insets::TLBR(0, 0, 0, 8);

SkPath GetClipPath(gfx::Size size) {
  int width = size.width();
  int height = size.height();

  auto top_left = SkPoint::Make(0, 0);
  auto top_right = SkPoint::Make(width, 0);
  auto bottom_left = SkPoint::Make(0, height);
  auto bottom_right = SkPoint::Make(width, height);
  int radius = kRefreshBannerCornerRadius;
  int bottom_radius = mahi_constants::kPanelCornerRadius;

  const auto bottom_vertical_offset =
      SkPoint::Make(0.f, mahi_constants::kRefreshBannerStackDepth - 1);
  const auto bottom_horizontal_offset = SkPoint::Make(bottom_radius, 0.f);

  return SkPath()
      // Start just before the curve of the top-left corner.
      .moveTo(radius, 0.f)
      // Draw the top-left rounded corner.
      .lineTo(top_left)
      // Draw the bottom-left rounded corner and the vertical line
      // connecting it to the top-left corner.
      .lineTo(bottom_left)
      .arcTo(bottom_left - bottom_vertical_offset,
             bottom_left - bottom_vertical_offset + bottom_horizontal_offset,
             radius)
      // Draw the bottom-right rounded corner and the horizontal line
      // connecting it to the bottom-left corner.
      .arcTo(bottom_right - bottom_vertical_offset, bottom_right, bottom_radius)
      .lineTo(bottom_right)
      .lineTo(top_right)
      .close();
}

}  // namespace

RefreshBannerView::RefreshBannerView(MahiUiController* ui_controller)
    : MahiUiController::Delegate(ui_controller), ui_controller_(ui_controller) {
  CHECK(ui_controller_);

  SetUseDefaultFillLayout(true);
  SetID(mahi_constants::ViewId::kRefreshView);
  SetBackground(views::CreateRoundedRectBackground(
      cros_tokens::kCrosSysSystemPrimaryContainer, /*radius=*/0));
  SetVisible(false);

  // We need to paint this view to a layer for animations.
  SetPaintToLayer();
  layer()->SetRoundedCornerRadius(gfx::RoundedCornersF(
      kRefreshBannerCornerRadius, kRefreshBannerCornerRadius, 0, 0));

  auto* const manager = chromeos::MahiManager::Get();

  AddChildView(
      views::Builder<views::FlexLayoutView>()
          .CopyAddressTo(&main_container_)
          .SetOrientation(views::LayoutOrientation::kHorizontal)
          .SetInteriorMargin(kRefreshBannerInteriorMargin)
          .SetPaintToLayer()
          .AddChild(
              views::Builder<views::Label>()
                  .CopyAddressTo(&title_label_)
                  .SetID(mahi_constants::ViewId::kBannerTitleLabel)
                  .SetText(l10n_util::GetStringFUTF16(
                      IDS_ASH_MAHI_REFRESH_BANNER_LABEL_TEXT,
                      manager ? manager->GetContentTitle()
                              : base::EmptyString16()))
                  .SetAutoColorReadabilityEnabled(false)
                  .SetEnabledColor(
                      cros_tokens::kCrosSysSystemOnPrimaryContainer)
                  .SetFontList(
                      TypographyProvider::Get()->ResolveTypographyToken(
                          TypographyToken::kCrosAnnotation2))
                  .SetHorizontalAlignment(gfx::ALIGN_LEFT)
                  .SetProperty(views::kFlexBehaviorKey,
                               views::FlexSpecification(
                                   views::LayoutOrientation::kHorizontal,
                                   views::MinimumFlexSizeRule::kScaleToZero,
                                   views::MaximumFlexSizeRule::kUnbounded))
                  .SetProperty(views::kMarginsKey, kTitleLabelMargin))
          .AddChild(views::Builder<views::Button>(CreateRefreshButton()))
          .Build());

  main_container_->layer()->SetFillsBoundsOpaquely(false);
}

RefreshBannerView::~RefreshBannerView() = default;

void RefreshBannerView::Show() {
  auto* manager = chromeos::MahiManager::Get();
  if (!manager) {
    CHECK_IS_TEST();
    return;
  }

  title_label_->SetText(l10n_util::GetStringFUTF16(
      IDS_ASH_MAHI_REFRESH_BANNER_LABEL_TEXT, manager->GetContentTitle()));

  // Abort all running animations before showing the banner, to prevent fade
  // out animations from hiding the banner again right after it is shown.
  CHECK(layer());
  layer()->GetAnimator()->AbortAllAnimations();

  if (GetVisible()) {
    return;
  }

  SetVisible(true);

  auto bounds = GetLocalBounds();
  auto clip_rect = gfx::Rect(0, bounds.height() - 1, bounds.width(), 1);

  layer()->SetClipRect(clip_rect);
  layer()->SetOpacity(0.0);

  // Anchor the panel's contents to the center of the visible area.
  auto y_offset =
      clip_rect.CenterPoint().y() - main_container_->bounds().CenterPoint().y();
  gfx::Transform transform;
  transform.Translate(gfx::Vector2d(0, y_offset));
  main_container_->layer()->SetTransform(transform);

  views::AnimationBuilder()
      .Once()
      .SetDuration(kRefreshBannerSlideAnimationDurationMs)
      .SetTransform(main_container_, gfx::Transform(),
                    gfx::Tween::ACCEL_20_DECEL_100)
      .SetClipRect(this, gfx::Rect(bounds), gfx::Tween::ACCEL_20_DECEL_100)
      .At(base::Milliseconds(0))
      .SetDuration(kRefreshBannerOpacityAnimationDurationMs)
      .SetOpacity(this, 1.0);
}

void RefreshBannerView::Hide() {
  if (GetVisible()) {
    views::AnimationBuilder()
        .OnEnded(base::BindOnce(
            [](const base::WeakPtr<views::View>& view) {
              if (view) {
                view->SetVisible(false);
              }
            },
            weak_ptr_factory_.GetWeakPtr()))
        .Once()
        .SetDuration(kRefreshBannerOpacityAnimationDurationMs)
        .SetOpacity(this, 0.0);
  }
}

std::unique_ptr<IconButton> RefreshBannerView::CreateRefreshButton() {
  auto icon_button =
      IconButton::Builder()
          .SetViewId(mahi_constants::ViewId::kRefreshButton)
          .SetCallback(base::BindRepeating(
              [](MahiUiController* ui_controller) {
                ui_controller->RefreshContents();
                base::UmaHistogramEnumeration(
                    mahi_constants::kMahiButtonClickHistogramName,
                    mahi_constants::PanelButton::kRefreshButton);
              },
              // Using `base::Unretained()` is safe here since
              // `ui_controller` outlives this `RefreshBannerView`.
              base::Unretained(ui_controller_)))
          .SetVectorIcon(&vector_icons::kReloadChromeRefreshIcon)
          .SetType(IconButton::Type::kSmallProminentFloating)
          .SetAccessibleName(l10n_util::GetStringUTF16(
              IDS_ASH_MAHI_REFRESH_BANNER_BUTTON_ACCESSIBLE_NAME))
          .Build();
  icon_button->SetIconColor(cros_tokens::kCrosSysSystemOnPrimaryContainer);
  views::FocusRing::Get(icon_button.get())
      ->SetColorId(cros_tokens::kCrosSysSystemOnPrimaryContainer);

  return icon_button;
}

void RefreshBannerView::OnBoundsChanged(const gfx::Rect& old_bounds) {
  SetClipPath(GetClipPath(GetContentsBounds().size()));
  layer()->SetClipRect(GetLocalBounds());
}

void RefreshBannerView::ViewHierarchyChanged(
    const views::ViewHierarchyChangedDetails& details) {
  // Make sure the refresh banner is always shown on top.
  if (layer() && layer()->parent()) {
    layer()->parent()->StackAtTop(layer());
  }
}

void RefreshBannerView::VisibilityChanged(View* starting_from,
                                          bool is_visible) {
  if (!is_visible || GetContentsBounds().size().IsZero()) {
    return;
  }
  SetClipPath(GetClipPath(GetContentsBounds().size()));
}

views::View* RefreshBannerView::GetView() {
  return this;
}

bool RefreshBannerView::GetViewVisibility(VisibilityState state) const {
  // Do not change visibility because visibility depends on the refresh
  // availability instead of `state`.
  return GetVisible();
}

void RefreshBannerView::OnUpdated(const MahiUiUpdate& update) {
  switch (update.type()) {
    case MahiUiUpdateType::kRefreshAvailabilityUpdated:
      if (update.GetRefreshAvailability()) {
        Show();
      } else {
        Hide();
      }
      return;
    case MahiUiUpdateType::kContentsRefreshInitiated:
      Hide();
      return;
    case MahiUiUpdateType::kErrorReceived:
    case MahiUiUpdateType::kAnswerLoaded:
    case MahiUiUpdateType::kOutlinesLoaded:
    case MahiUiUpdateType::kPanelBoundsChanged:
    case MahiUiUpdateType::kQuestionAndAnswerViewNavigated:
    case MahiUiUpdateType::kQuestionPosted:
    case MahiUiUpdateType::kQuestionReAsked:
    case MahiUiUpdateType::kSummaryLoaded:
    case MahiUiUpdateType::kSummaryAndOutlinesSectionNavigated:
    case MahiUiUpdateType::kSummaryAndOutlinesReloaded:
    case MahiUiUpdateType::kElucidationRequested:
    case MahiUiUpdateType::kElucidationLoaded:
      return;
  }
}

BEGIN_METADATA(RefreshBannerView)
END_METADATA

}  // namespace ash