File: lens_preselection_bubble.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 (249 lines) | stat: -rw-r--r-- 10,513 bytes parent folder | download | duplicates (2)
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
// 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/lens/lens_preselection_bubble.h"

#include <memory>

#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "build/branding_buildflags.h"
#include "chrome/app/vector_icons/vector_icons.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/lens/lens_help_menu_utils.h"
#include "chrome/browser/ui/lens/lens_overlay_controller.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "components/lens/lens_features.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/base/models/image_model.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/mojom/menu_source_type.mojom.h"
#include "ui/color/color_provider.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/menus/simple_menu_model.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/button/image_button_factory.h"
#include "ui/views/controls/button/menu_button.h"
#include "ui/views/controls/button/menu_button_controller.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/window/dialog_delegate.h"

namespace lens {
namespace {

// The minimum y value in screen coordinates for the preselection bubble.
const int kPreselectionBubbleMinY = 8;

}  // namespace

LensPreselectionBubble::LensPreselectionBubble(
    base::WeakPtr<LensOverlayController> lens_overlay_controller,
    views::View* anchor_view,
    bool offline,
    ExitClickedCallback exit_clicked_callback,
    base::OnceClosure on_cancel_callback)
    : BubbleDialogDelegateView(anchor_view,
                               views::BubbleBorder::NONE,
                               views::BubbleBorder::NO_SHADOW),
      lens_overlay_controller_(lens_overlay_controller),
      offline_(offline),
      exit_clicked_callback_(std::move(exit_clicked_callback)) {
  SetShowCloseButton(false);
  set_close_on_deactivate(false);
  DialogDelegate::SetButtons(static_cast<int>(ui::mojom::DialogButton::kNone));
  set_corner_radius(48);
  SetBackgroundColor(kColorLensOverlayToastBackground);
  SetProperty(views::kElementIdentifierKey, kLensPreselectionBubbleElementId);
  SetAccessibleWindowRole(ax::mojom::Role::kAlertDialog);
  SetCancelCallback(std::move(on_cancel_callback));
}

LensPreselectionBubble::~LensPreselectionBubble() = default;

void LensPreselectionBubble::Init() {
  views::BoxLayout* layout =
      SetLayoutManager(std::make_unique<views::BoxLayout>(
          views::BoxLayout::Orientation::kHorizontal, gfx::Insets()));
  offline_ ? set_margins(gfx::Insets::TLBR(6, 16, 6, 6))
           : set_margins(gfx::Insets::TLBR(12, 16, 12, 16));

  // Set bubble icon and text
  const std::u16string online_toast_text =
      lens::features::IsSimplifiedSelectionEnabled()
          ? l10n_util::GetStringUTF16(
                IDS_LENS_OVERLAY_INITIAL_TOAST_MESSAGE_SIMPLIFIED)
          : l10n_util::GetStringUTF16(IDS_LENS_OVERLAY_INITIAL_TOAST_MESSAGE);
  const std::u16string toast_text =
      offline_ ? l10n_util::GetStringUTF16(
                     IDS_LENS_OVERLAY_INITIAL_TOAST_ERROR_MESSAGE)
               : online_toast_text;
  SetAccessibleTitle(toast_text);
  icon_view_ = AddChildView(std::make_unique<views::ImageView>());
  label_ = AddChildView(std::make_unique<views::Label>(toast_text));
  label_->SetMultiLine(false);
  label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  label_->SetAllowCharacterBreak(false);
  if (lens::features::IsLensOverlayContextualSearchboxEnabled()) {
    auto button = views::CreateVectorImageButtonWithNativeTheme(
        base::RepeatingClosure(), kHelpMenuIcon, 20,
        kColorLensOverlayToastForeground, kColorLensOverlayToastForeground);
    views::HighlightPathGenerator::Install(
        button.get(),
        std::make_unique<views::CircleHighlightPathGenerator>(gfx::Insets()));
    button->SetTooltipText(
        l10n_util::GetStringUTF16(IDS_LENS_OVERLAY_MORE_OPTIONS_BUTTON_LABEL));
    more_info_button_ = AddChildView(std::move(button));
    more_info_button_->SetButtonController(
        std::make_unique<views::MenuButtonController>(
            more_info_button_,
            base::BindRepeating(&LensPreselectionBubble::OpenMoreInfoMenu,
                                base::Unretained(this)),
            std::make_unique<views::Button::DefaultButtonControllerDelegate>(
                more_info_button_)));
  }
  layout->set_between_child_spacing(8);
  // Need to set this false so label color token doesn't get changed by
  // changed by SetEnabledColor() color mapper. Color tokens provided
  // have enough contrast.
  label_->SetAutoColorReadabilityEnabled(false);
  if (offline_) {
    exit_button_ = AddChildView(std::make_unique<views::MdTextButton>(
        std::move(exit_clicked_callback_),
        l10n_util::GetStringUTF16(
            IDS_LENS_OVERLAY_INITIAL_TOAST_ERROR_EXIT_BUTTON_TEXT)));
    exit_button_->SetProperty(views::kMarginsKey,
                              gfx::Insets::TLBR(0, 8, 0, 0));
    exit_button_->SetPreferredSize(gfx::Size(55, 36));
    exit_button_->SetStyle(ui::ButtonStyle::kProminent);
    exit_button_->SetProperty(views::kElementIdentifierKey,
                              kLensPreselectionBubbleExitButtonElementId);
  }
  NotifyAccessibilityEventDeprecated(ax::mojom::Event::kAlert, true);
}

void LensPreselectionBubble::SetLabelText(int string_id) {
  // If the bubble had offline state, we don't want to reset the text.
  if (offline_) {
    return;
  }

  const std::u16string new_toast_text = l10n_util::GetStringUTF16(string_id);
  SetAccessibleTitle(new_toast_text);
  label_->SetText(new_toast_text);
  SizeToContents();
}

gfx::Rect LensPreselectionBubble::GetBubbleBounds() {
  views::View* anchor_view = GetAnchorView();
  if (!anchor_view) {
    return gfx::Rect();
  }

  const bool is_tab_strip_visible = lens_overlay_controller_->GetTabInterface()
                                        ->GetBrowserWindowInterface()
                                        ->IsTabStripVisible();
  const gfx::Size bubble_size =
      GetWidget()->GetContentsView()->GetPreferredSize();
  const gfx::Rect anchor_bounds = anchor_view->GetBoundsInScreen();

  const int x =
      anchor_bounds.x() + (anchor_bounds.width() - bubble_size.width()) / 2;
  // Take bubble out of its original bounds to cross "line of death". Since, the
  // preselection bubble is anchored to the overlay, the line of death is above
  // the top of the anchor bounds. However, if not tab strip is visible, and
  // therefore there is no line of death to cross, we instead want to set the
  // preselection bubble to be kPreselectionBubbleMinY from the top of the
  // overlay.
  const int y = is_tab_strip_visible
                    ? anchor_bounds.y() - bubble_size.height() / 2
                    : anchor_bounds.y() + kPreselectionBubbleMinY;
  return gfx::Rect(x, y, bubble_size.width(), bubble_size.height());
}

void LensPreselectionBubble::OnThemeChanged() {
  BubbleDialogDelegateView::OnThemeChanged();
  const auto* color_provider = GetColorProvider();
  icon_view_->SetImage(ui::ImageModel::FromVectorIcon(
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
      offline_ ? vector_icons::kErrorOutlineIcon
               : vector_icons::kGoogleLensMonochromeLogoIcon,
#else
      offline_ ? vector_icons::kErrorOutlineIcon
               : vector_icons::kSearchChromeRefreshIcon,
#endif
      color_provider->GetColor(kColorLensOverlayToastForeground),
      /*icon_size=*/24));
  label_->SetEnabledColor(
      color_provider->GetColor(kColorLensOverlayToastForeground));

  if (offline_) {
    CHECK(exit_button_);
    exit_button_->SetEnabledTextColors(
        color_provider->GetColor(kColorLensOverlayToastForeground));
    exit_button_->SetBorder(views::CreateRoundedRectBorder(
        1, 48, color_provider->GetColor(kColorLensOverlayToastButtonBorder)));
    exit_button_->SetBgColorIdOverride(kColorLensOverlayToastBackground);
  }
}

void LensPreselectionBubble::OpenMoreInfoMenu() {
  auto menu_model = std::make_unique<ui::SimpleMenuModel>(this);
  menu_model->AddItem(COMMAND_MY_ACTIVITY,
                      l10n_util::GetStringUTF16(IDS_LENS_OVERLAY_MY_ACTIVITY));
  menu_model->AddItem(COMMAND_LEARN_MORE,
                      l10n_util::GetStringUTF16(IDS_LENS_OVERLAY_LEARN_MORE));
  menu_model->AddItem(COMMAND_SEND_FEEDBACK,
                      l10n_util::GetStringUTF16(IDS_LENS_SEND_FEEDBACK));
  more_info_menu_model_ = std::move(menu_model);
  menu_runner_ = std::make_unique<views::MenuRunner>(
      more_info_menu_model_.get(), views::MenuRunner::HAS_MNEMONICS);
  menu_runner_->RunMenuAt(more_info_button_->GetWidget(),
                          static_cast<views::MenuButtonController*>(
                              more_info_button_->button_controller()),
                          more_info_button_->GetAnchorBoundsInScreen(),
                          views::MenuAnchorPosition::kTopRight,
                          ui::mojom::MenuSourceType::kNone);
}

void LensPreselectionBubble::ExecuteCommand(int command_id, int event_flags) {
  CHECK(lens_overlay_controller_);
  switch (command_id) {
    case COMMAND_MY_ACTIVITY: {
      ActivityRequestedByEvent(lens_overlay_controller_->GetTabInterface(),
                               event_flags);
      break;
    }
    case COMMAND_LEARN_MORE: {
      InfoRequestedByEvent(lens_overlay_controller_->GetTabInterface(),
                           event_flags);
      break;
    }
    case COMMAND_SEND_FEEDBACK: {
      FeedbackRequestedByEvent(lens_overlay_controller_->GetTabInterface(),
                               event_flags);
      break;
    }
    default: {
      NOTREACHED() << "Unknown option";
    }
  }
}

BEGIN_METADATA(LensPreselectionBubble)
END_METADATA

}  // namespace lens