Commit 51d1ff49 authored by Keren Zhu's avatar Keren Zhu Committed by Commit Bot

views: Make LabelButton default constructible

This CL is part of the effort to make Views default
constructible, allowing UI to be built in a declarative fashion.

Bug: 1108460
Change-Id: I28650681fa1fa7254ce3199c69a9b39bb49ec01d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2337973
Commit-Queue: Keren Zhu <kerenzhu@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#795116}
parent 3e6b141a
...@@ -13,8 +13,7 @@ ...@@ -13,8 +13,7 @@
namespace ash { namespace ash {
BottomStatusIndicator::BottomStatusIndicator(TappedCallback on_tapped_callback) BottomStatusIndicator::BottomStatusIndicator(TappedCallback on_tapped_callback)
: LabelButton(this, base::string16()), : LabelButton(this), on_tapped_callback_(std::move(on_tapped_callback)) {
on_tapped_callback_(std::move(on_tapped_callback)) {
label()->SetAutoColorReadabilityEnabled(false); label()->SetAutoColorReadabilityEnabled(false);
label()->SetFontList( label()->SetFontList(
views::Label::GetDefaultFontList().DeriveWithSizeDelta(1)); views::Label::GetDefaultFontList().DeriveWithSizeDelta(1));
......
...@@ -39,12 +39,10 @@ gfx::Rect GetBoundsInRoot(const gfx::Rect& bounds_in_screen, ...@@ -39,12 +39,10 @@ gfx::Rect GetBoundsInRoot(const gfx::Rect& bounds_in_screen,
class TestFocusView : public views::WidgetDelegateView { class TestFocusView : public views::WidgetDelegateView {
public: public:
TestFocusView() TestFocusView()
: button_1_(new views::LabelButton(nullptr, {})), : button_1_(AddChildView(std::make_unique<views::LabelButton>())),
button_2_(new views::LabelButton(nullptr, {})) { button_2_(AddChildView(std::make_unique<views::LabelButton>())) {
button_1_->SetFocusForPlatform(); button_1_->SetFocusForPlatform();
button_2_->SetFocusForPlatform(); button_2_->SetFocusForPlatform();
AddChildView(button_1_);
AddChildView(button_2_);
} }
~TestFocusView() override = default; ~TestFocusView() override = default;
......
...@@ -26,7 +26,7 @@ ExtensionsMenuButton::ExtensionsMenuButton( ...@@ -26,7 +26,7 @@ ExtensionsMenuButton::ExtensionsMenuButton(
ExtensionsMenuItemView* parent, ExtensionsMenuItemView* parent,
ToolbarActionViewController* controller, ToolbarActionViewController* controller,
bool allow_pinning) bool allow_pinning)
: views::LabelButton(this, base::string16(), views::style::CONTEXT_BUTTON), : views::LabelButton(this),
browser_(browser), browser_(browser),
parent_(parent), parent_(parent),
controller_(controller), controller_(controller),
......
...@@ -129,16 +129,14 @@ class IconLabelBubbleView::HighlightPathGenerator ...@@ -129,16 +129,14 @@ class IconLabelBubbleView::HighlightPathGenerator
IconLabelBubbleView::IconLabelBubbleView(const gfx::FontList& font_list, IconLabelBubbleView::IconLabelBubbleView(const gfx::FontList& font_list,
Delegate* delegate) Delegate* delegate)
: LabelButton(nullptr, base::string16()), : delegate_(delegate),
delegate_(delegate), separator_view_(AddChildView(std::make_unique<SeparatorView>(this))) {
separator_view_(new SeparatorView(this)) {
DCHECK(delegate_); DCHECK(delegate_);
SetFontList(font_list); SetFontList(font_list);
SetHorizontalAlignment(gfx::ALIGN_LEFT); SetHorizontalAlignment(gfx::ALIGN_LEFT);
separator_view_->SetVisible(ShouldShowSeparator()); separator_view_->SetVisible(ShouldShowSeparator());
AddChildView(separator_view_);
set_ink_drop_visible_opacity( set_ink_drop_visible_opacity(
GetOmniboxStateOpacity(OmniboxPartState::SELECTED)); GetOmniboxStateOpacity(OmniboxPartState::SELECTED));
......
...@@ -36,9 +36,9 @@ class VIEWS_EXPORT LabelButton : public Button, public NativeThemeDelegate { ...@@ -36,9 +36,9 @@ class VIEWS_EXPORT LabelButton : public Button, public NativeThemeDelegate {
// Creates a LabelButton with ButtonPressed() events sent to |listener| and // Creates a LabelButton with ButtonPressed() events sent to |listener| and
// label |text|. |button_context| is a value from views::style::TextContext // label |text|. |button_context| is a value from views::style::TextContext
// and determines the appearance of |text|. // and determines the appearance of |text|.
LabelButton(ButtonListener* listener, explicit LabelButton(ButtonListener* listener = nullptr,
const base::string16& text, const base::string16& text = base::string16(),
int button_context = style::CONTEXT_BUTTON); int button_context = style::CONTEXT_BUTTON);
~LabelButton() override; ~LabelButton() override;
// Gets or sets the image shown for the specified button state. // Gets or sets the image shown for the specified button state.
......
...@@ -100,8 +100,8 @@ class LabelButtonTest : public test::WidgetTest { ...@@ -100,8 +100,8 @@ class LabelButtonTest : public test::WidgetTest {
// The test code below is not prepared to handle dark mode. // The test code below is not prepared to handle dark mode.
test_widget_->GetNativeTheme()->set_use_dark_colors(false); test_widget_->GetNativeTheme()->set_use_dark_colors(false);
button_ = new TestLabelButton; button_ = test_widget_->GetContentsView()->AddChildView(
test_widget_->GetContentsView()->AddChildView(button_); std::make_unique<TestLabelButton>());
// Establish the expected text colors for testing changes due to state. // Establish the expected text colors for testing changes due to state.
themed_normal_text_color_ = button_->GetNativeTheme()->GetSystemColor( themed_normal_text_color_ = button_->GetNativeTheme()->GetSystemColor(
......
...@@ -2479,14 +2479,14 @@ TEST_F(MenuControllerTest, SetSelectionIndices_NestedButtons) { ...@@ -2479,14 +2479,14 @@ TEST_F(MenuControllerTest, SetSelectionIndices_NestedButtons) {
container_view->AddChildView(new Label()); container_view->AddChildView(new Label());
// Add two focusable buttons (buttons in menus are always focusable). // Add two focusable buttons (buttons in menus are always focusable).
Button* const button1 = new LabelButton(nullptr, base::string16()); Button* const button1 =
container_view->AddChildView(std::make_unique<LabelButton>());
button1->SetFocusBehavior(View::FocusBehavior::ALWAYS); button1->SetFocusBehavior(View::FocusBehavior::ALWAYS);
button1->GetViewAccessibility().OverrideRole(ax::mojom::Role::kMenuItem); button1->GetViewAccessibility().OverrideRole(ax::mojom::Role::kMenuItem);
container_view->AddChildView(button1); Button* const button2 =
Button* const button2 = new LabelButton(nullptr, base::string16()); container_view->AddChildView(std::make_unique<LabelButton>());
button2->GetViewAccessibility().OverrideRole(ax::mojom::Role::kMenuItem); button2->GetViewAccessibility().OverrideRole(ax::mojom::Role::kMenuItem);
button2->SetFocusBehavior(View::FocusBehavior::ALWAYS); button2->SetFocusBehavior(View::FocusBehavior::ALWAYS);
container_view->AddChildView(button2);
OpenMenu(menu_item()); OpenMenu(menu_item());
......
...@@ -595,11 +595,12 @@ TEST_F(MenuRunnerImplTest, FocusOnMenuClose) { ...@@ -595,11 +595,12 @@ TEST_F(MenuRunnerImplTest, FocusOnMenuClose) {
new internal::MenuRunnerImpl(menu_item_view()); new internal::MenuRunnerImpl(menu_item_view());
// Create test button that has focus. // Create test button that has focus.
auto button_managed = std::make_unique<LabelButton>();
button_managed->SetID(1);
button_managed->SetSize(gfx::Size(20, 20));
LabelButton* button = LabelButton* button =
new LabelButton(nullptr, base::string16(), style::CONTEXT_BUTTON); owner()->GetRootView()->AddChildView(std::move(button_managed));
button->SetID(1);
button->SetSize(gfx::Size(20, 20));
owner()->GetRootView()->AddChildView(button);
button->SetFocusBehavior(View::FocusBehavior::ALWAYS); button->SetFocusBehavior(View::FocusBehavior::ALWAYS);
button->GetWidget()->widget_delegate()->SetCanActivate(true); button->GetWidget()->widget_delegate()->SetCanActivate(true);
button->GetWidget()->Activate(); button->GetWidget()->Activate();
......
...@@ -81,7 +81,7 @@ class FlexibleRoleTestView : public View { ...@@ -81,7 +81,7 @@ class FlexibleRoleTestView : public View {
class TestLabelButton : public LabelButton { class TestLabelButton : public LabelButton {
public: public:
TestLabelButton() : LabelButton(nullptr, base::string16()) { TestLabelButton() {
// Make sure the label doesn't cover the hit test co-ordinates. // Make sure the label doesn't cover the hit test co-ordinates.
label()->SetSize(gfx::Size(1, 1)); label()->SetSize(gfx::Size(1, 1));
} }
......
...@@ -948,9 +948,9 @@ TEST_F(NativeWidgetMacTest, Tooltips) { ...@@ -948,9 +948,9 @@ TEST_F(NativeWidgetMacTest, Tooltips) {
const base::string16 long_tooltip(2000, 'W'); const base::string16 long_tooltip(2000, 'W');
// Create a nested layout to test corner cases. // Create a nested layout to test corner cases.
LabelButton* back = new LabelButton(nullptr, base::string16()); LabelButton* back =
widget->GetContentsView()->AddChildView(std::make_unique<LabelButton>());
back->SetBounds(10, 10, 80, 80); back->SetBounds(10, 10, 80, 80);
widget->GetContentsView()->AddChildView(back);
widget->Show(); widget->Show();
ui::test::EventGenerator event_generator(GetContext(), ui::test::EventGenerator event_generator(GetContext(),
...@@ -962,9 +962,9 @@ TEST_F(NativeWidgetMacTest, Tooltips) { ...@@ -962,9 +962,9 @@ TEST_F(NativeWidgetMacTest, Tooltips) {
// Create a new button for the "front", and set the tooltip, but don't add it // Create a new button for the "front", and set the tooltip, but don't add it
// to the view hierarchy yet. // to the view hierarchy yet.
LabelButton* front = new LabelButton(nullptr, base::string16()); auto front_managed = std::make_unique<LabelButton>();
front->SetBounds(20, 20, 40, 40); front_managed->SetBounds(20, 20, 40, 40);
front->SetTooltipText(tooltip_front); front_managed->SetTooltipText(tooltip_front);
// Changing the tooltip text shouldn't require an additional mousemove to take // Changing the tooltip text shouldn't require an additional mousemove to take
// effect. // effect.
...@@ -973,7 +973,7 @@ TEST_F(NativeWidgetMacTest, Tooltips) { ...@@ -973,7 +973,7 @@ TEST_F(NativeWidgetMacTest, Tooltips) {
EXPECT_EQ(tooltip_back, TooltipTextForWidget(widget)); EXPECT_EQ(tooltip_back, TooltipTextForWidget(widget));
// Adding a new view under the mouse should also take immediate effect. // Adding a new view under the mouse should also take immediate effect.
back->AddChildView(front); LabelButton* front = back->AddChildView(std::move(front_managed));
EXPECT_EQ(tooltip_front, TooltipTextForWidget(widget)); EXPECT_EQ(tooltip_front, TooltipTextForWidget(widget));
// A long tooltip will be wrapped by Cocoa, but the full string should appear. // A long tooltip will be wrapped by Cocoa, but the full string should appear.
......
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