Add ui::single_container for wrapper elements

This commit is contained in:
Nikita Lisitsa 2022-03-10 12:21:17 +03:00
parent 4a6d3a9d88
commit e630316a05
8 changed files with 45 additions and 63 deletions

View file

@ -1,6 +1,6 @@
#pragma once
#include <psemek/ui/element.hpp>
#include <psemek/ui/single_container.hpp>
#include <functional>
@ -8,12 +8,8 @@ namespace psemek::ui
{
struct event_interceptor
: element
: single_container
{
children_range children() const override { return children_; }
virtual std::shared_ptr<element> set_child(std::shared_ptr<element> c);
geom::box<float, 2> size_constraints() const override;
struct shape const & shape() const override;
@ -32,9 +28,6 @@ namespace psemek::ui
void draw(painter &) const override {}
private:
std::shared_ptr<element> child_;
element * children_[1]{nullptr};
bool mouseover_ = false;
std::function<bool(mouse_move const &)> mouse_move_callback_;

View file

@ -1,17 +1,13 @@
#pragma once
#include <psemek/ui/element.hpp>
#include <psemek/ui/single_container.hpp>
namespace psemek::ui
{
struct frame
: element
: single_container
{
children_range children() const override { return children_; }
virtual std::shared_ptr<element> set_child(std::shared_ptr<element> c);
virtual void set_min_size(std::optional<geom::vector<float, 2>> const & size);
virtual void set_max_size(std::optional<geom::vector<float, 2>> const & size);
virtual void set_fixed_size(geom::vector<float, 2> const & size);
@ -24,9 +20,6 @@ namespace psemek::ui
private:
std::optional<geom::vector<float, 2>> min_size_;
std::optional<geom::vector<float, 2>> max_size_;
std::shared_ptr<element> child_;
element * children_[1]{nullptr};
};
}

View file

@ -1,18 +1,14 @@
#pragma once
#include <psemek/ui/element.hpp>
#include <psemek/ui/single_container.hpp>
#include <psemek/ui/box_shape.hpp>
namespace psemek::ui
{
struct scroller
: element
: single_container
{
children_range children() const override { return children_; }
virtual std::shared_ptr<element> set_child(std::shared_ptr<element> c);
enum class direction
{
horizontal,
@ -77,9 +73,6 @@ namespace psemek::ui
geom::vector<float, 2> shift_{0.f, 0.f};
geom::vector<float, 2> shift_tgt_{0.f, 0.f};
std::shared_ptr<element> child_;
element * children_[1]{nullptr};
void clamp_shift();
};

View file

@ -0,0 +1,21 @@
#pragma once
#include <psemek/ui/element.hpp>
namespace psemek::ui
{
struct single_container
: element
{
children_range children() const override { return children_; }
virtual std::shared_ptr<element> set_child(std::shared_ptr<element> c);
virtual std::shared_ptr<element> child() { return child_; }
protected:
std::shared_ptr<element> child_;
element * children_[1]{nullptr};
};
}

View file

@ -4,18 +4,6 @@
namespace psemek::ui
{
std::shared_ptr<element> event_interceptor::set_child(std::shared_ptr<element> c)
{
auto old = std::move(child_);
if (old) old->set_parent(nullptr);
child_ = std::move(c);
if (child_) child_->set_parent(this);
children_[0] = child_.get();
return old;
}
geom::box<float, 2> event_interceptor::size_constraints() const
{
if (child_)

View file

@ -3,18 +3,6 @@
namespace psemek::ui
{
std::shared_ptr<element> frame::set_child(std::shared_ptr<element> c)
{
auto old = std::move(child_);
if (old) old->set_parent(nullptr);
child_ = std::move(c);
if (child_) child_->set_parent(this);
children_[0] = child_.get();
return old;
}
void frame::set_min_size(std::optional<geom::vector<float, 2>> const & size)
{
min_size_ = size;

View file

@ -5,18 +5,6 @@
namespace psemek::ui
{
std::shared_ptr<element> scroller::set_child(std::shared_ptr<element> c)
{
auto old = std::move(child_);
if (old) old->set_parent(nullptr);
child_ = std::move(c);
if (child_) child_->set_parent(this);
children_[0] = child_.get();
return old;
}
void scroller::set_preferred_direction(direction d)
{
preferred_direction_ = d;

View file

@ -0,0 +1,18 @@
#include <psemek/ui/single_container.hpp>
namespace psemek::ui
{
std::shared_ptr<element> single_container::set_child(std::shared_ptr<element> c)
{
auto old = std::move(child_);
if (old) old->set_parent(nullptr);
child_ = std::move(c);
if (child_) child_->set_parent(this);
children_[0] = child_.get();
return old;
}
}