45Component& ComponentBase::ChildAt(
size_t i) {
46 assert(i < ChildCount());
52size_t ComponentBase::ChildCount()
const {
53 return children_.size();
58int ComponentBase::Index()
const {
59 if (parent_ ==
nullptr) {
63 for (
const Component& child : parent_->children_) {
64 if (child.get() ==
this) {
77 child->parent_ =
this;
78 children_.push_back(std::move(child));
85void ComponentBase::Detach() {
86 if (parent_ ==
nullptr) {
89 auto it = std::find_if(std::begin(parent_->children_),
90 std::end(parent_->children_),
92 return this == that.get();
96 parent->children_.erase(it);
101void ComponentBase::DetachAllChildren() {
102 while (!children_.empty()) {
103 children_[0]->Detach();
111Element ComponentBase::Render() {
122 class Wrapper :
public Node {
124 bool active_ =
false;
126 Wrapper(Element child,
bool active)
127 : Node({std::move(child)}), active_(active) {}
129 void SetBox(Box box)
override {
131 children_[0]->SetBox(box);
134 void ComputeRequirement()
override {
135 Node::ComputeRequirement();
136 requirement_.focused.component_active = active_;
140 return std::make_shared<Wrapper>(std::move(element), Active());
148 if (children_.size() == 1) {
149 return children_.front()->Render();
152 return text(
"Not implemented component");
163 if (child->OnEvent(event)) {
175 for (
const Component& child : children_) {
176 child->OnAnimation(params);
184 for (
auto& child : children_) {
185 if (child->Focusable()) {
196bool ComponentBase::Focusable()
const {
197 for (
const Component& child : children_) {
198 if (child->Focusable()) {
207bool ComponentBase::Active()
const {
208 return parent_ ==
nullptr || parent_->ActiveChild().get() ==
this;
216bool ComponentBase::Focused()
const {
217 const auto* current =
this;
218 while (current && current->Active()) {
219 current = current->parent_;
221 return !current && Focusable();
238void ComponentBase::TakeFocus() {
241 parent->SetActiveChild(child);
252 return event.screen_->CaptureMouse();
254 return std::make_unique<CaptureMouseImpl>();
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
virtual Component ActiveChild()
Return the currently Active child.
virtual void SetActiveChild(ComponentBase *child)
Make the |child| to be the "active" one.
virtual Element OnRender()
Draw the component. Build a ftxui::Element to be drawn on the ftxi::Screen representing this ftxui::C...
virtual bool OnEvent(Event)
Called in response to an event.
ComponentBase(Components children)
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::shared_ptr< Node > Element
Element text(std::wstring text)
Display a piece of unicode text.
std::shared_ptr< ComponentBase > Component
Represent an event. It can be key press event, a terminal resize, or more ...