Commit 7e7659f4 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Clean up *layout_example* some.

Remove DISALLOW_COPY, use constexpr more, move constants as close to
first use as possible, etc.

This was originally motivated by wanting to change the
Combobox::set_callback() call to set_closure(); that's present here, but
I couldn't help cleaning up at least some of the rest of this too.

Switching to layout managers (from explicit positioning) also fixes some
graphical bugs that resulted from comboboxes' preferred sizes changing
after creation.

Bug: none
Change-Id: Iad0d483327a07ad865b252f5ab58e90037512125
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2421029
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#809163}
parent 0793a571
......@@ -32,55 +32,6 @@ BoxLayoutExample::BoxLayoutExample() : LayoutExampleBase("Box Layout") {}
BoxLayoutExample::~BoxLayoutExample() = default;
void BoxLayoutExample::CreateAdditionalControls(int vertical_pos) {
static const char* orientation_values[2] = {"Horizontal", "Vertical"};
static const char* main_axis_values[3] = {"Start", "Center", "End"};
static const char* cross_axis_values[4] = {"Stretch", "Start", "Center",
"End"};
orientation_ = CreateAndAddCombobox(base::ASCIIToUTF16("Orientation"),
orientation_values, 2, &vertical_pos);
main_axis_alignment_ = CreateAndAddCombobox(
base::ASCIIToUTF16("Main axis"), main_axis_values, 3, &vertical_pos);
cross_axis_alignment_ = CreateAndAddCombobox(
base::ASCIIToUTF16("Cross axis"), cross_axis_values, 4, &vertical_pos);
between_child_spacing_ =
CreateAndAddTextfield(base::ASCIIToUTF16("Child spacing"), &vertical_pos);
default_flex_ =
CreateAndAddTextfield(base::ASCIIToUTF16("Default flex"), &vertical_pos);
min_cross_axis_size_ = CreateAndAddTextfield(
base::ASCIIToUTF16("Min cross axis"), &vertical_pos);
CreateMarginsTextFields(base::ASCIIToUTF16("Insets"), &border_insets_,
&vertical_pos);
collapse_margins_ = CreateAndAddCheckbox(
base::ASCIIToUTF16("Collapse margins"), &vertical_pos);
UpdateLayoutManager();
}
void BoxLayoutExample::ButtonPressedImpl(Button* sender) {
if (sender == collapse_margins_) {
RefreshLayoutPanel(true);
}
}
void BoxLayoutExample::OnPerformAction(Combobox* combobox) {
if (combobox == orientation_) {
UpdateLayoutManager();
} else if (combobox == main_axis_alignment_) {
layout_->set_main_axis_alignment(static_cast<BoxLayout::MainAxisAlignment>(
main_axis_alignment_->GetSelectedIndex()));
} else if (combobox == cross_axis_alignment_) {
layout_->set_cross_axis_alignment(
static_cast<BoxLayout::CrossAxisAlignment>(
cross_axis_alignment_->GetSelectedIndex()));
}
RefreshLayoutPanel(false);
}
void BoxLayoutExample::ContentsChanged(Textfield* textfield,
const base::string16& new_contents) {
if (textfield == between_child_spacing_) {
......@@ -102,40 +53,97 @@ void BoxLayoutExample::ContentsChanged(Textfield* textfield,
RefreshLayoutPanel(false);
}
void BoxLayoutExample::UpdateBorderInsets() {
layout_->set_inside_border_insets(TextfieldsToInsets(border_insets_));
void BoxLayoutExample::ButtonPressedImpl(Button* sender) {
if (sender == collapse_margins_)
RefreshLayoutPanel(true);
}
void BoxLayoutExample::CreateAdditionalControls() {
constexpr const char* kOrientationValues[2] = {"Horizontal", "Vertical"};
orientation_ = CreateAndAddCombobox(
base::ASCIIToUTF16("Orientation"), kOrientationValues,
base::size(kOrientationValues),
base::BindRepeating(&LayoutExampleBase::RefreshLayoutPanel,
base::Unretained(this), true));
constexpr const char* kMainAxisValues[3] = {"Start", "Center", "End"};
main_axis_alignment_ = CreateAndAddCombobox(
base::ASCIIToUTF16("Main axis"), kMainAxisValues,
base::size(kMainAxisValues),
base::BindRepeating(&BoxLayoutExample::MainAxisAlignmentChanged,
base::Unretained(this)));
constexpr const char* kCrossAxisValues[4] = {"Stretch", "Start", "Center",
"End"};
cross_axis_alignment_ = CreateAndAddCombobox(
base::ASCIIToUTF16("Cross axis"), kCrossAxisValues,
base::size(kCrossAxisValues),
base::BindRepeating(&BoxLayoutExample::CrossAxisAlignmentChanged,
base::Unretained(this)));
between_child_spacing_ =
CreateAndAddTextfield(base::ASCIIToUTF16("Child spacing"));
default_flex_ = CreateAndAddTextfield(base::ASCIIToUTF16("Default flex"));
min_cross_axis_size_ =
CreateAndAddTextfield(base::ASCIIToUTF16("Min cross axis"));
CreateMarginsTextFields(base::ASCIIToUTF16("Insets"), &border_insets_);
collapse_margins_ =
CreateAndAddCheckbox(base::ASCIIToUTF16("Collapse margins"));
UpdateLayoutManager();
}
void BoxLayoutExample::UpdateLayoutManager() {
View* const panel = layout_panel();
int child_spacing;
int default_flex;
int min_cross_size;
base::StringToInt(between_child_spacing_->GetText(), &child_spacing);
base::StringToInt(default_flex_->GetText(), &default_flex);
base::StringToInt(min_cross_axis_size_->GetText(), &min_cross_size);
auto layout = std::make_unique<BoxLayout>(
layout_ = panel->SetLayoutManager(std::make_unique<BoxLayout>(
orientation_->GetSelectedIndex() == 0
? BoxLayout::Orientation::kHorizontal
: BoxLayout::Orientation::kVertical,
gfx::Insets(0, 0), child_spacing, collapse_margins_->GetChecked());
layout->set_cross_axis_alignment(static_cast<BoxLayout::CrossAxisAlignment>(
gfx::Insets(), child_spacing, collapse_margins_->GetChecked()));
layout_->set_cross_axis_alignment(static_cast<BoxLayout::CrossAxisAlignment>(
cross_axis_alignment_->GetSelectedIndex()));
layout->set_main_axis_alignment(static_cast<BoxLayout::MainAxisAlignment>(
layout_->set_main_axis_alignment(static_cast<BoxLayout::MainAxisAlignment>(
main_axis_alignment_->GetSelectedIndex()));
layout->SetDefaultFlex(default_flex);
layout->set_minimum_cross_axis_size(min_cross_size);
View* const panel = layout_panel();
layout_ = panel->SetLayoutManager(std::move(layout));
int default_flex;
base::StringToInt(default_flex_->GetText(), &default_flex);
layout_->SetDefaultFlex(default_flex);
int min_cross_size;
base::StringToInt(min_cross_axis_size_->GetText(), &min_cross_size);
layout_->set_minimum_cross_axis_size(min_cross_size);
UpdateBorderInsets();
for (View* child : panel->children()) {
ChildPanel* child_panel = static_cast<ChildPanel*>(child);
int flex = child_panel->GetFlex();
const int flex = static_cast<ChildPanel*>(child)->GetFlex();
if (flex < 0)
layout_->ClearFlexForView(child_panel);
layout_->ClearFlexForView(child);
else
layout_->SetFlexForView(child_panel, flex);
layout_->SetFlexForView(child, flex);
}
}
void BoxLayoutExample::UpdateBorderInsets() {
layout_->set_inside_border_insets(TextfieldsToInsets(border_insets_));
}
void BoxLayoutExample::MainAxisAlignmentChanged() {
layout_->set_main_axis_alignment(static_cast<BoxLayout::MainAxisAlignment>(
main_axis_alignment_->GetSelectedIndex()));
RefreshLayoutPanel(false);
}
void BoxLayoutExample::CrossAxisAlignmentChanged() {
layout_->set_cross_axis_alignment(static_cast<BoxLayout::CrossAxisAlignment>(
cross_axis_alignment_->GetSelectedIndex()));
RefreshLayoutPanel(false);
}
} // namespace examples
} // namespace views
......@@ -5,7 +5,6 @@
#ifndef UI_VIEWS_EXAMPLES_BOX_LAYOUT_EXAMPLE_H_
#define UI_VIEWS_EXAMPLES_BOX_LAYOUT_EXAMPLE_H_
#include "base/macros.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/examples/layout_example_base.h"
......@@ -22,24 +21,24 @@ namespace examples {
class VIEWS_EXAMPLES_EXPORT BoxLayoutExample : public LayoutExampleBase {
public:
BoxLayoutExample();
BoxLayoutExample(const BoxLayoutExample&) = delete;
BoxLayoutExample& operator=(const BoxLayoutExample&) = delete;
~BoxLayoutExample() override;
private:
// Set the border insets on the current BoxLayout instance.
void UpdateBorderInsets();
// LayoutExampleBase:
void OnPerformAction(Combobox* combobox) override;
// TextfieldController:
void ContentsChanged(Textfield* sender,
const base::string16& new_contents) override;
// LayoutExampleBase:
void ButtonPressedImpl(Button* sender) override;
void CreateAdditionalControls(int vertical_start_pos) override;
void CreateAdditionalControls() override;
void UpdateLayoutManager() override;
// Set the border insets on the current BoxLayout instance.
void UpdateBorderInsets();
void MainAxisAlignmentChanged();
void CrossAxisAlignmentChanged();
BoxLayout* layout_ = nullptr;
Combobox* orientation_ = nullptr;
Combobox* main_axis_alignment_ = nullptr;
......@@ -49,8 +48,6 @@ class VIEWS_EXAMPLES_EXPORT BoxLayoutExample : public LayoutExampleBase {
Textfield* min_cross_axis_size_ = nullptr;
InsetTextfields border_insets_;
Checkbox* collapse_margins_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(BoxLayoutExample);
};
} // namespace examples
......
......@@ -32,55 +32,6 @@ FlexLayoutExample::FlexLayoutExample() : LayoutExampleBase("Flex Layout") {}
FlexLayoutExample::~FlexLayoutExample() = default;
void FlexLayoutExample::CreateAdditionalControls(int vertical_pos) {
static const char* const orientation_values[2] = {"Horizontal", "Vertical"};
static const char* const main_axis_values[3] = {"Start", "Center", "End"};
static const char* const cross_axis_values[4] = {"Stretch", "Start", "Center",
"End"};
orientation_ = CreateAndAddCombobox(base::ASCIIToUTF16("Orientation"),
orientation_values, 2, &vertical_pos);
main_axis_alignment_ = CreateAndAddCombobox(
base::ASCIIToUTF16("Main axis"), main_axis_values, 3, &vertical_pos);
cross_axis_alignment_ = CreateAndAddCombobox(
base::ASCIIToUTF16("Cross axis"), cross_axis_values, 4, &vertical_pos);
CreateMarginsTextFields(base::ASCIIToUTF16("Interior margin"),
&interior_margin_, &vertical_pos);
CreateMarginsTextFields(base::ASCIIToUTF16("Default margins"),
&default_child_margins_, &vertical_pos);
collapse_margins_ = CreateAndAddCheckbox(
base::ASCIIToUTF16("Collapse margins"), &vertical_pos);
ignore_default_main_axis_margins_ = CreateAndAddCheckbox(
base::ASCIIToUTF16("Ignore main axis margins"), &vertical_pos);
layout_ = layout_panel()->SetLayoutManager(std::make_unique<FlexLayout>());
}
void FlexLayoutExample::OnPerformAction(Combobox* combobox) {
static const LayoutOrientation orientations[2] = {
LayoutOrientation::kHorizontal, LayoutOrientation::kVertical};
static const LayoutAlignment main_axis_alignments[3] = {
LayoutAlignment::kStart, LayoutAlignment::kCenter, LayoutAlignment::kEnd};
static const LayoutAlignment cross_axis_alignments[4] = {
LayoutAlignment::kStretch, LayoutAlignment::kStart,
LayoutAlignment::kCenter, LayoutAlignment::kEnd};
if (combobox == orientation_) {
layout_->SetOrientation(orientations[combobox->GetSelectedIndex()]);
} else if (combobox == main_axis_alignment_) {
layout_->SetMainAxisAlignment(
main_axis_alignments[combobox->GetSelectedIndex()]);
} else if (combobox == cross_axis_alignment_) {
layout_->SetCrossAxisAlignment(
cross_axis_alignments[combobox->GetSelectedIndex()]);
}
RefreshLayoutPanel(false);
}
void FlexLayoutExample::ContentsChanged(Textfield* sender,
const base::string16& new_contents) {
layout_->SetInteriorMargin(
......@@ -100,14 +51,51 @@ void FlexLayoutExample::ButtonPressedImpl(Button* sender) {
RefreshLayoutPanel(false);
}
void FlexLayoutExample::CreateAdditionalControls() {
constexpr const char* kOrientationValues[2] = {"Horizontal", "Vertical"};
orientation_ = CreateAndAddCombobox(
base::ASCIIToUTF16("Orientation"), kOrientationValues,
base::size(kOrientationValues),
base::BindRepeating(&FlexLayoutExample::OrientationChanged,
base::Unretained(this)));
constexpr const char* kMainAxisValues[3] = {"Start", "Center", "End"};
main_axis_alignment_ = CreateAndAddCombobox(
base::ASCIIToUTF16("Main axis"), kMainAxisValues,
base::size(kMainAxisValues),
base::BindRepeating(&FlexLayoutExample::MainAxisAlignmentChanged,
base::Unretained(this)));
constexpr const char* kCrossAxisValues[4] = {"Stretch", "Start", "Center",
"End"};
cross_axis_alignment_ = CreateAndAddCombobox(
base::ASCIIToUTF16("Cross axis"), kCrossAxisValues,
base::size(kCrossAxisValues),
base::BindRepeating(&FlexLayoutExample::CrossAxisAlignmentChanged,
base::Unretained(this)));
CreateMarginsTextFields(base::ASCIIToUTF16("Interior margin"),
&interior_margin_);
CreateMarginsTextFields(base::ASCIIToUTF16("Default margins"),
&default_child_margins_);
collapse_margins_ =
CreateAndAddCheckbox(base::ASCIIToUTF16("Collapse margins"));
ignore_default_main_axis_margins_ =
CreateAndAddCheckbox(base::ASCIIToUTF16("Ignore main axis margins"));
layout_ = layout_panel()->SetLayoutManager(std::make_unique<FlexLayout>());
}
void FlexLayoutExample::UpdateLayoutManager() {
for (View* child : layout_panel()->children()) {
ChildPanel* panel = static_cast<ChildPanel*>(child);
int flex = panel->GetFlex();
const int flex = static_cast<ChildPanel*>(child)->GetFlex();
if (flex < 0)
panel->ClearProperty(views::kFlexBehaviorKey);
child->ClearProperty(views::kFlexBehaviorKey);
else
panel->SetProperty(views::kFlexBehaviorKey, GetFlexSpecification(flex));
child->SetProperty(views::kFlexBehaviorKey, GetFlexSpecification(flex));
}
}
......@@ -121,5 +109,29 @@ FlexSpecification FlexLayoutExample::GetFlexSpecification(int weight) const {
.WithWeight(0);
}
void FlexLayoutExample::OrientationChanged() {
constexpr LayoutOrientation kOrientations[2] = {
LayoutOrientation::kHorizontal, LayoutOrientation::kVertical};
layout_->SetOrientation(kOrientations[orientation_->GetSelectedIndex()]);
RefreshLayoutPanel(false);
}
void FlexLayoutExample::MainAxisAlignmentChanged() {
constexpr LayoutAlignment kMainAxisAlignments[3] = {
LayoutAlignment::kStart, LayoutAlignment::kCenter, LayoutAlignment::kEnd};
layout_->SetMainAxisAlignment(
kMainAxisAlignments[main_axis_alignment_->GetSelectedIndex()]);
RefreshLayoutPanel(false);
}
void FlexLayoutExample::CrossAxisAlignmentChanged() {
constexpr LayoutAlignment kCrossAxisAlignments[4] = {
LayoutAlignment::kStretch, LayoutAlignment::kStart,
LayoutAlignment::kCenter, LayoutAlignment::kEnd};
layout_->SetCrossAxisAlignment(
kCrossAxisAlignments[cross_axis_alignment_->GetSelectedIndex()]);
RefreshLayoutPanel(false);
}
} // namespace examples
} // namespace views
......@@ -5,7 +5,6 @@
#ifndef UI_VIEWS_EXAMPLES_FLEX_LAYOUT_EXAMPLE_H_
#define UI_VIEWS_EXAMPLES_FLEX_LAYOUT_EXAMPLE_H_
#include "base/macros.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/textfield/textfield_controller.h"
......@@ -23,19 +22,24 @@ namespace examples {
class VIEWS_EXAMPLES_EXPORT FlexLayoutExample : public LayoutExampleBase {
public:
FlexLayoutExample();
FlexLayoutExample(const FlexLayoutExample&) = delete;
FlexLayoutExample& operator=(const FlexLayoutExample&) = delete;
~FlexLayoutExample() override;
private:
// LayoutExampleBase:
void OnPerformAction(Combobox* combobox) override;
void ContentsChanged(Textfield* sender,
const base::string16& new_contents) override;
void ButtonPressedImpl(Button* sender) override;
void CreateAdditionalControls(int vertical_start_pos) override;
void CreateAdditionalControls() override;
void UpdateLayoutManager() override;
FlexSpecification GetFlexSpecification(int weight) const;
void OrientationChanged();
void MainAxisAlignmentChanged();
void CrossAxisAlignmentChanged();
FlexLayout* layout_ = nullptr;
Combobox* orientation_ = nullptr;
Combobox* main_axis_alignment_ = nullptr;
......@@ -44,8 +48,6 @@ class VIEWS_EXAMPLES_EXPORT FlexLayoutExample : public LayoutExampleBase {
InsetTextfields interior_margin_;
InsetTextfields default_child_margins_;
Checkbox* ignore_default_main_axis_margins_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(FlexLayoutExample);
};
} // namespace examples
......
This diff is collapsed.
......@@ -41,16 +41,22 @@ class VIEWS_EXAMPLES_EXPORT LayoutExampleBase : public ExampleBase,
class ChildPanel : public View, public TextfieldController {
public:
explicit ChildPanel(LayoutExampleBase* example);
ChildPanel(const ChildPanel&) = delete;
ChildPanel& operator=(const ChildPanel&) = delete;
~ChildPanel() override;
// View
bool OnMousePressed(const ui::MouseEvent& event) override;
// View:
void Layout() override;
bool OnMousePressed(const ui::MouseEvent& event) override;
void SetSelected(bool value);
bool selected() const { return selected_; }
int GetFlex();
int GetFlex() const;
protected:
// View:
void OnThemeChanged() override;
private:
// TextfieldController
......@@ -64,11 +70,11 @@ class VIEWS_EXAMPLES_EXPORT LayoutExampleBase : public ExampleBase,
Textfield* flex_;
InsetTextfields margin_;
gfx::Size preferred_size_;
DISALLOW_COPY_AND_ASSIGN(ChildPanel);
};
explicit LayoutExampleBase(const char* title);
LayoutExampleBase(const LayoutExampleBase&) = delete;
LayoutExampleBase& operator=(const LayoutExampleBase&) = delete;
~LayoutExampleBase() override;
// Force the box_layout_panel_ to layout and repaint.
......@@ -82,29 +88,23 @@ class VIEWS_EXAMPLES_EXPORT LayoutExampleBase : public ExampleBase,
View* layout_panel() { return layout_panel_; }
// Creates and adds a Combobox with a label with |label_text| to the left.
// Adjust |vertical_pos| to |vertical_pos| + combo_box->height() + kSpacing.
// Sets |combobox_callback| as the callback for the created combobox.
Combobox* CreateAndAddCombobox(const base::string16& label_text,
const char* const* items,
int count,
int* vertical_pos);
base::RepeatingClosure combobox_callback);
// Creates and adds a Textfield with a label with |label_text| to the left.
// Adjusts |vertical_pos| to |vertical_pos| + combo_box->height() + kSpacing.
Textfield* CreateAndAddTextfield(const base::string16& label_text,
int* vertical_pos);
Textfield* CreateAndAddTextfield(const base::string16& label_text);
// Creates a set of labeled Textfields with |label_text|, and four text fields
// arranged at compass points representing a set of insets. |vertical_pos| is
// updated to the bottom of the last Textfield + kSpacing, and |textfields| is
// arranged at compass points representing a set of insets. |textfields| is
// populated with the fields that are created.
void CreateMarginsTextFields(const base::string16& label_text,
InsetTextfields* textfields,
int* vertical_pos);
InsetTextfields* textfields);
// Creates and adds a Checkbox with label |label_text|. Adjust |vertical_pos|
// to |vertical_pos| + checkbox->height() + kSpacing.
Checkbox* CreateAndAddCheckbox(const base::string16& label_text,
int* vertical_pos);
// Creates and adds a Checkbox with label |label_text|.
Checkbox* CreateAndAddCheckbox(const base::string16& label_text);
// ButtonListener:
// Be sure to call LayoutExampleBase::ButtonPressed() to ensure the "add"
......@@ -119,33 +119,22 @@ class VIEWS_EXAMPLES_EXPORT LayoutExampleBase : public ExampleBase,
gfx::Size GetNewChildPanelPreferredSize();
// Called by CreateExampleView() to create any additional controls required by
// the specific layout. |vertical_start_pos| tells the control where to start
// placing new controls (i.e. the bottom of the existing common controls).
virtual void CreateAdditionalControls(int vertical_start_pos) = 0;
// the specific layout.
virtual void CreateAdditionalControls() = 0;
// Handles buttons added by derived classes after button handling for
// common controls is done.
virtual void ButtonPressedImpl(Button* sender);
// Combobox callback defined by child classes.
virtual void OnPerformAction(views::Combobox* combobox) = 0;
virtual void ButtonPressedImpl(Button* sender) = 0;
// Performs layout-specific update of the layout manager.
virtual void UpdateLayoutManager() = 0;
private:
// Creates and adds a Textfield at the current position of |horizontal_pos|
// and |vertical_pos|. Update |horizontal_pos| to |horizontal_pos| +
// text_field->width() + kSpacing.
Textfield* CreateAndAddRawTextfield(int vertical_pos, int* horizontal_pos);
View* layout_panel_ = nullptr;
View* control_panel_ = nullptr;
LabelButton* add_button_ = nullptr;
Textfield* preferred_width_view_ = nullptr;
Textfield* preferred_height_view_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(LayoutExampleBase);
};
} // namespace examples
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment