1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/examples/button_sticker_sheet.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/layout/table_layout.h"
namespace views::examples {
namespace {
// Creates a stretchy table layout: there are |ncols| columns, separated from
// each other by padding columns, and all non-padding columns have equal flex
// weight and will flex in either dimension as needed.
TableLayout* MakeStretchyTableLayout(View* host, int ncols) {
const float kPaddingResizesEqually = 1.0;
const int kPaddingWidth = 30;
const int kColumnWidth = 96;
auto* layout = host->SetLayoutManager(std::make_unique<views::TableLayout>());
for (int i = 0; i < ncols; ++i) {
if (i != 0) {
layout->AddPaddingColumn(kPaddingResizesEqually, kPaddingWidth);
}
layout->AddColumn(LayoutAlignment::kStretch, LayoutAlignment::kStretch,
TableLayout::kFixedSize, TableLayout::ColumnSize::kFixed,
kColumnWidth, kColumnWidth);
}
return layout;
}
std::unique_ptr<View> MakePlainLabel(const std::string& text) {
return std::make_unique<Label>(base::ASCIIToUTF16(text));
}
// Adds a label whose text is |label_text| and then all the views in |views|.
template <typename T>
void AddLabeledRow(View* parent,
const std::string& label_text,
std::vector<std::unique_ptr<T>> views) {
parent->AddChildView(MakePlainLabel(label_text));
for (auto& view : views) {
parent->AddChildView(std::move(view));
}
}
// 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.
std::vector<std::unique_ptr<MdTextButton>> MakeButtonsInState(
Button::ButtonState state) {
std::vector<std::unique_ptr<MdTextButton>> buttons;
const std::u16string button_text = u"Button";
auto primary = std::make_unique<views::MdTextButton>(
Button::PressedCallback(), button_text);
primary->SetStyle(ui::ButtonStyle::kProminent);
primary->SetState(state);
buttons.push_back(std::move(primary));
auto secondary = std::make_unique<views::MdTextButton>(
Button::PressedCallback(), button_text);
secondary->SetState(state);
buttons.push_back(std::move(secondary));
return buttons;
}
} // namespace
ButtonStickerSheet::ButtonStickerSheet()
: ExampleBase("Button (Sticker Sheet)") {}
ButtonStickerSheet::~ButtonStickerSheet() = default;
void ButtonStickerSheet::CreateExampleView(View* container) {
TableLayout* layout = MakeStretchyTableLayout(container, 3);
for (int i = 0; i < 6; ++i) {
const int kPaddingRowHeight = 8;
layout->AddRows(1, TableLayout::kFixedSize)
.AddPaddingRow(TableLayout::kFixedSize, kPaddingRowHeight);
}
// The title row has an empty row label.
std::vector<std::unique_ptr<View>> plainLabel;
plainLabel.push_back(MakePlainLabel("Primary"));
plainLabel.push_back(MakePlainLabel("Secondary"));
AddLabeledRow(container, std::string(), std::move(plainLabel));
AddLabeledRow(container, "Default", MakeButtonsInState(Button::STATE_NORMAL));
AddLabeledRow(container, "Normal", MakeButtonsInState(Button::STATE_NORMAL));
AddLabeledRow(container, "Hovered",
MakeButtonsInState(Button::STATE_HOVERED));
AddLabeledRow(container, "Pressed",
MakeButtonsInState(Button::STATE_PRESSED));
AddLabeledRow(container, "Disabled",
MakeButtonsInState(Button::STATE_DISABLED));
}
} // namespace views::examples
|