Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

**DONT MERGE** [BraveNews] Refactor brave news's button and dialog and fix dialog not getting closed while clicking that button. #24274

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,10 @@ source_set("ui") {
"views/brave_help_bubble/brave_help_bubble_delegate_view.h",
"views/brave_help_bubble/brave_help_bubble_host_view.cc",
"views/brave_help_bubble/brave_help_bubble_host_view.h",
"views/brave_news/brave_news_action_icon_view.cc",
"views/brave_news/brave_news_action_icon_view.h",
"views/brave_news/brave_news_bubble_controller.cc",
"views/brave_news/brave_news_bubble_controller.h",
"views/frame/vertical_tab_strip_region_view.cc",
"views/frame/vertical_tab_strip_region_view.h",
"views/frame/vertical_tab_strip_root_view.cc",
Expand All @@ -1058,8 +1062,6 @@ 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_news_location_view.cc",
"views/location_bar/brave_news_location_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
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// 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 http://mozilla.org/MPL/2.0/.
// You can obtain one at https://mozilla.org/MPL/2.0/.

#include "brave/browser/ui/views/location_bar/brave_news_location_view.h"
#include "brave/browser/ui/views/brave_news/brave_news_action_icon_view.h"

#include <memory>
#include <utility>
Expand All @@ -12,6 +12,7 @@
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "brave/browser/brave_news/brave_news_tab_helper.h"
#include "brave/browser/ui/views/brave_news/brave_news_bubble_controller.h"
#include "brave/browser/ui/views/brave_news/brave_news_bubble_view.h"
#include "brave/components/brave_news/common/pref_names.h"
#include "brave/components/vector_icons/vector_icons.h"
Expand All @@ -33,7 +34,7 @@ constexpr SkColor kSubscribedDarkColor = SkColorSetRGB(115, 122, 222);

} // namespace

BraveNewsLocationView::BraveNewsLocationView(
BraveNewsActionIconView::BraveNewsActionIconView(
Profile* profile,
IconLabelBubbleView::Delegate* icon_label_bubble_delegate,
PageActionIconView::Delegate* page_action_icon_delegate)
Expand All @@ -46,26 +47,22 @@ BraveNewsLocationView::BraveNewsLocationView(

should_show_.Init(brave_news::prefs::kShouldShowToolbarButton,
profile->GetPrefs(),
base::BindRepeating(&BraveNewsLocationView::UpdateImpl,
base::BindRepeating(&BraveNewsActionIconView::UpdateImpl,
base::Unretained(this)));
opted_in_.Init(brave_news::prefs::kBraveNewsOptedIn, profile->GetPrefs(),
base::BindRepeating(&BraveNewsLocationView::UpdateImpl,
base::BindRepeating(&BraveNewsActionIconView::UpdateImpl,
base::Unretained(this)));
news_enabled_.Init(brave_news::prefs::kNewTabPageShowToday,
profile->GetPrefs(),
base::BindRepeating(&BraveNewsLocationView::UpdateImpl,
base::BindRepeating(&BraveNewsActionIconView::UpdateImpl,
base::Unretained(this)));

Update();
}

BraveNewsLocationView::~BraveNewsLocationView() = default;
BraveNewsActionIconView::~BraveNewsActionIconView() = default;

views::BubbleDialogDelegate* BraveNewsLocationView::GetBubble() const {
return bubble_view_;
}

void BraveNewsLocationView::UpdateImpl() {
void BraveNewsActionIconView::UpdateImpl() {
auto* contents = GetWebContents();
BraveNewsTabHelper* tab_helper =
contents ? BraveNewsTabHelper::FromWebContents(contents) : nullptr;
Expand Down Expand Up @@ -114,30 +111,30 @@ void BraveNewsLocationView::UpdateImpl() {
SetVisible(is_visible);
}

void BraveNewsLocationView::WebContentsDestroyed() {
void BraveNewsActionIconView::WebContentsDestroyed() {
page_feeds_observer_.Reset();
Observe(nullptr);
}

const gfx::VectorIcon& BraveNewsLocationView::GetVectorIcon() const {
const gfx::VectorIcon& BraveNewsActionIconView::GetVectorIcon() const {
return kLeoRssIcon;
}

std::u16string BraveNewsLocationView::GetTextForTooltipAndAccessibleName()
std::u16string BraveNewsActionIconView::GetTextForTooltipAndAccessibleName()
const {
return l10n_util::GetStringUTF16(IDS_BRAVE_NEWS_ACTION_VIEW_TOOLTIP);
}

bool BraveNewsLocationView::ShouldShowLabel() const {
bool BraveNewsActionIconView::ShouldShowLabel() const {
return false;
}

void BraveNewsLocationView::OnAvailableFeedsChanged(
void BraveNewsActionIconView::OnAvailableFeedsChanged(
const std::vector<GURL>& feeds) {
Update();
}

void BraveNewsLocationView::OnThemeChanged() {
void BraveNewsActionIconView::OnThemeChanged() {
bool subscribed = false;
if (auto* contents = GetWebContents()) {
subscribed = BraveNewsTabHelper::FromWebContents(contents)->IsSubscribed();
Expand All @@ -146,27 +143,12 @@ void BraveNewsLocationView::OnThemeChanged() {
PageActionIconView::OnThemeChanged();
}

void BraveNewsLocationView::OnExecuting(
void BraveNewsActionIconView::OnExecuting(
PageActionIconView::ExecuteSource execute_source) {
// If the bubble is already open, do nothing.
if (IsBubbleShowing()) {
return;
}

auto* contents = GetWebContents();
if (!contents) {
return;
}

bubble_view_ = new BraveNewsBubbleView(this, contents);
bubble_view_->SetCloseCallback(base::BindOnce(
&BraveNewsLocationView::OnBubbleClosed, base::Unretained(this)));
auto* bubble_widget =
views::BubbleDialogDelegateView::CreateBubble(bubble_view_);
bubble_widget->Show();
ShowBraveNewsBubble();
}

void BraveNewsLocationView::UpdateIconColor(bool subscribed) {
void BraveNewsActionIconView::UpdateIconColor(bool subscribed) {
SkColor icon_color;
if (subscribed) {
auto is_dark = GetNativeTheme()->GetPreferredColorScheme() ==
Expand All @@ -178,9 +160,28 @@ void BraveNewsLocationView::UpdateIconColor(bool subscribed) {
SetIconColor(icon_color);
}

void BraveNewsLocationView::OnBubbleClosed() {
bubble_view_ = nullptr;
brave_news::BraveNewsBubbleController* BraveNewsActionIconView::GetController()
const {
auto* web_contents = GetWebContents();
return web_contents ? brave_news::BraveNewsBubbleController::
CreateOrGetFromWebContents(web_contents)
: nullptr;
}

views::BubbleDialogDelegate* BraveNewsActionIconView::GetBubble() const {
auto* controller = GetController();
return controller ? controller->GetBubble() : nullptr;
}

void BraveNewsActionIconView::ShowBraveNewsBubble() {
if (auto* controller = GetController()) {
controller->ShowBubble(AsWeakPtr());
}
}

base::WeakPtr<BraveNewsActionIconView> BraveNewsActionIconView::AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}

BEGIN_METADATA(BraveNewsLocationView)
BEGIN_METADATA(BraveNewsActionIconView)
END_METADATA
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// 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 http://mozilla.org/MPL/2.0/.
// You can obtain one at https://mozilla.org/MPL/2.0/.

#ifndef BRAVE_BROWSER_UI_VIEWS_LOCATION_BAR_BRAVE_NEWS_LOCATION_VIEW_H_
#define BRAVE_BROWSER_UI_VIEWS_LOCATION_BAR_BRAVE_NEWS_LOCATION_VIEW_H_
#ifndef BRAVE_BROWSER_UI_VIEWS_BRAVE_NEWS_BRAVE_NEWS_ACTION_ICON_VIEW_H_
#define BRAVE_BROWSER_UI_VIEWS_BRAVE_NEWS_BRAVE_NEWS_ACTION_ICON_VIEW_H_

#include <string>
#include <vector>
Expand All @@ -18,22 +18,27 @@
#include "ui/views/view.h"

class Profile;
class BraveNewsBubbleView;

namespace brave_news {
class BraveNewsBubbleController;
}

// LocationBar action for Brave News which shows a bubble allowing the user to
// manage feed subscriptions for the current Tab
class BraveNewsLocationView : public PageActionIconView,
public BraveNewsTabHelper::PageFeedsObserver,
public content::WebContentsObserver {
METADATA_HEADER(BraveNewsLocationView, PageActionIconView)
class BraveNewsActionIconView : public PageActionIconView,
public BraveNewsTabHelper::PageFeedsObserver,
public content::WebContentsObserver {
METADATA_HEADER(BraveNewsActionIconView, PageActionIconView)
public:
BraveNewsLocationView(
BraveNewsActionIconView(
Profile* profile,
IconLabelBubbleView::Delegate* icon_label_bubble_delegate,
PageActionIconView::Delegate* page_action_icon_delegate);
BraveNewsLocationView(const BraveNewsLocationView&) = delete;
BraveNewsLocationView& operator=(const BraveNewsLocationView&) = delete;
~BraveNewsLocationView() override;
BraveNewsActionIconView(const BraveNewsActionIconView&) = delete;
BraveNewsActionIconView& operator=(const BraveNewsActionIconView&) = delete;
~BraveNewsActionIconView() override;

base::WeakPtr<BraveNewsActionIconView> AsWeakPtr();

// PageActionIconView:
views::BubbleDialogDelegate* GetBubble() const override;
Expand All @@ -51,21 +56,24 @@ class BraveNewsLocationView : public PageActionIconView,
void WebContentsDestroyed() override;

protected:
brave_news::BraveNewsBubbleController* GetController() const;

// PageActionIconView:
void OnExecuting(PageActionIconView::ExecuteSource execute_source) override;
const gfx::VectorIcon& GetVectorIcon() const override;

private:
void UpdateIconColor(bool subscribed);
void OnBubbleClosed();
void ShowBraveNewsBubble();

base::ScopedObservation<BraveNewsTabHelper,
BraveNewsTabHelper::PageFeedsObserver>
page_feeds_observer_{this};
BooleanPrefMember should_show_;
BooleanPrefMember opted_in_;
BooleanPrefMember news_enabled_;
raw_ptr<BraveNewsBubbleView> bubble_view_ = nullptr;

base::WeakPtrFactory<BraveNewsActionIconView> weak_ptr_factory_{this};
};

#endif // BRAVE_BROWSER_UI_VIEWS_LOCATION_BAR_BRAVE_NEWS_LOCATION_VIEW_H_
#endif // BRAVE_BROWSER_UI_VIEWS_BRAVE_NEWS_BRAVE_NEWS_ACTION_ICON_VIEW_H_
60 changes: 60 additions & 0 deletions browser/ui/views/brave_news/brave_news_bubble_controller.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* 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/brave_news/brave_news_bubble_controller.h"

#include <vector>

#include "base/memory/ptr_util.h"
#include "brave/browser/ui/views/brave_news/brave_news_action_icon_view.h"
#include "brave/browser/ui/views/brave_news/brave_news_bubble_view.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"

namespace brave_news {
// static
BraveNewsBubbleController*
BraveNewsBubbleController::CreateOrGetFromWebContents(
content::WebContents* web_contents) {
CHECK(web_contents);
BraveNewsBubbleController::CreateForWebContents(web_contents);
return BraveNewsBubbleController::FromWebContents(web_contents);
}

BraveNewsBubbleController::~BraveNewsBubbleController() = default;

void BraveNewsBubbleController::ShowBubble(
base::WeakPtr<BraveNewsActionIconView> anchor_view) {
if (!anchor_view) {
return;
}

bubble_ = new BraveNewsBubbleView(anchor_view.get(), web_contents_);
views::BubbleDialogDelegateView::CreateBubble(
base::WrapUnique(
static_cast<views::BubbleDialogDelegateView*>(bubble_.get())))
->Show();
}

BraveNewsBubbleView* BraveNewsBubbleController::GetBubble() {
return bubble_;
}

void BraveNewsBubbleController::OnBubbleClosed() {
bubble_ = nullptr;
}

base::WeakPtr<BraveNewsBubbleController>
BraveNewsBubbleController::AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}

BraveNewsBubbleController::BraveNewsBubbleController(
content::WebContents* web_contents)
: content::WebContentsUserData<BraveNewsBubbleController>(*web_contents),
web_contents_(web_contents) {}

WEB_CONTENTS_USER_DATA_KEY_IMPL(BraveNewsBubbleController);

} // namespace brave_news
49 changes: 49 additions & 0 deletions browser/ui/views/brave_news/brave_news_bubble_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* 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_BRAVE_NEWS_BRAVE_NEWS_BUBBLE_CONTROLLER_H_
#define BRAVE_BROWSER_UI_VIEWS_BRAVE_NEWS_BRAVE_NEWS_BUBBLE_CONTROLLER_H_

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "content/public/browser/web_contents_user_data.h"

namespace content {
class WebContents;
}

class BraveNewsBubbleView;
class BraveNewsActionIconView;

namespace brave_news {

class BraveNewsBubbleController
: public content::WebContentsUserData<BraveNewsBubbleController> {
public:
static BraveNewsBubbleController* CreateOrGetFromWebContents(
content::WebContents* web_contents);

~BraveNewsBubbleController() override;

void ShowBubble(base::WeakPtr<BraveNewsActionIconView> anchor_view);
BraveNewsBubbleView* GetBubble();
void OnBubbleClosed();
base::WeakPtr<BraveNewsBubbleController> AsWeakPtr();

private:
friend class content::WebContentsUserData<BraveNewsBubbleController>;
WEB_CONTENTS_USER_DATA_KEY_DECL();

explicit BraveNewsBubbleController(content::WebContents* web_contents);

raw_ptr<BraveNewsBubbleView> bubble_ = nullptr;
raw_ptr<content::WebContents> web_contents_ = nullptr;

base::WeakPtrFactory<BraveNewsBubbleController> weak_ptr_factory_{this};
};

} // namespace brave_news

#endif // BRAVE_BROWSER_UI_VIEWS_BRAVE_NEWS_BRAVE_NEWS_BUBBLE_CONTROLLER_H_
Loading
Loading