0

Use IsViewClass<T> rather than class name comparisons.

Reworking the metadata, and IsViewClass<T> is faster and generally more accurate.

Bug: N/A
Change-Id: I9dd6f02c34a4894225216a17d074d0120d2428f2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4902563
Commit-Queue: Allen Bauer <kylixrd@chromium.org>
Reviewed-by: Scott Violet <sky@chromium.org>
Reviewed-by: Ted Choc <tedchoc@chromium.org>
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Cr-Commit-Position: refs/heads/main@{#1204723}
This commit is contained in:
Allen Bauer
2023-10-03 17:18:29 +00:00
committed by Chromium LUCI CQ
parent db3e80a740
commit d836b83f59
18 changed files with 535 additions and 301 deletions

@ -48,6 +48,7 @@
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/base/dragdrop/drop_target_event.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/ui_base_types.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/compositor/animation_throughput_reporter.h"
@ -639,10 +640,6 @@ void AppListBubbleView::UpdateForNewSortingOrder(
std::move(done_closure));
}
const char* AppListBubbleView::GetClassName() const {
return "AppListBubbleView";
}
bool AppListBubbleView::AcceleratorPressed(const ui::Accelerator& accelerator) {
switch (accelerator.key_code()) {
case ui::VKEY_ESCAPE:
@ -871,4 +868,7 @@ void AppListBubbleView::MaybeFocusAndActivateSearchBox() {
search_box_view_->search_box()->RequestFocus();
}
BEGIN_METADATA(AppListBubbleView, views::View)
END_METADATA
} // namespace ash

@ -16,6 +16,7 @@
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/views/view.h"
namespace ash {
@ -42,6 +43,8 @@ class ASH_EXPORT AppListBubbleView : public views::View,
public SearchBoxViewDelegate,
public AppListFolderController {
public:
METADATA_HEADER(AppListBubbleView);
AppListBubbleView(AppListViewDelegate* view_delegate,
ApplicationDragAndDropHost* drag_and_drop_host);
AppListBubbleView(const AppListBubbleView&) = delete;
@ -96,7 +99,6 @@ class ASH_EXPORT AppListBubbleView : public views::View,
base::OnceClosure update_position_closure);
// views::View:
const char* GetClassName() const override;
bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
void Layout() override;
bool GetDropFormats(int* formats,

@ -35,6 +35,7 @@
#include "base/functional/callback.h"
#include "base/strings/string_util.h"
#include "ui/aura/window.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/views/border.h"
@ -104,14 +105,11 @@ PaginationModel* AppListMainView::GetAppsPaginationModel() {
->pagination_model();
}
const char* AppListMainView::GetClassName() const {
return "AppListMainView";
}
void AppListMainView::Layout() {
gfx::Rect rect = GetContentsBounds();
if (!rect.IsEmpty())
if (!rect.IsEmpty()) {
contents_view_->SetBoundsRect(rect);
}
}
void AppListMainView::QueryChanged(const std::u16string& trimmed_query,
@ -125,8 +123,9 @@ void AppListMainView::QueryChanged(const std::u16string& trimmed_query,
void AppListMainView::ActiveChanged(SearchBoxViewBase* sender) {
// Do not update views on closing.
if (app_list_view_->app_list_state() == AppListViewState::kClosed)
if (app_list_view_->app_list_state() == AppListViewState::kClosed) {
return;
}
if (search_box_view_->is_search_box_active()) {
// Show zero state suggestions when search box is activated with an empty
@ -189,4 +188,7 @@ void AppListMainView::CloseButtonPressed() {
search_box_view_->ClearSearch();
}
BEGIN_METADATA(AppListMainView, views::View)
END_METADATA
} // namespace ash

@ -11,6 +11,7 @@
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/timer/timer.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/views/view.h"
namespace ash {
@ -28,6 +29,8 @@ class SearchBoxViewBase;
class ASH_EXPORT AppListMainView : public views::View,
public SearchBoxViewDelegate {
public:
METADATA_HEADER(AppListMainView);
AppListMainView(AppListViewDelegate* delegate, AppListView* app_list_view);
AppListMainView(const AppListMainView&) = delete;
@ -50,7 +53,6 @@ class ASH_EXPORT AppListMainView : public views::View,
AppListViewDelegate* view_delegate() { return delegate_; }
// Overridden from views::View:
const char* GetClassName() const override;
void Layout() override;
private:

File diff suppressed because it is too large Load Diff

@ -7,6 +7,7 @@
#include "ash/frame/non_client_frame_view_ash.h"
#include "chromeos/ui/frame/caption_buttons/caption_button_model.h"
#include "chromeos/ui/frame/default_frame_header.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/caption_button_types.h"
#include "ui/views/window/frame_caption_button.h"
@ -79,8 +80,8 @@ void AssistantWebViewDelegateImpl::UpdateBackButtonVisibility(
auto* non_client_view = widget->non_client_view();
DCHECK_EQ(NonClientFrameViewAsh::kViewClassName,
non_client_view->frame_view()->GetClassName());
DCHECK(
views::IsViewClass<NonClientFrameViewAsh>(non_client_view->frame_view()));
auto* frame_view_ash =
static_cast<NonClientFrameViewAsh*>(non_client_view->frame_view());

@ -15,6 +15,7 @@
#include "ui/gfx/image/image_unittest_util.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget.h"
namespace ash {
@ -94,11 +95,8 @@ class BluetoothDeviceListItemMultipleBatteryViewTest : public AshTestBase {
}
void BatteryViewExistsAtIndex(int index) {
EXPECT_EQ(bluetooth_device_list_multiple_battery_item()
->children()
.at(index)
->GetClassName(),
BluetoothDeviceListItemBatteryView::kViewClassName);
EXPECT_TRUE(views::IsViewClass<BluetoothDeviceListItemBatteryView>(
bluetooth_device_list_multiple_battery_item()->children().at(index)));
}
private:

@ -2,11 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/network/network_detailed_network_view_impl.h"
#include "ash/system/network/network_detailed_network_view.h"
#include <memory>
#include "ash/system/network/network_detailed_network_view.h"
#include "ash/system/network/network_detailed_network_view_impl.h"
#include "ash/system/network/network_detailed_view.h"
#include "ash/system/network/network_list_mobile_header_view.h"
#include "ash/system/network/network_list_network_item_view.h"
@ -217,15 +217,12 @@ TEST_F(NetworkDetailedNetworkViewTest, ViewsAreCreated) {
NetworkListNetworkItemView* network_list_item =
AddNetworkListItem(NetworkType::kWiFi);
ASSERT_NE(nullptr, network_list_item);
EXPECT_STREQ("NetworkListNetworkItemView", network_list_item->GetClassName());
NetworkListWifiHeaderView* wifi_section = AddWifiSectionHeader();
ASSERT_NE(nullptr, wifi_section);
EXPECT_STREQ("NetworkListWifiHeaderView", wifi_section->GetClassName());
NetworkListMobileHeaderView* mobile_section = AddMobileSectionHeader();
ASSERT_NE(nullptr, mobile_section);
EXPECT_STREQ("NetworkListMobileHeaderView", mobile_section->GetClassName());
}
TEST_F(NetworkDetailedNetworkViewTest, ToggleInteractions) {

@ -72,6 +72,7 @@
#include "ui/views/style/typography_provider.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_utils.h"
namespace ash {
namespace {
@ -972,7 +973,7 @@ bool CalendarView::IsDateCellViewFocused() {
return false;
}
return focused_view->GetClassName() == CalendarDateCellView::kViewClassName;
return views::IsViewClass<CalendarDateCellView>(focused_view);
}
bool CalendarView::IsAnimating() {
@ -1723,9 +1724,9 @@ void CalendarView::OnEvent(ui::Event* event) {
// Sometimes the position of the upper row cells, which should be
// focused next, are above (and hidden behind) the header buttons. So
// this loop skips those buttons.
while (current_focusable_view &&
current_focusable_view->GetClassName() !=
CalendarDateCellView::kViewClassName) {
while (
current_focusable_view &&
!views::IsViewClass<CalendarDateCellView>(current_focusable_view)) {
current_focusable_view = focus_manager->GetNextFocusableView(
current_focusable_view, GetWidget(),
/*reverse=*/key_code == ui::VKEY_UP,
@ -1833,7 +1834,7 @@ void CalendarView::OnMonthArrowButtonActivated(bool up,
void CalendarView::AdjustDateCellVoxBounds() {
auto* focused_view = GetFocusManager()->GetFocusedView();
DCHECK_EQ(focused_view->GetClassName(), CalendarDateCellView::kViewClassName);
DCHECK(views::IsViewClass<CalendarDateCellView>(focused_view));
// When the Chrome Vox focusing box is in a `ScrollView`, the hidden content
// height, which is `scroll_view_->GetVisibleRect().y()` should also be added.

@ -634,8 +634,9 @@ void RecordAppLaunch(Profile* profile, const GURL& url) {
extensions::ExtensionRegistry::Get(profile)
->enabled_extensions()
.GetAppByURL(url);
if (!extension)
if (!extension) {
return;
}
extensions::RecordAppLaunchType(extension_misc::APP_LAUNCH_BOOKMARK_BAR,
extension->GetType());
@ -755,8 +756,9 @@ BookmarkBarView::BookmarkBarView(Browser* browser, BrowserView* browser_view)
SetPaintToLayer();
size_animation_.Reset(1);
if (!gfx::Animation::ShouldRenderRichAnimation())
if (!gfx::Animation::ShouldRenderRichAnimation()) {
animations_enabled = false;
}
// May be null for tests.
if (browser_view)
@ -769,8 +771,9 @@ BookmarkBarView::BookmarkBarView(Browser* browser, BrowserView* browser_view)
}
BookmarkBarView::~BookmarkBarView() {
if (bookmark_model_)
if (bookmark_model_) {
bookmark_model_->RemoveObserver(this);
}
// It's possible for the menu to outlive us, reset the observer to make sure
// it doesn't have a reference to us.
@ -803,8 +806,9 @@ void BookmarkBarView::SetPageNavigator(content::PageNavigator* navigator) {
}
void BookmarkBarView::SetInfoBarVisible(bool infobar_visible) {
if (infobar_visible == infobar_visible_)
if (infobar_visible == infobar_visible_) {
return;
}
infobar_visible_ = infobar_visible;
OnPropertyChanged(&infobar_visible_, views::kPropertyEffectsLayout);
}
@ -824,8 +828,9 @@ void BookmarkBarView::SetBookmarkBarState(
}
} else {
size_animation_.Reset(state == BookmarkBar::SHOW ? 1 : 0);
if (!animations_enabled)
if (!animations_enabled) {
AnimationEnded(&size_animation_);
}
}
#if BUILDFLAG(IS_MAC)
@ -865,8 +870,9 @@ const BookmarkNode* BookmarkBarView::GetNodeForButtonAtModelIndex(
// Then check the bookmark buttons.
for (size_t i = 0; i < bookmark_buttons_.size(); ++i) {
views::View* child = bookmark_buttons_[i];
if (!child->GetVisible())
if (!child->GetVisible()) {
break;
}
if (child->bounds().Contains(adjusted_loc))
return bookmark_model_->bookmark_bar_node()->children()[i].get();
}
@ -888,18 +894,22 @@ const BookmarkNode* BookmarkBarView::GetNodeForButtonAtModelIndex(
}
MenuButton* BookmarkBarView::GetMenuButtonForNode(const BookmarkNode* node) {
if (node == managed_->managed_node())
if (node == managed_->managed_node()) {
return managed_bookmarks_button_;
if (node == bookmark_model_->other_node())
}
if (node == bookmark_model_->other_node()) {
return other_bookmarks_button_;
if (node == bookmark_model_->bookmark_bar_node())
}
if (node == bookmark_model_->bookmark_bar_node()) {
return overflow_button_;
}
// TODO: add logic to handle saved groups node(crbug.com/1223929 and
// crbug.com/1223919)
absl::optional<size_t> index =
bookmark_model_->bookmark_bar_node()->GetIndexOf(node);
if (!index.has_value() || !node->is_folder())
if (!index.has_value() || !node->is_folder()) {
return nullptr;
}
return static_cast<MenuButton*>(bookmark_buttons_[index.value()]);
}
@ -958,8 +968,9 @@ std::u16string BookmarkBarView::CreateToolTipForURLAndTitle(
// Only show the URL if the url and title differ.
if (title != base::UTF8ToUTF16(url.spec())) {
if (!result.empty())
if (!result.empty()) {
result.push_back('\n');
}
// We need to explicitly specify the directionality of the URL's text to
// make sure it is treated as an LTR string when the context is RTL. For
@ -1026,8 +1037,9 @@ gfx::Size BookmarkBarView::GetMinimumSize() const {
void BookmarkBarView::Layout() {
// Skip layout during destruction, when no model exists.
if (!bookmark_model_)
if (!bookmark_model_) {
return;
}
int x = GetLeadingMargin();
static constexpr int kBookmarkBarTrailingMargin = 8;
@ -1265,8 +1277,9 @@ void BookmarkBarView::PaintChildren(const views::PaintInfo& paint_info) {
bool BookmarkBarView::GetDropFormats(
int* formats,
std::set<ui::ClipboardFormatType>* format_types) {
if (!bookmark_model_ || !bookmark_model_->loaded())
if (!bookmark_model_ || !bookmark_model_->loaded()) {
return false;
}
*formats = ui::OSExchangeData::URL;
format_types->insert(bookmarks::BookmarkNodeData::GetBookmarkFormatType());
return true;
@ -1282,8 +1295,9 @@ bool BookmarkBarView::CanDrop(const ui::OSExchangeData& data) {
bookmarks::prefs::kEditBookmarksEnabled))
return false;
if (!drop_info_.get())
if (!drop_info_.get()) {
drop_info_ = std::make_unique<DropInfo>();
}
// Only accept drops of 1 node, which is the case for all data dragged from
// bookmark bar and menus.
@ -1293,8 +1307,9 @@ bool BookmarkBarView::CanDrop(const ui::OSExchangeData& data) {
void BookmarkBarView::OnDragEntered(const ui::DropTargetEvent& event) {}
int BookmarkBarView::OnDragUpdated(const ui::DropTargetEvent& event) {
if (!drop_info_.get())
if (!drop_info_.get()) {
return 0;
}
if (drop_info_->valid &&
(drop_info_->x == event.x() && drop_info_->y == event.y())) {
@ -1324,8 +1339,9 @@ int BookmarkBarView::OnDragUpdated(const ui::DropTargetEvent& event) {
drop_info_->valid = true;
if (drop_info_->is_menu_showing) {
if (bookmark_drop_menu_)
if (bookmark_drop_menu_) {
bookmark_drop_menu_->Cancel();
}
drop_info_->is_menu_showing = false;
}
@ -1364,8 +1380,9 @@ views::View::DropCallback BookmarkBarView::GetDropCallback(
const ui::DropTargetEvent& event) {
StopShowFolderDropMenuTimer();
if (bookmark_drop_menu_)
if (bookmark_drop_menu_) {
bookmark_drop_menu_->Cancel();
}
if (!drop_info_ || !drop_info_->valid ||
drop_info_->location.operation == DragOperation::kNone)
@ -1398,8 +1415,9 @@ void BookmarkBarView::VisibilityChanged(View* starting_from, bool is_visible) {
void BookmarkBarView::ChildPreferredSizeChanged(views::View* child) {
// only rerender
if (child != saved_tab_group_bar_)
if (child != saved_tab_group_bar_) {
return;
}
InvalidateDrop();
}
@ -1411,8 +1429,9 @@ void BookmarkBarView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
void BookmarkBarView::AnimationProgressed(const gfx::Animation* animation) {
// |browser_view_| can be null during tests.
if (browser_view_)
if (browser_view_) {
browser_view_->ToolbarSizeChanged(true);
}
}
void BookmarkBarView::AnimationEnded(const gfx::Animation* animation) {
@ -1473,8 +1492,9 @@ void BookmarkBarView::BookmarkNodeMoved(BookmarkModel* model,
BookmarkNodeRemovedImpl(model, old_parent, old_index);
if (BookmarkNodeAddedImpl(model, new_parent, new_index))
needs_layout_and_paint = true;
if (needs_layout_and_paint)
if (needs_layout_and_paint) {
LayoutAndPaint();
}
drop_weak_ptr_factory_.InvalidateWeakPtrs();
}
@ -1485,8 +1505,9 @@ void BookmarkBarView::BookmarkNodeAdded(BookmarkModel* model,
bool added_by_user) {
// See comment in BookmarkNodeMoved() for details on this.
InvalidateDrop();
if (BookmarkNodeAddedImpl(model, parent, index))
if (BookmarkNodeAddedImpl(model, parent, index)) {
LayoutAndPaint();
}
drop_weak_ptr_factory_.InvalidateWeakPtrs();
}
@ -1502,8 +1523,9 @@ void BookmarkBarView::BookmarkNodeRemoved(BookmarkModel* model,
// Close the menu if the menu is showing for the deleted node.
if (bookmark_menu_ && bookmark_menu_->node() == node)
bookmark_menu_->Cancel();
if (BookmarkNodeRemovedImpl(model, parent, old_index))
if (BookmarkNodeRemovedImpl(model, parent, old_index)) {
LayoutAndPaint();
}
drop_weak_ptr_factory_.InvalidateWeakPtrs();
}
@ -1775,8 +1797,9 @@ void BookmarkBarView::Init() {
managed_ = ManagedBookmarkServiceFactory::GetForProfile(browser_->profile());
if (bookmark_model_) {
bookmark_model_->AddObserver(this);
if (bookmark_model_->loaded())
if (bookmark_model_->loaded()) {
BookmarkModelLoaded(bookmark_model_, false);
}
// else case: we'll receive notification back from the BookmarkModel when
// done loading, then we'll populate the bar.
}
@ -1812,13 +1835,12 @@ std::unique_ptr<MenuButton> BookmarkBarView::CreateManagedBookmarksButton() {
}
std::unique_ptr<MenuButton> BookmarkBarView::CreateOverflowButton() {
auto button =
std::make_unique<BookmarkMenuButtonBase>(base::BindRepeating(
[](BookmarkBarView* bar, const ui::Event& event) {
bar->OnMenuButtonPressed(bar->bookmark_model_->bookmark_bar_node(),
event);
},
base::Unretained(this)));
auto button = std::make_unique<BookmarkMenuButtonBase>(base::BindRepeating(
[](BookmarkBarView* bar, const ui::Event& event) {
bar->OnMenuButtonPressed(bar->bookmark_model_->bookmark_bar_node(),
event);
},
base::Unretained(this)));
// The overflow button's image contains an arrow and therefore it is a
// direction sensitive image and we need to flip it if the UI layout is
@ -1951,8 +1973,9 @@ bool BookmarkBarView::BookmarkNodeAddedImpl(BookmarkModel* model,
const BookmarkNode* parent,
size_t index) {
const bool needs_layout_and_paint = UpdateOtherAndManagedButtonsVisibility();
if (parent != model->bookmark_bar_node())
if (parent != model->bookmark_bar_node()) {
return needs_layout_and_paint;
}
if (index < bookmark_buttons_.size()) {
const BookmarkNode* node = parent->children()[index].get();
InsertBookmarkButtonAtIndex(CreateBookmarkButton(node), index);
@ -1973,8 +1996,9 @@ bool BookmarkBarView::BookmarkNodeRemovedImpl(BookmarkModel* model,
// Only children of the bookmark_bar_node get buttons.
return needs_layout;
}
if (index >= bookmark_buttons_.size())
if (index >= bookmark_buttons_.size()) {
return needs_layout;
}
views::LabelButton* button = bookmark_buttons_[index];
bookmark_buttons_.erase(bookmark_buttons_.cbegin() + index);
@ -2010,8 +2034,9 @@ void BookmarkBarView::BookmarkNodeChangedImpl(BookmarkModel* model,
views::LabelButton* button = bookmark_buttons_[index];
const int old_pref_width = button->GetPreferredSize().width();
ConfigureButton(node, button);
if (old_pref_width != button->GetPreferredSize().width())
if (old_pref_width != button->GetPreferredSize().width()) {
LayoutAndPaint();
}
}
void BookmarkBarView::ShowDropFolderForNode(const BookmarkNode* node) {
@ -2024,8 +2049,9 @@ void BookmarkBarView::ShowDropFolderForNode(const BookmarkNode* node) {
}
MenuButton* menu_button = GetMenuButtonForNode(node);
if (!menu_button)
if (!menu_button) {
return;
}
size_t start_index = 0;
if (node == bookmark_model_->bookmark_bar_node())
@ -2178,8 +2204,9 @@ void BookmarkBarView::InvalidateDrop() {
drop_info_->valid = false;
SchedulePaint();
}
if (bookmark_drop_menu_)
if (bookmark_drop_menu_) {
bookmark_drop_menu_->Cancel();
}
StopShowFolderDropMenuTimer();
}
@ -2200,8 +2227,9 @@ void BookmarkBarView::WriteBookmarkDragData(const BookmarkNode* node,
void BookmarkBarView::UpdateAppearanceForTheme() {
// We don't always have a color provider (ui tests, for example).
const ui::ColorProvider* color_provider = GetColorProvider();
if (!color_provider)
if (!color_provider) {
return;
}
for (size_t i = 0; i < bookmark_buttons_.size(); ++i) {
ConfigureButton(bookmark_model_->bookmark_bar_node()->children()[i].get(),
bookmark_buttons_[i]);
@ -2251,8 +2279,9 @@ bool BookmarkBarView::UpdateOtherAndManagedButtonsVisibility() {
browser_->profile()->GetPrefs()->GetBoolean(
bookmarks::prefs::kShowManagedBookmarksInBookmarkBar);
bool update_managed = show_managed != managed_bookmarks_button_->GetVisible();
if (update_managed)
if (update_managed) {
managed_bookmarks_button_->SetVisible(show_managed);
}
return update_other || update_managed;
}
@ -2277,16 +2306,18 @@ void BookmarkBarView::OnAppsPageShortcutVisibilityPrefChanged() {
// Only perform layout if required.
bool visible =
chrome::ShouldShowAppsShortcutInBookmarkBar(browser_->profile());
if (apps_page_shortcut_->GetVisible() == visible)
if (apps_page_shortcut_->GetVisible() == visible) {
return;
}
apps_page_shortcut_->SetVisible(visible);
UpdateBookmarksSeparatorVisibility();
LayoutAndPaint();
}
void BookmarkBarView::OnShowManagedBookmarksPrefChanged() {
if (UpdateOtherAndManagedButtonsVisibility())
if (UpdateOtherAndManagedButtonsVisibility()) {
LayoutAndPaint();
}
}
void BookmarkBarView::InsertBookmarkButtonAtIndex(
@ -2304,9 +2335,8 @@ void BookmarkBarView::InsertBookmarkButtonAtIndex(
DCHECK_EQ(*i++, saved_tab_group_bar_);
}
const auto is_bookmark_button = [this](const auto* v) {
const char* class_name = v->GetClassName();
return (class_name == BookmarkButton::kViewClassName ||
class_name == BookmarkFolderButton::kViewClassName) &&
return (views::IsViewClass<BookmarkButton>(v) ||
views::IsViewClass<BookmarkFolderButton>(v)) &&
v != overflow_button_ && v != other_bookmarks_button_;
};
i = std::find_if_not(i, children().cend(), is_bookmark_button);
@ -2321,8 +2351,9 @@ void BookmarkBarView::InsertBookmarkButtonAtIndex(
size_t BookmarkBarView::GetIndexForButton(views::View* button) {
auto it = base::ranges::find(bookmark_buttons_, button);
if (it == bookmark_buttons_.cend())
if (it == bookmark_buttons_.cend()) {
return static_cast<size_t>(-1);
}
return static_cast<size_t>(it - bookmark_buttons_.cbegin());
}

@ -29,6 +29,7 @@
#include "ui/views/layout/box_layout.h"
#include "ui/views/style/typography.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_utils.h"
namespace media_message_center {
@ -284,21 +285,25 @@ MediaNotificationViewImpl::MediaNotificationViewImpl(
message_center::kNotificationCornerRadius);
UpdateViewForExpandedState();
if (header_row_)
if (header_row_) {
header_row_->SetExpandButtonEnabled(GetExpandable());
}
if (item_)
if (item_) {
item_->SetView(this);
}
}
MediaNotificationViewImpl::~MediaNotificationViewImpl() {
if (item_)
if (item_) {
item_->SetView(nullptr);
}
}
void MediaNotificationViewImpl::SetExpanded(bool expanded) {
if (expanded_ == expanded)
if (expanded_ == expanded) {
return;
}
expanded_ = expanded;
@ -320,17 +325,20 @@ void MediaNotificationViewImpl::UpdateCornerRadius(int top_radius,
void MediaNotificationViewImpl::SetForcedExpandedState(
bool* forced_expanded_state) {
if (forced_expanded_state) {
if (forced_expanded_state_ == *forced_expanded_state)
if (forced_expanded_state_ == *forced_expanded_state) {
return;
}
forced_expanded_state_ = *forced_expanded_state;
} else {
if (!forced_expanded_state_.has_value())
if (!forced_expanded_state_.has_value()) {
return;
}
forced_expanded_state_ = absl::nullopt;
}
if (header_row_)
if (header_row_) {
header_row_->SetExpandButtonEnabled(GetExpandable());
}
UpdateViewForExpandedState();
}
@ -413,8 +421,9 @@ void MediaNotificationViewImpl::UpdateWithMediaMetadata(
RecordMetadataHistogram(Metadata::kArtist);
}
if (!metadata.album.empty())
if (!metadata.album.empty()) {
RecordMetadataHistogram(Metadata::kAlbum);
}
RecordMetadataHistogram(Metadata::kCount);
@ -430,8 +439,9 @@ void MediaNotificationViewImpl::UpdateWithMediaActions(
const base::flat_set<media_session::mojom::MediaSessionAction>& actions) {
enabled_actions_ = actions;
if (header_row_)
if (header_row_) {
header_row_->SetExpandButtonEnabled(GetExpandable());
}
UpdateViewForExpandedState();
PreferredSizeChanged();
@ -448,8 +458,9 @@ void MediaNotificationViewImpl::UpdateWithMediaArtwork(
UMA_HISTOGRAM_BOOLEAN(kArtworkHistogramName, has_artwork_);
if (GetWidget())
if (GetWidget()) {
UpdateForegroundColor();
}
container_->OnMediaArtworkChanged(image);
@ -462,20 +473,23 @@ void MediaNotificationViewImpl::UpdateWithMediaArtwork(
void MediaNotificationViewImpl::UpdateWithFavicon(const gfx::ImageSkia& icon) {
GetMediaNotificationBackground()->UpdateFavicon(icon);
if (GetWidget())
if (GetWidget()) {
UpdateForegroundColor();
}
SchedulePaint();
}
void MediaNotificationViewImpl::UpdateWithVectorIcon(
const gfx::VectorIcon* vector_icon) {
if (!header_row_)
if (!header_row_) {
return;
}
vector_header_icon_ = vector_icon;
UpdateAppIconVisibility(header_row_, vector_header_icon_ != nullptr);
if (GetWidget())
if (GetWidget()) {
UpdateForegroundColor();
}
}
void MediaNotificationViewImpl::UpdateDeviceSelectorVisibility(bool visible) {
@ -514,14 +528,16 @@ void MediaNotificationViewImpl::UpdateActionButtonsVisibility() {
action_button->SetVisible(should_show);
if (should_invalidate)
if (should_invalidate) {
action_button->InvalidateLayout();
}
if (action_button == picture_in_picture_button_) {
pip_button_separator_view_->SetVisible(should_show);
if (should_invalidate)
if (should_invalidate) {
pip_button_separator_view_->InvalidateLayout();
}
}
}
@ -585,8 +601,9 @@ void MediaNotificationViewImpl::UpdateViewForExpandedState() {
SchedulePaint();
}
if (header_row_)
if (header_row_) {
header_row_->SetExpanded(expanded);
}
container_->OnExpanded(expanded);
UpdateActionButtonsVisibility();
@ -671,8 +688,9 @@ MediaNotificationViewImpl::GetMediaNotificationBackground() {
}
bool MediaNotificationViewImpl::GetExpandable() const {
if (forced_expanded_state_.has_value())
if (forced_expanded_state_.has_value()) {
return false;
}
base::flat_set<MediaSessionAction> ignored_actions = {
GetPlayPauseIgnoredAction(GetActionFromButtonTag(*play_pause_button_)),
@ -687,8 +705,9 @@ bool MediaNotificationViewImpl::GetExpandable() const {
}
bool MediaNotificationViewImpl::GetActuallyExpanded() const {
if (forced_expanded_state_.has_value())
if (forced_expanded_state_.has_value()) {
return forced_expanded_state_.value();
}
return expanded_ && GetExpandable();
}
@ -758,8 +777,9 @@ void MediaNotificationViewImpl::UpdateForegroundColor() {
// Update action buttons.
for (views::View* child : playback_button_container_->children()) {
// Skip the play pause button since it is a special case.
if (child == play_pause_button_)
if (child == play_pause_button_) {
continue;
}
views::ImageButton* button = static_cast<views::ImageButton*>(child);
@ -781,8 +801,9 @@ void MediaNotificationViewImpl::ButtonPressed(views::Button* button) {
}
void MediaNotificationViewImpl::MaybeShowOrHideArtistLabel() {
if (!is_cros_)
if (!is_cros_) {
return;
}
artist_label_->SetVisible(!artist_label_->GetText().empty() || has_artwork_);
}
@ -793,8 +814,8 @@ std::vector<views::View*> MediaNotificationViewImpl::GetButtons() {
playback_button_container_->children().cbegin(),
playback_button_container_->children().cend());
base::EraseIf(buttons, [](views::View* view) {
return !(view->GetClassName() == views::ImageButton::kViewClassName ||
view->GetClassName() == views::ToggleImageButton::kViewClassName);
return !(views::IsViewClass<views::ImageButton>(view) ||
views::IsViewClass<views::ToggleImageButton>(view));
});
return buttons;
}

@ -32,6 +32,7 @@
#include "ui/views/controls/image_view.h"
#include "ui/views/test/button_test_api.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/view_utils.h"
namespace media_message_center {
@ -425,9 +426,10 @@ TEST_F(MediaNotificationViewImplTest, PlayToggle_FromObserver_Empty) {
EnableAction(MediaSessionAction::kPlay);
{
views::ToggleImageButton* button = static_cast<views::ToggleImageButton*>(
GetButtonForAction(MediaSessionAction::kPlay));
ASSERT_EQ(views::ToggleImageButton::kViewClassName, button->GetClassName());
views::ToggleImageButton* button =
views::AsViewClass<views::ToggleImageButton>(
GetButtonForAction(MediaSessionAction::kPlay));
ASSERT_TRUE(button);
EXPECT_FALSE(button->GetToggled());
}
@ -435,9 +437,10 @@ TEST_F(MediaNotificationViewImplTest, PlayToggle_FromObserver_Empty) {
media_session::mojom::MediaSessionInfo::New());
{
views::ToggleImageButton* button = static_cast<views::ToggleImageButton*>(
GetButtonForAction(MediaSessionAction::kPlay));
ASSERT_EQ(views::ToggleImageButton::kViewClassName, button->GetClassName());
views::ToggleImageButton* button =
views::AsViewClass<views::ToggleImageButton>(
GetButtonForAction(MediaSessionAction::kPlay));
ASSERT_TRUE(button);
EXPECT_FALSE(button->GetToggled());
}
}
@ -447,9 +450,10 @@ TEST_F(MediaNotificationViewImplTest, PlayToggle_FromObserver_PlaybackState) {
EnableAction(MediaSessionAction::kPause);
{
views::ToggleImageButton* button = static_cast<views::ToggleImageButton*>(
GetButtonForAction(MediaSessionAction::kPlay));
ASSERT_EQ(views::ToggleImageButton::kViewClassName, button->GetClassName());
views::ToggleImageButton* button =
views::AsViewClass<views::ToggleImageButton>(
GetButtonForAction(MediaSessionAction::kPlay));
ASSERT_TRUE(button);
EXPECT_FALSE(button->GetToggled());
}
@ -461,9 +465,10 @@ TEST_F(MediaNotificationViewImplTest, PlayToggle_FromObserver_PlaybackState) {
view()->UpdateWithMediaSessionInfo(session_info.Clone());
{
views::ToggleImageButton* button = static_cast<views::ToggleImageButton*>(
GetButtonForAction(MediaSessionAction::kPause));
ASSERT_EQ(views::ToggleImageButton::kViewClassName, button->GetClassName());
views::ToggleImageButton* button =
views::AsViewClass<views::ToggleImageButton>(
GetButtonForAction(MediaSessionAction::kPause));
ASSERT_TRUE(button);
EXPECT_TRUE(button->GetToggled());
}
@ -472,9 +477,10 @@ TEST_F(MediaNotificationViewImplTest, PlayToggle_FromObserver_PlaybackState) {
view()->UpdateWithMediaSessionInfo(session_info.Clone());
{
views::ToggleImageButton* button = static_cast<views::ToggleImageButton*>(
GetButtonForAction(MediaSessionAction::kPlay));
ASSERT_EQ(views::ToggleImageButton::kViewClassName, button->GetClassName());
views::ToggleImageButton* button =
views::AsViewClass<views::ToggleImageButton>(
GetButtonForAction(MediaSessionAction::kPlay));
ASSERT_TRUE(button);
EXPECT_FALSE(button->GetToggled());
}
}

@ -2,12 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/ui_devtools/views/overlay_agent_views.h"
#include "base/strings/stringprintf.h"
#include "components/ui_devtools/ui_devtools_unittest_utils.h"
#include "components/ui_devtools/ui_element.h"
#include "components/ui_devtools/views/dom_agent_views.h"
#include "components/ui_devtools/views/overlay_agent_views.h"
#include "components/ui_devtools/views/view_element.h"
#include "components/ui_devtools/views/widget_element.h"
#include "ui/events/base_event_utils.h"
@ -16,6 +15,7 @@
#include "ui/events/types/event_type.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget_utils.h"
#include "ui/views/window/non_client_view.h"
@ -254,8 +254,7 @@ TEST_F(OverlayAgentTest, HighlightRects) {
// view and adding the subviews directly causes NonClientView's hit test to
// fail.
views::View* contents_view = widget()->GetContentsView();
DCHECK_EQ(contents_view->GetClassName(),
views::NonClientView::kViewClassName);
DCHECK(views::IsViewClass<views::NonClientView>(contents_view));
views::NonClientView* non_client_view =
static_cast<views::NonClientView*>(contents_view);
views::View* client_view = non_client_view->client_view();

@ -44,6 +44,7 @@
#include "ui/views/controls/label.h"
#include "ui/views/controls/menu/menu_config.h"
#include "ui/views/controls/menu/menu_controller.h"
#include "ui/views/view_utils.h"
#include "ui/views/views_delegate.h"
#include "ui/views/widget/native_widget_mac.h"
#include "ui/views/widget/widget.h"
@ -1135,8 +1136,7 @@ bool NativeWidgetMacNSWindowHost::GetIsFocusedViewTextual(bool* is_textual) {
views::FocusManager* focus_manager =
root_view_ ? root_view_->GetWidget()->GetFocusManager() : nullptr;
*is_textual = focus_manager && focus_manager->GetFocusedView() &&
focus_manager->GetFocusedView()->GetClassName() ==
views::Label::kViewClassName;
IsViewClass<views::Label>(focus_manager->GetFocusedView());
return true;
}

@ -21,6 +21,7 @@
#include "ui/views/controls/focus_ring.h"
#include "ui/views/resources/grit/views_resources.h"
#include "ui/views/vector_icons.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget.h"
namespace views {
@ -73,8 +74,9 @@ bool RadioButton::IsGroupFocusTraversable() const {
void RadioButton::OnFocus() {
Checkbox::OnFocus();
if (select_on_focus_)
if (select_on_focus_) {
SetChecked(true);
}
}
void RadioButton::OnThemeChanged() {
@ -94,8 +96,9 @@ void RadioButton::RequestFocusFromEvent() {
void RadioButton::NotifyClick(const ui::Event& event) {
// Set the checked state to true only if we are unchecked, since we can't
// be toggled on and off like a checkbox.
if (!GetChecked())
if (!GetChecked()) {
SetChecked(true);
}
LabelButton::NotifyClick(event);
}
@ -104,8 +107,9 @@ ui::NativeTheme::Part RadioButton::GetThemePart() const {
}
void RadioButton::SetChecked(bool checked) {
if (checked == RadioButton::GetChecked())
if (checked == RadioButton::GetChecked()) {
return;
}
if (checked) {
// We can't start from the root view because other parts of the UI might use
// radio buttons with the same group. This can happen when re-using the same
@ -114,7 +118,7 @@ void RadioButton::SetChecked(bool checked) {
GetViewsInGroupFromParent(GetGroup(), &other);
for (auto* peer : other) {
if (peer != this) {
DCHECK(!strcmp(peer->GetClassName(), kViewClassName))
DCHECK(IsViewClass<RadioButton>(peer))
<< "radio-button-nt has same group as non radio-button-nt views.";
static_cast<RadioButton*>(peer)->SetChecked(false);
}
@ -143,8 +147,9 @@ SkPath RadioButton::GetFocusRingPath() const {
}
void RadioButton::GetViewsInGroupFromParent(int group, Views* views) {
if (parent())
if (parent()) {
parent()->GetViewsInGroup(group, views);
}
}
BEGIN_METADATA(RadioButton, Checkbox)

@ -98,8 +98,9 @@ int CheckScrollBounds(int viewport_size, int content_size, int current_pos) {
void ConstrainScrollToBounds(View* viewport,
View* view,
bool scroll_with_layers_enabled) {
if (!view)
if (!view) {
return;
}
// Note that even when ScrollView::ScrollsWithLayers() is true, the header row
// scrolls by repainting.
@ -131,10 +132,12 @@ int AdjustPosition(int current_position,
int new_position,
int content_size,
int viewport_size) {
if (-current_position == new_position)
if (-current_position == new_position) {
return new_position;
if (new_position < 0)
}
if (new_position < 0) {
return 0;
}
const int max_position = std::max(0, content_size - viewport_size);
return (new_position > max_position) ? max_position : new_position;
}
@ -151,8 +154,9 @@ class ScrollView::Viewport : public View {
~Viewport() override = default;
void ScrollRectToVisible(const gfx::Rect& rect) override {
if (children().empty() || !parent())
if (children().empty() || !parent()) {
return;
}
// If scrolling is disabled, it may have been handled by a parent View class
// so fall back to it.
@ -206,15 +210,17 @@ class ScrollView::Viewport : public View {
private:
void UpdateContentsViewportLayer() {
if (!layer())
if (!layer()) {
return;
}
const ui::LayerType new_layer_type =
CalculateLayerTypeForContentsViewport();
bool layer_needs_update = layer()->type() != new_layer_type;
if (layer_needs_update)
if (layer_needs_update) {
SetContentsViewportLayer(new_layer_type);
}
}
// Calculates the layer type to use for |contents_viewport_|.
@ -314,8 +320,9 @@ ScrollView::ScrollView(ScrollWithLayers scroll_with_layers)
more_content_right_->SetVisible(false);
more_content_bottom_->SetVisible(false);
if (scroll_with_layers_enabled_)
if (scroll_with_layers_enabled_) {
EnableViewportLayer();
}
// If we're scrolling with layers, paint the overflow indicators to the layer.
if (ScrollsWithLayers()) {
@ -347,8 +354,9 @@ std::unique_ptr<ScrollView> ScrollView::CreateScrollViewWithBorder() {
ScrollView* ScrollView::GetScrollViewForContents(View* contents) {
View* grandparent =
contents->parent() ? contents->parent()->parent() : nullptr;
if (!grandparent || grandparent->GetClassName() != ScrollView::kViewClassName)
if (!grandparent || !IsViewClass<ScrollView>(grandparent)) {
return nullptr;
}
auto* scroll_view = static_cast<ScrollView*>(grandparent);
DCHECK_EQ(contents, scroll_view->contents());
@ -383,8 +391,9 @@ void ScrollView::SetContentsLayerType(ui::LayerType layer_type) {
// layer are needed, consult with the owner.
DCHECK(layer_type == ui::LAYER_TEXTURED || layer_type == ui::LAYER_NOT_DRAWN);
if (layer_type_ == layer_type)
if (layer_type_ == layer_type) {
return;
}
layer_type_ = layer_type;
}
@ -411,8 +420,9 @@ void ScrollView::SetViewportRoundedCornerRadius(
}
void ScrollView::SetBackgroundColor(const absl::optional<SkColor>& color) {
if (background_color_ == color && !background_color_id_)
if (background_color_ == color && !background_color_id_) {
return;
}
background_color_ = color;
background_color_id_ = absl::nullopt;
UpdateBackground();
@ -421,8 +431,9 @@ void ScrollView::SetBackgroundColor(const absl::optional<SkColor>& color) {
void ScrollView::SetBackgroundThemeColorId(
const absl::optional<ui::ColorId>& color_id) {
if (background_color_id_ == color_id && !background_color_)
if (background_color_id_ == color_id && !background_color_) {
return;
}
background_color_id_ = color_id;
background_color_ = absl::nullopt;
UpdateBackground();
@ -430,8 +441,9 @@ void ScrollView::SetBackgroundThemeColorId(
}
gfx::Rect ScrollView::GetVisibleRect() const {
if (!contents_)
if (!contents_) {
return gfx::Rect();
}
gfx::PointF offset = CurrentOffset();
return gfx::Rect(offset.x(), offset.y(), contents_viewport_->width(),
contents_viewport_->height());
@ -439,8 +451,9 @@ gfx::Rect ScrollView::GetVisibleRect() const {
void ScrollView::SetHorizontalScrollBarMode(
ScrollBarMode horizontal_scroll_bar_mode) {
if (horizontal_scroll_bar_mode_ == horizontal_scroll_bar_mode)
if (horizontal_scroll_bar_mode_ == horizontal_scroll_bar_mode) {
return;
}
horizontal_scroll_bar_mode_ = horizontal_scroll_bar_mode;
OnPropertyChanged(&horizontal_scroll_bar_mode_, kPropertyEffectsPaint);
@ -453,8 +466,9 @@ void ScrollView::SetHorizontalScrollBarMode(
void ScrollView::SetVerticalScrollBarMode(
ScrollBarMode vertical_scroll_bar_mode) {
if (vertical_scroll_bar_mode_ == vertical_scroll_bar_mode)
if (vertical_scroll_bar_mode_ == vertical_scroll_bar_mode) {
return;
}
// Enabling vertical scrolling is incompatible with all scrolling being
// interpreted as horizontal.
@ -488,15 +502,17 @@ void ScrollView::SetTreatAllScrollEventsAsHorizontal(
}
void ScrollView::SetAllowKeyboardScrolling(bool allow_keyboard_scrolling) {
if (allow_keyboard_scrolling_ == allow_keyboard_scrolling)
if (allow_keyboard_scrolling_ == allow_keyboard_scrolling) {
return;
}
allow_keyboard_scrolling_ = allow_keyboard_scrolling;
OnPropertyChanged(&allow_keyboard_scrolling_, kPropertyEffectsNone);
}
void ScrollView::SetDrawOverflowIndicator(bool draw_overflow_indicator) {
if (draw_overflow_indicator_ == draw_overflow_indicator)
if (draw_overflow_indicator_ == draw_overflow_indicator) {
return;
}
draw_overflow_indicator_ = draw_overflow_indicator;
OnPropertyChanged(&draw_overflow_indicator_, kPropertyEffectsPaint);
}
@ -505,8 +521,9 @@ View* ScrollView::SetCustomOverflowIndicator(OverflowIndicatorAlignment side,
std::unique_ptr<View> indicator,
int thickness,
bool fills_opaquely) {
if (thickness < 0)
if (thickness < 0) {
thickness = 0;
}
if (ScrollsWithLayers()) {
indicator->SetPaintToLayer();
@ -577,8 +594,9 @@ ScrollBar* ScrollView::SetVerticalScrollBar(
}
void ScrollView::SetHasFocusIndicator(bool has_focus_indicator) {
if (has_focus_indicator == draw_focus_indicator_)
if (has_focus_indicator == draw_focus_indicator_) {
return;
}
draw_focus_indicator_ = has_focus_indicator;
views::FocusRing::Get(this)->SchedulePaint();
@ -608,8 +626,9 @@ gfx::Size ScrollView::CalculatePreferredSize() const {
}
int ScrollView::GetHeightForWidth(int width) const {
if (!is_bounded())
if (!is_bounded()) {
return View::GetHeightForWidth(width);
}
gfx::Insets insets = GetInsets();
width = std::max(0, width - insets.width());
@ -626,14 +645,16 @@ void ScrollView::Layout() {
#if BUILDFLAG(IS_MAC)
// On Mac, scrollbars may update their style one at a time, so they may
// temporarily be of different types. Refuse to lay out at this point.
if (horiz_sb_->OverlapsContent() != vert_sb_->OverlapsContent())
if (horiz_sb_->OverlapsContent() != vert_sb_->OverlapsContent()) {
return;
}
#endif
DCHECK_EQ(horiz_sb_->OverlapsContent(), vert_sb_->OverlapsContent());
}
if (views::FocusRing::Get(this))
if (views::FocusRing::Get(this)) {
views::FocusRing::Get(this)->Layout();
}
gfx::Rect available_rect = GetContentsBounds();
if (is_bounded()) {
@ -694,8 +715,9 @@ void ScrollView::Layout() {
// Give |contents_| a chance to update its bounds if it depends on the
// viewport.
if (contents_)
if (contents_) {
contents_->Layout();
}
bool should_layout_contents = false;
bool horiz_sb_required = false;
@ -759,8 +781,9 @@ void ScrollView::Layout() {
// Update to the real client size with the visible scrollbars.
contents_viewport_->SetBoundsRect(viewport_bounds);
if (should_layout_contents && contents_)
if (should_layout_contents && contents_) {
contents_->Layout();
}
// Even when |contents_| needs to scroll, it can still be narrower or wider
// the viewport. So ensure the scrolling layer can fill the viewport, so that
@ -798,8 +821,9 @@ void ScrollView::Layout() {
header_viewport_->SetBounds(contents_x, contents_y, viewport_bounds.width(),
header_height);
if (header_)
if (header_) {
header_->Layout();
}
ConstrainScrollToBounds(header_viewport_, header_,
scroll_with_layers_enabled_);
@ -807,19 +831,22 @@ void ScrollView::Layout() {
scroll_with_layers_enabled_);
SchedulePaint();
UpdateScrollBarPositions();
if (contents_)
if (contents_) {
UpdateOverflowIndicatorVisibility(CurrentOffset());
}
}
bool ScrollView::OnKeyPressed(const ui::KeyEvent& event) {
bool processed = false;
if (!allow_keyboard_scrolling_)
if (!allow_keyboard_scrolling_) {
return false;
}
// Give vertical scrollbar priority
if (IsVerticalScrollEnabled())
if (IsVerticalScrollEnabled()) {
processed = vert_sb_->OnKeyPressed(event);
}
if (!processed && IsHorizontalScrollEnabled())
processed = horiz_sb_->OnKeyPressed(event);
@ -850,8 +877,9 @@ bool ScrollView::OnMouseWheel(const ui::MouseWheelEvent& e) {
}
void ScrollView::OnScrollEvent(ui::ScrollEvent* event) {
if (!contents_)
if (!contents_) {
return;
}
// Possibly force the scroll event to horizontal based on the configuration
// option.
@ -885,10 +913,12 @@ void ScrollView::OnScrollEvent(ui::ScrollEvent* event) {
vert_sb_->ObserveScrollEvent(e);
// Need to copy state back to original event.
if (e.handled())
if (e.handled()) {
event->SetHandled();
if (e.stopped_propagation())
}
if (e.stopped_propagation()) {
event->StopPropagation();
}
}
void ScrollView::OnGestureEvent(ui::GestureEvent* event) {
@ -925,8 +955,9 @@ void ScrollView::OnThemeChanged() {
void ScrollView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
View::GetAccessibleNodeData(node_data);
if (!contents_)
if (!contents_) {
return;
}
node_data->role = ax::mojom::Role::kScrollView;
node_data->AddIntAttribute(ax::mojom::IntAttribute::kScrollX,
@ -945,8 +976,9 @@ void ScrollView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
}
bool ScrollView::HandleAccessibleAction(const ui::AXActionData& action_data) {
if (!contents_)
if (!contents_) {
return View::HandleAccessibleAction(action_data);
}
switch (action_data.action) {
case ax::mojom::Action::kScrollLeft:
@ -966,21 +998,24 @@ bool ScrollView::HandleAccessibleAction(const ui::AXActionData& action_data) {
}
void ScrollView::ScrollToPosition(ScrollBar* source, int position) {
if (!contents_)
if (!contents_) {
return;
}
gfx::PointF offset = CurrentOffset();
if (source == horiz_sb_ && IsHorizontalScrollEnabled()) {
position = AdjustPosition(offset.x(), position, contents_->width(),
contents_viewport_->width());
if (offset.x() == position)
if (offset.x() == position) {
return;
}
offset.set_x(position);
} else if (source == vert_sb_ && IsVerticalScrollEnabled()) {
position = AdjustPosition(offset.y(), position, contents_->height(),
contents_viewport_->height());
if (offset.y() == position)
if (offset.y() == position) {
return;
}
offset.set_y(position);
}
ScrollToOffset(offset);
@ -1010,13 +1045,15 @@ bool ScrollView::DoesViewportOrScrollViewHaveLayer() const {
}
void ScrollView::UpdateViewportLayerForClipping() {
if (scroll_with_layers_enabled_)
if (scroll_with_layers_enabled_) {
return;
}
const bool has_layer = DoesViewportOrScrollViewHaveLayer();
const bool needs_layer = DoesDescendantHaveLayer(contents_viewport_);
if (has_layer == needs_layer)
if (has_layer == needs_layer) {
return;
}
if (needs_layer)
EnableViewportLayer();
else
@ -1067,8 +1104,9 @@ void ScrollView::ScrollContentsRegionToBeVisible(const gfx::Rect& rect) {
// and the height is (max_y - y) to take into account the clipping of
// either viewport or the content size.
const gfx::Rect vis_rect = GetVisibleRect();
if (vis_rect.Contains(gfx::Rect(x, y, max_x - x, max_y - y)))
if (vis_rect.Contains(gfx::Rect(x, y, max_x - x, max_y - y))) {
return;
}
// Shift contents_'s X and Y so that the region is visible. If we
// need to shift up or left from where we currently are then we need
@ -1128,8 +1166,9 @@ void ScrollView::ComputeScrollBarsVisibility(const gfx::Size& vp_size,
// Make sure that a single scrollbar is created and visible as needed
void ScrollView::SetControlVisibility(View* control, bool should_show) {
if (!control)
if (!control) {
return;
}
if (should_show) {
if (!control->GetVisible()) {
AddChildView(control);
@ -1142,8 +1181,9 @@ void ScrollView::SetControlVisibility(View* control, bool should_show) {
}
void ScrollView::UpdateScrollBarPositions() {
if (!contents_)
if (!contents_) {
return;
}
const gfx::PointF offset = CurrentOffset();
if (IsHorizontalScrollEnabled()) {
@ -1164,8 +1204,9 @@ gfx::PointF ScrollView::CurrentOffset() const {
}
void ScrollView::ScrollByOffset(const gfx::PointF& offset) {
if (!contents_)
if (!contents_) {
return;
}
gfx::PointF current_offset = CurrentOffset();
ScrollToOffset(gfx::PointF(current_offset.x() + offset.x(),
@ -1182,8 +1223,9 @@ void ScrollView::ScrollToOffset(const gfx::PointF& offset) {
}
bool ScrollView::ScrollsWithLayers() const {
if (!scroll_with_layers_enabled_)
if (!scroll_with_layers_enabled_) {
return false;
}
// Just check for the presence of a layer since it's cheaper than querying the
// Feature flag each time.
return contents_viewport_->layer() != nullptr;
@ -1202,8 +1244,9 @@ bool ScrollView::IsVerticalScrollEnabled() const {
}
void ScrollView::EnableViewportLayer() {
if (DoesViewportOrScrollViewHaveLayer())
if (DoesViewportOrScrollViewHaveLayer()) {
return;
}
contents_viewport_->InitializeContentsViewportLayer();
contents_viewport_->layer()->SetMasksToBounds(true);
more_content_left_->SetPaintToLayer();
@ -1230,8 +1273,9 @@ void ScrollView::OnScrolled(const gfx::PointF& offset) {
}
void ScrollView::ScrollHeader() {
if (!header_)
if (!header_) {
return;
}
int x_offset = CurrentOffset().x();
if (header_->x() != -x_offset) {
@ -1246,8 +1290,9 @@ void ScrollView::AddBorder() {
}
void ScrollView::UpdateBorder() {
if (!draw_border_ || !GetWidget())
if (!draw_border_ || !GetWidget()) {
return;
}
SetBorder(CreateSolidBorder(
1, GetColorProvider()->GetColor(
@ -1256,8 +1301,9 @@ void ScrollView::UpdateBorder() {
}
void ScrollView::UpdateBackground() {
if (!GetWidget())
if (!GetWidget()) {
return;
}
const absl::optional<SkColor> background_color = GetBackgroundColor();
@ -1363,8 +1409,9 @@ int VariableRowHeightScrollHelper::GetPageScrollIncrement(
ScrollView* scroll_view,
bool is_horizontal,
bool is_positive) {
if (is_horizontal)
if (is_horizontal) {
return 0;
}
// y coordinate is most likely negative.
int y = abs(scroll_view->contents()->y());
int vis_height = scroll_view->contents()->parent()->height();
@ -1389,8 +1436,9 @@ int VariableRowHeightScrollHelper::GetLineScrollIncrement(
ScrollView* scroll_view,
bool is_horizontal,
bool is_positive) {
if (is_horizontal)
if (is_horizontal) {
return 0;
}
// y coordinate is most likely negative.
int y = abs(scroll_view->contents()->y());
RowInfo row = GetRowInfo(y);
@ -1421,8 +1469,9 @@ FixedRowHeightScrollHelper::FixedRowHeightScrollHelper(int top_margin,
VariableRowHeightScrollHelper::RowInfo FixedRowHeightScrollHelper::GetRowInfo(
int y) {
if (y < top_margin_)
if (y < top_margin_) {
return RowInfo(0, top_margin_);
}
return RowInfo((y - top_margin_) / row_height_ * row_height_ + top_margin_,
row_height_);
}

@ -29,6 +29,7 @@
#include "ui/views/style/typography.h"
#include "ui/views/style/typography_provider.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_utils.h"
namespace views {
@ -97,8 +98,9 @@ void StyledLabel::SetText(std::u16string text) {
// text elider tries to do so internally. There's no obvious reason to
// preserve trailing whitespace anyway.
base::TrimWhitespace(std::move(text), base::TRIM_TRAILING, &text);
if (text_ == text)
if (text_ == text) {
return;
}
text_ = text;
SetAccessibleName(text_);
@ -137,8 +139,9 @@ int StyledLabel::GetTextContext() const {
}
void StyledLabel::SetTextContext(int text_context) {
if (text_context_ == text_context)
if (text_context_ == text_context) {
return;
}
text_context_ = text_context;
SetAccessibleRole(text_context_ == style::CONTEXT_DIALOG_TITLE
@ -152,8 +155,9 @@ int StyledLabel::GetDefaultTextStyle() const {
}
void StyledLabel::SetDefaultTextStyle(int text_style) {
if (default_text_style_ == text_style)
if (default_text_style_ == text_style) {
return;
}
default_text_style_ = text_style;
OnPropertyChanged(&default_text_style_, kPropertyEffectsPreferredSizeChanged);
@ -179,8 +183,9 @@ int StyledLabel::GetLineHeight() const {
}
void StyledLabel::SetLineHeight(int line_height) {
if (line_height_ == line_height)
if (line_height_ == line_height) {
return;
}
line_height_ = line_height;
OnPropertyChanged(&line_height_, kPropertyEffectsPreferredSizeChanged);
@ -209,8 +214,9 @@ bool StyledLabel::GetAutoColorReadabilityEnabled() const {
}
void StyledLabel::SetAutoColorReadabilityEnabled(bool auto_color_readability) {
if (auto_color_readability_enabled_ == auto_color_readability)
if (auto_color_readability_enabled_ == auto_color_readability) {
return;
}
auto_color_readability_enabled_ = auto_color_readability;
OnPropertyChanged(&auto_color_readability_enabled_, kPropertyEffectsPaint);
@ -221,8 +227,9 @@ bool StyledLabel::GetSubpixelRenderingEnabled() const {
}
void StyledLabel::SetSubpixelRenderingEnabled(bool subpixel_rendering_enabled) {
if (subpixel_rendering_enabled_ == subpixel_rendering_enabled)
if (subpixel_rendering_enabled_ == subpixel_rendering_enabled) {
return;
}
subpixel_rendering_enabled_ = subpixel_rendering_enabled;
OnPropertyChanged(&subpixel_rendering_enabled_, kPropertyEffectsPaint);
@ -329,8 +336,9 @@ void StyledLabel::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) {
DCHECK_NE(gfx::ALIGN_TO_HEAD, alignment);
alignment = gfx::MaybeFlipForRTL(alignment);
if (horizontal_alignment_ == alignment)
if (horizontal_alignment_ == alignment) {
return;
}
horizontal_alignment_ = alignment;
PreferredSizeChanged();
}
@ -346,8 +354,7 @@ void StyledLabel::ClickFirstLinkForTesting() {
}
views::Link* StyledLabel::GetFirstLinkForTesting() {
const auto it = base::ranges::find(children(), LinkFragment::kViewClassName,
&View::GetClassName);
const auto it = base::ranges::find_if(children(), &IsViewClass<LinkFragment>);
DCHECK(it != children().cend());
return static_cast<views::Link*>(*it);
}
@ -409,8 +416,9 @@ void StyledLabel::CalculateLayout(int width) const {
}
gfx::Range range = gfx::Range::InvalidRange();
if (current_range != style_ranges_.end())
if (current_range != style_ranges_.end()) {
range = current_range->range;
}
const size_t position = text_.size() - remaining_string.size();
std::vector<std::u16string> substrings;
@ -528,8 +536,9 @@ void StyledLabel::CalculateLayout(int width) const {
std::max(line_size.height(), child_size.height()));
views.push_back(child_view);
if (label)
if (label) {
layout_views_->owned_views.push_back(std::move(label));
}
remaining_string = remaining_string.substr(chunk.size());
@ -596,8 +605,9 @@ std::unique_ptr<Label> StyledLabel::CreateLabel(
} else if (default_enabled_color_id_) {
result->SetEnabledColorId(default_enabled_color_id_);
}
if (!style_info.tooltip.empty())
if (!style_info.tooltip.empty()) {
result->SetTooltipText(style_info.tooltip);
}
if (!style_info.accessible_name.empty())
result->SetAccessibleName(style_info.accessible_name);
if (absl::holds_alternative<SkColor>(displayed_on_background_color_)) {
@ -618,8 +628,7 @@ void StyledLabel::UpdateLabelBackgroundColor() {
if (!child->GetProperty(kStyledLabelCustomViewKey)) {
// TODO(kylixrd): Should updating the label background color even be
// allowed if there are custom views?
DCHECK((child->GetClassName() == Label::kViewClassName) ||
(child->GetClassName() == LinkFragment::kViewClassName));
DCHECK(IsViewClass<Label>(child) || IsViewClass<LinkFragment>(child));
static_cast<Label*>(child)->SetBackgroundColorId(
absl::holds_alternative<ui::ColorId>(displayed_on_background_color_)
? absl::optional<ui::ColorId>(

@ -24,6 +24,7 @@
#include "ui/views/buildflags.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/style/platform_style.h"
#include "ui/views/view_utils.h"
#include "ui/views/views_features.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_observer.h"
@ -297,10 +298,7 @@ std::unique_ptr<NonClientFrameView> DialogDelegate::CreateDialogFrameView(
const DialogClientView* DialogDelegate::GetDialogClientView() const {
if (!GetWidget())
return nullptr;
const views::View* client_view = GetWidget()->client_view();
return client_view->GetClassName() == DialogClientView::kViewClassName
? static_cast<const DialogClientView*>(client_view)
: nullptr;
return AsViewClass<DialogClientView>(GetWidget()->client_view());
}
DialogClientView* DialogDelegate::GetDialogClientView() {
@ -346,7 +344,7 @@ views::View* DialogDelegate::GetFootnoteViewForTesting() const {
// it to create anything other than a BubbleFrameView.
// TODO(https://crbug.com/1011446): Make CreateDialogFrameView final, then
// remove this DCHECK.
DCHECK_EQ(frame->GetClassName(), BubbleFrameView::kViewClassName);
DCHECK(IsViewClass<BubbleFrameView>(frame));
return static_cast<BubbleFrameView*>(frame)->GetFootnoteView();
}