#ifndef UI_VIEWS_EXAMPLES_LAYOUT_EXAMPLE_BASE_H_
#define UI_VIEWS_EXAMPLES_LAYOUT_EXAMPLE_BASE_H_
#include "base/containers/span.h"
#include "base/memory/raw_ptr.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/size.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/examples/example_base.h"
#include "ui/views/layout/delegating_layout_manager.h"
#include "ui/views/view.h"
namespace views {
class Combobox;
namespace examples {
class VIEWS_EXAMPLES_EXPORT LayoutExampleBase : public ExampleBase,
public TextfieldController {
public:
struct InsetTextfields {
void ResetControllers();
raw_ptr<Textfield> left = nullptr;
raw_ptr<Textfield> top = nullptr;
raw_ptr<Textfield> right = nullptr;
raw_ptr<Textfield> bottom = nullptr;
};
class ChildPanel : public View,
public TextfieldController,
public LayoutDelegate {
METADATA_HEADER(ChildPanel, View)
public:
explicit ChildPanel(LayoutExampleBase* example);
ChildPanel(const ChildPanel&) = delete;
ChildPanel& operator=(const ChildPanel&) = delete;
~ChildPanel() override;
bool OnMousePressed(const ui::MouseEvent& event) override;
ProposedLayout CalculateProposedLayout(
const SizeBounds& size_bounds) const override;
void SetSelected(bool value);
bool selected() const { return selected_; }
int GetFlex() const;
protected:
void OnThemeChanged() override;
private:
void ContentsChanged(Textfield* sender,
const std::u16string& new_contents) override;
Textfield* CreateTextfield(const std::u16string& name);
raw_ptr<LayoutExampleBase> example_;
bool selected_ = false;
raw_ptr<Textfield> flex_;
InsetTextfields margin_;
gfx::Size preferred_size_;
};
explicit LayoutExampleBase(const char* title);
LayoutExampleBase(const LayoutExampleBase&) = delete;
LayoutExampleBase& operator=(const LayoutExampleBase&) = delete;
~LayoutExampleBase() override;
void RefreshLayoutPanel(bool update_layout);
static gfx::Insets TextfieldsToInsets(
const InsetTextfields& textfields,
const gfx::Insets& default_insets = gfx::Insets());
protected:
View* layout_panel() { return layout_panel_; }
Combobox* CreateAndAddCombobox(const std::u16string& label_text,
base::span<const char* const> items,
base::RepeatingClosure combobox_callback);
Textfield* CreateAndAddTextfield(const std::u16string& label_text);
void CreateMarginsTextFields(const std::u16string& label_text,
InsetTextfields* textfields);
Checkbox* CreateAndAddCheckbox(const std::u16string& label_text,
base::RepeatingClosure checkbox_callback);
void CreateExampleView(View* container) final;
gfx::Size GetNewChildPanelPreferredSize();
virtual void CreateAdditionalControls() = 0;
virtual void UpdateLayoutManager() = 0;
private:
void AddButtonPressed();
raw_ptr<View> layout_panel_ = nullptr;
raw_ptr<View> control_panel_ = nullptr;
raw_ptr<LabelButton> add_button_ = nullptr;
raw_ptr<Textfield> preferred_width_view_ = nullptr;
raw_ptr<Textfield> preferred_height_view_ = nullptr;
};
}
}
#endif