From 7f95d599540c4f336f7271526441d5bc308bbb3c Mon Sep 17 00:00:00 2001 From: Tushar Maheshwari Date: Wed, 21 Jul 2021 04:07:44 +0530 Subject: [PATCH] Deduplicate logic in ComponentBase members (#162) - Invoke DetachAllChildren from ~ComponentBase - Define Focused using Active - Compact TakeFocus loop code - const-correctness for Parent, Active and Focused --- include/ftxui/component/component_base.hpp | 8 +++---- src/ftxui/component/component.cpp | 25 ++++++++-------------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/include/ftxui/component/component_base.hpp b/include/ftxui/component/component_base.hpp index 9f786a8..78e380c 100644 --- a/include/ftxui/component/component_base.hpp +++ b/include/ftxui/component/component_base.hpp @@ -26,7 +26,7 @@ class ComponentBase { virtual ~ComponentBase(); // Component hierarchy: - ComponentBase* Parent(); + ComponentBase* Parent() const; Component& ChildAt(size_t i); size_t ChildCount() const; void Add(Component children); @@ -52,9 +52,9 @@ class ComponentBase { virtual Component ActiveChild(); // Whether this is the active child of its parent. - bool Active(); + bool Active() const; // Whether all the ancestors are active. - bool Focused(); + bool Focused() const; // Make the |child| to be the "active" one. virtual void SetActiveChild(ComponentBase* child); @@ -66,7 +66,7 @@ class ComponentBase { protected: CapturedMouse CaptureMouse(const Event& event); - std::vector children_; + Components children_; private: ComponentBase* parent_ = nullptr; diff --git a/src/ftxui/component/component.cpp b/src/ftxui/component/component.cpp index cd0c6cc..8c9ba56 100644 --- a/src/ftxui/component/component.cpp +++ b/src/ftxui/component/component.cpp @@ -18,15 +18,14 @@ class CaptureMouseImpl : public CapturedMouseInterface {}; } // namespace ComponentBase::~ComponentBase() { - while (children_.size() != 0) - children_.back()->Detach(); + DetachAllChildren(); } /// @brief Return the parent ComponentBase, or nul if any. /// @see Detach /// @see Parent /// @ingroup component -ComponentBase* ComponentBase::Parent() { +ComponentBase* ComponentBase::Parent() const { return parent_; } @@ -109,7 +108,7 @@ Component ComponentBase::ActiveChild() { /// @brief Returns if the element if the currently active child of its parent. /// @ingroup component -bool ComponentBase::Active() { +bool ComponentBase::Active() const { return !parent_ || parent_->ActiveChild().get() == this; } @@ -117,16 +116,12 @@ bool ComponentBase::Active() { /// True when the ComponentBase is focused by the user. An element is Focused /// when it is with all its ancestors the ActiveChild() of their parents. /// @ingroup component -bool ComponentBase::Focused() { - ComponentBase* current = this; - for (;;) { - ComponentBase* parent = current->parent_; - if (!parent) - return true; - if (parent->ActiveChild().get() != current) - return false; - current = parent; +bool ComponentBase::Focused() const { + auto current = this; + while (current && current->Active()) { + current = current->parent_; } + return !current; } /// @brief Make the |child| to be the "active" one. @@ -145,11 +140,9 @@ void ComponentBase::SetActiveChild(Component child) { /// @ingroup component void ComponentBase::TakeFocus() { ComponentBase* child = this; - ComponentBase* parent = parent_; - while (parent) { + while (ComponentBase* parent = child->parent_) { parent->SetActiveChild(child); child = parent; - parent = parent->parent_; } }