LibGUI: Make ToolBarContainer better at reacting to child events

Now you can remove a ToolBar from a ToolBarContainer and it will update
its own preferred size automatically.
This commit is contained in:
Andreas Kling 2020-04-25 17:15:04 +02:00
parent 9c772a64a1
commit 1887dc6de4
Notes: sideshowbarker 2024-07-19 07:18:10 +09:00
2 changed files with 28 additions and 7 deletions

View file

@ -32,9 +32,34 @@
namespace GUI {
void ToolBarContainer::child_event(Core::ChildEvent& event)
{
Frame::child_event(event);
if (event.type() == Core::Event::ChildAdded) {
if (event.child() && event.child()->is_widget())
did_add_toolbar((Widget&)*event.child());
} else if (event.type() == Core::Event::ChildRemoved) {
if (event.child() && event.child()->is_widget()) {
did_remove_toolbar((Widget&)*event.child());
}
}
}
void ToolBarContainer::did_remove_toolbar(Widget& toolbar)
{
m_toolbars.remove_first_matching([&](auto& entry) { return entry.ptr() == &toolbar; });
recompute_preferred_size();
}
void ToolBarContainer::did_add_toolbar(Widget& toolbar)
{
m_toolbars.append(toolbar);
recompute_preferred_size();
}
void ToolBarContainer::recompute_preferred_size()
{
int preferred_size = 4 + (m_toolbars.size() - 1) * 2;
for (auto& toolbar : m_toolbars) {

View file

@ -35,20 +35,16 @@ class ToolBarContainer : public Frame {
C_OBJECT(ToolBarContainer);
public:
template<class T, class... Args>
inline T& add(Args&&... args)
{
auto& child = Frame::add<T>(forward<Args>(args)...);
did_add_toolbar(child);
return child;
}
private:
explicit ToolBarContainer(Gfx::Orientation = Gfx::Orientation::Horizontal);
virtual void paint_event(GUI::PaintEvent&) override;
virtual void child_event(Core::ChildEvent&) override;
void did_add_toolbar(Widget&);
void did_remove_toolbar(Widget&);
void recompute_preferred_size();
Gfx::Orientation m_orientation { Gfx::Orientation::Horizontal };