Commit 65d61ff1 authored by Allen Bauer's avatar Allen Bauer Committed by Commit Bot

Added IsViewClass<T> function for use in place of GetClassName() comparisons.

Added metadata to Tab, TabSlotView

https://docs.google.com/document/d/1FXacwqJjW4ircJ19gsypRnLAWMYmM8sFTyDCpBH48jI/edit?usp=sharing

Bug: 938501
Change-Id: I659ca7eea66d8b50e0609d08b29cfc8efd3b8809
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2429515
Commit-Queue: Allen Bauer <kylixrd@chromium.org>
Reviewed-by: default avatarPeter Boström <pbos@chromium.org>
Cr-Commit-Position: refs/heads/master@{#810503}
parent 86b6a1fd
......@@ -29,6 +29,7 @@
#include "media/base/media_switches.h"
#include "services/media_session/public/mojom/media_session.mojom.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/view_utils.h"
using media_session::mojom::MediaSessionAction;
......@@ -527,7 +528,7 @@ class MediaDialogViewBrowserTest : public InProcessBrowserTest {
// media_message_center::MediaNotificationViewImpl sets the tags of its action
// buttons to the MediaSessionAction value.
views::ImageButton* GetButtonForAction(views::View* view, int action) {
if (view->GetClassName() == views::ImageButton::kViewClassName) {
if (views::IsViewClass<views::ImageButton>(view)) {
views::ImageButton* image_button = static_cast<views::ImageButton*>(view);
if (image_button->tag() == action)
return image_button;
......
......@@ -76,6 +76,7 @@
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/controls/label.h"
#include "ui/views/metadata/metadata_impl_macros.h"
#include "ui/views/rect_based_targeting_utils.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
......@@ -175,9 +176,6 @@ class Tab::TabCloseButtonObserver : public views::ViewObserver {
// Tab -------------------------------------------------------------------------
// static
const char Tab::kViewClassName[] = "Tab";
// static
void Tab::SetShowHoverCardOnMouseHoverForTesting(bool value) {
g_show_hover_card_on_mouse_hover = value;
......@@ -423,10 +421,6 @@ void Tab::Layout() {
focus_ring_->Layout();
}
const char* Tab::GetClassName() const {
return kViewClassName;
}
bool Tab::OnKeyPressed(const ui::KeyEvent& event) {
if (event.key_code() == ui::VKEY_RETURN && !IsSelected()) {
controller_->SelectTab(this, event);
......@@ -1049,3 +1043,6 @@ void Tab::UpdateForegroundColors() {
SchedulePaint();
}
BEGIN_METADATA(Tab, TabSlotView)
END_METADATA
......@@ -24,6 +24,7 @@
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/masked_targeter_delegate.h"
#include "ui/views/metadata/metadata_header_macros.h"
#include "ui/views/view_observer.h"
class AlertIndicator;
......@@ -53,8 +54,7 @@ class Tab : public gfx::AnimationDelegate,
public views::ViewObserver,
public TabSlotView {
public:
// The Tab's class name.
static const char kViewClassName[];
METADATA_HEADER(Tab);
// When the content's width of the tab shrinks to below this size we should
// hide the close button on inactive tabs. Any smaller and they're too easy
......@@ -83,7 +83,6 @@ class Tab : public gfx::AnimationDelegate,
// TabSlotView:
void Layout() override;
const char* GetClassName() const override;
bool OnKeyPressed(const ui::KeyEvent& event) override;
bool OnKeyReleased(const ui::KeyEvent& event) override;
bool OnMousePressed(const ui::MouseEvent& event) override;
......
......@@ -3,6 +3,10 @@
// found in the LICENSE file.
#include "chrome/browser/ui/views/tabs/tab_slot_view.h"
#include "ui/views/metadata/metadata_impl_macros.h"
TabSlotView::TabSlotView() {}
TabSlotView::~TabSlotView() {}
BEGIN_METADATA(TabSlotView, views::View)
END_METADATA
......@@ -7,11 +7,14 @@
#include "chrome/browser/ui/views/tabs/tab_strip_layout.h"
#include "components/tab_groups/tab_group_id.h"
#include "ui/views/metadata/metadata_header_macros.h"
#include "ui/views/view.h"
// View that can be laid out in the tabstrip.
class TabSlotView : public views::View {
public:
METADATA_HEADER(TabSlotView);
enum class ViewType {
kTab,
kTabGroupHeader,
......
......@@ -94,6 +94,7 @@
#include "ui/views/view_observer.h"
#include "ui/views/view_targeter.h"
#include "ui/views/view_targeter_delegate.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/root_view.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/non_client_view.h"
......@@ -1984,8 +1985,7 @@ void TabStrip::OnMouseEventInTab(views::View* source,
// Record time from cursor entering the tabstrip to first tap on a tab to
// switch.
if (mouse_entered_tabstrip_time_.has_value() &&
event.type() == ui::ET_MOUSE_PRESSED &&
!strcmp(source->GetClassName(), Tab::kViewClassName)) {
event.type() == ui::ET_MOUSE_PRESSED && views::IsViewClass<Tab>(source)) {
UMA_HISTOGRAM_MEDIUM_TIMES(
"TabStrip.TimeToSwitch",
base::TimeTicks::Now() - mouse_entered_tabstrip_time_.value());
......@@ -2426,7 +2426,7 @@ views::View* TabStrip::GetTooltipHandlerForPoint(const gfx::Point& point) {
// Return any view that isn't a Tab or this TabStrip immediately. We don't
// want to interfere.
views::View* v = View::GetTooltipHandlerForPoint(point);
if (v && v != this && strcmp(v->GetClassName(), Tab::kViewClassName))
if (v && v != this && !views::IsViewClass<Tab>(v))
return v;
views::View* tab = FindTabHitByPoint(point);
......@@ -3359,7 +3359,7 @@ void TabStrip::TabContextMenuController::ShowContextMenuForViewImpl(
ui::MenuSourceType source_type) {
// We are only intended to be installed as a context-menu handler for tabs, so
// this cast should be safe.
DCHECK_EQ(Tab::kViewClassName, source->GetClassName());
DCHECK(views::IsViewClass<Tab>(source));
Tab* const tab = static_cast<Tab*>(source);
if (tab->closing())
return;
......@@ -3798,7 +3798,7 @@ views::View* TabStrip::TargetForRect(views::View* root, const gfx::Rect& rect) {
// Return any view that isn't a Tab or this TabStrip immediately. We don't
// want to interfere.
views::View* v = views::ViewTargeterDelegate::TargetForRect(root, rect);
if (v && v != this && strcmp(v->GetClassName(), Tab::kViewClassName))
if (v && v != this && !views::IsViewClass<Tab>(v))
return v;
views::View* tab = FindTabHitByPoint(point);
......
......@@ -42,6 +42,7 @@
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_targeter.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget.h"
namespace {
......@@ -50,7 +51,7 @@ namespace {
// found tab view, on NULL if none is found.
views::View* FindTabView(views::View* view) {
views::View* current = view;
while (current && strcmp(current->GetClassName(), Tab::kViewClassName)) {
while (current && !views::IsViewClass<Tab>(current)) {
current = current->parent();
}
return current;
......
......@@ -241,6 +241,7 @@ component("views") {
"view_targeter.h",
"view_targeter_delegate.h",
"view_tracker.h",
"view_utils.h",
"views_delegate.h",
"views_export.h",
"views_features.h",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_VIEWS_VIEW_UTILS_H_
#define UI_VIEWS_VIEW_UTILS_H_
#include <type_traits>
#include "ui/views/metadata/metadata_types.h"
#include "ui/views/view.h"
namespace views {
template <typename V>
bool IsViewClass(View* view) {
static_assert(std::is_base_of<View, V>::value, "Only View classes supported");
metadata::ClassMetaData* parent = V::MetaData();
metadata::ClassMetaData* child = view->GetClassMetaData();
while (child && child != parent)
child = child->parent_class_meta_data();
return !!child;
}
} // namespace views
#endif // UI_VIEWS_VIEW_UTILS_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment