Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: ui/views/examples/button_sticker_sheet.cc

Issue 2296343004: views_examples: add Button sticker sheet (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/views/examples/button_sticker_sheet.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/views/controls/button/button.h"
9 #include "ui/views/controls/button/md_text_button.h"
10 #include "ui/views/layout/grid_layout.h"
11
12 namespace views {
13 namespace examples {
14
15 namespace {
16
17 // The id of the only ColumnSet in a stretchy grid (see below).
18 const int kStretchyGridColumnSetId = 0;
19
20 // Creates a stretchy grid layout: there are |ncols| columns, separated from
21 // each other by padding columns, and all non-padding columns have equal flex
22 // weight and will flex in either dimension as needed. The resulting grid layout
23 // has only one ColumnSet (numbered kStretchyGridColumnSetId).
24 GridLayout* MakeStretchyGridLayout(View* host, int ncols) {
25 const float kPaddingDoesNotResize = 0.0;
26 const int kPaddingWidth = 8;
27 const GridLayout::Alignment kColumnStretchesHorizontally = GridLayout::FILL;
28 const GridLayout::Alignment kColumnStretchesVertically = GridLayout::FILL;
29 const float kColumnResizesEqually = 1.0;
30 const GridLayout::SizeType kColumnUsesPreferredSize = GridLayout::USE_PREF;
31
32 GridLayout* layout = new GridLayout(host);
33 ColumnSet* columns = layout->AddColumnSet(kStretchyGridColumnSetId);
34 for (int i = 0; i < ncols; ++i) {
35 if (i != 0)
36 columns->AddPaddingColumn(kPaddingDoesNotResize, kPaddingWidth);
37 columns->AddColumn(kColumnStretchesHorizontally, kColumnStretchesVertically,
38 kColumnResizesEqually, kColumnUsesPreferredSize, 0,
39 0); // These two are ignored when using PREF.
40 }
41 return layout;
42 }
43
44 View* MakePlainLabel(const std::string& text) {
45 return new Label(base::ASCIIToUTF16(text));
46 }
47
48 // Add a row containing a label whose text is |label_text| and then all the
49 // views in |views| to the supplied GridLayout, with padding between rows.
50 void AddLabelledRowToGridLayout(GridLayout* layout,
51 const std::string& label_text,
52 std::vector<View*> views) {
53 const float kRowDoesNotResizeVertically = 0.0;
54 const int kPaddingRowHeight = 8;
55 layout->StartRow(kRowDoesNotResizeVertically, kStretchyGridColumnSetId);
56 layout->AddView(MakePlainLabel(label_text));
57 for (const auto& view : views)
58 layout->AddView(view);
59 // This gets added extraneously after the last row, but it doesn't hurt and
60 // means there's no need to keep track of whether to add it or not.
61 layout->AddPaddingRow(kRowDoesNotResizeVertically, kPaddingRowHeight);
62 }
63
64 // Constructs a pair of MdTextButtons in the specified |state| with the
65 // specified |listener|, and returns them in |*primary| and |*secondary|. The
66 // button in |*primary| is a call-to-action button, and the button in
67 // |*secondary| is a regular button.
68 void MakeButtonsInState(MdTextButton** primary,
69 MdTextButton** secondary,
70 ButtonListener* listener,
71 Button::ButtonState state) {
72 const base::string16 button_text = base::ASCIIToUTF16("Button");
73 *primary = MdTextButton::CreateMdButton(listener, button_text);
74 (*primary)->SetCallToAction(true);
75 (*primary)->SetState(state);
76
77 *secondary = MdTextButton::CreateMdButton(listener, button_text);
78 (*secondary)->SetState(state);
79 }
80
81 } // namespace
82
83 ButtonStickerSheet::ButtonStickerSheet()
84 : ExampleBase("Button (Sticker Sheet)") {}
85
86 ButtonStickerSheet::~ButtonStickerSheet() {}
87
88 void ButtonStickerSheet::CreateExampleView(View* container) {
89 GridLayout* layout = MakeStretchyGridLayout(container, 3);
90 container->SetLayoutManager(layout);
91
92 // The title row has an empty row label.
93 AddLabelledRowToGridLayout(
94 layout, "", {MakePlainLabel("Primary"), MakePlainLabel("Secondary")});
sky 2016/09/01 19:28:40 "" -> std::string()
Elly Fong-Jones 2016/09/02 11:29:39 Done.
95
96 MdTextButton* primary = nullptr;
97 MdTextButton* secondary = nullptr;
98
99 MakeButtonsInState(&primary, &secondary, this, Button::STATE_NORMAL);
100 AddLabelledRowToGridLayout(layout, "Default", {primary, secondary});
101 MakeButtonsInState(&primary, &secondary, this, Button::STATE_NORMAL);
102 AddLabelledRowToGridLayout(layout, "Normal", {primary, secondary});
103 MakeButtonsInState(&primary, &secondary, this, Button::STATE_HOVERED);
104 AddLabelledRowToGridLayout(layout, "Hovered", {primary, secondary});
105 MakeButtonsInState(&primary, &secondary, this, Button::STATE_PRESSED);
106 AddLabelledRowToGridLayout(layout, "Pressed", {primary, secondary});
107 MakeButtonsInState(&primary, &secondary, this, Button::STATE_DISABLED);
108 AddLabelledRowToGridLayout(layout, "Disabled", {primary, secondary});
109
110 MakeButtonsInState(&primary, &secondary, this, Button::STATE_NORMAL);
111 primary->OnFocus();
112 secondary->OnFocus();
113 AddLabelledRowToGridLayout(layout, "Focused", {primary, secondary});
114 }
115
116 void ButtonStickerSheet::ButtonPressed(Button* button, const ui::Event& event) {
117 // Ignore button presses.
118 }
119
120 } // namespace examples
121 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698