Skip to content

Commit

Permalink
Merge pull request #15612 from brave/sidebar_on_mouseover_right_sidebar
Browse files Browse the repository at this point in the history
Fixed on mouseover doesn't work with right side sidebar
  • Loading branch information
simonhong authored Oct 24, 2022
2 parents 8a5ae36 + 47471b3 commit cfef9b5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
26 changes: 26 additions & 0 deletions browser/ui/sidebar/sidebar_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ class SidebarBrowserTest : public InProcessBrowserTest {
return static_cast<BraveBrowserView*>(view)->vertical_tab_strip_host_view_;
}

views::Widget* GetEventDetectWidget() {
auto* sidebar_container_view =
static_cast<SidebarContainerView*>(controller()->sidebar());
return sidebar_container_view->GetEventDetectWidget()->widget_.get();
}

void SimulateSidebarItemClickAt(int index) const {
auto* sidebar_container_view =
static_cast<SidebarContainerView*>(controller()->sidebar());
Expand Down Expand Up @@ -221,6 +227,26 @@ IN_PROC_BROWSER_TEST_F(SidebarBrowserTest, IterateBuiltInWebTypeTest) {
EXPECT_EQ(0, tab_model()->active_index());
}

IN_PROC_BROWSER_TEST_F(SidebarBrowserTest, EventDetectWidgetTest) {
auto* widget = GetEventDetectWidget();
auto* service = SidebarServiceFactory::GetForProfile(browser()->profile());
auto* browser_view = BrowserView::GetBrowserViewForBrowser(browser());
auto* contents_container = browser_view->contents_container();
auto* prefs = browser()->profile()->GetPrefs();

// Check widget is located on left side when sidebar on left.
prefs->SetBoolean(prefs::kSidePanelHorizontalAlignment, false);
service->SetSidebarShowOption(
SidebarService::ShowSidebarOption::kShowOnMouseOver);
EXPECT_EQ(contents_container->GetBoundsInScreen().x(),
widget->GetWindowBoundsInScreen().x());

// Check widget is located on right side when sidebar on right.
prefs->SetBoolean(prefs::kSidePanelHorizontalAlignment, true);
EXPECT_EQ(contents_container->GetBoundsInScreen().right(),
widget->GetWindowBoundsInScreen().right());
}

class SidebarBrowserTestWithVerticalTabs : public SidebarBrowserTest {
public:
SidebarBrowserTestWithVerticalTabs() {
Expand Down
15 changes: 11 additions & 4 deletions browser/ui/views/sidebar/sidebar_container_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,21 @@ void SidebarContainerView::Init() {
// Hide by default. Visibility will be controlled by show options later.
DoHideSidebar(false);
UpdateToolbarButtonVisibility();
SetSidebarOnLeft(sidebar_on_left_);
}

void SidebarContainerView::SetSidebarOnLeft(bool sidebar_on_left) {
if (sidebar_on_left_ == sidebar_on_left)
return;

sidebar_on_left_ = sidebar_on_left;
if (sidebar_control_view_) {
sidebar_control_view_->SetSidebarOnLeft(sidebar_on_left_);
}

if (!initialized_)
return;

DCHECK(sidebar_control_view_);
sidebar_control_view_->SetSidebarOnLeft(sidebar_on_left_);
GetEventDetectWidget()->SetSidebarOnLeft(sidebar_on_left_);

DCHECK(side_panel_);
side_panel_->SetHorizontalAlignment(
Expand Down Expand Up @@ -187,7 +195,6 @@ void SidebarContainerView::AddChildViews() {
// we want the controls first.
sidebar_control_view_ =
AddChildViewAt(std::make_unique<SidebarControlView>(this, browser_), 0);
sidebar_control_view_->SetSidebarOnLeft(sidebar_on_left_);
}

void SidebarContainerView::Layout() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,21 @@ SidebarShowOptionsEventDetectWidget::~SidebarShowOptionsEventDetectWidget() =

void SidebarShowOptionsEventDetectWidget::Show() {
DCHECK(widget_);
widget_->Show();
AdjustWidgetBounds();
widget_->Show();
}

void SidebarShowOptionsEventDetectWidget::Hide() {
DCHECK(widget_);
widget_->Hide();
}

void SidebarShowOptionsEventDetectWidget::SetSidebarOnLeft(
bool sidebar_on_left) {
sidebar_on_left_ = sidebar_on_left;
AdjustWidgetBounds();
}

std::unique_ptr<views::Widget>
SidebarShowOptionsEventDetectWidget::CreateWidget(Delegate* delegate) {
std::unique_ptr<views::Widget> widget(new views::Widget);
Expand All @@ -93,9 +99,21 @@ void SidebarShowOptionsEventDetectWidget::OnViewBoundsChanged(
}

void SidebarShowOptionsEventDetectWidget::AdjustWidgetBounds() {
auto rect = browser_view_->contents_container()->bounds();
// Convert contents container's rect into widget's coordinate
// to use it as a detect widget's bounds as detect widget is parented
// to browser widget.
auto rect = browser_view_->contents_container()->GetLocalBounds();
auto point = rect.origin();
views::View::ConvertPointToTarget(browser_view_->contents_container(),
browser_view_->GetWidget()->GetRootView(),
&point);
rect.set_origin(point);
constexpr int kWidgetNarrowWidth = 7;
if (!sidebar_on_left_) {
rect.set_x(rect.right() - kWidgetNarrowWidth);
}
rect.set_width(kWidgetNarrowWidth);

contents_view_->SetPreferredSize(rect.size());
widget_->SetBounds(rect);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ namespace views {
class Widget;
} // namespace views

namespace sidebar {
class SidebarBrowserTest;
} // namespace sidebar

class BraveBrowserView;

// Monitors mouse event to show sidebar when mouse is around the left or
// right side of browser window.
class SidebarShowOptionsEventDetectWidget : public views::ViewObserver,
public views::WidgetDelegate {
public:
Expand All @@ -40,16 +46,20 @@ class SidebarShowOptionsEventDetectWidget : public views::ViewObserver,

void Show();
void Hide();
void SetSidebarOnLeft(bool sidebar_on_left);

// views::ViewObserver overrides:
void OnViewBoundsChanged(views::View* observed_view) override;

private:
friend class sidebar::SidebarBrowserTest;

class ContentsView;

std::unique_ptr<views::Widget> CreateWidget(Delegate* delegate);
void AdjustWidgetBounds();

bool sidebar_on_left_ = true;
raw_ptr<BraveBrowserView> browser_view_ = nullptr;
raw_ptr<ContentsView> contents_view_ = nullptr;
raw_ptr<Delegate> delegate_ = nullptr;
Expand Down

0 comments on commit cfef9b5

Please sign in to comment.