File: sharing_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 (195 lines) | stat: -rw-r--r-- 6,024 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
// Copyright 2019 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/sharing/sharing_icon_view.h"

#include "chrome/app/vector_icons/vector_icons.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/scoped_canvas.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"

namespace {
// Progress state when the full length of the animation text is visible.
constexpr double kAnimationTextFullLengthShownProgressState = 0.5;
}  // namespace

SharingIconView::SharingIconView(
    IconLabelBubbleView::Delegate* icon_label_bubble_delegate,
    PageActionIconView::Delegate* page_action_icon_delegate,
    GetControllerCallback get_controller_callback,
    GetBubbleCallback get_bubble_callback)
    : PageActionIconView(/*command_updater=*/nullptr,
                         /*command_id=*/0,
                         icon_label_bubble_delegate,
                         page_action_icon_delegate,
                         "ClickToCall"),  // Naming corresponds to
                                          // PageActionIconType.
      get_controller_callback_(std::move(get_controller_callback)),
      get_bubble_callback_(std::move(get_bubble_callback)) {
  SetVisible(false);
  SetUpForInOutAnimation();

  SetAccessibleIsIgnoredIfNeeded();
}

SharingIconView::~SharingIconView() = default;

SharingUiController* SharingIconView::GetController() const {
  content::WebContents* web_contents = GetWebContents();
  return web_contents ? get_controller_callback_.Run(web_contents) : nullptr;
}

void SharingIconView::StartLoadingAnimation() {
  if (loading_animation_) {
    return;
  }

  loading_animation_ = true;
  AnimateIn(IDS_BROWSER_SHARING_OMNIBOX_SENDING_LABEL);
  SchedulePaint();
}

void SharingIconView::StopLoadingAnimation() {
  if (!loading_animation_) {
    return;
  }

  loading_animation_ = false;
  UnpauseAnimation();
  SchedulePaint();
}

void SharingIconView::UpdateImpl() {
  auto* controller = GetController();
  if (!controller) {
    return;
  }

  GetViewAccessibility().SetName(
      controller->GetTextForTooltipAndAccessibleName());

  // To ensure that we reset error icon badge.
  if (!GetVisible()) {
    should_show_error_ = controller->HasSendFailed();
    UpdateIconImage();
  }

  if (controller->is_loading()) {
    StartLoadingAnimation();
  } else {
    StopLoadingAnimation();
  }

  if (last_controller_ != controller) {
    ResetSlideAnimation(/*show=*/false);
  }

  last_controller_ = controller;

  const bool is_bubble_showing = IsBubbleShowing();
  const bool is_visible =
      is_bubble_showing || loading_animation_ || label()->GetVisible();

  SetVisible(is_visible);
  UpdateInkDrop(is_bubble_showing);
}

void SharingIconView::AnimationProgressed(const gfx::Animation* animation) {
  if (animation->is_animating() &&
      GetAnimationValue() >= kAnimationTextFullLengthShownProgressState &&
      loading_animation_) {
    PauseAnimation();
  }
  UpdateOpacity();
  return PageActionIconView::AnimationProgressed(animation);
}

void SharingIconView::AnimationEnded(const gfx::Animation* animation) {
  PageActionIconView::AnimationEnded(animation);
  UpdateOpacity();

  auto* controller = GetController();
  if (controller && should_show_error_ != controller->HasSendFailed()) {
    should_show_error_ = controller->HasSendFailed();
    UpdateIconImage();
    controller->MaybeShowErrorDialog();
  }
  Update();
}

void SharingIconView::UpdateOpacity() {
  if (!IsShrinking()) {
    DestroyLayer();
    SetTextSubpixelRenderingEnabled(true);
    return;
  }

  if (!layer()) {
    SetPaintToLayer();
    SetTextSubpixelRenderingEnabled(false);
    layer()->SetFillsBoundsOpaquely(false);
  }

  int kLargeNumber = 100;
  layer()->SetOpacity(GetWidthBetween(0, kLargeNumber) /
                      static_cast<float>(kLargeNumber));
}

void SharingIconView::UpdateInkDrop(bool activate) {
  auto target_state =
      activate ? views::InkDropState::ACTIVATED : views::InkDropState::HIDDEN;
  if (views::InkDrop::Get(this)->GetInkDrop()->GetTargetInkDropState() !=
      target_state) {
    views::InkDrop::Get(this)->AnimateToState(target_state, /*event=*/nullptr);
  }
}

bool SharingIconView::IsTriggerableEvent(const ui::Event& event) {
  // We do nothing when the icon is clicked.
  return false;
}

const gfx::VectorIcon& SharingIconView::GetVectorIconBadge() const {
  return should_show_error_ ? vector_icons::kBlockedBadgeIcon
                            : gfx::VectorIcon::EmptyIcon();
}

void SharingIconView::OnExecuting(
    PageActionIconView::ExecuteSource execute_source) {}

views::BubbleDialogDelegate* SharingIconView::GetBubble() const {
  auto* controller = GetController();
  return controller ? get_bubble_callback_.Run(controller->dialog()) : nullptr;
}

const gfx::VectorIcon& SharingIconView::GetVectorIcon() const {
  auto* controller = GetController();
  return controller ? controller->GetVectorIcon()
                    : gfx::VectorIcon::EmptyIcon();
}

void SharingIconView::SetAccessibleIsIgnoredIfNeeded() {
  auto* controller = GetController();
  if (controller && !controller->HasAccessibleUi()) {
    // This should rarely be true. One example where it is true is the
    // SmsRemoteFetcherUiController: crrev.com/c/2964059 stopped all UI
    // from being shown and removed the accessible name. Setting the state
    // to ignored is needed to stop the UI from being shown to assistive
    // technologies.
    GetViewAccessibility().SetIsIgnored(true);
    return;
  }
}

BEGIN_METADATA(SharingIconView)
END_METADATA