Skip to content

Commit

Permalink
Return vector<unique_ptr<MdTextButton>> in MakeButtonsInState().
Browse files Browse the repository at this point in the history
In order to better convey property object ownership,
changed MakeButtonsInState() to returning two unique_ptr<MdTextButton>s.

Bug: 948287
Change-Id: I29bb117ba9ad7215c1be92435f4b622bb83dc1aa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1628639
Commit-Queue: Yeol Park <peary2@gmail.com>
Reviewed-by: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#664272}
  • Loading branch information
peary2 authored and Commit Bot committed May 29, 2019
1 parent a0ef970 commit d4c5807
Showing 1 changed file with 38 additions and 36 deletions.
74 changes: 38 additions & 36 deletions ui/views/examples/button_sticker_sheet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,41 +43,45 @@ GridLayout* MakeStretchyGridLayout(View* host, int ncols) {
return layout;
}

View* MakePlainLabel(const std::string& text) {
return new Label(base::ASCIIToUTF16(text));
std::unique_ptr<View> MakePlainLabel(const std::string& text) {
return std::make_unique<Label>(base::ASCIIToUTF16(text));
}

// Add a row containing a label whose text is |label_text| and then all the
// views in |views| to the supplied GridLayout, with padding between rows.
template <typename T>
void AddLabelledRowToGridLayout(GridLayout* layout,
const std::string& label_text,
std::vector<View*> views) {
std::vector<std::unique_ptr<T>> views) {
const float kRowDoesNotResizeVertically = 0.0;
const int kPaddingRowHeight = 8;
layout->StartRow(kRowDoesNotResizeVertically, kStretchyGridColumnSetId);
layout->AddView(MakePlainLabel(label_text));
for (auto* view : views)
layout->AddView(view);
layout->AddView(MakePlainLabel(label_text).get());
for (auto& view : views)
layout->AddView(view.release());
// This gets added extraneously after the last row, but it doesn't hurt and
// means there's no need to keep track of whether to add it or not.
layout->AddPaddingRow(kRowDoesNotResizeVertically, kPaddingRowHeight);
}

// Constructs a pair of MdTextButtons in the specified |state| with the
// specified |listener|, and returns them in |*primary| and |*secondary|. The
// button in |*primary| is a call-to-action button, and the button in
// |*secondary| is a regular button.
void MakeButtonsInState(MdTextButton** primary,
MdTextButton** secondary,
ButtonListener* listener,
Button::ButtonState state) {
// specified |listener|, and returns them in |primary| and |secondary|. The
// button in |primary| is a call-to-action button, and the button in
// |secondary| is a regular button.
std::vector<std::unique_ptr<MdTextButton>> MakeButtonsInState(
ButtonListener* listener,
Button::ButtonState state) {
std::vector<std::unique_ptr<MdTextButton>> buttons;
const base::string16 button_text = base::ASCIIToUTF16("Button");
*primary = MdTextButton::Create(listener, button_text).release();
(*primary)->SetProminent(true);
(*primary)->SetState(state);

*secondary = MdTextButton::Create(listener, button_text).release();
(*secondary)->SetState(state);
auto primary = MdTextButton::Create(listener, button_text);
primary->SetProminent(true);
primary->SetState(state);
buttons.push_back(std::move(primary));

auto secondary = MdTextButton::Create(listener, button_text);
secondary->SetState(state);
buttons.push_back(std::move(secondary));
return buttons;
}

} // namespace
Expand All @@ -91,23 +95,21 @@ void ButtonStickerSheet::CreateExampleView(View* container) {
GridLayout* layout = MakeStretchyGridLayout(container, 3);

// The title row has an empty row label.
AddLabelledRowToGridLayout(
layout, std::string(),
{MakePlainLabel("Primary"), MakePlainLabel("Secondary")});

MdTextButton* primary = nullptr;
MdTextButton* secondary = nullptr;

MakeButtonsInState(&primary, &secondary, this, Button::STATE_NORMAL);
AddLabelledRowToGridLayout(layout, "Default", {primary, secondary});
MakeButtonsInState(&primary, &secondary, this, Button::STATE_NORMAL);
AddLabelledRowToGridLayout(layout, "Normal", {primary, secondary});
MakeButtonsInState(&primary, &secondary, this, Button::STATE_HOVERED);
AddLabelledRowToGridLayout(layout, "Hovered", {primary, secondary});
MakeButtonsInState(&primary, &secondary, this, Button::STATE_PRESSED);
AddLabelledRowToGridLayout(layout, "Pressed", {primary, secondary});
MakeButtonsInState(&primary, &secondary, this, Button::STATE_DISABLED);
AddLabelledRowToGridLayout(layout, "Disabled", {primary, secondary});
std::vector<std::unique_ptr<View>> plainLabel;
plainLabel.push_back(MakePlainLabel("Primary"));
plainLabel.push_back(MakePlainLabel("Secondary"));
AddLabelledRowToGridLayout(layout, std::string(), std::move(plainLabel));

AddLabelledRowToGridLayout(layout, "Default",
MakeButtonsInState(this, Button::STATE_NORMAL));
AddLabelledRowToGridLayout(layout, "Normal",
MakeButtonsInState(this, Button::STATE_NORMAL));
AddLabelledRowToGridLayout(layout, "Hovered",
MakeButtonsInState(this, Button::STATE_HOVERED));
AddLabelledRowToGridLayout(layout, "Pressed",
MakeButtonsInState(this, Button::STATE_PRESSED));
AddLabelledRowToGridLayout(layout, "Disabled",
MakeButtonsInState(this, Button::STATE_DISABLED));
}

void ButtonStickerSheet::ButtonPressed(Button* button, const ui::Event& event) {
Expand Down

0 comments on commit d4c5807

Please sign in to comment.