Skip to content

Commit

Permalink
Added possibility to sort widgets. (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
1aam2am1 authored Aug 18, 2020
1 parent cd6e814 commit 0c1e1c7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions include/TGUI/Container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ namespace tgui
return m_widgets;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Sorts a list of all the widgets in this container
///
/// @param function comparison function object (i.e. an object that satisfies the requirements of Compare) which
/// returns true if the first argument is less than (i.e. is ordered before) the second.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename Function>
void sortWidgets(Function&& function)
{
std::sort(m_widgets.begin(), m_widgets.end(), std::forward<Function>(function));
}

#ifndef TGUI_REMOVE_DEPRECATED_CODE
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns a list of the names of all the widgets in this container
Expand Down
16 changes: 16 additions & 0 deletions tests/Container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,22 @@ TEST_CASE("[Container]")
REQUIRE(widget3->getWidgetName() == "w003");
}

SECTION("sortWidgets")
{
REQUIRE(container->getWidgets().size() == 3);
widget1->setWidgetName("1");
widget2->setWidgetName("2");
widget3->setWidgetName("3");

container->getContainer()->sortWidgets([](const tgui::Widget::Ptr& p, const tgui::Widget::Ptr& p2){
return p->getWidgetName()[0] < p2->getWidgetName()[0];
});

REQUIRE(container->getWidgets()[0] == widget1);
REQUIRE(container->getWidgets()[1] == widget2);
REQUIRE(container->getWidgets()[2] == widget3);
}

SECTION("focus")
{
auto editBox1 = tgui::EditBox::create();
Expand Down

0 comments on commit 0c1e1c7

Please sign in to comment.