FTXUI  2.0.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
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, Components
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 ComponentBase* parent = parent_;
68 parent_ = nullptr;
69 parent->children_.erase(it); // Might delete |this|.
70}
71
72/// @brief Remove all children.
73/// @ingroup component
75 while (!children_.empty())
76 children_[0]->Detach();
77}
78
79/// @brief Draw the component.
80/// Build a ftxui::Element to be drawn on the ftxi::Screen representing this
81/// ftxui::ComponentBase.
82/// @ingroup component
84 if (children_.size() == 1)
85 return children_.front()->Render();
86
87 return text("Not implemented component");
88}
89
90/// @brief Called in response to an event.
91/// @param event The event.
92/// @return True when the event has been handled.
93/// The default implementation called OnEvent on every child until one return
94/// true. If none returns true, return false.
95/// @ingroup component
97 for (Component& child : children_) {
98 if (child->OnEvent(event))
99 return true;
100 }
101 return false;
102}
103
104/// @brief Return the currently Active child.
105/// @return the currently Active child.
106/// @ingroup component
108 return children_.empty() ? nullptr : children_.front();
109}
110
111/// @brief Return true when the component contains focusable elements.
112/// The non focusable Components will be skipped when navigating using the
113/// keyboard.
114/// @ingroup component
116 for (const Component& child : children_) {
117 if (child->Focusable())
118 return true;
119 }
120 return false;
121}
122
123/// @brief Returns if the element if the currently active child of its parent.
124/// @ingroup component
126 return !parent_ || parent_->ActiveChild().get() == this;
127}
128
129/// @brief Returns if the elements if focused by the user.
130/// True when the ComponentBase is focused by the user. An element is Focused
131/// when it is with all its ancestors the ActiveChild() of their parents.
132/// @ingroup component
134 auto current = this;
135 while (current && current->Active()) {
136 current = current->parent_;
137 }
138 return !current;
139}
140
141/// @brief Make the |child| to be the "active" one.
142/// @param child the child to become active.
143/// @ingroup component
145
146/// @brief Make the |child| to be the "active" one.
147/// @param child the child to become active.
148/// @ingroup component
150 SetActiveChild(child.get());
151}
152
153/// @brief Configure all the ancestors to give focus to this component.
154/// @ingroup component
156 ComponentBase* child = this;
157 while (ComponentBase* parent = child->parent_) {
158 parent->SetActiveChild(child);
159 child = parent;
160 }
161}
162
163/// @brief Take the CapturedMouse if available. There is only one component of
164/// them. It represents a component taking priority over others.
165/// @param event
166/// @ingroup component
168 if (event.screen_)
169 return event.screen_->CaptureMouse();
170 return std::make_unique<CaptureMouseImpl>();
171}
172
173} // namespace ftxui
174
175// Copyright 2020 Arthur Sonzogni. All rights reserved.
176// Use of this source code is governed by the MIT license that can be found in
177// 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:83
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:74
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:96
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:18
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
ScreenInteractive * screen_
Definition event.hpp:77