Commit a066d837 authored by Wei Li's avatar Wei Li Committed by Commit Bot

Add builder for Checkbox

Allows Checkbox class to use builder pattern, and converts the Checkbox
example to use the builder.

Also add callback property into Button class so Button class and its
subclasses can use callback in their metadata and builders.

Bug: 1130078
Change-Id: Ifd2e7cebd172d8fc91cbce50c0dacafd6b5ecf9f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2490339
Commit-Queue: Wei Li <weili@chromium.org>
Reviewed-by: default avatarAllen Bauer <kylixrd@chromium.org>
Cr-Commit-Position: refs/heads/master@{#820069}
parent 1adf6b49
......@@ -715,6 +715,7 @@ DEFINE_ENUM_CONVERTERS(
BEGIN_METADATA(Button, InkDropHostView)
ADD_PROPERTY_METADATA(base::string16, AccessibleName)
ADD_PROPERTY_METADATA(PressedCallback, Callback)
ADD_PROPERTY_METADATA(bool, AnimateOnStateChange)
ADD_PROPERTY_METADATA(bool, HasInkDropActionOnClick)
ADD_PROPERTY_METADATA(bool, HideInkDropWhenShowingContextMenu)
......
......@@ -310,6 +310,9 @@ class VIEWS_EXPORT Button : public InkDropHostView,
FocusRing* focus_ring() { return focus_ring_; }
// Getter used by metadata only.
const PressedCallback& GetCallback() const { return callback_; }
private:
friend class test::ButtonTestApi;
FRIEND_TEST_ALL_PREFIXES(BlueButtonTest, Border);
......@@ -382,6 +385,7 @@ class VIEWS_EXPORT Button : public InkDropHostView,
BEGIN_VIEW_BUILDER(VIEWS_EXPORT, Button, InkDropHostView)
VIEW_BUILDER_PROPERTY(base::string16, AccessibleName)
VIEW_BUILDER_PROPERTY(Button::PressedCallback, Callback)
VIEW_BUILDER_PROPERTY(base::TimeDelta, AnimationDuration)
VIEW_BUILDER_PROPERTY(bool, AnimateOnStateChange)
VIEW_BUILDER_PROPERTY(bool, HasInkDropActionOnClick)
......
......@@ -14,6 +14,7 @@
#include "cc/paint/paint_flags.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/metadata/view_factory.h"
namespace gfx {
struct VectorIcon;
......@@ -90,6 +91,11 @@ class VIEWS_EXPORT Checkbox : public LabelButton {
DISALLOW_COPY_AND_ASSIGN(Checkbox);
};
BEGIN_VIEW_BUILDER(VIEWS_EXPORT, Checkbox, LabelButton)
VIEW_BUILDER_PROPERTY(bool, Checked)
VIEW_BUILDER_PROPERTY(bool, MultiLine)
END_VIEW_BUILDER(VIEWS_EXPORT, Checkbox)
} // namespace views
#endif // UI_VIEWS_CONTROLS_BUTTON_CHECKBOX_H_
......@@ -47,6 +47,9 @@ void ButtonExample::CreateExampleView(View* container) {
container->SetLayoutManager(std::make_unique<FillLayout>());
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
auto start_throbber_cb = [](MdTextButton* button) {
button->StartThrobbing(5);
};
auto view =
Builder<BoxLayoutView>()
.SetOrientation(BoxLayout::Orientation::kVertical)
......@@ -59,37 +62,36 @@ void ButtonExample::CreateExampleView(View* container) {
.CopyAddressTo(&label_button_)
.SetText(ASCIIToUTF16(kLabelButton))
.SetFocusForPlatform()
.SetRequestFocusOnPress(true),
.SetRequestFocusOnPress(true)
.SetCallback(base::BindRepeating(
&ButtonExample::LabelButtonPressed,
base::Unretained(this), label_button_)),
Builder<MdTextButton>()
.CopyAddressTo(&md_button_)
.SetText(base::ASCIIToUTF16("Material Design")),
.SetText(base::ASCIIToUTF16("Material Design"))
.SetCallback(
base::BindRepeating(start_throbber_cb, md_button_)),
Builder<MdTextButton>()
.CopyAddressTo(&md_disabled_button_)
.SetText(ASCIIToUTF16("Material Design Disabled Button"))
.SetState(Button::STATE_DISABLED),
.SetState(Button::STATE_DISABLED)
.SetCallback(base::BindRepeating(start_throbber_cb,
md_disabled_button_)),
Builder<MdTextButton>()
.CopyAddressTo(&md_default_button_)
.SetText(base::ASCIIToUTF16("Default"))
.SetIsDefault(true),
.SetIsDefault(true)
.SetCallback(base::BindRepeating(start_throbber_cb,
md_default_button_)),
Builder<ImageButton>()
.CopyAddressTo(&image_button_)
.SetFocusForPlatform()
.SetRequestFocusOnPress(true)})
.SetRequestFocusOnPress(true)
.SetCallback(
base::BindRepeating(&ButtonExample::ImageButtonPressed,
base::Unretained(this)))})
.Build();
auto start_throbber_cb = [](Button* button) { button->StartThrobbing(5); };
label_button_->SetCallback(
base::BindRepeating(&ButtonExample::LabelButtonPressed,
base::Unretained(this), label_button_));
md_button_->SetCallback(base::BindRepeating(start_throbber_cb, md_button_));
md_disabled_button_->SetCallback(
base::BindRepeating(&ButtonExample::LabelButtonPressed,
base::Unretained(this), md_disabled_button_));
md_default_button_->SetCallback(
base::BindRepeating(start_throbber_cb, md_default_button_));
image_button_->SetCallback(base::BindRepeating(
&ButtonExample::ImageButtonPressed, base::Unretained(this)));
image_button_->SetImage(ImageButton::STATE_NORMAL,
rb.GetImageNamed(IDR_CLOSE).ToImageSkia());
image_button_->SetImage(ImageButton::STATE_HOVERED,
......@@ -123,9 +125,9 @@ void ButtonExample::LabelButtonPressed(LabelButton* label_button,
} else if (event.IsShiftDown()) {
if (event.IsAltDown()) {
// Toggle focusability.
label_button_->IsAccessibilityFocusable()
? label_button_->SetFocusBehavior(View::FocusBehavior::NEVER)
: label_button_->SetFocusForPlatform();
label_button->IsAccessibilityFocusable()
? label_button->SetFocusBehavior(View::FocusBehavior::NEVER)
: label_button->SetFocusForPlatform();
}
} else if (event.IsAltDown()) {
label_button->SetIsDefault(!label_button->GetIsDefault());
......
......@@ -23,11 +23,13 @@ CheckboxExample::~CheckboxExample() = default;
void CheckboxExample::CreateExampleView(View* container) {
container->SetLayoutManager(std::make_unique<FillLayout>());
button_ = container->AddChildView(std::make_unique<Checkbox>(
base::ASCIIToUTF16("Checkbox"),
base::BindRepeating(
[](int* count) { PrintStatus("Pressed! count: %d", ++(*count)); },
&count_)));
container->AddChildView(
views::Builder<Checkbox>()
.SetText(base::ASCIIToUTF16("Checkbox"))
.SetCallback(base::BindRepeating(
[](int* count) { PrintStatus("Pressed! count: %d", ++(*count)); },
&count_))
.Build());
}
} // namespace examples
......
......@@ -24,9 +24,7 @@ class VIEWS_EXAMPLES_EXPORT CheckboxExample : public ExampleBase {
void CreateExampleView(View* container) override;
private:
// The only control in this test.
Checkbox* button_ = nullptr;
// The number of times the contained checkbox has been clicked.
int count_ = 0;
DISALLOW_COPY_AND_ASSIGN(CheckboxExample);
......
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