File: price_insights_icon_view.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (202 lines) | stat: -rw-r--r-- 7,292 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
// Copyright 2023 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/price_insights_icon_view.h"

#include <string_view>

#include "base/metrics/histogram_functions.h"
#include "base/timer/timer.h"
#include "chrome/browser/feature_engagement/tracker_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/commerce/commerce_ui_tab_helper.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/views/side_panel/side_panel_entry_id.h"
#include "chrome/browser/ui/views/side_panel/side_panel_ui.h"
#include "components/commerce/core/commerce_feature_list.h"
#include "components/commerce/core/shopping_service.h"
#include "components/feature_engagement/public/feature_constants.h"
#include "components/feature_engagement/public/tracker.h"
#include "components/strings/grit/components_strings.h"
#include "components/tabs/public/tab_interface.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/views/accessibility/view_accessibility.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/animation/ink_drop_state.h"
#include "ui/views/view_class_properties.h"

PriceInsightsIconView::PriceInsightsIconView(
    IconLabelBubbleView::Delegate* icon_label_bubble_delegate,
    PageActionIconView::Delegate* page_action_icon_delegate,
    Profile* profile)
    : PageActionIconView(nullptr,
                         0,
                         icon_label_bubble_delegate,
                         page_action_icon_delegate,
                         "PriceInsights"),
      profile_(profile) {
  SetUpForInOutAnimation();
  SetProperty(views::kElementIdentifierKey, kPriceInsightsChipElementId);
  GetViewAccessibility().SetName(
      l10n_util::GetStringUTF16(IDS_SHOPPING_INSIGHTS_ICON_TOOLTIP_TEXT));
}
PriceInsightsIconView::~PriceInsightsIconView() = default;

views::BubbleDialogDelegate* PriceInsightsIconView::GetBubble() const {
  return nullptr;
}

const gfx::VectorIcon& PriceInsightsIconView::GetVectorIcon() const {
  return vector_icons::kShoppingBagRefreshIcon;
}

void PriceInsightsIconView::UpdateImpl() {
  bool should_show = ShouldShow();

  if (should_show) {
    MaybeShowPageActionLabel();
  } else {
    scoped_window_call_to_action_ptr_.reset();
    HidePageActionLabel();
  }
  UpdateBackground();

  SetVisible(should_show);
}

void PriceInsightsIconView::HidePageActionLabel() {
  UnpauseAnimation();
  ResetSlideAnimation(false);
}

void PriceInsightsIconView::MaybeShowPageActionLabel() {
  auto* tab_helper = tabs::TabInterface::GetFromContents(GetWebContents())
                         ->GetTabFeatures()
                         ->commerce_ui_tab_helper();

  if (!tab_helper || !tab_helper->ShouldExpandPageActionIcon(
                         PageActionIconType::kPriceInsights)) {
    return;
  }

  if (!tabs::TabInterface::GetFromContents(GetWebContents())
           ->GetBrowserWindowInterface()
           ->CanShowCallToAction()) {
    return;
  }

  scoped_window_call_to_action_ptr_ =
      tabs::TabInterface::GetFromContents(GetWebContents())
          ->GetBrowserWindowInterface()
          ->ShowCallToAction();

  should_extend_label_shown_duration_ = true;
  UpdatePriceInsightsIconLabel();

  AnimateIn(std::nullopt);
}

PriceInsightsIconLabelType PriceInsightsIconView::GetLabelTypeForPage() {
  auto* web_contents = GetWebContents();

  if (!web_contents) {
    return PriceInsightsIconLabelType::kNone;
  }
  auto* tab_helper = tabs::TabInterface::GetFromContents(web_contents)
                         ->GetTabFeatures()
                         ->commerce_ui_tab_helper();
  CHECK(tab_helper);

  return tab_helper->GetPriceInsightsIconLabelTypeForPage();
}

void PriceInsightsIconView::UpdatePriceInsightsIconLabel() {
  PriceInsightsIconLabelType label_type = GetLabelTypeForPage();
  if (label_type == PriceInsightsIconLabelType::kPriceIsLow) {
    SetLabel(
        l10n_util::GetStringUTF16(
            IDS_SHOPPING_INSIGHTS_ICON_EXPANDED_TEXT_LOW_PRICE),
        l10n_util::GetStringUTF16(IDS_SHOPPING_INSIGHTS_ICON_TOOLTIP_TEXT));
  } else if (label_type == PriceInsightsIconLabelType::kPriceIsHigh) {
    SetLabel(
        l10n_util::GetStringUTF16(
            IDS_SHOPPING_INSIGHTS_ICON_EXPANDED_TEXT_HIGH_PRICE),
        l10n_util::GetStringUTF16(IDS_SHOPPING_INSIGHTS_ICON_TOOLTIP_TEXT));
  } else {
    SetLabel(u"", l10n_util::GetStringUTF16(
                      IDS_SHOPPING_INSIGHTS_ICON_TOOLTIP_TEXT));
  }
  SetBackgroundVisibility(BackgroundVisibility::kWithLabel);
}

void PriceInsightsIconView::AnimationProgressed(
    const gfx::Animation* animation) {
  PageActionIconView::AnimationProgressed(animation);
  // When the label is fully revealed pause the animation for
  // kLabelPersistDuration before resuming the animation and allowing the label
  // to animate out. This is currently set to show for 12s including the in/out
  // animation.
  // TODO(crbug.com/40832707): This approach of inspecting the animation
  // progress to extend the animation duration is quite hacky. This should be
  // removed and the IconLabelBubbleView API expanded to support a finer level
  // of control.
  constexpr double kAnimationValueWhenLabelFullyShown = 0.5;
  constexpr base::TimeDelta kLabelPersistDuration = base::Seconds(10.8);
  if (should_extend_label_shown_duration_ &&
      GetAnimationValue() >= kAnimationValueWhenLabelFullyShown) {
    should_extend_label_shown_duration_ = false;
    PauseAnimation();
    animate_out_timer_.Start(
        FROM_HERE, kLabelPersistDuration,
        base::BindRepeating(&PriceInsightsIconView::UnpauseAnimation,
                            base::Unretained(this)));
  }
}

void PriceInsightsIconView::OnExecuting(
    PageActionIconView::ExecuteSource execute_source) {
  auto* web_contents = GetWebContents();
  if (!web_contents) {
    return;
  }
  auto* tab_helper = tabs::TabInterface::GetFromContents(web_contents)
                         ->GetTabFeatures()
                         ->commerce_ui_tab_helper();
  CHECK(tab_helper);

  tab_helper->OnPriceInsightsIconClicked();
  SetHighlighted(false);
}

bool PriceInsightsIconView::ShouldShow() const {
  if (delegate()->ShouldHidePageActionIcons()) {
    return false;
  }
  auto* web_contents = GetWebContents();
  if (!web_contents) {
    return false;
  }
  auto* tab_helper = tabs::TabInterface::GetFromContents(web_contents)
                         ->GetTabFeatures()
                         ->commerce_ui_tab_helper();

  return tab_helper && tab_helper->ShouldShowPriceInsightsIconView();
}

std::u16string_view PriceInsightsIconView::GetIconLabelForTesting() const {
  return label()->GetText();
}

bool PriceInsightsIconView::IsIconHighlightedForTesting() {
  return views::InkDrop::Get(this)->GetInkDrop()->GetTargetInkDropState() ==
         views::InkDropState::ACTIVATED;
}

BEGIN_METADATA(PriceInsightsIconView)
END_METADATA