Skip to content

Commit

Permalink
Updates Open in Tor view in location bar. (uplift to 1.68.x) (#24551)
Browse files Browse the repository at this point in the history
* Uplift of #24447 (squashed) to beta

* Merge pull request #24561 from brave/maxk-update-open-in-tor-button

Updates open in Tor button.

---------

Co-authored-by: Max Karolinskiy <[email protected]>
  • Loading branch information
brave-builds and mkarolin authored Jul 10, 2024
1 parent 4055640 commit c66581b
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 93 deletions.
6 changes: 3 additions & 3 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ Or change later at <ph name="SETTINGS_EXTENIONS_LINK">$2<ex>brave://settings/ext
<message name="IDS_PROFILES_OPEN_TOR_PROFILE_BUTTON" desc="Button in the avatar menu bubble view to open a Tor window.">
Open Private Window with Tor
</message>
<message name="IDS_LOCATION_BAR_OPEN_IN_TOR" desc="Button in location bar to open onion available site in tor window.">
Tor
<message name="IDS_LOCATION_BAR_OPEN_IN_TOR_TOOLTIP_TEXT" desc="The tooltip text for open available onion site in tor window.">
Open in Tor: <ph name="ADDRESS">$1</ph>
</message>
<message name="IDS_PROFILES_TOR_PROFILE_NAME" desc="Name of the Tor profile.">
Tor
Expand Down Expand Up @@ -357,7 +357,7 @@ Or change later at <ph name="SETTINGS_EXTENIONS_LINK">$2<ex>brave://settings/ext
.onion
</message>
<message name="IDS_LOCATION_BAR_ONION_AVAILABLE_TOOLTIP_TEXT" desc="The tooltip text for onion available button.">
Open using .onion address
Open using .onion address: <ph name="ADDRESS">$1</ph>
</message>
<message name="IDS_CONTENT_CONTEXT_FORCE_PASTE" desc="The name of the context menu item to paste without triggering onpaste handlers">
Force Paste
Expand Down
1 change: 0 additions & 1 deletion app/vector_icons/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ aggregate_vector_icons("brave_vector_icons") {
"brave_translate.icon",
"download_unlock.icon",
"open_in_ipfs.icon",
"open_in_tor.icon",
"sidebar_toolbar_button.icon",
"sidebar_toolbar_button_right.icon",
"web_discovery_infobar_close_button.icon",
Expand Down
36 changes: 0 additions & 36 deletions app/vector_icons/open_in_tor.icon

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "content/public/test/test_navigation_observer.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/event.h"
#include "ui/gfx/geometry/point.h"
Expand Down Expand Up @@ -127,9 +128,14 @@ class OnionLocationNavigationThrottleBrowserTest : public InProcessBrowserTest {
ASSERT_TRUE(onion_button);
EXPECT_TRUE(onion_button->GetVisible());
EXPECT_EQ(onion_button->GetText(),
brave_l10n::GetLocalizedResourceUTF16String(
(is_tor ? IDS_LOCATION_BAR_ONION_AVAILABLE
: IDS_LOCATION_BAR_OPEN_IN_TOR)));
is_tor ? brave_l10n::GetLocalizedResourceUTF16String(
IDS_LOCATION_BAR_ONION_AVAILABLE)
: u"");
EXPECT_TRUE(
onion_button->GetTooltipText().starts_with(l10n_util::GetStringFUTF16(
is_tor ? IDS_LOCATION_BAR_ONION_AVAILABLE_TOOLTIP_TEXT
: IDS_LOCATION_BAR_OPEN_IN_TOR_TOOLTIP_TEXT,
u"")));

ui_test_utils::BrowserChangeObserver browser_creation_observer(
nullptr, ui_test_utils::BrowserChangeObserver::ChangeType::kAdded);
Expand Down
21 changes: 14 additions & 7 deletions browser/ui/views/location_bar/brave_location_bar_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,12 @@ void BraveLocationBarView::OnChanged() {
LocationBarView::OnChanged();
}

std::vector<views::View*> BraveLocationBarView::GetTrailingViews() {
std::vector<views::View*> BraveLocationBarView::GetRightMostTrailingViews() {
std::vector<views::View*> views;
if (brave_news_location_view_) {
views.push_back(brave_news_location_view_);
}
#if BUILDFLAG(ENABLE_TOR)
if (onion_location_view_) {
views.push_back(onion_location_view_);
}
#endif

#if BUILDFLAG(ENABLE_IPFS)
if (ipfs_location_view_) {
views.push_back(ipfs_location_view_);
Expand All @@ -294,6 +290,16 @@ std::vector<views::View*> BraveLocationBarView::GetTrailingViews() {
return views;
}

std::vector<views::View*> BraveLocationBarView::GetLeftMostTrailingViews() {
std::vector<views::View*> views;
#if BUILDFLAG(ENABLE_TOR)
if (onion_location_view_) {
views.push_back(onion_location_view_);
}
#endif
return views;
}

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

Expand Down Expand Up @@ -358,7 +364,8 @@ void BraveLocationBarView::ChildVisibilityChanged(views::View* child) {
// the size changes when an icon is shown or hidden. The LocationBarView
// does not listen to ChildVisibilityChanged events so we must make we Layout
// and re-caculate trailing decorator positions when a child changes.
if (base::Contains(GetTrailingViews(), child)) {
if (base::Contains(GetLeftMostTrailingViews(), child) ||
base::Contains(GetRightMostTrailingViews(), child)) {
DeprecatedLayoutImmediately();
SchedulePaint();
}
Expand Down
5 changes: 4 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 @@ -73,7 +73,10 @@ class BraveLocationBarView : public LocationBarView {
IPFSLocationView* GetIPFSLocationView() { return ipfs_location_view_; }
#endif
// LocationBarView:
std::vector<views::View*> GetTrailingViews() override;
// Views that locates at right side of upstream's trailing views.
std::vector<views::View*> GetRightMostTrailingViews() override;
// Views that locates at left side of upstream's trailing views.
std::vector<views::View*> GetLeftMostTrailingViews() override;
void RefreshBackground() override;
ui::ImageModel GetLocationIcon(LocationIconView::Delegate::IconFetchedCallback
on_icon_fetched) const override;
Expand Down
59 changes: 34 additions & 25 deletions browser/ui/views/location_bar/onion_location_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "brave/app/vector_icons/vector_icons.h"
#include "brave/browser/tor/tor_profile_manager.h"
#include "brave/components/l10n/common/localization_util.h"
#include "brave/components/tor/onion_location_tab_helper.h"
#include "brave/components/vector_icons/vector_icons.h"
#include "brave/grit/brave_generated_resources.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_window.h"
Expand Down Expand Up @@ -43,10 +43,16 @@

namespace {

constexpr SkColor kOpenInTorBg = SkColorSetRGB(0x6a, 0x37, 0x85);
constexpr SkColor kIconColor = SkColorSetRGB(0xf0, 0xf2, 0xff);
constexpr SkColor kOpenInTorBg = SkColorSetRGB(0x8c, 0x30, 0xbb);
constexpr SkColor kIconColor = SK_ColorWHITE;
constexpr SkColor kTextColor = SK_ColorWHITE;
constexpr int kIconSize = 12;
constexpr int kFontHeight = 18;
constexpr int kIconSize = 18;
constexpr int kIconLabelSpacing = 4;
constexpr int kCornerRadius = 8;
constexpr auto kButtonInsets = gfx::Insets::TLBR(5, 4, 5, 4);
constexpr auto kButtonInsetsTor = gfx::Insets::TLBR(5, 4, 5, 8);
constexpr auto kViewInsets = gfx::Insets::VH(1, 3);

// Sets the focus and ink drop highlight path to match the background
// along with it's corner radius.
Expand All @@ -60,40 +66,40 @@ class HighlightPathGenerator : public views::HighlightPathGenerator {
SkPath GetHighlightPath(const views::View* view) override {
const gfx::Rect highlight_bounds = view->GetLocalBounds();
const SkRect rect = RectToSkRect(highlight_bounds);
const int corner_radius = view->height() / 2;
return SkPath().addRoundRect(rect, corner_radius, corner_radius);
return SkPath().addRoundRect(rect, kCornerRadius, kCornerRadius);
}
};

class OnionLocationButtonView : public views::LabelButton {
public:
explicit OnionLocationButtonView(Profile* profile)
: LabelButton(base::BindRepeating(&OnionLocationButtonView::ButtonPressed,
base::Unretained(this)),
brave_l10n::GetLocalizedResourceUTF16String(
IDS_LOCATION_BAR_OPEN_IN_TOR)),
base::Unretained(this))),
profile_(profile) {
if (profile->IsTor()) {
if (profile_->IsTor()) {
SetText(brave_l10n::GetLocalizedResourceUTF16String(
IDS_LOCATION_BAR_ONION_AVAILABLE));
SetTooltipText(brave_l10n::GetLocalizedResourceUTF16String(
IDS_LOCATION_BAR_ONION_AVAILABLE_TOOLTIP_TEXT));
}
// Render vector icon
const gfx::ImageSkia image =
gfx::CreateVectorIcon(kOpenInTorIcon, kIconSize, kIconColor);
SetImageModel(views::Button::STATE_NORMAL,
ui::ImageModel::FromImageSkia(image));
// Set style specifics

// Set text style specifics
SetEnabledTextColors(kTextColor);
SetTextColor(views::Button::STATE_DISABLED, kTextColor);
SetHorizontalAlignment(gfx::ALIGN_RIGHT);
SetImageLabelSpacing(6);
label()->SetFontList(
label()->font_list().DeriveWithHeightUpperBound(kFontHeight));

// Render vector icon
SetImageModel(views::Button::STATE_NORMAL,
ui::ImageModel::FromVectorIcon(kLeoProductTorIcon, kIconColor,
kIconSize));

// Show icon on the left
SetHorizontalAlignment(gfx::ALIGN_LEFT);
SetImageLabelSpacing(kIconLabelSpacing);

auto* ink_drop = views::InkDrop::Get(this);
ink_drop->SetMode(views::InkDropHost::InkDropMode::ON);
SetBorder(views::CreateEmptyBorder(
GetLayoutInsets(LOCATION_BAR_ICON_INTERIOR_PADDING)));
SetBorder(views::CreateEmptyBorder(profile_->IsTor() ? kButtonInsetsTor
: kButtonInsets));
SetHasInkDropActionOnClick(true);
ink_drop->SetVisibleOpacity(kToolbarInkDropVisibleOpacity);
UpdateBorder();
Expand All @@ -109,7 +115,10 @@ class OnionLocationButtonView : public views::LabelButton {

void SetOnionLocation(const GURL& location) {
onion_location_ = location;
SetTooltipText(base::UTF8ToUTF16(onion_location_.spec()));
SetTooltipText(l10n_util::GetStringFUTF16(
profile_->IsTor() ? IDS_LOCATION_BAR_ONION_AVAILABLE_TOOLTIP_TEXT
: IDS_LOCATION_BAR_OPEN_IN_TOR_TOOLTIP_TEXT,
base::UTF8ToUTF16(onion_location_.spec())));
}

private:
Expand All @@ -121,7 +130,7 @@ class OnionLocationButtonView : public views::LabelButton {

void UpdateBorder() {
SetBackground(
views::CreateRoundedRectBackground(kOpenInTorBg, height() / 2));
views::CreateRoundedRectBackground(kOpenInTorBg, kCornerRadius));
}

void ButtonPressed() {
Expand All @@ -135,7 +144,7 @@ class OnionLocationButtonView : public views::LabelButton {
} // namespace

OnionLocationView::OnionLocationView(Profile* profile) {
SetBorder(views::CreateEmptyBorder(gfx::Insets::VH(1, 3)));
SetBorder(views::CreateEmptyBorder(kViewInsets));
SetVisible(false);
// automatic layout
auto vertical_container_layout = std::make_unique<views::BoxLayout>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
text_left = 5; \
}

#define BRAVE_LAYOUT_TRAILING_DECORATIONS \
auto right_most = GetTrailingViews(); \
for (auto* item : base::Reversed(right_most)) { \
if (item->GetVisible()) \
trailing_decorations.AddDecoration(vertical_padding, location_height, \
false, 0, /*intra_item_padding=*/0, \
0, item); \
#define BRAVE_LAYOUT_RIGHT_MOST_TRAILING_DECORATIONS \
auto right_most_trailing_views = GetRightMostTrailingViews(); \
for (auto* item : base::Reversed(right_most_trailing_views)) { \
add_trailing_decoration(item, /*intra_item_padding=*/0); \
}

#define BRAVE_LAYOUT_LEFT_MOST_TRAILING_DECORATIONS \
auto left_most_trailing_views = GetLeftMostTrailingViews(); \
for (auto* item : base::Reversed(left_most_trailing_views)) { \
add_trailing_decoration(item, /*intra_item_padding=*/0); \
}

#define OmniboxViewViews BraveOmniboxViewViews
Expand All @@ -47,9 +50,14 @@
#undef PageActionIconContainerView
#undef ChromeOmniboxClient
#undef OmniboxViewViews
#undef BRAVE_LAYOUT_TRAILING_DECORATIONS
#undef BRAVE_LAYOUT_LEFT_MOST_TRAILING_DECORATIONS
#undef BRAVE_LAYOUT_RIGHT_MOST_TRAILING_DECORATIONS
#undef BRAVE_LAYOUT_LEADING_DECORATIONS

std::vector<views::View*> LocationBarView::GetTrailingViews() {
std::vector<views::View*> LocationBarView::GetRightMostTrailingViews() {
return std::vector<views::View*>();
}

std::vector<views::View*> LocationBarView::GetLeftMostTrailingViews() {
return std::vector<views::View*>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_
#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_

#define BRAVE_LOCATION_BAR_VIEW_H_ \
private: \
friend class BraveLocationBarView; \
\
public: \
virtual std::vector<views::View*> GetTrailingViews();
#define BRAVE_LOCATION_BAR_VIEW_H_ \
private: \
friend class BraveLocationBarView; \
\
public: \
virtual std::vector<views::View*> GetRightMostTrailingViews(); \
virtual std::vector<views::View*> GetLeftMostTrailingViews();

#define OnOmniboxBlurred virtual OnOmniboxBlurred
#define GetBorderRadius virtual GetBorderRadius
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/chrome/browser/ui/views/location_bar/location_bar_view.cc b/chrome/browser/ui/views/location_bar/location_bar_view.cc
index de89b448cc7b50c0711955e96b911efb89f61166..6ddc079b7479c73a9ff106d36ac33520eb488356 100644
index de89b448cc7b50c0711955e96b911efb89f61166..a689f4cae301dbfc586ff841173069cfd09bddc7 100644
--- a/chrome/browser/ui/views/location_bar/location_bar_view.cc
+++ b/chrome/browser/ui/views/location_bar/location_bar_view.cc
@@ -705,6 +705,7 @@ void LocationBarView::Layout(PassKey) {
Expand All @@ -14,7 +14,15 @@ index de89b448cc7b50c0711955e96b911efb89f61166..6ddc079b7479c73a9ff106d36ac33520
}
};

+ BRAVE_LAYOUT_TRAILING_DECORATIONS
+ BRAVE_LAYOUT_RIGHT_MOST_TRAILING_DECORATIONS
add_trailing_decoration(page_action_icon_container_,
/*intra_item_padding=*/0);
for (ContentSettingImageView* view : base::Reversed(content_setting_views_)) {
@@ -804,6 +806,7 @@ void LocationBarView::Layout(PassKey) {
}

add_trailing_decoration(clear_all_button_, /*intra_item_padding=*/0);
+ BRAVE_LAYOUT_LEFT_MOST_TRAILING_DECORATIONS

// Perform layout.
int entry_width = width();

0 comments on commit c66581b

Please sign in to comment.