98 lines
1.8 KiB
C++
98 lines
1.8 KiB
C++
#include <psemek/ui/element.hpp>
|
|
|
|
#include <psemek/util/recursive.hpp>
|
|
|
|
#include <psemek/log/log.hpp>
|
|
|
|
namespace psemek::ui
|
|
{
|
|
|
|
element * element::root()
|
|
{
|
|
element * r = this;
|
|
while (r->parent()) r = r->parent();
|
|
return r;
|
|
}
|
|
|
|
element const * element::root() const
|
|
{
|
|
element const * r = this;
|
|
while (r->parent()) r = r->parent();
|
|
return r;
|
|
}
|
|
|
|
async::event_loop * element::loop() const
|
|
{
|
|
element const * e = this;
|
|
while (!e->loop_ && e->parent()) e = e->parent();
|
|
return e->loop_;
|
|
}
|
|
|
|
void element::reshape()
|
|
{
|
|
reshape(shape().bbox());
|
|
}
|
|
|
|
geom::box<float, 2> element::size_constraints() const
|
|
{
|
|
static float const inf = std::numeric_limits<float>::infinity();
|
|
return {{{0.f, inf}, {0.f, inf}}};
|
|
}
|
|
|
|
std::shared_ptr<style const> element::set_style(std::shared_ptr<struct style const> st)
|
|
{
|
|
std::swap(st, style_);
|
|
|
|
auto visitor = util::recursive([](auto && self, element * e) -> void {
|
|
e->merged_style_ = nullptr;
|
|
for (auto c : e->children())
|
|
if (c) self(c);
|
|
});
|
|
visitor(this);
|
|
|
|
return st;
|
|
}
|
|
|
|
std::shared_ptr<struct style const> element::merged_style() const
|
|
{
|
|
if (!merged_style_)
|
|
{
|
|
if (!style_)
|
|
{
|
|
if (parent())
|
|
merged_style_ = parent()->merged_style();
|
|
else
|
|
merged_style_ = std::make_shared<struct style>(default_style());
|
|
}
|
|
else
|
|
{
|
|
auto merged_style = std::make_shared<struct style>(*style_);
|
|
if (parent())
|
|
merge(*merged_style, *parent()->merged_style());
|
|
else
|
|
merge(*merged_style, default_style());
|
|
|
|
merged_style_ = merged_style;
|
|
}
|
|
}
|
|
|
|
return merged_style_;
|
|
}
|
|
|
|
void element::post(std::function<void()> f)
|
|
{
|
|
auto l = loop();
|
|
if (l)
|
|
l->post(std::move(f));
|
|
}
|
|
|
|
void element::post_reshape()
|
|
{
|
|
auto weak_self = weak_from_this();
|
|
post([weak_self]{
|
|
if (auto self = weak_self.lock())
|
|
self->root()->reshape();
|
|
});
|
|
}
|
|
|
|
}
|