Skip to content

Commit

Permalink
Added search promotion button in omnibox
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhong committed Sep 5, 2024
1 parent ed77d85 commit 0a95238
Show file tree
Hide file tree
Showing 19 changed files with 319 additions and 5 deletions.
7 changes: 7 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,13 @@ Or change later at <ph name="SETTINGS_EXTENIONS_LINK">$2<ex>brave://settings/ext
<message name="IDS_ACC_BRAVE_SEARCH_CONVERSION_DISMISS_BUTTON" desc="Announcement when the dismiss button is focused.">
Dismiss Brave search conversion, press Enter to remove this suggestion
</message>
<message name="IDS_BRAVE_SEARCH_CONVERSION_BUTTON_LABEL" desc="The text for the search conversion promotion button">
Try Brave Search
</message>
<message name="IDS_BRAVE_SEARCH_CONVERSION_BUTTON_TOOLTIP" desc="The text for the search conversion button tooltip">
Switch to Brave Search
</message>

<!-- web Discovery InfoBar -->
<message name="IDS_WEB_DISCOVERY_INFOBAR_MESSAGE" desc="The text for web discovery infobar message">
Help improve <ph name="SEARCH_NAME">$1<ex>Brave Search</ex></ph> by sending anonymous usage data. <ph name="MORE_INFO">$2<ex>More info</ex></ph>
Expand Down
2 changes: 2 additions & 0 deletions app/theme/brave_theme_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
<structure type="chrome_scaled_image" name="IDR_BRAVE_SEARCH_CONVERSION_BANNER_GRAPHIC_DARK" file="brave/brave_search_conversion_banner_graphic_dark.png" />
<structure type="chrome_scaled_image" name="IDR_BRAVE_SEARCH_CONVERSION_BANNER_GRAPHIC_DDG" file="brave/brave_search_conversion_banner_graphic_ddg.png" />
<structure type="chrome_scaled_image" name="IDR_BRAVE_SEARCH_CONVERSION_BANNER_GRAPHIC_DDG_DARK" file="brave/brave_search_conversion_banner_graphic_ddg_dark.png" />
<structure type="chrome_scaled_image" name="IDR_BRAVE_SEARCH_CONVERSION_BUTTON_DDG_ICON" file="brave/brave_search_conversion_button_ddg_icon.png" />
<structure type="chrome_scaled_image" name="IDR_BRAVE_SEARCH_CONVERSION_BUTTON_BRAVE_SEARCH_ICON" file="brave/brave_search_conversion_button_brave_search_icon.png" />
<structure type="chrome_scaled_image" name="IDR_BRAVE_SEARCH_LOGO_IN_SEARCH_PROMOTION" file="brave/brave_search_logo_in_search_promotion.png" />
</if>
</structures>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,8 @@ source_set("ui") {
"views/frame/vertical_tab_strip_widget_delegate_view.h",
"views/location_bar/brave_location_bar_view.cc",
"views/location_bar/brave_location_bar_view.h",
"views/location_bar/brave_search_conversion_promotion_button_view.cc",
"views/location_bar/brave_search_conversion_promotion_button_view.h",
"views/location_bar/brave_star_view.cc",
"views/location_bar/brave_star_view.h",
"views/profiles/brave_avatar_toolbar_button.cc",
Expand Down
11 changes: 10 additions & 1 deletion browser/ui/color/brave_color_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@
E_CPONLY(kColorSearchConversionBannerTypeBackgroundBorderHovered) \
E_CPONLY(kColorSearchConversionBannerTypeBackgroundGradientFrom) \
E_CPONLY(kColorSearchConversionBannerTypeBackgroundGradientTo) \
E_CPONLY(kColorSearchConversionBannerTypeDescText)
E_CPONLY(kColorSearchConversionBannerTypeDescText) \
E_CPONLY(kColorSearchConversionButtonBorder) \
E_CPONLY(kColorSearchConversionButtonBackground) \
E_CPONLY(kColorSearchConversionButtonBackgroundHovered) \
E_CPONLY(kColorSearchConversionButtonText) \
E_CPONLY(kColorSearchConversionButtonCaratRight) \
E_CPONLY(kColorSearchConversionButtonCloseButton) \
E_CPONLY(kColorSearchConversionButtonCloseButtonHovered) \
E_CPONLY(kColorSearchConversionButtonShadow1) \
E_CPONLY(kColorSearchConversionButtonShadow2)

#define BRAVE_SIDEBAR_COLOR_IDS \
E_CPONLY(kColorSidebarAddBubbleBackground) \
Expand Down
21 changes: 21 additions & 0 deletions browser/ui/color/brave_color_mixer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,26 @@ void AddBraveColorMixerForAllThemes(ui::ColorProvider* provider,
mixer[kColorToolbarButtonActivated] = {SkColorSetRGB(0x7C, 0x91, 0xFF)};
mixer[kColorSidebarButtonPressed] = {kColorToolbarButtonActivated};
mixer[kColorToolbarContentAreaSeparator] = {kColorToolbar};

// Search conversion button in omnibox.
mixer[kColorSearchConversionButtonText] = {nala::kColorPrimary60};
mixer[kColorSearchConversionButtonBorder] = {nala::kColorPrimary20};
mixer[kColorSearchConversionButtonBackground] = {nala::kColorPrimary10};
mixer[kColorSearchConversionButtonBackgroundHovered] = {
nala::kColorPrimary20};
mixer[kColorSearchConversionButtonText] = {nala::kColorPrimary60};
mixer[kColorSearchConversionButtonCloseButton] = {
ui::AlphaBlend({nala::kColorIconInteractive},
{kColorSearchConversionButtonBackground}, 0.5 * 0xff)};
mixer[kColorSearchConversionButtonCloseButtonHovered] = {
ui::AlphaBlend({nala::kColorIconInteractive},
{kColorSearchConversionButtonBackground}, 0.7 * 0xff)};
mixer[kColorSearchConversionButtonCaratRight] = {
kColorSearchConversionButtonCloseButton};
mixer[kColorSearchConversionButtonShadow1] = {
SkColorSetA(SK_ColorBLACK, 0.05 * 255)};
mixer[kColorSearchConversionButtonShadow2] = {
SkColorSetA(SK_ColorBLACK, 0.1 * 255)};
}

} // namespace
Expand Down Expand Up @@ -361,6 +381,7 @@ void AddBraveLightThemeColorMixer(ui::ColorProvider* provider,
PickColorContrastingToToolbar(key, mixer, SkColorSetRGB(0x49, 0x50, 0x57),
SkColorSetRGB(0xFF, 0xFF, 0xFF))};
mixer[kColorMenuItemSubText] = {SkColorSetRGB(0x86, 0x8E, 0x96)};

// It's "Themeable/Blue/10" but leo/color.h doesn't have it.
mixer[kColorSearchConversionBannerTypeBackground] = {
SkColorSetRGB(0xEA, 0xF1, 0xFF)};
Expand Down
32 changes: 32 additions & 0 deletions browser/ui/views/location_bar/brave_location_bar_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
#include "brave/browser/ui/tabs/features.h"
#include "brave/browser/ui/views/brave_actions/brave_actions_container.h"
#include "brave/browser/ui/views/brave_news/brave_news_action_icon_view.h"
#include "brave/browser/ui/views/location_bar/brave_search_conversion_promotion_button_view.h"
#include "brave/browser/ui/views/playlist/playlist_action_icon_view.h"
#include "brave/browser/ui/views/toolbar/brave_toolbar_view.h"
#include "brave/components/commander/common/buildflags/buildflags.h"
#include "brave/components/l10n/common/localization_util.h"
#include "brave/components/omnibox/browser/leo_provider.h"
#include "brave/components/omnibox/browser/promotion_utils.h"
#include "brave/grit/brave_theme_resources.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/themes/theme_service_factory.h"
Expand Down Expand Up @@ -125,6 +128,9 @@ void BraveLocationBarView::Init() {
AddChildView(std::make_unique<OnionLocationView>(browser_->profile()));
#endif

promotion_button_ = AddChildView(
std::make_unique<BraveSearchConversionPromotionButtonView>());

// brave action buttons
brave_actions_ = AddChildView(
std::make_unique<BraveActionsContainer>(browser_, profile()));
Expand Down Expand Up @@ -189,6 +195,14 @@ void BraveLocationBarView::OnOmniboxBlurred() {
LocationBarView::OnOmniboxBlurred();
}

void BraveLocationBarView::Layout(PassKey) {
if (promotion_button_) {
promotion_button_->SetVisible(false);
}

LayoutSuperclass<LocationBarView>(this);
}

void BraveLocationBarView::OnChanged() {
auto hide_page_actions = ShouldHidePageActionIcons();
if (brave_actions_) {
Expand Down Expand Up @@ -234,6 +248,24 @@ std::vector<views::View*> BraveLocationBarView::GetLeftMostTrailingViews() {
return views;
}

bool BraveLocationBarView::ShouldShowSearchPromotionButton() {
if (!promotion_button_) {
return false;
}

const AutocompleteMatch match = omnibox_view_->model()->CurrentMatch(nullptr);
return AutocompleteMatch::IsSearchType(match.type) &&
omnibox_view_->model()->PopupIsOpen() &&
!IsBraveSearchPromotionMatch(match) &&
!LeoProvider::IsMatchFromLeoProvider(match) &&
!GURL(omnibox_view_->GetText()).is_valid() &&
!ShouldChipOverrideLocationIcon();
}

views::View* BraveLocationBarView::GetSearchPromotionButton() const {
return promotion_button_;
}

void BraveLocationBarView::RefreshBackground() {
LocationBarView::RefreshBackground();

Expand Down
7 changes: 6 additions & 1 deletion browser/ui/views/location_bar/brave_location_bar_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

class BraveActionsContainer;
class BraveActionsContainerTest;
class BraveSearchConversionPromotionButtonView;
class PlaylistActionIconView;
class RewardsBrowserTest;
class SkPath;
Expand Down Expand Up @@ -70,8 +71,11 @@ class BraveLocationBarView : public LocationBarView {
std::vector<views::View*> GetRightMostTrailingViews() override;
// Views that locates at left side of upstream's trailing views.
std::vector<views::View*> GetLeftMostTrailingViews() override;
bool ShouldShowSearchPromotionButton() override;
views::View* GetSearchPromotionButton() const override;
void RefreshBackground() override;
void OnOmniboxBlurred() override;
void Layout(PassKey) override;

// views::View:
gfx::Size CalculatePreferredSize(
Expand All @@ -80,7 +84,7 @@ class BraveLocationBarView : public LocationBarView {
void ChildVisibilityChanged(views::View* child) override;
void AddedToWidget() override;
int GetBorderRadius() const override;
void FocusLocation(bool is_user_initiated) override;
void FocusLocation(bool is_user_initated) override;

SkPath GetFocusRingHighlightPath() const;
ContentSettingImageView* GetContentSettingsImageViewForTesting(size_t idx);
Expand Down Expand Up @@ -113,6 +117,7 @@ class BraveLocationBarView : public LocationBarView {
std::unique_ptr<ViewShadow> shadow_;
raw_ptr<BraveActionsContainer> brave_actions_ = nullptr;
raw_ptr<BraveNewsActionIconView> brave_news_action_icon_view_ = nullptr;
raw_ptr<BraveSearchConversionPromotionButtonView> promotion_button_ = nullptr;
#if BUILDFLAG(ENABLE_TOR)
raw_ptr<OnionLocationView> onion_location_view_ = nullptr;
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/* Copyright (c) 2024 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "brave/browser/ui/views/location_bar/brave_search_conversion_promotion_button_view.h"

#include "brave/browser/ui/color/brave_color_id.h"
#include "brave/components/vector_icons/vector_icons.h"
#include "brave/grit/brave_generated_resources.h"
#include "brave/grit/brave_theme_resources.h"
#include "chrome/browser/ui/layout_constants.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/resource/resource_bundle.h"
#include "ui/gfx/font_list.h"
#include "ui/views/background.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/painter.h"

using views::BoxLayout;

BraveSearchConversionPromotionButtonView::
BraveSearchConversionPromotionButtonView() {
// Hovering on close button should not make this as normal state.
SetNotifyEnterExitOnChild(true);
SetLayoutManager(
std::make_unique<BoxLayout>(BoxLayout::Orientation::kHorizontal,
gfx::Insets::VH(0, 6),
/*between_child_spacing*/ 4))
->set_cross_axis_alignment(BoxLayout::CrossAxisAlignment::kCenter);
SetTooltipText(
l10n_util::GetStringUTF16(IDS_BRAVE_SEARCH_CONVERSION_BUTTON_TOOLTIP));
AddChildViews();
Update();
}

BraveSearchConversionPromotionButtonView::
~BraveSearchConversionPromotionButtonView() = default;

void BraveSearchConversionPromotionButtonView::StateChanged(
views::Button::ButtonState old_state) {
Update();
}

void BraveSearchConversionPromotionButtonView::OnThemeChanged() {
Button::OnThemeChanged();
SetupShadow();
Update();
}

float BraveSearchConversionPromotionButtonView::GetCornerRadius() const {
return GetLayoutConstant(LOCATION_BAR_CHILD_CORNER_RADIUS);
}

void BraveSearchConversionPromotionButtonView::UpdateBackgroundAndBorders() {
auto* cp = GetColorProvider();
if (!cp) {
return;
}

const auto bg_color =
cp->GetColor(GetState() == views::Button::STATE_NORMAL
? kColorSearchConversionButtonBackground
: kColorSearchConversionButtonBackgroundHovered);
const auto stroke_color = cp->GetColor(kColorSearchConversionButtonBorder);
SetBackground(views::CreateBackgroundFromPainter(
views::Painter::CreateRoundRectWith1PxBorderPainter(
bg_color, stroke_color, GetCornerRadius(), SkBlendMode::kSrcOver,
true /* antialias */, true /* should_border_scale */)));
}

void BraveSearchConversionPromotionButtonView::AddChildViews() {
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
AddChildView(std::make_unique<views::ImageView>())
->SetImage(
*rb.GetImageSkiaNamed(IDR_BRAVE_SEARCH_CONVERSION_BUTTON_DDG_ICON));

AddChildView(
std::make_unique<views::ImageView>(ui::ImageModel::FromVectorIcon(
kLeoCaratLastIcon, kColorSearchConversionButtonCaratRight, 14)));
AddChildView(std::make_unique<views::ImageView>(
ui::ImageModel::FromImageSkia(*rb.GetImageSkiaNamed(
IDR_BRAVE_SEARCH_CONVERSION_BUTTON_BRAVE_SEARCH_ICON))));

auto title_font_list =
views::Label::GetDefaultFontList()
.DeriveWithWeight(gfx::Font::Weight::SEMIBOLD)
.DeriveWithStyle(gfx::Font::NORMAL)
.DeriveWithHeightUpperBound(18)
.DeriveWithSizeDelta(
12 - views::Label::GetDefaultFontList().GetFontSize());
views::Label::CustomFont custom_font{title_font_list};
auto* button_label = AddChildView(std::make_unique<views::Label>(
l10n_util::GetStringUTF16(IDS_BRAVE_SEARCH_CONVERSION_BUTTON_LABEL),
custom_font));
button_label->SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT);
button_label->SetVerticalAlignment(gfx::VerticalAlignment::ALIGN_MIDDLE);
button_label->SetEnabledColorId(kColorSearchConversionButtonText);
button_label->SetBackgroundColor(SK_ColorTRANSPARENT);

auto set_image = [](views::ImageButton* close_button,
views::Button::ButtonState state, int color_id) {
close_button->SetImageModel(state, ui::ImageModel::FromVectorIcon(
kLeoCloseCircleIcon, color_id, 16));
};
auto* close_button = AddChildView(std::make_unique<views::ImageButton>());
set_image(close_button, views::Button::STATE_NORMAL,
kColorSearchConversionButtonCloseButton);
set_image(close_button, views::Button::STATE_HOVERED,
kColorSearchConversionButtonCloseButtonHovered);
close_button->SetTooltipText(l10n_util::GetStringUTF16(
IDS_BRAVE_SEARCH_CONVERSION_CLOSE_BUTTON_TOOLTIP));
}

void BraveSearchConversionPromotionButtonView::Update() {
UpdateBackgroundAndBorders();
UpdateShadow();
}

void BraveSearchConversionPromotionButtonView::SetupShadow() {
auto* cp = GetColorProvider();
if (!cp) {
return;
}

ViewShadow::ShadowParameters shadow_config1{
.offset_x = 0,
.offset_y = 1,
.blur_radius = 0,
.shadow_color = cp->GetColor(kColorSearchConversionButtonShadow1)};

const int radius = GetCornerRadius();
ViewShadow::ShadowParameters shadow_config2{
.offset_x = 0,
.offset_y = 1,
.blur_radius = radius,
.shadow_color = cp->GetColor(kColorSearchConversionButtonShadow2)};

shadow1_ = std::make_unique<ViewShadow>(this, radius, shadow_config1);
shadow2_ = std::make_unique<ViewShadow>(this, radius, shadow_config2);
}

void BraveSearchConversionPromotionButtonView::UpdateShadow() {
if (!shadow1_ || !shadow2_) {
return;
}

const bool is_hovered = GetState() == views::Button::STATE_HOVERED;
shadow1_->SetVisible(is_hovered);
shadow2_->SetVisible(is_hovered);
}

BEGIN_METADATA(BraveSearchConversionPromotionButtonView)
END_METADATA
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* Copyright (c) 2024 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_UI_VIEWS_LOCATION_BAR_BRAVE_SEARCH_CONVERSION_PROMOTION_BUTTON_VIEW_H_
#define BRAVE_BROWSER_UI_VIEWS_LOCATION_BAR_BRAVE_SEARCH_CONVERSION_PROMOTION_BUTTON_VIEW_H_

#include <memory>

#include "base/memory/raw_ptr.h"
#include "brave/browser/ui/views/view_shadow.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/views/controls/button/button.h"

class ViewShadow;

class BraveSearchConversionPromotionButtonView : public views::Button {
METADATA_HEADER(BraveSearchConversionPromotionButtonView, views::Button)

public:
BraveSearchConversionPromotionButtonView();
~BraveSearchConversionPromotionButtonView() override;

private:
// views::Button overrides:
void StateChanged(views::Button::ButtonState old_state) override;
void OnThemeChanged() override;

void Update();

float GetCornerRadius() const;
void UpdateBackgroundAndBorders();
void AddChildViews();
void SetupShadow();
void UpdateShadow();

std::unique_ptr<ViewShadow> shadow1_;
std::unique_ptr<ViewShadow> shadow2_;
};

#endif // BRAVE_BROWSER_UI_VIEWS_LOCATION_BAR_BRAVE_SEARCH_CONVERSION_PROMOTION_BUTTON_VIEW_H_
Loading

0 comments on commit 0a95238

Please sign in to comment.