Add loop posting utilities in ui::element

This commit is contained in:
Nikita Lisitsa 2021-02-25 14:59:27 +03:00
parent e6b731f5d7
commit 8c04087a44
3 changed files with 24 additions and 1 deletions

View file

@ -51,6 +51,9 @@ namespace psemek::ui
virtual ~element() {} virtual ~element() {}
virtual void post(std::function<void()> f);
virtual void post_reshape();
private: private:
element * parent_ = nullptr; element * parent_ = nullptr;
async::executor * loop_ = nullptr; async::executor * loop_ = nullptr;

View file

@ -47,7 +47,7 @@ namespace psemek::ui
{ {
state_ = state_t::mousedown; state_ = state_t::mousedown;
if (callback_) if (callback_)
root()->loop()->post(callback_); post(callback_);
on_state_changed(); on_state_changed();
return true; return true;
} }

View file

@ -1,5 +1,7 @@
#include <psemek/ui/element.hpp> #include <psemek/ui/element.hpp>
#include <psemek/log/log.hpp>
namespace psemek::ui namespace psemek::ui
{ {
@ -34,4 +36,22 @@ namespace psemek::ui
return st; return st;
} }
void element::post(std::function<void()> f)
{
auto l = root()->loop();
if (l)
l->post(std::move(f));
else
log::warning() << "posting ui event while not attached to event loop";
}
void element::post_reshape()
{
auto weak_self = weak_from_this();
post([weak_self]{
if (auto self = weak_self.lock())
self->root()->reshape();
});
}
} }