FTXUI  0.11.1
C++ functional terminal UI.
component_base.hpp
Go to the documentation of this file.
1 #ifndef FTXUI_COMPONENT_BASE_HPP
2 #define FTXUI_COMPONENT_BASE_HPP
3 
4 #include <memory> // for unique_ptr
5 #include <vector> // for vector
6 
7 #include "ftxui/component/captured_mouse.hpp" // for CaptureMouse
8 #include "ftxui/dom/elements.hpp" // for Element
9 
10 namespace ftxui {
11 
12 class Delegate;
13 class Focus;
14 struct Event;
15 
16 class ComponentBase;
17 using Component = std::shared_ptr<ComponentBase>;
18 using Components = std::vector<Component>;
19 
20 /// @brief It implement rendering itself as ftxui::Element. It implement
21 /// keyboard navigation by responding to ftxui::Event.
22 /// @ingroup component
24  public:
25  // virtual Destructor.
26  virtual ~ComponentBase();
27 
28  // Component hierarchy:
29  ComponentBase* Parent() const;
30  Component& ChildAt(size_t i);
31  size_t ChildCount() const;
32  void Add(Component children);
33  void Detach();
34  void DetachAllChildren();
35 
36  // Renders the component.
37  virtual Element Render();
38 
39  // Handles an event.
40  // By default, reduce on children with a lazy OR.
41  //
42  // Returns whether the event was handled or not.
43  virtual bool OnEvent(Event);
44 
45  // Focus management ----------------------------------------------------------
46  //
47  // If this component contains children, this indicates which one is active,
48  // nullptr if none is active.
49  //
50  // We say an element has the focus if the chain of ActiveChild() from the
51  // root component contains this object.
52  virtual Component ActiveChild();
53 
54  // Return true when the component contains focusable elements.
55  // The non focusable Component will be skipped when navigating using the
56  // keyboard.
57  virtual bool Focusable() const;
58 
59  // Whether this is the active child of its parent.
60  bool Active() const;
61  // Whether all the ancestors are active.
62  bool Focused() const;
63 
64  // Make the |child| to be the "active" one.
65  virtual void SetActiveChild(ComponentBase* child);
66  void SetActiveChild(Component child);
67 
68  // Configure all the ancestors to give focus to this component.
69  void TakeFocus();
70 
71  protected:
72  CapturedMouse CaptureMouse(const Event& event);
73 
75 
76  private:
77  ComponentBase* parent_ = nullptr;
78 };
79 
80 } // namespace ftxui
81 
82 #endif /* end of include guard: FTXUI_COMPONENT_BASE_HPP */
83 
84 // Copyright 2020 Arthur Sonzogni. All rights reserved.
85 // Use of this source code is governed by the MIT license that can be found in
86 // 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...
Definition: component.cpp:114
bool Focused() const
Returns if the elements if focused by the user. True when the ComponentBase is focused by the user....
Definition: component.cpp:132
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:166
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.
Definition: component.cpp:154
bool Active() const
Returns if the element if the currently active child of its parent.
Definition: component.cpp:124
virtual Component ActiveChild()
Return the currently Active child.
Definition: component.cpp:106
void DetachAllChildren()
Remove all children.
Definition: component.cpp:73
virtual void SetActiveChild(ComponentBase *child)
Make the |child| to be the "active" one.
Definition: component.cpp:143
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:16
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:25