diff --git a/Common/UI/View.h b/Common/UI/View.h index 0a2b4b4137..23f85bd0fd 100644 --- a/Common/UI/View.h +++ b/Common/UI/View.h @@ -471,6 +471,9 @@ public: const std::string &Tag() const { return tag_; } void SetTag(std::string_view str) { tag_ = str; } + // The group this view was added to, if any. + ViewGroup *Parent() const { return parent_; } + // Fake RTTI virtual bool IsViewGroup() const { return false; } virtual bool ContainsSubview(const View *view) const { return false; } @@ -518,6 +521,9 @@ protected: DialogResult autoResult_ = DR_OK; private: + friend class ViewGroup; // Sets parent_. + ViewGroup *parent_ = nullptr; + std::function enabledFunc_; bool *enabledPtr_ = nullptr; bool enabled_ = true; diff --git a/Common/UI/ViewGroup.cpp b/Common/UI/ViewGroup.cpp index 6835ece25d..e7fcd9c37a 100644 --- a/Common/UI/ViewGroup.cpp +++ b/Common/UI/ViewGroup.cpp @@ -164,6 +164,7 @@ bool ViewGroup::ReplaceSubview(View *view, View *newView) { for (int i = 0; i < (int)views_.size(); i++) { if (views_[i] == view) { views_[i] = newView; + SetParentOf(newView); delete view; return true; } @@ -1042,8 +1043,7 @@ CollapsibleSection::CollapsibleSection(std::string_view title, LayoutParams *lay open_ = &localOpen_; SetSpacing(0.0f); - header_ = new CollapsibleHeader(open_, title); - views_.push_back(header_); + header_ = Add(new CollapsibleHeader(open_, title)); header_->OnClick.Add([=](UI::EventParams &) { // Change the visibility of all children except the first one. // Later maybe try something more ambitious. diff --git a/Common/UI/ViewGroup.h b/Common/UI/ViewGroup.h index 7ed8258e8f..8605d3deaa 100644 --- a/Common/UI/ViewGroup.h +++ b/Common/UI/ViewGroup.h @@ -53,11 +53,13 @@ public: template T *Add(T *view) { views_.push_back(view); + SetParentOf(view); return view; } template T *Insert(int index, T *view) { views_.insert(views_.begin() + index, view); + SetParentOf(view); return view; } @@ -108,6 +110,9 @@ protected: std::string DescribeListUnordered(std::string_view heading) const; std::string DescribeListOrdered(std::string_view heading) const; + // Anything that adds to views_ without Add or Insert must call this. + void SetParentOf(View *view) { view->parent_ = this; } + std::vector views_; View *defaultFocusView_ = nullptr; Drawable bg_;