From 402b73f5cf50d2e49ac0a2ce0d95edbb01d7d37f Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 2 Nov 2021 00:08:10 +0400 Subject: [PATCH] Fix crash in TWidgetHelper::show/hideChildren. --- ui/rp_widget.cpp | 24 ++++++++++++++++++++++++ ui/rp_widget.h | 18 ++++++++---------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/ui/rp_widget.cpp b/ui/rp_widget.cpp index 318df7b..053360c 100644 --- a/ui/rp_widget.cpp +++ b/ui/rp_widget.cpp @@ -12,6 +12,30 @@ #include namespace Ui { +namespace { + +[[nodiscard]] std::vector> GetChildWidgets( + not_null widget) { + const auto &children = widget->children(); + auto result = std::vector>(); + result.reserve(children.size()); + for (const auto child : children) { + if (child && child->isWidgetType()) { + result.push_back(static_cast(child)); + } + } + return result; +} + +} // namespace + +void ToggleChildrenVisibility(not_null widget, bool visible) { + for (const auto &child : GetChildWidgets(widget)) { + if (child) { + child->setVisible(visible); + } + } +} void ResizeFitChild( not_null parent, diff --git a/ui/rp_widget.h b/ui/rp_widget.h index 263c59e..f1260b9 100644 --- a/ui/rp_widget.h +++ b/ui/rp_widget.h @@ -18,6 +18,12 @@ #include #include +namespace Ui { + +void ToggleChildrenVisibility(not_null widget, bool visible); + +} // namespace Ui + class TWidget; template @@ -30,18 +36,10 @@ public: } void hideChildren() { - for (auto child : Base::children()) { - if (child->isWidgetType()) { - static_cast(child)->hide(); - } - } + Ui::ToggleChildrenVisibility(this, false); } void showChildren() { - for (auto child : Base::children()) { - if (child->isWidgetType()) { - static_cast(child)->show(); - } - } + Ui::ToggleChildrenVisibility(this, true); } void moveToLeft(int x, int y, int outerw = 0) {