Skip to content

Commit

Permalink
Toolbar SidePanel button only opens items added on the sidebar, and h…
Browse files Browse the repository at this point in the history
…ides if none are added
  • Loading branch information
petemill committed Aug 19, 2022
1 parent 54599e7 commit fd2a6b3
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 2 deletions.
23 changes: 23 additions & 0 deletions browser/ui/views/sidebar/sidebar_container_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/side_panel/side_panel_entry.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/native_web_keyboard_event.h"
#include "content/public/browser/web_contents.h"
Expand Down Expand Up @@ -115,6 +116,7 @@ void SidebarContainerView::Init() {
AddChildViews();
// Hide by default. Visibility will be controlled by show options later.
DoHideSidebar(false);
UpdateToolbarButtonVisibility();
}

void SidebarContainerView::SetSidebarShowOption(
Expand Down Expand Up @@ -278,6 +280,16 @@ void SidebarContainerView::OnActiveIndexChanged(int old_index, int new_index) {
InvalidateLayout();
}

void SidebarContainerView::OnItemAdded(const sidebar::SidebarItem& item,
int index,
bool user_gesture) {
UpdateToolbarButtonVisibility();
}

void SidebarContainerView::OnItemRemoved(int index) {
UpdateToolbarButtonVisibility();
}

SidebarShowOptionsEventDetectWidget*
SidebarContainerView::GetEventDetectWidget() {
if (!show_options_widget_) {
Expand Down Expand Up @@ -322,6 +334,17 @@ void SidebarContainerView::DoHideSidebar(bool show_event_detect_widget) {
InvalidateLayout();
}

void SidebarContainerView::UpdateToolbarButtonVisibility() {
// Coordinate sidebar toolbar button visibility based on
// whether there are any sibar items with a sidepanel.
// This is similar to how chromium's side_panel_coordinator View
// also has some control on the toolbar button.
auto has_panel_item =
GetSidebarService(browser_)->GetDefaultPanelItem().has_value();
auto* browser_view = BrowserView::GetBrowserViewForBrowser(browser_);
browser_view->toolbar()->side_panel_button()->SetVisible(has_panel_item);
}

void SidebarContainerView::StartBrowserWindowEventMonitoring() {
if (browser_window_event_monitor_)
return;
Expand Down
5 changes: 5 additions & 0 deletions browser/ui/views/sidebar/sidebar_container_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ class SidebarContainerView
void ShowSidebar() override;

// sidebar::SidebarModel::Observer overrides:
void OnItemAdded(const sidebar::SidebarItem& item,
int index,
bool user_gesture) override;
void OnActiveIndexChanged(int old_index, int new_index) override;
void OnItemRemoved(int index) override;

// SidePanelEntryObserver:
void OnEntryShown(SidePanelEntry* entry) override;
Expand All @@ -103,6 +107,7 @@ class SidebarContainerView
void ShowSidebar(bool show_sidebar, bool show_event_detect_widget);
SidebarShowOptionsEventDetectWidget* GetEventDetectWidget();
bool ShouldForceShowSidebar() const;
void UpdateToolbarButtonVisibility();

// On some condition(ex, add item bubble is visible),
// sidebar should not be hidden even if mouse goes out from sidebar ui.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,39 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.

#include "brave/browser/ui/sidebar/sidebar_service_factory.h"
#include "brave/browser/ui/views/sidebar/sidebar_side_panel_utils.h"
#include "brave/components/sidebar/sidebar_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/views/side_panel/side_panel_entry.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace {

absl::optional<SidePanelEntry::Id> GetDefaultEntryId(Profile* profile) {
auto* service = sidebar::SidebarServiceFactory::GetForProfile(profile);
auto panel_item = service->GetDefaultPanelItem();
if (panel_item.has_value()) {
return SidePanelIdFromSideBarItem(panel_item.value());
}
return absl::nullopt;
}

} // namespace

// Brave has its own side panel navigation in the form of the SideBar, so
// hide the Chromium combobox-style header.
#define BRAVE_SIDE_PANEL_COORDINATOR_CREATE_HEADER header->SetVisible(false);

// Choose Brave's own default, and exclude items that user has removed
// from sidebar. If none are enabled, do nothing.
#define BRAVE_SIDE_PANEL_COORDINATOR_SHOW \
if (!entry_id.has_value()) { \
entry_id = GetDefaultEntryId(browser_view_->GetProfile()); \
if (!entry_id.has_value()) \
return; \
}

#include "src/chrome/browser/ui/views/side_panel/side_panel_coordinator.cc"
#undef BRAVE_SIDE_PANEL_COORDINATOR_CREATE_HEADER
#undef BRAVE_SIDE_PANEL_COORDINATOR_SHOW
20 changes: 20 additions & 0 deletions components/sidebar/sidebar_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"

Expand Down Expand Up @@ -351,6 +352,25 @@ SidebarService::ShowSidebarOption SidebarService::GetSidebarShowOption() const {
return static_cast<ShowSidebarOption>(prefs_->GetInteger(kSidebarShowOption));
}

absl::optional<SidebarItem> SidebarService::GetDefaultPanelItem() const {
static const base::NoDestructor preferred_item_types(
std::vector<SidebarItem::BuiltInItemType>{
SidebarItem::BuiltInItemType::kReadingList,
SidebarItem::BuiltInItemType::kBookmarks});
absl::optional<SidebarItem> default_item;
for (const auto& type : *preferred_item_types) {
auto found_item_iter = base::ranges::find_if(
items_,
[type](SidebarItem item) { return (item.built_in_item_type == type); });
if (found_item_iter != items_.end()) {
default_item = *found_item_iter;
DCHECK_EQ(default_item->open_in_panel, true);
break;
}
}
return default_item;
}

void SidebarService::SetSidebarShowOption(ShowSidebarOption show_options) {
DCHECK_NE(ShowSidebarOption::kShowOnClick, show_options);
prefs_->SetInteger(kSidebarShowOption, static_cast<int>(show_options));
Expand Down
3 changes: 3 additions & 0 deletions components/sidebar/sidebar_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "components/keyed_service/core/keyed_service.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/version_info/channel.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

class PrefRegistrySimple;
class PrefService;
Expand Down Expand Up @@ -63,6 +64,8 @@ class SidebarService : public KeyedService {
ShowSidebarOption GetSidebarShowOption() const;
void SetSidebarShowOption(ShowSidebarOption show_options);

absl::optional<SidebarItem> GetDefaultPanelItem() const;

SidebarService(const SidebarService&) = delete;
SidebarService& operator=(const SidebarService&) = delete;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
diff --git a/chrome/browser/ui/views/side_panel/side_panel_coordinator.cc b/chrome/browser/ui/views/side_panel/side_panel_coordinator.cc
index 1efe9453a6d271fdb118f39f95630296de13a95e..3353a5aba43a3e8f36766c75e3adc16bbb34f314 100644
index 1efe9453a6d271fdb118f39f95630296de13a95e..ef134beaf3bf407580e6fa84a3bbc82f6856984a 100644
--- a/chrome/browser/ui/views/side_panel/side_panel_coordinator.cc
+++ b/chrome/browser/ui/views/side_panel/side_panel_coordinator.cc
@@ -419,6 +419,7 @@ std::unique_ptr<views::View> SidePanelCoordinator::CreateHeader() {
@@ -180,6 +180,7 @@ SidePanelCoordinator::~SidePanelCoordinator() {
}

void SidePanelCoordinator::Show(absl::optional<SidePanelEntry::Id> entry_id) {
+ BRAVE_SIDE_PANEL_COORDINATOR_SHOW
if (!entry_id.has_value())
entry_id = GetLastActiveEntryId().value_or(kDefaultEntry);

@@ -419,6 +420,7 @@ std::unique_ptr<views::View> SidePanelCoordinator::CreateHeader() {
ChromeLayoutProvider::Get()->GetDistanceMetric(
ChromeDistanceMetric::DISTANCE_SIDE_PANEL_HEADER_VECTOR_ICON_SIZE)));

Expand Down

0 comments on commit fd2a6b3

Please sign in to comment.