2018-10-10 01:06:03 +08:00
|
|
|
#include "ftxui/component/component.hpp"
|
|
|
|
#include <assert.h>
|
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2019-01-13 01:24:46 +08:00
|
|
|
void Component::Detach() { if (!parent_) return; auto it = std::find(std::begin(parent_->children_),
|
|
|
|
std::end(parent_->children_), this);
|
|
|
|
parent_->children_.erase(it);
|
2018-10-10 01:06:03 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
void Component::Attach(Component* parent) {
|
|
|
|
Detach();
|
|
|
|
parent_ = parent;
|
|
|
|
parent_->children_.push_back(this);
|
|
|
|
}
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
void Component::Add(Component* child) {
|
|
|
|
child->Attach(this);
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
Component::~Component() {
|
|
|
|
Detach();
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
bool Component::OnEvent(Event event) {
|
|
|
|
for(Component* child : children_) {
|
|
|
|
if (child->OnEvent(event))
|
2018-10-10 01:06:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
Component* Component::ActiveChild() {
|
|
|
|
return children_.empty() ? nullptr : children_.front();
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
Element Component::Render() {
|
2019-01-13 05:25:49 +08:00
|
|
|
if (children_.size() == 1)
|
|
|
|
return children_.front()->Render();
|
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
return text(L"Not implemented component");
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
bool Component::Focused() {
|
|
|
|
Component* current = this;
|
|
|
|
for(;;) {
|
|
|
|
Component* parent = current->parent_;
|
|
|
|
if (!parent)
|
|
|
|
return true;
|
|
|
|
if (parent->ActiveChild() != current)
|
|
|
|
return false;
|
|
|
|
current = parent;
|
|
|
|
}
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
} // namespace ftxui
|