[infobar] BraveConfirmInfoBar (4/N): refactor sync-account infobar (#36440)

BraveSyncAccountDeletedInfoBar layout is broken with InfobarRefresh.
As it's not compatible with ConfirmInfoBar, changed to subclass BraveConfirmInfoBar.

sync-account-deleted infobar: No regression w or w/o InfobarRefresh flag

Resolves - subtask of brave/brave-browser#48822
This commit is contained in:
Simon Hong
2026-05-15 12:20:01 +09:00
committed by GitHub
parent 956108c5cf
commit 42617bb0f9
7 changed files with 80 additions and 85 deletions
@@ -13,7 +13,6 @@
#include "brave/components/brave_sync/brave_sync_prefs.h"
#include "brave/components/constants/webui_url_constants.h"
#include "brave/grit/brave_generated_resources.h"
#include "chrome/browser/infobars/confirm_infobar_creator.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "components/infobars/content/content_infobar_manager.h"
@@ -46,20 +45,15 @@ void BraveSyncAccountDeletedInfoBarDelegate::Create(
return;
}
// Create custom confirm infobar
std::unique_ptr<infobars::InfoBar> infobar(
std::make_unique<BraveSyncAccountDeletedInfoBar>(
base::WrapUnique<ConfirmInfoBarDelegate>(
new BraveSyncAccountDeletedInfoBarDelegate(profile))));
// Show infobar
infobar_manager->AddInfoBar(std::move(infobar));
infobar_manager->AddInfoBar(std::make_unique<BraveSyncAccountDeletedInfoBar>(
base::WrapUnique<BraveConfirmInfoBarDelegate>(
new BraveSyncAccountDeletedInfoBarDelegate(profile))));
}
// Start class impl
BraveSyncAccountDeletedInfoBarDelegate::BraveSyncAccountDeletedInfoBarDelegate(
Profile* profile)
: ConfirmInfoBarDelegate(), profile_(profile) {}
: profile_(profile) {}
BraveSyncAccountDeletedInfoBarDelegate::
~BraveSyncAccountDeletedInfoBarDelegate() {}
@@ -8,7 +8,7 @@
#include "base/compiler_specific.h"
#include "base/memory/raw_ptr.h"
#include "components/infobars/core/confirm_infobar_delegate.h"
#include "brave/components/infobars/core/brave_confirm_infobar_delegate.h"
class Profile;
@@ -17,7 +17,8 @@ class WebContents;
}
// An infobar that is run with a string, "re-create account" link and a button.
class BraveSyncAccountDeletedInfoBarDelegate : public ConfirmInfoBarDelegate {
class BraveSyncAccountDeletedInfoBarDelegate
: public BraveConfirmInfoBarDelegate {
public:
BraveSyncAccountDeletedInfoBarDelegate(
const BraveSyncAccountDeletedInfoBarDelegate&) = delete;
@@ -78,6 +78,25 @@ class BraveConfirmInfoBar : public infobars::InfoBar, public views::View {
views::Checkbox* checkbox_for_testing() const { return checkbox_.get(); }
views::View* close_button_for_testing() const { return close_button_.get(); }
protected:
static void AssignWidths(Views* views, int available_width);
int GetStartX() const;
int GetEndX() const;
int OffsetY(views::View* view) const;
int NonLabelWidth() const;
// Subclasses (e.g. BraveSyncAccountDeletedInfoBar) override Layout() and
// reposition the views, so expose the children and the positioning
// helpers here.
raw_ptr<views::ImageView> icon_ = nullptr;
raw_ptr<views::Label> label_ = nullptr;
raw_ptr<views::MdTextButton> ok_button_ = nullptr;
raw_ptr<views::MdTextButton> cancel_button_ = nullptr;
raw_ptr<views::Link> link_ = nullptr;
raw_ptr<views::Checkbox> checkbox_ = nullptr;
raw_ptr<views::ImageButton> close_button_ = nullptr;
private:
// Tracks the previously focused external view so we can restore focus on
// dismissal; defined in the .cc.
@@ -92,31 +111,18 @@ class BraveConfirmInfoBar : public infobars::InfoBar, public views::View {
std::unique_ptr<views::Label> CreateLabel(const std::u16string& text) const;
std::unique_ptr<views::Link> CreateLink(const std::u16string& text);
void SetLabelDetails(views::Label* label) const;
static void AssignWidths(Views* views, int available_width);
static void AssignWidthsSorted(Views* views, int available_width);
int GetStartX() const;
int GetEndX() const;
int OffsetY(views::View* view) const;
gfx::Insets GetCloseButtonSpacing() const;
int GetElementSpacing() const;
void LinkClicked(const ui::Event& event);
// Brave-specific.
int NonLabelWidth() const;
void MaybeLayoutMultiLineLabelAndLink();
void OkButtonPressed();
void CancelButtonPressed();
void CloseButtonPressed();
void CheckboxPressed();
raw_ptr<views::ImageView> icon_ = nullptr;
raw_ptr<views::Label> label_ = nullptr;
raw_ptr<views::MdTextButton> ok_button_ = nullptr;
raw_ptr<views::MdTextButton> cancel_button_ = nullptr;
raw_ptr<views::Link> link_ = nullptr;
raw_ptr<views::Checkbox> checkbox_ = nullptr;
raw_ptr<views::ImageButton> close_button_ = nullptr;
std::unique_ptr<FocusTracker> focus_tracker_;
base::WeakPtrFactory<BraveConfirmInfoBar> weak_ptr_factory_{this};
@@ -8,6 +8,7 @@
#include <memory>
#include <utility>
#include "brave/browser/ui/views/infobars/brave_sync_account_deleted_infobar.h"
#include "brave/components/infobars/core/brave_confirm_infobar_delegate.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/test/views/chrome_views_test_base.h"
@@ -372,3 +373,32 @@ TEST_F(BraveConfirmInfoBarTest, CheckboxClickPropagatesToDelegate) {
ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0));
EXPECT_TRUE(delegate->checkbox_checked());
}
// BraveSyncAccountDeletedInfoBar overrides BraveConfirmInfoBar's Layout to
// produce "Text _link_ [ok_button]": link sits right after the label and
// the ok_button is pushed to the right edge.
TEST_F(BraveConfirmInfoBarTest, SyncAccountDeletedInfoBarLayout) {
auto delegate = std::make_unique<TestInfoBarDelegate>();
delegate->set_message_text(u"Your sync account was deleted.");
delegate->set_link_text(u"re-create");
delegate->set_buttons(ConfirmInfoBarDelegate::BUTTON_OK);
delegate->set_closeable(false);
auto infobar =
std::make_unique<BraveSyncAccountDeletedInfoBar>(std::move(delegate));
infobar->SetBounds(
0, 0, kTestInfoBarWidth,
ChromeLayoutProvider::Get()->GetDistanceMetric(DISTANCE_INFOBAR_HEIGHT));
views::test::RunScheduledLayout(infobar.get());
auto* label = infobar->label_for_testing();
auto* link = infobar->link_for_testing();
auto* ok = infobar->ok_button_for_testing();
ASSERT_TRUE(label);
ASSERT_TRUE(link);
ASSERT_TRUE(ok);
// Horizontal order: label < link < ok, left-to-right.
EXPECT_LE(label->bounds().right(), link->x());
EXPECT_LE(link->bounds().right(), ok->x());
}
@@ -5,55 +5,38 @@
#include "brave/browser/ui/views/infobars/brave_sync_account_deleted_infobar.h"
#include <algorithm>
#include <memory>
#include <utility>
#include "base/check.h"
#include "build/build_config.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/link.h"
#include "ui/views/view.h"
BraveSyncAccountDeletedInfoBar::BraveSyncAccountDeletedInfoBar(
std::unique_ptr<ConfirmInfoBarDelegate> delegate)
: ConfirmInfoBar(std::move(delegate)) {
DCHECK(
!(GetDelegate()->GetButtons() & ConfirmInfoBarDelegate::BUTTON_CANCEL));
std::unique_ptr<BraveConfirmInfoBarDelegate> delegate)
: BraveConfirmInfoBar(std::move(delegate)) {
CHECK(!(GetDelegate()->GetButtons() & ConfirmInfoBarDelegate::BUTTON_CANCEL));
}
BraveSyncAccountDeletedInfoBar::~BraveSyncAccountDeletedInfoBar() {}
BraveSyncAccountDeletedInfoBar::~BraveSyncAccountDeletedInfoBar() = default;
void BraveSyncAccountDeletedInfoBar::Layout(PassKey) {
LayoutSuperclass<InfoBarView>(this);
// Move the link to sit just to the right of the label and push the ok_button
// to the far right edge — producing "Text _link_ [ok_button]".
LayoutSuperclass<BraveConfirmInfoBar>(this);
if (ok_button_) {
ok_button_->SizeToPreferredSize();
}
CHECK(!cancel_button_);
CHECK(label_);
CHECK(link_);
CHECK(ok_button_);
int x = GetStartX();
Views views;
views.push_back(label_.get());
views.push_back(link_.get());
AssignWidths(&views, std::max(0, GetEndX() - x - NonLabelWidth()));
ChromeLayoutProvider* layout_provider = ChromeLayoutProvider::Get();
label_->SetPosition(gfx::Point(x, OffsetY(label_)));
if (!label_->GetText().empty()) {
x = label_->bounds().right() +
layout_provider->GetDistanceMetric(
views::DISTANCE_RELATED_LABEL_HORIZONTAL);
}
auto* layout_provider = ChromeLayoutProvider::Get();
const int x =
label_->bounds().right() + layout_provider->GetDistanceMetric(
views::DISTANCE_RELATED_LABEL_HORIZONTAL);
link_->SetPosition(gfx::Point(x, OffsetY(link_)));
DCHECK(!cancel_button_);
if (ok_button_) {
ok_button_->SetPosition(
gfx::Point(GetEndX() - ok_button_->width(), OffsetY(ok_button_)));
}
ok_button_->SetPosition(
gfx::Point(GetEndX() - ok_button_->width(), OffsetY(ok_button_)));
}
@@ -8,16 +8,15 @@
#include <memory>
#include "chrome/browser/ui/views/infobars/confirm_infobar.h"
#include "brave/browser/ui/views/infobars/brave_confirm_infobar.h"
// The customized ConfirmInfoBar:
// "Text _link_ [ok_button]"
// cancel_button is not supported
class BraveSyncAccountDeletedInfoBar : public ConfirmInfoBar {
// Customized BraveConfirmInfoBar:
// "Text _link_ [ok_button]"
// cancel_button is not supported.
class BraveSyncAccountDeletedInfoBar : public BraveConfirmInfoBar {
public:
explicit BraveSyncAccountDeletedInfoBar(
std::unique_ptr<ConfirmInfoBarDelegate> delegate);
std::unique_ptr<BraveConfirmInfoBarDelegate> delegate);
BraveSyncAccountDeletedInfoBar(const BraveSyncAccountDeletedInfoBar&) =
delete;
@@ -26,7 +25,7 @@ class BraveSyncAccountDeletedInfoBar : public ConfirmInfoBar {
~BraveSyncAccountDeletedInfoBar() override;
// InfoBarView:
// BraveConfirmInfoBar:
void Layout(PassKey) override;
};
@@ -1,18 +0,0 @@
/* Copyright (c) 2022 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_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_INFOBARS_CONFIRM_INFOBAR_H_
#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_INFOBARS_CONFIRM_INFOBAR_H_
// BraveSyncAccountDeletedInfoBar refers ConfirmInfoBar's private members.
#define OkButtonPressed(...) \
OkButtonPressed(__VA_ARGS__); \
friend class BraveSyncAccountDeletedInfoBar
#include <chrome/browser/ui/views/infobars/confirm_infobar.h> // IWYU pragma: export
#undef OkButtonPressed
#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_INFOBARS_CONFIRM_INFOBAR_H_