From 42617bb0f9631081c14c487454a559d98e2c8a88 Mon Sep 17 00:00:00 2001 From: Simon Hong Date: Fri, 15 May 2026 12:20:01 +0900 Subject: [PATCH] [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 --- ...e_sync_account_deleted_infobar_delegate.cc | 14 ++--- ...ve_sync_account_deleted_infobar_delegate.h | 5 +- .../ui/views/infobars/brave_confirm_infobar.h | 32 +++++++----- .../brave_confirm_infobar_unittest.cc | 30 +++++++++++ .../brave_sync_account_deleted_infobar.cc | 51 +++++++------------ .../brave_sync_account_deleted_infobar.h | 15 +++--- .../ui/views/infobars/confirm_infobar.h | 18 ------- 7 files changed, 80 insertions(+), 85 deletions(-) delete mode 100644 chromium_src/chrome/browser/ui/views/infobars/confirm_infobar.h diff --git a/browser/infobars/brave_sync_account_deleted_infobar_delegate.cc b/browser/infobars/brave_sync_account_deleted_infobar_delegate.cc index 93a04ac60ab..3c4fb8c0bbe 100644 --- a/browser/infobars/brave_sync_account_deleted_infobar_delegate.cc +++ b/browser/infobars/brave_sync_account_deleted_infobar_delegate.cc @@ -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 infobar( - std::make_unique( - base::WrapUnique( - new BraveSyncAccountDeletedInfoBarDelegate(profile)))); - - // Show infobar - infobar_manager->AddInfoBar(std::move(infobar)); + infobar_manager->AddInfoBar(std::make_unique( + base::WrapUnique( + new BraveSyncAccountDeletedInfoBarDelegate(profile)))); } // Start class impl BraveSyncAccountDeletedInfoBarDelegate::BraveSyncAccountDeletedInfoBarDelegate( Profile* profile) - : ConfirmInfoBarDelegate(), profile_(profile) {} + : profile_(profile) {} BraveSyncAccountDeletedInfoBarDelegate:: ~BraveSyncAccountDeletedInfoBarDelegate() {} diff --git a/browser/infobars/brave_sync_account_deleted_infobar_delegate.h b/browser/infobars/brave_sync_account_deleted_infobar_delegate.h index a09405a51ca..45a49b80f64 100644 --- a/browser/infobars/brave_sync_account_deleted_infobar_delegate.h +++ b/browser/infobars/brave_sync_account_deleted_infobar_delegate.h @@ -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; diff --git a/browser/ui/views/infobars/brave_confirm_infobar.h b/browser/ui/views/infobars/brave_confirm_infobar.h index d881538f205..be2c15cda06 100644 --- a/browser/ui/views/infobars/brave_confirm_infobar.h +++ b/browser/ui/views/infobars/brave_confirm_infobar.h @@ -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 icon_ = nullptr; + raw_ptr label_ = nullptr; + raw_ptr ok_button_ = nullptr; + raw_ptr cancel_button_ = nullptr; + raw_ptr link_ = nullptr; + raw_ptr checkbox_ = nullptr; + raw_ptr 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 CreateLabel(const std::u16string& text) const; std::unique_ptr 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 icon_ = nullptr; - raw_ptr label_ = nullptr; - raw_ptr ok_button_ = nullptr; - raw_ptr cancel_button_ = nullptr; - raw_ptr link_ = nullptr; - raw_ptr checkbox_ = nullptr; - raw_ptr close_button_ = nullptr; - std::unique_ptr focus_tracker_; base::WeakPtrFactory weak_ptr_factory_{this}; diff --git a/browser/ui/views/infobars/brave_confirm_infobar_unittest.cc b/browser/ui/views/infobars/brave_confirm_infobar_unittest.cc index d2ed5b267c5..2575e2ce38d 100644 --- a/browser/ui/views/infobars/brave_confirm_infobar_unittest.cc +++ b/browser/ui/views/infobars/brave_confirm_infobar_unittest.cc @@ -8,6 +8,7 @@ #include #include +#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(); + 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(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()); +} diff --git a/browser/ui/views/infobars/brave_sync_account_deleted_infobar.cc b/browser/ui/views/infobars/brave_sync_account_deleted_infobar.cc index 2a79389c040..f8f86f57936 100644 --- a/browser/ui/views/infobars/brave_sync_account_deleted_infobar.cc +++ b/browser/ui/views/infobars/brave_sync_account_deleted_infobar.cc @@ -5,55 +5,38 @@ #include "brave/browser/ui/views/infobars/brave_sync_account_deleted_infobar.h" -#include -#include #include #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 delegate) - : ConfirmInfoBar(std::move(delegate)) { - DCHECK( - !(GetDelegate()->GetButtons() & ConfirmInfoBarDelegate::BUTTON_CANCEL)); + std::unique_ptr delegate) + : BraveConfirmInfoBar(std::move(delegate)) { + CHECK(!(GetDelegate()->GetButtons() & ConfirmInfoBarDelegate::BUTTON_CANCEL)); } -BraveSyncAccountDeletedInfoBar::~BraveSyncAccountDeletedInfoBar() {} +BraveSyncAccountDeletedInfoBar::~BraveSyncAccountDeletedInfoBar() = default; void BraveSyncAccountDeletedInfoBar::Layout(PassKey) { - LayoutSuperclass(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(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_))); } diff --git a/browser/ui/views/infobars/brave_sync_account_deleted_infobar.h b/browser/ui/views/infobars/brave_sync_account_deleted_infobar.h index f05c8b2fa19..9d2b5b9b649 100644 --- a/browser/ui/views/infobars/brave_sync_account_deleted_infobar.h +++ b/browser/ui/views/infobars/brave_sync_account_deleted_infobar.h @@ -8,16 +8,15 @@ #include -#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 delegate); + std::unique_ptr delegate); BraveSyncAccountDeletedInfoBar(const BraveSyncAccountDeletedInfoBar&) = delete; @@ -26,7 +25,7 @@ class BraveSyncAccountDeletedInfoBar : public ConfirmInfoBar { ~BraveSyncAccountDeletedInfoBar() override; - // InfoBarView: + // BraveConfirmInfoBar: void Layout(PassKey) override; }; diff --git a/chromium_src/chrome/browser/ui/views/infobars/confirm_infobar.h b/chromium_src/chrome/browser/ui/views/infobars/confirm_infobar.h deleted file mode 100644 index a522c28aa6f..00000000000 --- a/chromium_src/chrome/browser/ui/views/infobars/confirm_infobar.h +++ /dev/null @@ -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 // IWYU pragma: export - -#undef OkButtonPressed - -#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_INFOBARS_CONFIRM_INFOBAR_H_