Add ui helpers for finding a specific element type in element tree

This commit is contained in:
Nikita Lisitsa 2022-03-10 12:19:26 +03:00
parent b215d52289
commit 73d05a5ec4

View file

@ -88,4 +88,35 @@ namespace psemek::ui
void post_delayed_callbacks() const;
};
template <typename Type>
Type * find_first_parent_of_type(element * e)
{
while (true)
{
if (!e)
return nullptr;
if (auto p = dynamic_cast<Type *>(e))
return p;
e = e->parent();
}
}
template <typename Type>
Type * find_last_parent_of_type(element * e)
{
Type * r = nullptr;
while (true)
{
if (auto p = find_first_parent_of_type<Type>(e))
{
r = p;
e = p->parent();
}
else
return r;
}
}
}