Skip to content

Commit 370801d

Browse files
committed
Enable sidebar on right side option
fix brave/brave-browser#25332 fix brave/brave-browser#25756 Also custom bubble border is removed and used upstream's bubble border. With upstream's bubble border, we don't need to care about arrow flipping whenever changing sidebar's option.
1 parent 4f1896a commit 370801d

35 files changed

+312
-347
lines changed

app/theme/brave_theme_resources.grd

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<structure type="chrome_scaled_image" name="IDR_SIDEBAR_CRYPTO_WALLET_FOCUSED" file="brave/sidebar_crypto_wallet_focused.png" />
4848
<structure type="chrome_scaled_image" name="IDR_SIDEBAR_HISTORY_FOCUSED" file="brave/sidebar_history_focused.png" />
4949
<structure type="chrome_scaled_image" name="IDR_SIDEBAR_ITEM_HIGHLIGHT" file="brave/sidebar_item_highlight.png" />
50+
<structure type="chrome_scaled_image" name="IDR_SIDEBAR_ITEM_HIGHLIGHT_RIGHT" file="brave/sidebar_item_highlight_right.png" />
5051
<structure type="chrome_scaled_image" name="IDR_SIDEBAR_ITEM_ADD_FOCUSED" file="brave/sidebar_item_add_focused.png" />
5152
<structure type="chrome_scaled_image" name="IDR_SIDEBAR_SETTINGS_FOCUSED" file="brave/sidebar_settings_focused.png" />
5253
</if>
Loading
Loading

browser/brave_profile_prefs.cc

+3
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
425425

426426
#if BUILDFLAG(ENABLE_SIDEBAR)
427427
sidebar::SidebarService::RegisterProfilePrefs(registry, chrome::GetChannel());
428+
// Set false for showing sidebar on left by default.
429+
registry->SetDefaultPrefValue(prefs::kSidePanelHorizontalAlignment,
430+
base::Value(false));
428431
#endif
429432

430433
#if !BUILDFLAG(IS_ANDROID)

browser/ui/BUILD.gn

-4
Original file line numberDiff line numberDiff line change
@@ -442,12 +442,8 @@ source_set("ui") {
442442
sources += [
443443
"views/frame/brave_contents_layout_manager.cc",
444444
"views/frame/brave_contents_layout_manager.h",
445-
"views/sidebar/bubble_border_with_arrow.cc",
446-
"views/sidebar/bubble_border_with_arrow.h",
447445
"views/sidebar/sidebar_add_item_bubble_delegate_view.cc",
448446
"views/sidebar/sidebar_add_item_bubble_delegate_view.h",
449-
"views/sidebar/sidebar_bubble_background.cc",
450-
"views/sidebar/sidebar_bubble_background.h",
451447
"views/sidebar/sidebar_button_view.cc",
452448
"views/sidebar/sidebar_button_view.h",
453449
"views/sidebar/sidebar_container_view.cc",

browser/ui/sidebar/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ source_set("browser_tests") {
7777
"//chrome/browser",
7878
"//chrome/browser/profiles:profile",
7979
"//chrome/browser/ui",
80+
"//chrome/common:constants",
8081
"//chrome/test:test_support_ui",
82+
"//components/prefs",
8183
"//content/test:test_support",
8284
"//ui/events",
8385
"//ui/gfx",

browser/ui/sidebar/sidebar_browsertest.cc

+62-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
#include "brave/browser/ui/views/sidebar/sidebar_control_view.h"
1515
#include "brave/browser/ui/views/sidebar/sidebar_items_contents_view.h"
1616
#include "brave/browser/ui/views/sidebar/sidebar_items_scroll_view.h"
17+
#include "brave/browser/ui/views/tabs/features.h"
1718
#include "brave/components/sidebar/sidebar_service.h"
1819
#include "chrome/browser/ui/browser_finder.h"
1920
#include "chrome/browser/ui/tabs/tab_strip_model.h"
21+
#include "chrome/common/pref_names.h"
2022
#include "chrome/test/base/in_process_browser_test.h"
2123
#include "chrome/test/base/ui_test_utils.h"
24+
#include "components/prefs/pref_service.h"
2225
#include "content/public/test/browser_test.h"
2326
#include "testing/gmock/include/gmock/gmock-matchers.h"
2427
#include "testing/gmock/include/gmock/gmock.h"
@@ -46,18 +49,23 @@ class SidebarBrowserTest : public InProcessBrowserTest {
4649
SidebarService::ShowSidebarOption::kShowAlways);
4750
}
4851

49-
BraveBrowser* brave_browser() {
52+
BraveBrowser* brave_browser() const {
5053
return static_cast<BraveBrowser*>(browser());
5154
}
5255

53-
SidebarModel* model() { return controller()->model(); }
54-
TabStripModel* tab_model() { return browser()->tab_strip_model(); }
56+
SidebarModel* model() const { return controller()->model(); }
57+
TabStripModel* tab_model() const { return browser()->tab_strip_model(); }
5558

56-
SidebarController* controller() {
59+
SidebarController* controller() const {
5760
return brave_browser()->sidebar_controller();
5861
}
5962

60-
void SimulateSidebarItemClickAt(int index) {
63+
views::View* GetVerticalTabsContainer() const {
64+
auto* view = BrowserView::GetBrowserViewForBrowser(browser());
65+
return static_cast<BraveBrowserView*>(view)->vertical_tabs_container_;
66+
}
67+
68+
void SimulateSidebarItemClickAt(int index) const {
6169
auto* sidebar_container_view =
6270
static_cast<SidebarContainerView*>(controller()->sidebar());
6371
auto sidebar_control_view = sidebar_container_view->sidebar_control_view_;
@@ -73,6 +81,12 @@ class SidebarBrowserTest : public InProcessBrowserTest {
7381
ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0);
7482
sidebar_items_contents_view->OnItemPressed(item, event);
7583
}
84+
85+
bool IsSidebarUIOnLeft() const {
86+
auto* sidebar_container_view =
87+
static_cast<SidebarContainerView*>(controller()->sidebar());
88+
return sidebar_container_view->sidebar_on_left_;
89+
}
7690
};
7791

7892
IN_PROC_BROWSER_TEST_F(SidebarBrowserTest, BasicTest) {
@@ -207,4 +221,47 @@ IN_PROC_BROWSER_TEST_F(SidebarBrowserTest, IterateBuiltInWebTypeTest) {
207221
EXPECT_EQ(0, tab_model()->active_index());
208222
}
209223

224+
class SidebarBrowserTestWithVerticalTabs : public SidebarBrowserTest {
225+
public:
226+
SidebarBrowserTestWithVerticalTabs() {
227+
feature_list_.InitAndEnableFeature(tabs::features::kBraveVerticalTabs);
228+
}
229+
~SidebarBrowserTestWithVerticalTabs() override = default;
230+
231+
base::test::ScopedFeatureList feature_list_;
232+
};
233+
234+
IN_PROC_BROWSER_TEST_F(SidebarBrowserTestWithVerticalTabs,
235+
SidebarRightSideTest) {
236+
auto* prefs = browser()->profile()->GetPrefs();
237+
auto* browser_view = BrowserView::GetBrowserViewForBrowser(browser());
238+
239+
auto* vertical_tabs_container = GetVerticalTabsContainer();
240+
auto* sidebar_container =
241+
static_cast<SidebarContainerView*>(controller()->sidebar());
242+
243+
// Sidebar is on left.
244+
EXPECT_TRUE(IsSidebarUIOnLeft());
245+
246+
// Check vertical tabs is located right after sidebar.
247+
EXPECT_EQ(sidebar_container->bounds().right(), vertical_tabs_container->x());
248+
249+
// Changed to sidebar on right side.
250+
prefs->SetBoolean(prefs::kSidePanelHorizontalAlignment, true);
251+
EXPECT_FALSE(IsSidebarUIOnLeft());
252+
253+
// Check vertical tabs is located at first.
254+
EXPECT_EQ(0, vertical_tabs_container->x());
255+
256+
// Check sidebar is located on the right side.
257+
EXPECT_EQ(sidebar_container->bounds().right(), browser_view->width());
258+
259+
// Changed to sidebar on left side again.
260+
prefs->SetBoolean(prefs::kSidePanelHorizontalAlignment, false);
261+
EXPECT_TRUE(IsSidebarUIOnLeft());
262+
263+
// Check vertical tabs is located right after sidebar.
264+
EXPECT_EQ(sidebar_container->bounds().right(), vertical_tabs_container->x());
265+
}
266+
210267
} // namespace sidebar

browser/ui/views/frame/brave_browser_view.cc

+27
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "chrome/browser/ui/views/frame/tab_strip_region_view.h"
3535
#include "chrome/browser/ui/views/tabs/tab_search_button.h"
3636
#include "chrome/browser/ui/views/toolbar/browser_app_menu_button.h"
37+
#include "chrome/common/pref_names.h"
3738
#include "extensions/buildflags/buildflags.h"
3839
#include "third_party/abseil-cpp/absl/types/optional.h"
3940
#include "ui/events/event_observer.h"
@@ -204,6 +205,14 @@ BraveBrowserView::BraveBrowserView(std::unique_ptr<Browser> browser)
204205
// re-ordering. FindBarHost widgets uses this view as a kHostViewKey.
205206
// See the comments of BrowserView::find_bar_host_view().
206207
ReorderChildView(find_bar_host_view_, -1);
208+
209+
if (can_have_sidebar) {
210+
pref_change_registrar_.Add(
211+
prefs::kSidePanelHorizontalAlignment,
212+
base::BindRepeating(&BraveBrowserView::OnPreferenceChanged,
213+
base::Unretained(this)));
214+
UpdateSideBarHorizontalAlignment();
215+
}
207216
}
208217

209218
void BraveBrowserView::OnPreferenceChanged(const std::string& pref_name) {
@@ -212,6 +221,11 @@ void BraveBrowserView::OnPreferenceChanged(const std::string& pref_name) {
212221
return;
213222
}
214223

224+
if (pref_name == prefs::kSidePanelHorizontalAlignment) {
225+
UpdateSideBarHorizontalAlignment();
226+
return;
227+
}
228+
215229
#if BUILDFLAG(ENABLE_BRAVE_VPN)
216230
if (pref_name == brave_vpn::prefs::kBraveVPNShowButton) {
217231
vpn_panel_controller_.ResetBubbleManager();
@@ -220,6 +234,19 @@ void BraveBrowserView::OnPreferenceChanged(const std::string& pref_name) {
220234
#endif
221235
}
222236

237+
void BraveBrowserView::UpdateSideBarHorizontalAlignment() {
238+
DCHECK(sidebar_container_view_);
239+
240+
const bool on_left = !GetProfile()->GetPrefs()->GetBoolean(
241+
prefs::kSidePanelHorizontalAlignment);
242+
243+
sidebar_container_view_->SetSidebarOnLeft(on_left);
244+
static_cast<BraveContentsLayoutManager*>(GetContentsLayoutManager())
245+
->set_sidebar_on_left(on_left);
246+
247+
contents_container_->Layout();
248+
}
249+
223250
void BraveBrowserView::UpdateSearchTabsButtonState() {
224251
if (auto* button = tab_strip_region_view()->tab_search_button()) {
225252
if (button) {

browser/ui/views/frame/brave_browser_view.h

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ namespace content {
3333
class WebContents;
3434
} // namespace content
3535

36+
namespace sidebar {
37+
class SidebarBrowserTest;
38+
} // namespace sidebar
39+
3640
class BraveBrowser;
3741
class ContentsLayoutManager;
3842
class SidebarContainerView;
@@ -74,6 +78,7 @@ class BraveBrowserView : public BrowserView {
7478
private:
7579
class TabCyclingEventHandler;
7680
friend class WindowClosingConfirmBrowserTest;
81+
friend class sidebar::SidebarBrowserTest;
7782

7883
static void SetDownloadConfirmReturnForTesting(bool allow);
7984

@@ -97,6 +102,7 @@ class BraveBrowserView : public BrowserView {
97102
BraveBrowser* GetBraveBrowser() const;
98103

99104
sidebar::Sidebar* InitSidebar() override;
105+
void UpdateSideBarHorizontalAlignment();
100106

101107
raw_ptr<SidebarContainerView> sidebar_container_view_ = nullptr;
102108
raw_ptr<views::View> sidebar_host_view_ = nullptr;

browser/ui/views/frame/brave_contents_layout_manager.cc

+36-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "brave/browser/ui/views/frame/brave_contents_layout_manager.h"
77

8+
#include <vector>
9+
810
#include "ui/views/view.h"
911

1012
BraveContentsLayoutManager::BraveContentsLayoutManager(
@@ -15,37 +17,61 @@ BraveContentsLayoutManager::BraveContentsLayoutManager(
1517
: ContentsLayoutManager(devtools_view, contents_view),
1618
sidebar_container_view_(sidebar_container_view),
1719
vertical_tabs_container_(vertical_tabs_container) {
18-
DCHECK(sidebar_container_view_);
20+
DCHECK(sidebar_container_view_ || vertical_tabs_container_);
1921
}
2022

2123
BraveContentsLayoutManager::~BraveContentsLayoutManager() = default;
2224

2325
void BraveContentsLayoutManager::Layout(views::View* contents_container) {
2426
DCHECK(host_ == contents_container);
2527

26-
int height = contents_container->height();
27-
int width = contents_container->width();
28+
int contents_height = contents_container->height();
29+
int contents_width = contents_container->width();
30+
31+
std::vector<views::View*> left_side_candidate_views;
32+
std::vector<views::View*> right_side_candidate_views;
33+
if (sidebar_on_left_) {
34+
left_side_candidate_views.push_back(sidebar_container_view_);
35+
} else {
36+
right_side_candidate_views.push_back(sidebar_container_view_);
37+
}
38+
left_side_candidate_views.push_back(vertical_tabs_container_);
39+
40+
int taken_left_width = 0;
41+
for (auto* view : left_side_candidate_views) {
42+
if (!view || !view->GetVisible())
43+
continue;
44+
45+
auto width = view->GetPreferredSize().width();
46+
const gfx::Rect bounds(taken_left_width, 0, width, contents_height);
47+
view->SetBoundsRect(host_->GetMirroredRect(bounds));
48+
taken_left_width += width;
49+
}
50+
contents_width -= taken_left_width;
2851

29-
int taken_width = 0;
30-
for (const auto view : {sidebar_container_view_, vertical_tabs_container_}) {
52+
int taken_right_width = 0;
53+
int right_side_x = contents_container->GetLocalBounds().right();
54+
for (auto* view : right_side_candidate_views) {
3155
if (!view || !view->GetVisible())
3256
continue;
3357

3458
auto width = view->GetPreferredSize().width();
35-
const gfx::Rect bounds(taken_width, 0, width, height);
59+
right_side_x -= width;
60+
taken_right_width += width;
61+
const gfx::Rect bounds(right_side_x, 0, width, contents_height);
3662
view->SetBoundsRect(host_->GetMirroredRect(bounds));
37-
taken_width += width;
3863
}
64+
contents_width -= taken_right_width;
3965

40-
gfx::Size container_size(width - taken_width, height);
66+
gfx::Size container_size(contents_width, contents_height);
4167
gfx::Rect new_devtools_bounds;
4268
gfx::Rect new_contents_bounds;
4369

4470
ApplyDevToolsContentsResizingStrategy(
4571
strategy_, container_size, &new_devtools_bounds, &new_contents_bounds);
4672

47-
new_devtools_bounds.Offset(taken_width, 0);
48-
new_contents_bounds.Offset(taken_width, 0);
73+
new_devtools_bounds.Offset(taken_left_width, 0);
74+
new_contents_bounds.Offset(taken_left_width, 0);
4975
// DevTools cares about the specific position, so we have to compensate RTL
5076
// layout here.
5177
devtools_view_->SetBoundsRect(host_->GetMirroredRect(new_devtools_bounds));

browser/ui/views/frame/brave_contents_layout_manager.h

+5
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ class BraveContentsLayoutManager : public ContentsLayoutManager {
2020
delete;
2121
~BraveContentsLayoutManager() override;
2222

23+
void set_sidebar_on_left(bool sidebar_on_left) {
24+
sidebar_on_left_ = sidebar_on_left;
25+
}
26+
2327
// ContentsLayoutManager overrides:
2428
void Layout(views::View* contents_container) override;
2529

2630
private:
2731
raw_ptr<views::View> sidebar_container_view_ = nullptr;
2832
raw_ptr<views::View> vertical_tabs_container_ = nullptr;
33+
bool sidebar_on_left_ = true;
2934
};
3035

3136
#endif // BRAVE_BROWSER_UI_VIEWS_FRAME_BRAVE_CONTENTS_LAYOUT_MANAGER_H_

browser/ui/views/side_panel/brave_side_panel.cc

+8-4
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ BraveSidePanel::~BraveSidePanel() {
3030
RemoveObserver(this);
3131
}
3232

33-
void BraveSidePanel::SetHorizontalAlignment(HorizontalAlignment alignment) {}
33+
void BraveSidePanel::SetHorizontalAlignment(HorizontalAlignment alignment) {
34+
horizontal_alighment_ = alignment;
35+
UpdateBorder();
36+
}
3437

3538
BraveSidePanel::HorizontalAlignment BraveSidePanel::GetHorizontalAlignment() {
36-
return kAlignLeft;
39+
return horizontal_alighment_;
3740
}
3841

3942
bool BraveSidePanel::IsRightAligned() {
40-
return false;
43+
return horizontal_alighment_ == kAlignRight;
4144
}
4245

4346
void BraveSidePanel::UpdateVisibility() {
@@ -51,7 +54,8 @@ void BraveSidePanel::UpdateBorder() {
5154
constexpr int kBorderThickness = 1;
5255
// Negative top border so panel is flush with main tab content
5356
SetBorder(views::CreateSolidSidedBorder(
54-
gfx::Insets::TLBR(-1, 0, 0, kBorderThickness),
57+
gfx::Insets::TLBR(-1, IsRightAligned() ? kBorderThickness : 0, 0,
58+
IsRightAligned() ? 0 : kBorderThickness),
5559
color_provider->GetColor(kColorToolbarContentAreaSeparator)));
5660
}
5761
}

browser/ui/views/side_panel/brave_side_panel.h

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class BraveSidePanel : public views::View,
5353
// views::ViewObserver:
5454
void OnChildViewAdded(View* observed_view, View* child) override;
5555
void OnChildViewRemoved(View* observed_view, View* child) override;
56+
57+
HorizontalAlignment horizontal_alighment_ = kAlignLeft;
5658
};
5759

5860
#endif // BRAVE_BROWSER_UI_VIEWS_SIDE_PANEL_BRAVE_SIDE_PANEL_H_

0 commit comments

Comments
 (0)