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
13namespace ftxui {
14
15class Delegate;
16class Focus;
17struct Event;
18
19namespace animation {
20class Params;
21} // namespace animation
22
23class ComponentBase;
24using Component = std::shared_ptr<ComponentBase>;
25using 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 // virtual Destructor.
33 virtual ~ComponentBase();
34
35 ComponentBase() = default;
36
37 // A component is not copiable.
38 ComponentBase(const ComponentBase&) = delete;
39 void operator=(const ComponentBase&) = delete;
40
41 // Component hierarchy:
42 ComponentBase* Parent() const;
43 Component& ChildAt(size_t i);
44 size_t ChildCount() const;
45 void Add(Component children);
46 void Detach();
47 void DetachAllChildren();
48
49 // Renders the component.
50 virtual Element Render();
51
52 // Handles an event.
53 // By default, reduce on children with a lazy OR.
54 //
55 // Returns whether the event was handled or not.
56 virtual bool OnEvent(Event);
57
58 // Handle an animation step.
59 virtual void OnAnimation(animation::Params& params);
60
61 // Focus management ----------------------------------------------------------
62 //
63 // If this component contains children, this indicates which one is active,
64 // nullptr if none is active.
65 //
66 // We say an element has the focus if the chain of ActiveChild() from the
67 // root component contains this object.
68 virtual Component ActiveChild();
69
70 // Return true when the component contains focusable elements.
71 // The non focusable Component will be skipped when navigating using the
72 // keyboard.
73 virtual bool Focusable() const;
74
75 // Whether this is the active child of its parent.
76 bool Active() const;
77 // Whether all the ancestors are active.
78 bool Focused() const;
79
80 // Make the |child| to be the "active" one.
81 virtual void SetActiveChild(ComponentBase* child);
82 void SetActiveChild(Component child);
83
84 // Configure all the ancestors to give focus to this component.
85 void TakeFocus();
86
87 protected:
88 CapturedMouse CaptureMouse(const Event& event);
89
91
92 private:
93 ComponentBase* parent_ = nullptr;
94};
95
96} // namespace ftxui
97
98#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:141
bool Focused() const
Returns if the elements if focused by the user. True when the ComponentBase is focused by the user....
Definition: component.cpp:161
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:195
void Add(Component children)
Add a child. @param child The child to be attached.
Definition: component.cpp:56
virtual Element Render()
Draw the component. Build a ftxui::Element to be drawn on the ftxi::Screen representing this ftxui::C...
Definition: component.cpp:92
void TakeFocus()
Configure all the ancestors to give focus to this component.
Definition: component.cpp:183
bool Active() const
Returns if the element if the currently active child of its parent.
Definition: component.cpp:152
virtual Component ActiveChild()
Return the currently Active child.
Definition: component.cpp:128
void DetachAllChildren()
Remove all children.
Definition: component.cpp:82
virtual void SetActiveChild(ComponentBase *child)
Make the |child| to be the "active" one.
Definition: component.cpp:172
void operator=(const ComponentBase &)=delete
size_t ChildCount() const
Returns the number of children.
Definition: component.cpp:49
ComponentBase * Parent() const
Return the parent ComponentBase, or nul if any.
Definition: component.cpp:36
virtual bool OnEvent(Event)
Called in response to an event.
Definition: component.cpp:106
void Detach()
Detach this child from its parent.
Definition: component.cpp:66
ComponentBase(const ComponentBase &)=delete
Component & ChildAt(size_t i)
Access the child at index i.
Definition: component.cpp:42
virtual ~ComponentBase()
Definition: component.cpp:28
virtual void OnAnimation(animation::Params &params)
Called in response to an animation event.
Definition: component.cpp:119
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::shared_ptr< Node > Element
Definition: elements.hpp:23
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:29