From 1887dc6de49d5cfba48ed73be346f158f023ff21 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 25 Apr 2020 17:15:04 +0200 Subject: 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. --- Libraries/LibGUI/ToolBarContainer.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'Libraries/LibGUI/ToolBarContainer.cpp') diff --git a/Libraries/LibGUI/ToolBarContainer.cpp b/Libraries/LibGUI/ToolBarContainer.cpp index 6d7bc8507c..4a453cacbe 100644 --- a/Libraries/LibGUI/ToolBarContainer.cpp +++ b/Libraries/LibGUI/ToolBarContainer.cpp @@ -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) { -- cgit v1.2.3