FTXUI  0.9.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
component.cpp
Go to the documentation of this file.
1#include <stddef.h> // for size_t
2#include <algorithm> // for find_if, max
3#include <cassert> // for assert
4#include <iterator> // for begin, end
5#include <utility> // for move
6#include <vector> // for vector, __alloc_traits<>::value_type
7
8#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse, CapturedMouseInterface
10#include "ftxui/component/component_base.hpp" // for ComponentBase, Component
11#include "ftxui/component/event.hpp" // for Event
12#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
13#include "ftxui/dom/elements.hpp" // for text, Element
14
15namespace ftxui {
16
17namespace {
18class CaptureMouseImpl : public CapturedMouseInterface {};
19} // namespace
20
24
25/// @brief Return the parent ComponentBase, or nul if any.
26/// @see Detach
27/// @see Parent
28/// @ingroup component
30 return parent_;
31}
32
33/// @brief Access the child at index `i`.
34/// @ingroup component
36 assert(i < ChildCount());
37 return children_[i];
38}
39
40/// @brief Returns the number of children.
41/// @ingroup component
43 return children_.size();
44}
45
46/// @brief Add a child.
47/// @@param child The child to be attached.
48/// @ingroup component
50 child->Detach();
51 child->parent_ = this;
52 children_.push_back(std::move(child));
53}
54
55/// @brief Detach this child from its parent.
56/// @see Detach
57/// @see Parent
58/// @ingroup component
60 if (!parent_)
61 return;
62 auto it = std::find_if(std::begin(parent_->children_), //
63 std::end(parent_->children_), //
64 [this](const Component& that) { //
65 return this == that.get();
66 });
67 parent_->children_.erase(it);
68 parent_ = nullptr;
69}
70
71/// @brief Remove all children.
72/// @ingroup component
74 while (!children_.empty())
75 children_[0]->Detach();
76}
77
78/// @brief Draw the component.
79/// Build a ftxui::Element to be drawn on the ftxi::Screen representing this
80/// ftxui::ComponentBase.
81/// @ingroup component
83 if (children_.size() == 1)
84 return children_.front()->Render();
85
86 return text("Not implemented component");
87}
88
89/// @brief Called in response to an event.
90/// @param event The event.
91/// @return True when the event has been handled.
92/// The default implementation called OnEvent on every child until one return
93/// true. If none returns true, return false.
94/// @ingroup component
96 for (Component& child : children_) {
97 if (child->OnEvent(event))
98 return true;
99 }
100 return false;
101}
102
103/// @brief Return the currently Active child.
104/// @return the currently Active child.
105/// @ingroup component
107 return children_.empty() ? nullptr : children_.front();
108}
109
110/// @brief Return true when the component contains focusable elements.
111/// The non focusable Components will be skipped when navigating using the
112/// keyboard.
113/// @ingroup component
115 for (const Component& child : children_) {
116 if (child->Focusable())
117 return true;
118 }
119 return false;
120}
121
122/// @brief Returns if the element if the currently active child of its parent.
123/// @ingroup component
125 return !parent_ || parent_->ActiveChild().get() == this;
126}
127
128/// @brief Returns if the elements if focused by the user.
129/// True when the ComponentBase is focused by the user. An element is Focused
130/// when it is with all its ancestors the ActiveChild() of their parents.
131/// @ingroup component
133 auto current = this;
134 while (current && current->Active()) {
135 current = current->parent_;
136 }
137 return !current;
138}
139
140/// @brief Make the |child| to be the "active" one.
141/// @param child the child to become active.
142/// @ingroup component
144
145/// @brief Make the |child| to be the "active" one.
146/// @param child the child to become active.
147/// @ingroup component
149 SetActiveChild(child.get());
150}
151
152/// @brief Configure all the ancestors to give focus to this component.
153/// @ingroup component
155 ComponentBase* child = this;
156 while (ComponentBase* parent = child->parent_) {
157 parent->SetActiveChild(child);
158 child = parent;
159 }
160}
161
162/// @brief Take the CapturedMouse if available. There is only one component of
163/// them. It represents a component taking priority over others.
164/// @param event
165/// @ingroup component
167 if (event.screen_)
168 return event.screen_->CaptureMouse();
169 return std::make_unique<CaptureMouseImpl>();
170}
171
172} // namespace ftxui
173
174// Copyright 2020 Arthur Sonzogni. All rights reserved.
175// Use of this source code is governed by the MIT license that can be found in
176// the LICENSE file.
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
virtual bool Focusable() const
Return true when the component contains focusable elements. The non focusable Components will be skip...
bool Focused() const
Returns if the elements if focused by the user. True when the ComponentBase is focused by the user....
CapturedMouse CaptureMouse(const Event &event)
Take the CapturedMouse if available. There is only one component of them. It represents a component t...
void Add(Component children)
Add a child. @param child The child to be attached.
Definition component.cpp:49
virtual Element Render()
Draw the component. Build a ftxui::Element to be drawn on the ftxi::Screen representing this ftxui::C...
Definition component.cpp:82
void TakeFocus()
Configure all the ancestors to give focus to this component.
bool Active() const
Returns if the element if the currently active child of its parent.
virtual Component ActiveChild()
Return the currently Active child.
void DetachAllChildren()
Remove all children.
Definition component.cpp:73
virtual void SetActiveChild(ComponentBase *child)
Make the |child| to be the "active" one.
size_t ChildCount() const
Returns the number of children.
Definition component.cpp:42
ComponentBase * Parent() const
Return the parent ComponentBase, or nul if any.
Definition component.cpp:29
virtual bool OnEvent(Event)
Called in response to an event.
Definition component.cpp:95
void Detach()
Detach this child from its parent.
Definition component.cpp:59
Component & ChildAt(size_t i)
Access the child at index i.
Definition component.cpp:35
virtual ~ComponentBase()
Definition component.cpp:21
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::shared_ptr< Node > Element
Definition elements.hpp:15
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:106
std::shared_ptr< ComponentBase > Component
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:25