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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: ui/views/examples/button_sticker_sheet.cc
diff --git a/ui/views/examples/button_sticker_sheet.cc b/ui/views/examples/button_sticker_sheet.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e2666e35a215a99729838245dee6d3eb0110b7ea
--- /dev/null
+++ b/ui/views/examples/button_sticker_sheet.cc
@@ -0,0 +1,121 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// 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 "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/grid_layout.h"
+
+namespace views {
+namespace examples {
+
+namespace {
+
+// The id of the only ColumnSet in a stretchy grid (see below).
+const int kStretchyGridColumnSetId = 0;
+
+// Creates a stretchy grid 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. The resulting grid layout
+// has only one ColumnSet (numbered kStretchyGridColumnSetId).
+GridLayout* MakeStretchyGridLayout(View* host, int ncols) {
+ const float kPaddingDoesNotResize = 0.0;
+ const int kPaddingWidth = 8;
+ const GridLayout::Alignment kColumnStretchesHorizontally = GridLayout::FILL;
+ const GridLayout::Alignment kColumnStretchesVertically = GridLayout::FILL;
+ const float kColumnResizesEqually = 1.0;
+ const GridLayout::SizeType kColumnUsesPreferredSize = GridLayout::USE_PREF;
+
+ GridLayout* layout = new GridLayout(host);
+ ColumnSet* columns = layout->AddColumnSet(kStretchyGridColumnSetId);
+ for (int i = 0; i < ncols; ++i) {
+ if (i != 0)
+ columns->AddPaddingColumn(kPaddingDoesNotResize, kPaddingWidth);
+ columns->AddColumn(kColumnStretchesHorizontally, kColumnStretchesVertically,
+ kColumnResizesEqually, kColumnUsesPreferredSize, 0,
+ 0); // These two are ignored when using PREF.
+ }
+ return layout;
+}
+
+View* MakePlainLabel(const std::string& text) {
+ return new 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.
+void AddLabelledRowToGridLayout(GridLayout* layout,
+ const std::string& label_text,
+ std::vector<View*> views) {
+ const float kRowDoesNotResizeVertically = 0.0;
+ const int kPaddingRowHeight = 8;
+ layout->StartRow(kRowDoesNotResizeVertically, kStretchyGridColumnSetId);
+ layout->AddView(MakePlainLabel(label_text));
+ for (const auto& view : views)
+ layout->AddView(view);
+ // 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) {
+ const base::string16 button_text = base::ASCIIToUTF16("Button");
+ *primary = MdTextButton::CreateMdButton(listener, button_text);
+ (*primary)->SetCallToAction(true);
+ (*primary)->SetState(state);
+
+ *secondary = MdTextButton::CreateMdButton(listener, button_text);
+ (*secondary)->SetState(state);
+}
+
+} // namespace
+
+ButtonStickerSheet::ButtonStickerSheet()
+ : ExampleBase("Button (Sticker Sheet)") {}
+
+ButtonStickerSheet::~ButtonStickerSheet() {}
+
+void ButtonStickerSheet::CreateExampleView(View* container) {
+ GridLayout* layout = MakeStretchyGridLayout(container, 3);
+ container->SetLayoutManager(layout);
+
+ // The title row has an empty row label.
+ AddLabelledRowToGridLayout(
+ layout, "", {MakePlainLabel("Primary"), MakePlainLabel("Secondary")});
sky 2016/09/01 19:28:40 "" -> std::string()
Elly Fong-Jones 2016/09/02 11:29:39 Done.
+
+ 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});
+
+ MakeButtonsInState(&primary, &secondary, this, Button::STATE_NORMAL);
+ primary->OnFocus();
+ secondary->OnFocus();
+ AddLabelledRowToGridLayout(layout, "Focused", {primary, secondary});
+}
+
+void ButtonStickerSheet::ButtonPressed(Button* button, const ui::Event& event) {
+ // Ignore button presses.
+}
+
+} // namespace examples
+} // namespace views

Powered by Google App Engine
This is Rietveld 408576698