File: intent_chip_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 (142 lines) | stat: -rw-r--r-- 4,991 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
// Copyright 2022 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/location_bar/intent_chip_button.h"

#include "base/check.h"
#include "base/check_is_test.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "build/build_config.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/intent_picker_tab_helper.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/intent_picker_bubble_view.h"
#include "chrome/browser/ui/views/location_bar/omnibox_chip_button.h"
#include "chrome/grit/generated_resources.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/base/ui_base_features.h"
#include "ui/views/style/platform_style.h"
#include "ui/views/view_class_properties.h"

IntentChipButton::IntentChipButton(Browser* browser,
                                   PageActionIconView::Delegate* delegate)
    : OmniboxChipButton(base::BindRepeating(&IntentChipButton::HandlePressed,
                                            base::Unretained(this))),
      browser_(browser),
      delegate_(delegate) {
  DCHECK(browser);
  SetIcon(kOpenInNewChromeRefreshIcon);
  SetText(l10n_util::GetStringUTF16(IDS_INTENT_CHIP_OPEN_IN_APP));
  SetFocusBehavior(views::PlatformStyle::kDefaultFocusBehavior);
  SetTooltipText(l10n_util::GetStringUTF16(IDS_INTENT_CHIP_OPEN_IN_APP));
  SetProperty(views::kElementIdentifierKey, kIntentChipElementId);
  label()->SetTextStyle(views::style::STYLE_BODY_3_EMPHASIS);
}

IntentChipButton::~IntentChipButton() = default;

void IntentChipButton::Update() {
  bool was_visible = GetVisible();
  bool is_visible = GetShowChip();
  SetVisible(is_visible);

  if (is_visible) {
    bool expanded = GetChipExpanded();
    SetTheme(expanded ? OmniboxChipTheme::kLowVisibility
                      : OmniboxChipTheme::kIconStyle);
    // TODO(pkasting): This should animate in the way other OmniboxChipButtons
    // are instructed to do (e.g. with non-zero duration when first expanding),
    // but simply passing a non-zero duration here would animate even when
    // that's undesirable (e.g. when switching tabs). In the meantime, we can't
    // simply use ResetAnimation() here because that won't trigger
    // end-of-animation updates like PreferredSizeChanged(). This should be
    // refactored to use common logic with other chips. Note that if this begins
    // to animate in some cases, the browsertests will likely need updates to
    // disable those animations.
    static constexpr auto kAnimationDuration = base::TimeDelta();
    if (expanded) {
      AnimateExpand(kAnimationDuration);
    } else {
      AnimateCollapse(kAnimationDuration);
    }
  }
  if (browser_->window() && was_visible && !is_visible) {
    IntentPickerBubbleView::CloseCurrentBubble();
  }
}

ui::ImageModel IntentChipButton::GetAppIconForTesting() const {
  CHECK_IS_TEST();
  return GetAppIcon();
}

bool IntentChipButton::GetShowChip() const {
  if (delegate_->ShouldHidePageActionIcons()) {
    return false;
  }

  auto* tab_helper = GetTabHelper();
  return tab_helper && tab_helper->should_show_icon();
}

bool IntentChipButton::GetChipExpanded() const {
  auto* tab_helper = GetTabHelper();
  return tab_helper && tab_helper->ShouldShowExpandedChip();
}

ui::ImageModel IntentChipButton::GetAppIcon() const {
  if (auto* tab_helper = GetTabHelper()) {
    return tab_helper->app_icon();
  }
  return ui::ImageModel();
}

void IntentChipButton::HandlePressed() {
  content::WebContents* web_contents =
      delegate_->GetWebContentsForPageActionIconView();
  const GURL& url = web_contents->GetURL();
  GetTabHelper()->ShowIntentPickerBubbleOrLaunchApp(url);
}

IntentPickerTabHelper* IntentChipButton::GetTabHelper() const {
  if (browser_->profile()->IsOffTheRecord()) {
    return nullptr;
  }

  content::WebContents* web_contents =
      delegate_->GetWebContentsForPageActionIconView();
  if (!web_contents) {
    return nullptr;
  }

  return IntentPickerTabHelper::FromWebContents(web_contents);
}

ui::ImageModel IntentChipButton::GetIconImageModel() const {
  auto icon = GetAppIcon();
  if (icon.IsEmpty()) {
    return OmniboxChipButton::GetIconImageModel();
  }
  return icon;
}

ui::ColorId IntentChipButton::GetBackgroundColorId() const {
  DCHECK(GetOmniboxChipTheme() != OmniboxChipTheme::kIconStyle);
  return kColorOmniboxIntentChipBackground;
}

ui::ColorId IntentChipButton::GetForegroundColorId() const {
  return GetOmniboxChipTheme() == OmniboxChipTheme::kIconStyle
             ? kColorOmniboxResultsIcon
             : kColorOmniboxIntentChipIcon;
}

BEGIN_METADATA(IntentChipButton)
END_METADATA