Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/base/material_design/material_design_controller.h" | |
| 9 #include "ui/views/controls/button/button.h" | |
| 10 #include "ui/views/controls/button/md_text_button.h" | |
| 11 #include "ui/views/layout/grid_layout.h" | |
| 12 | |
| 13 namespace views { | |
| 14 namespace examples { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // The id of the only ColumnSet in a stretchy grid (see below). | |
| 19 const int kStretchyGridColumnSetId = 0; | |
| 20 | |
| 21 // Creates a stretchy grid layout: there are |ncols| columns, separated from | |
| 22 // each other by padding columns, and all non-padding columns have equal flex | |
| 23 // weight and will flex in either dimension as needed. The resulting grid layout | |
| 24 // has only one ColumnSet (numbered kStretchyGridColumnSetId). | |
| 25 GridLayout* MakeStretchyGridLayout(View* host, int ncols) { | |
| 26 const float kPaddingDoesNotResize = 0.0; | |
| 27 const int kPaddingWidth = 8; | |
| 28 const GridLayout::Alignment kColumnStretchesHorizontally = GridLayout::FILL; | |
| 29 const GridLayout::Alignment kColumnStretchesVertically = GridLayout::FILL; | |
| 30 const float kColumnResizesEqually = 1.0; | |
| 31 const GridLayout::SizeType kColumnUsesPreferredSize = GridLayout::USE_PREF; | |
| 32 | |
| 33 GridLayout* layout = new GridLayout(host); | |
| 34 ColumnSet* columns = layout->AddColumnSet(kStretchyGridColumnSetId); | |
| 35 for (int i = 0; i < ncols; ++i) { | |
| 36 if (i != 0) | |
| 37 columns->AddPaddingColumn(kPaddingDoesNotResize, kPaddingWidth); | |
| 38 columns->AddColumn(kColumnStretchesHorizontally, kColumnStretchesVertically, | |
| 39 kColumnResizesEqually, kColumnUsesPreferredSize, 0, | |
| 40 0); // These two are ignored when using PREF. | |
| 41 } | |
| 42 return layout; | |
| 43 } | |
| 44 | |
| 45 View* MakePlainLabel(const std::string& text) { | |
|
sky
2016/09/02 14:31:19
optional: I wouldn't bother with a one line functi
| |
| 46 return new Label(base::ASCIIToUTF16(text)); | |
| 47 } | |
| 48 | |
| 49 // Add a row containing a label whose text is |label_text| and then all the | |
| 50 // views in |views| to the supplied GridLayout, with padding between rows. | |
| 51 void AddLabelledRowToGridLayout(GridLayout* layout, | |
| 52 const std::string& label_text, | |
| 53 std::vector<View*> views) { | |
| 54 const float kRowDoesNotResizeVertically = 0.0; | |
| 55 const int kPaddingRowHeight = 8; | |
| 56 layout->StartRow(kRowDoesNotResizeVertically, kStretchyGridColumnSetId); | |
| 57 layout->AddView(MakePlainLabel(label_text)); | |
| 58 for (const auto& view : views) | |
| 59 layout->AddView(view); | |
| 60 // This gets added extraneously after the last row, but it doesn't hurt and | |
| 61 // means there's no need to keep track of whether to add it or not. | |
| 62 layout->AddPaddingRow(kRowDoesNotResizeVertically, kPaddingRowHeight); | |
| 63 } | |
| 64 | |
| 65 // Constructs a pair of MdTextButtons in the specified |state| with the | |
| 66 // specified |listener|, and returns them in |*primary| and |*secondary|. The | |
| 67 // button in |*primary| is a call-to-action button, and the button in | |
| 68 // |*secondary| is a regular button. | |
| 69 void MakeButtonsInState(MdTextButton** primary, | |
| 70 MdTextButton** secondary, | |
| 71 ButtonListener* listener, | |
| 72 Button::ButtonState state) { | |
| 73 const base::string16 button_text = base::ASCIIToUTF16("Button"); | |
| 74 *primary = MdTextButton::CreateMdButton(listener, button_text); | |
| 75 (*primary)->SetCallToAction(true); | |
| 76 (*primary)->SetState(state); | |
| 77 | |
| 78 *secondary = MdTextButton::CreateMdButton(listener, button_text); | |
| 79 (*secondary)->SetState(state); | |
| 80 } | |
| 81 | |
| 82 } // namespace | |
| 83 | |
| 84 ButtonStickerSheet::ButtonStickerSheet() | |
| 85 : ExampleBase("Button (Sticker Sheet)") {} | |
| 86 | |
| 87 ButtonStickerSheet::~ButtonStickerSheet() {} | |
| 88 | |
| 89 void ButtonStickerSheet::CreateExampleView(View* container) { | |
| 90 GridLayout* layout = MakeStretchyGridLayout(container, 3); | |
| 91 container->SetLayoutManager(layout); | |
| 92 | |
| 93 if (!ui::MaterialDesignController::IsSecondaryUiMaterial()) { | |
| 94 const char* kNeedsMdWarning = | |
| 95 "This will look wrong without --secondary-ui-md."; | |
| 96 layout->StartRow(0, 0); | |
| 97 layout->AddView(MakePlainLabel(kNeedsMdWarning), 3, 1); | |
| 98 } | |
| 99 | |
| 100 // The title row has an empty row label. | |
| 101 AddLabelledRowToGridLayout( | |
| 102 layout, std::string(), | |
| 103 {MakePlainLabel("Primary"), MakePlainLabel("Secondary")}); | |
| 104 | |
| 105 MdTextButton* primary = nullptr; | |
| 106 MdTextButton* secondary = nullptr; | |
| 107 | |
| 108 MakeButtonsInState(&primary, &secondary, this, Button::STATE_NORMAL); | |
| 109 AddLabelledRowToGridLayout(layout, "Default", {primary, secondary}); | |
| 110 MakeButtonsInState(&primary, &secondary, this, Button::STATE_NORMAL); | |
| 111 AddLabelledRowToGridLayout(layout, "Normal", {primary, secondary}); | |
| 112 MakeButtonsInState(&primary, &secondary, this, Button::STATE_HOVERED); | |
| 113 AddLabelledRowToGridLayout(layout, "Hovered", {primary, secondary}); | |
| 114 MakeButtonsInState(&primary, &secondary, this, Button::STATE_PRESSED); | |
| 115 AddLabelledRowToGridLayout(layout, "Pressed", {primary, secondary}); | |
| 116 MakeButtonsInState(&primary, &secondary, this, Button::STATE_DISABLED); | |
| 117 AddLabelledRowToGridLayout(layout, "Disabled", {primary, secondary}); | |
| 118 | |
| 119 MakeButtonsInState(&primary, &secondary, this, Button::STATE_NORMAL); | |
| 120 primary->OnFocus(); | |
| 121 secondary->OnFocus(); | |
| 122 AddLabelledRowToGridLayout(layout, "Focused", {primary, secondary}); | |
| 123 } | |
| 124 | |
| 125 void ButtonStickerSheet::ButtonPressed(Button* button, const ui::Event& event) { | |
| 126 // Ignore button presses. | |
| 127 } | |
| 128 | |
| 129 } // namespace examples | |
| 130 } // namespace views | |
| OLD | NEW |