Allows components to remove a child or access to children in general (#152)

Allows components to remove a child or access to children in general.

Co-authored-by: Felix Heitmann <fheitmann@se-gpu-03.intern.plath.de>
Co-authored-by: ArthurSonzogni <sonzogniarthur@gmail.com>
This commit is contained in:
Felix Heitmann
2021-07-15 15:29:33 +02:00
committed by GitHub
parent 23789c2d7b
commit c34494ce26
3 changed files with 171 additions and 23 deletions

View File

@@ -1,4 +1,6 @@
#include <stddef.h> // for size_t
#include <algorithm> // for find_if, max
#include <cassert> // for assert
#include <iterator> // for begin, end
#include <utility> // for move
@@ -24,7 +26,6 @@ ComponentBase::~ComponentBase() {
}
/// @brief Return the parent ComponentBase, or nul if any.
/// @see Attach
/// @see Detach
/// @see Parent
/// @ingroup component
@@ -32,7 +33,20 @@ ComponentBase* ComponentBase::Parent() {
return parent_;
}
/// @brief Add a children.
/// @brief Access the child at index `i`.
/// @ingroup component
Component& ComponentBase::ChildAt(size_t i) {
assert(i < ChildCount());
return children_[i];
}
/// @brief Returns the number of children.
/// @ingroup component
size_t ComponentBase::ChildCount() const {
return children_.size();
}
/// @brief Add a child.
/// @@param child The child to be attached.
/// @ingroup component
void ComponentBase::Add(Component child) {
@@ -41,6 +55,29 @@ void ComponentBase::Add(Component child) {
children_.push_back(std::move(child));
}
/// @brief Detach this child from its parent.
/// @see Detach
/// @see Parent
/// @ingroup component
void ComponentBase::Detach() {
if (!parent_)
return;
auto it = std::find_if(std::begin(parent_->children_), //
std::end(parent_->children_), //
[this](const Component& that) { //
return this == that.get();
});
parent_->children_.erase(it);
parent_ = nullptr;
}
/// @brief Remove all children.
/// @ingroup component
void ComponentBase::DetachAllChildren() {
while (!children_.empty())
children_[0]->Detach();
}
/// @brief Draw the component.
/// Build a ftxui::Element to be drawn on the ftxi::Screen representing this
/// ftxui::ComponentBase.
@@ -129,23 +166,6 @@ CapturedMouse ComponentBase::CaptureMouse(const Event& event) {
return event.screen_->CaptureMouse();
}
/// @brief Detach this children from its parent.
/// @see Attach
/// @see Detach
/// @see Parent
/// @ingroup component
void ComponentBase::Detach() {
if (!parent_)
return;
auto it = std::find_if(std::begin(parent_->children_), //
std::end(parent_->children_), //
[this](const Component& that) { //
return this == that.get();
});
parent_->children_.erase(it);
parent_ = nullptr;
}
} // namespace ftxui
// Copyright 2020 Arthur Sonzogni. All rights reserved.