File: product_specifications_button.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 (352 lines) | stat: -rw-r--r-- 12,711 bytes parent folder | download | duplicates (6)
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// 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 "chrome/browser/ui/views/commerce/product_specifications_button.h"

#include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/views/tabs/tab_strip_controller.h"
#include "chrome/grit/generated_resources.h"
#include "components/strings/grit/components_strings.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/compositor/layer.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/mouse_watcher_view_host.h"
#include "ui/views/view_class_properties.h"

namespace {
// TODO(b/335015993): These constants are shared with TabSearchContainer, and
// should be moved into one place. Similar for the LockedExpansionMode enum used
// in this file.
constexpr int kProductSpecificationsButtonCornerRadius = 10;
constexpr int kProductSpecificationsButtonLabelMargin = 10;
constexpr int kProductSpecificationsCloseButtonMargin = 8;
constexpr int kProductSpecificationsCloseButtonSize = 16;

constexpr base::TimeDelta kExpansionInDuration = base::Milliseconds(500);
constexpr base::TimeDelta kExpansionOutDuration = base::Milliseconds(250);
constexpr base::TimeDelta kOpacityInDuration = base::Milliseconds(300);
constexpr base::TimeDelta kOpacityOutDuration = base::Milliseconds(100);
constexpr base::TimeDelta kOpacityDelay = base::Milliseconds(100);
constexpr base::TimeDelta kShowDuration = base::Seconds(20);

}  // namespace

ProductSpecificationsButton::ProductSpecificationsButton(
    TabStripController* tab_strip_controller,
    TabStripModel* tab_strip_model,
    commerce::ProductSpecificationsEntryPointController* entry_point_controller,
    bool before_tab_strip,
    View* locked_expansion_view)
    : TabStripControlButton(
          tab_strip_controller,
          base::BindRepeating(&ProductSpecificationsButton::OnClicked,
                              base::Unretained(this)),
          l10n_util::GetStringUTF16(IDS_COMPARE_ENTRY_POINT_DEFAULT),
          Edge::kNone),
      locked_expansion_view_(locked_expansion_view),
      tab_strip_model_(tab_strip_model),
      entry_point_controller_(entry_point_controller) {
  mouse_watcher_ = std::make_unique<views::MouseWatcher>(
      std::make_unique<views::MouseWatcherViewHost>(locked_expansion_view,
                                                    gfx::Insets()),
      this);
  if (entry_point_controller_) {
    entry_point_controller_observations_.Observe(entry_point_controller_);
  }
  auto* const layout_manager =
      SetLayoutManager(std::make_unique<views::BoxLayout>());
  layout_manager->set_main_axis_alignment(
      views::BoxLayout::MainAxisAlignment::kEnd);

  SetProperty(views::kElementIdentifierKey,
              kProductSpecificationsButtonElementId);

  SetTooltipText(l10n_util::GetStringUTF16(IDS_COMPARE_ENTRY_POINT_DEFAULT));
  // TODO(b/325661685): Set accessibility name of the button.
  SetLabelStyle(views::style::STYLE_BODY_3_EMPHASIS);
  label()->SetElideBehavior(gfx::ElideBehavior::NO_ELIDE);

  const gfx::Insets label_margin =
      gfx::Insets().set_left(kProductSpecificationsButtonLabelMargin);
  label()->SetProperty(views::kMarginsKey, label_margin);

  SetForegroundFrameActiveColorId(kColorTabSearchButtonCRForegroundFrameActive);
  SetForegroundFrameInactiveColorId(
      kColorTabSearchButtonCRForegroundFrameInactive);
  SetBackgroundFrameActiveColorId(kColorNewTabButtonCRBackgroundFrameActive);
  SetBackgroundFrameInactiveColorId(
      kColorNewTabButtonCRBackgroundFrameInactive);
  SetCloseButton(base::BindRepeating(&ProductSpecificationsButton::OnDismissed,
                                     base::Unretained(this)));

  const int space_between_buttons = 2;
  gfx::Insets margin = gfx::Insets();
  if (before_tab_strip) {
    margin.set_left(space_between_buttons);
  } else {
    margin.set_right(space_between_buttons);
  }
  SetProperty(views::kMarginsKey, margin);
  SetOpacity(0);

  expansion_animation_.SetTweenType(gfx::Tween::Type::ACCEL_20_DECEL_100);
  opacity_animation_.SetTweenType(gfx::Tween::Type::LINEAR);

  set_paint_transparent_for_custom_image_theme(false);

  layout_manager->SetFlexForView(close_button_, 1);

  SetLayoutManager(std::make_unique<views::FlexLayout>());

  UpdateColors();

  // Button is not visible by default to avoid grabbing focus.
  SetVisible(false);
}

ProductSpecificationsButton::~ProductSpecificationsButton() = default;

gfx::Size ProductSpecificationsButton::CalculatePreferredSize(
    const views::SizeBounds& available_size) const {
  const int full_width =
      GetLayoutManager()->GetPreferredSize(this, available_size).width();
  const int width = full_width * width_factor_;
  const int height =
      TabStripControlButton::CalculatePreferredSize(available_size).height();
  return gfx::Size(width, height);
}

void ProductSpecificationsButton::MouseMovedOutOfHost() {
  SetLockedExpansionMode(LockedExpansionMode::kNone);
}

void ProductSpecificationsButton::AnimationCanceled(
    const gfx::Animation* animation) {
  AnimationEnded(animation);
}

void ProductSpecificationsButton::AnimationEnded(
    const gfx::Animation* animation) {
  ApplyAnimationValue(animation);
  // If the button went from shown -> hidden, unblock the tab strip from
  // showing other modal UIs. Compare to 0.5 to distinguish between show/hide
  // while avoiding potentially inexact float comparison to 0.0.
  // When hiding animation finishes, set the button to not visible to avoid
  // grabbing focus.
  if (animation == &expansion_animation_ &&
      animation->GetCurrentValue() < 0.5) {
    SetVisible(false);
    if (scoped_tab_strip_modal_ui_) {
      scoped_tab_strip_modal_ui_.reset();
    }
  }
}

void ProductSpecificationsButton::AnimationProgressed(
    const gfx::Animation* animation) {
  ApplyAnimationValue(animation);
}

void ProductSpecificationsButton::Show() {
  // If the button is already showing, don't update locked expansion mode.
  if (expansion_animation_.IsShowing()) {
    return;
  }

  if (locked_expansion_view_->IsMouseHovered()) {
    SetLockedExpansionMode(LockedExpansionMode::kWillShow);
  }
  if (locked_expansion_mode_ == LockedExpansionMode::kNone) {
    ExecuteShow();
  }
}

void ProductSpecificationsButton::Hide() {
  if (locked_expansion_view_->IsMouseHovered()) {
    SetLockedExpansionMode(LockedExpansionMode::kWillHide);
  }
  if (locked_expansion_mode_ == LockedExpansionMode::kNone) {
    ExecuteHide();
  }
}

void ProductSpecificationsButton::ShowEntryPointWithTitle(
    const std::u16string& title) {
  SetText(title);
  SetTooltipText(title);
  Show();
}

void ProductSpecificationsButton::HideEntryPoint() {
  Hide();
  base::RecordAction(
      base::UserMetricsAction("Commerce.Compare.ProactiveChipDisqualified"));
}

void ProductSpecificationsButton::SetOpacity(float factor) {
  label()->layer()->SetOpacity(factor);
  close_button_->layer()->SetOpacity(factor);
}

int ProductSpecificationsButton::GetCornerRadius() const {
  return kProductSpecificationsButtonCornerRadius;
}

void ProductSpecificationsButton::ApplyAnimationValue(
    const gfx::Animation* animation) {
  float value = animation->GetCurrentValue();
  if (animation == &expansion_animation_) {
    SetWidthFactor(value);
  } else if (animation == &opacity_animation_) {
    SetOpacity(value);
  }
}

void ProductSpecificationsButton::ExecuteShow() {
  // If the tab strip already has a modal UI showing, exit early.
  if (!tab_strip_model_->CanShowModalUI()) {
    return;
  }

  scoped_tab_strip_modal_ui_ = tab_strip_model_->ShowModalUI();

  expansion_animation_.SetSlideDuration(
      gfx::Animation::RichAnimationDuration(kExpansionInDuration));
  opacity_animation_.SetSlideDuration(
      gfx::Animation::RichAnimationDuration(kOpacityInDuration));
  const base::TimeDelta delay =
      gfx::Animation::RichAnimationDuration(kOpacityDelay);
  opacity_animation_delay_timer_.Start(
      FROM_HERE, delay, this,
      &ProductSpecificationsButton::ShowOpacityAnimation);

  SetVisible(true);
  expansion_animation_.Show();

  hide_button_timer_.Start(FROM_HERE, kShowDuration, this,
                           &ProductSpecificationsButton::OnTimeout);
  base::RecordAction(
      base::UserMetricsAction("Commerce.Compare.ProactiveChipShown"));
}

void ProductSpecificationsButton::ExecuteHide() {
  hide_button_timer_.Stop();
  expansion_animation_.SetSlideDuration(
      gfx::Animation::RichAnimationDuration(kExpansionOutDuration));
  expansion_animation_.Hide();

  opacity_animation_.SetSlideDuration(
      gfx::Animation::RichAnimationDuration(kOpacityOutDuration));
  opacity_animation_.Hide();
  if (entry_point_controller_) {
    entry_point_controller_->OnEntryPointHidden();
  }
}

void ProductSpecificationsButton::OnClicked() {
  if (entry_point_controller_) {
    entry_point_controller_->OnEntryPointExecuted();
  }
  ExecuteHide();
  base::RecordAction(
      base::UserMetricsAction("Commerce.Compare.ProactiveChipClicked"));
}

void ProductSpecificationsButton::OnDismissed() {
  ExecuteHide();
  if (entry_point_controller_) {
    entry_point_controller_->OnEntryPointDismissed();
  }
  base::RecordAction(
      base::UserMetricsAction("Commerce.Compare.ProactiveChipDismissed"));
}

void ProductSpecificationsButton::OnTimeout() {
  Hide();
  base::RecordAction(
      base::UserMetricsAction("Commerce.Compare.ProactiveChipIgnored"));
}

void ProductSpecificationsButton::SetCloseButton(
    views::LabelButton::PressedCallback pressed_callback) {
  auto close_button =
      std::make_unique<views::LabelButton>(std::move(pressed_callback));
  close_button->SetTooltipText(
      l10n_util::GetStringUTF16(IDS_TOOLTIP_COMPARE_ENTRY_POINT_CLOSE));

  const ui::ImageModel icon_image_model = ui::ImageModel::FromVectorIcon(
      vector_icons::kCloseChromeRefreshIcon,
      kColorTabSearchButtonCRForegroundFrameActive,
      kProductSpecificationsCloseButtonSize);

  close_button->SetImageModel(views::Button::STATE_NORMAL, icon_image_model);
  close_button->SetImageModel(views::Button::STATE_HOVERED, icon_image_model);
  close_button->SetImageModel(views::Button::STATE_PRESSED, icon_image_model);

  close_button->SetPaintToLayer();
  close_button->layer()->SetFillsBoundsOpaquely(false);

  views::InkDrop::Get(close_button.get())
      ->SetMode(views::InkDropHost::InkDropMode::ON);
  views::InkDrop::Get(close_button.get())->SetHighlightOpacity(0.16f);
  views::InkDrop::Get(close_button.get())->SetVisibleOpacity(0.14f);
  views::InkDrop::Get(close_button.get())
      ->SetBaseColorId(kColorTabSearchButtonCRForegroundFrameActive);

  auto ink_drop_highlight_path =
      std::make_unique<views::CircleHighlightPathGenerator>(gfx::Insets());
  views::HighlightPathGenerator::Install(close_button.get(),
                                         std::move(ink_drop_highlight_path));

  close_button->SetPreferredSize(
      gfx::Size(kProductSpecificationsCloseButtonSize,
                kProductSpecificationsCloseButtonSize));
  close_button->SetBorder(nullptr);

  const gfx::Insets margin =
      gfx::Insets().set_left_right(kProductSpecificationsCloseButtonMargin,
                                   kProductSpecificationsCloseButtonMargin);
  close_button->SetProperty(views::kMarginsKey, margin);

  close_button_ = AddChildView(std::move(close_button));
}

void ProductSpecificationsButton::SetLockedExpansionMode(
    LockedExpansionMode mode) {
  if (mode == LockedExpansionMode::kNone) {
    if (locked_expansion_mode_ == LockedExpansionMode::kWillShow) {
      // Check if the entry point is still eligible for showing.
      if (entry_point_controller_ &&
          entry_point_controller_->ShouldExecuteEntryPointShow()) {
        ExecuteShow();
      }
    } else if (locked_expansion_mode_ == LockedExpansionMode::kWillHide) {
      ExecuteHide();
    }
  } else {
    mouse_watcher_->Start(GetWidget()->GetNativeWindow());
  }
  locked_expansion_mode_ = mode;
}

void ProductSpecificationsButton::SetWidthFactor(float factor) {
  width_factor_ = factor;
  PreferredSizeChanged();
}

void ProductSpecificationsButton::ShowOpacityAnimation() {
  opacity_animation_.Show();
}

BEGIN_METADATA(ProductSpecificationsButton)
END_METADATA