FTXUI  5.0.0
C++ functional terminal UI.
component_base.hpp
Go to the documentation of this file.
1 // Copyright 2020 Arthur Sonzogni. All rights reserved.
2 // Use of this source code is governed by the MIT license that can be found in
3 // the LICENSE file.
4 #ifndef FTXUI_COMPONENT_BASE_HPP
5 #define FTXUI_COMPONENT_BASE_HPP
6 
7 #include <memory> // for unique_ptr
8 #include <vector> // for vector
9 
10 #include "ftxui/component/captured_mouse.hpp" // for CaptureMouse
11 #include "ftxui/dom/elements.hpp" // for Element
12 
13 namespace ftxui {
14 
15 class Delegate;
16 class Focus;
17 struct Event;
18 
19 namespace animation {
20 class Params;
21 } // namespace animation
22 
23 class ComponentBase;
24 using Component = std::shared_ptr<ComponentBase>;
25 using Components = std::vector<Component>;
26 
27 /// @brief It implement rendering itself as ftxui::Element. It implement
28 /// keyboard navigation by responding to ftxui::Event.
29 /// @ingroup component
31  public:
32  explicit ComponentBase(Components children)
33  : children_(std::move(children)) {}
34  virtual ~ComponentBase();
35  ComponentBase() = default;
36 
37  // A component is not copyable/movable.
38  ComponentBase(const ComponentBase&) = delete;
42 
43  // Component hierarchy:
44  ComponentBase* Parent() const;
45  Component& ChildAt(size_t i);
46  size_t ChildCount() const;
47  int Index() const;
48  void Add(Component children);
49  void Detach();
50  void DetachAllChildren();
51 
52  // Renders the component.
53  virtual Element Render();
54 
55  // Handles an event.
56  // By default, reduce on children with a lazy OR.
57  //
58  // Returns whether the event was handled or not.
59  virtual bool OnEvent(Event);
60 
61  // Handle an animation step.
62  virtual void OnAnimation(animation::Params& params);
63 
64  // Focus management ----------------------------------------------------------
65  //
66  // If this component contains children, this indicates which one is active,
67  // nullptr if none is active.
68  //
69  // We say an element has the focus if the chain of ActiveChild() from the
70  // root component contains this object.
71  virtual Component ActiveChild();
72 
73  // Return true when the component contains focusable elements.
74  // The non focusable Component will be skipped when navigating using the
75  // keyboard.
76  virtual bool Focusable() const;
77 
78  // Whether this is the active child of its parent.
79  bool Active() const;
80  // Whether all the ancestors are active.
81  bool Focused() const;
82 
83  // Make the |child| to be the "active" one.
84  virtual void SetActiveChild(ComponentBase* child);
85  void SetActiveChild(Component child);
86 
87  // Configure all the ancestors to give focus to this component.
88  void TakeFocus();
89 
90  protected:
91  CapturedMouse CaptureMouse(const Event& event);
92 
94 
95  private:
96  ComponentBase* parent_ = nullptr;
97 };
98 
99 } // namespace ftxui
100 
101 #endif /* end of include guard: FTXUI_COMPONENT_BASE_HPP */
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...
Definition: component.cpp:158
bool Focused() const
Returns if the elements if focused by the user. True when the ComponentBase is focused by the user....
Definition: component.cpp:178
CapturedMouse CaptureMouse(const Event &event)
Take the CapturedMouse if available. There is only one component of them. It represents a component t...
Definition: component.cpp:212
void Add(Component children)
Add a child. @param child The child to be attached.
Definition: component.cpp:73
virtual Element Render()
Draw the component. Build a ftxui::Element to be drawn on the ftxi::Screen representing this ftxui::C...
Definition: component.cpp:109
void TakeFocus()
Configure all the ancestors to give focus to this component.
Definition: component.cpp:200
bool Active() const
Returns if the element if the currently active child of its parent.
Definition: component.cpp:169
virtual Component ActiveChild()
Return the currently Active child.
Definition: component.cpp:145
ComponentBase & operator=(const ComponentBase &)=delete
void DetachAllChildren()
Remove all children.
Definition: component.cpp:99
int Index() const
Return index of the component in its parent. -1 if no parent.
Definition: component.cpp:56
ComponentBase & operator=(ComponentBase &&)=delete
size_t ChildCount() const
Returns the number of children.
Definition: component.cpp:50
ComponentBase(ComponentBase &&)=delete
ComponentBase * Parent() const
Return the parent ComponentBase, or nul if any.
Definition: component.cpp:37
virtual void SetActiveChild(ComponentBase *child)
virtual bool OnEvent(Event)
Called in response to an event.
Definition: component.cpp:123
void Detach()
Detach this child from its parent.
Definition: component.cpp:83
ComponentBase(const ComponentBase &)=delete
Component & ChildAt(size_t i)
Access the child at index i.
Definition: component.cpp:43
ComponentBase(Components children)
virtual ~ComponentBase()
Definition: component.cpp:29
virtual void OnAnimation(animation::Params &params)
Called in response to an animation event.
Definition: component.cpp:136
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::shared_ptr< Node > Element
Definition: elements.hpp:22
std::shared_ptr< ComponentBase > Component
std::vector< Component > Components
Represent an event. It can be key press event, a terminal resize, or more ...
Definition: event.hpp:27