FTXUI  3.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
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
10namespace ftxui {
11
12class Delegate;
13class Focus;
14struct Event;
15
16namespace animation {
17class Params;
18} // namespace animation
19
20class ComponentBase;
21using Component = std::shared_ptr<ComponentBase>;
22using Components = std::vector<Component>;
23
24/// @brief It implement rendering itself as ftxui::Element. It implement
25/// keyboard navigation by responding to ftxui::Event.
26/// @ingroup component
28 public:
29 // virtual Destructor.
30 virtual ~ComponentBase();
31
32 // Component hierarchy:
33 ComponentBase* Parent() const;
34 Component& ChildAt(size_t i);
35 size_t ChildCount() const;
36 void Add(Component children);
37 void Detach();
38 void DetachAllChildren();
39
40 // Renders the component.
41 virtual Element Render();
42
43 // Handles an event.
44 // By default, reduce on children with a lazy OR.
45 //
46 // Returns whether the event was handled or not.
47 virtual bool OnEvent(Event);
48
49 // Handle an animation step.
50 virtual void OnAnimation(animation::Params& params);
51
52 // Focus management ----------------------------------------------------------
53 //
54 // If this component contains children, this indicates which one is active,
55 // nullptr if none is active.
56 //
57 // We say an element has the focus if the chain of ActiveChild() from the
58 // root component contains this object.
59 virtual Component ActiveChild();
60
61 // Return true when the component contains focusable elements.
62 // The non focusable Component will be skipped when navigating using the
63 // keyboard.
64 virtual bool Focusable() const;
65
66 // Whether this is the active child of its parent.
67 bool Active() const;
68 // Whether all the ancestors are active.
69 bool Focused() const;
70
71 // Make the |child| to be the "active" one.
72 virtual void SetActiveChild(ComponentBase* child);
73 void SetActiveChild(Component child);
74
75 // Configure all the ancestors to give focus to this component.
76 void TakeFocus();
77
78 protected:
79 CapturedMouse CaptureMouse(const Event& event);
80
82
83 private:
84 ComponentBase* parent_ = nullptr;
85};
86
87} // namespace ftxui
88
89#endif /* end of include guard: FTXUI_COMPONENT_BASE_HPP */
90
91// Copyright 2020 Arthur Sonzogni. All rights reserved.
92// Use of this source code is governed by the MIT license that can be found in
93// 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:53
virtual Element Render()
Draw the component. Build a ftxui::Element to be drawn on the ftxi::Screen representing this ftxui::C...
Definition component.cpp:89
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:79
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:46
ComponentBase * Parent() const
Return the parent ComponentBase, or nul if any.
Definition component.cpp:33
virtual bool OnEvent(Event)
Called in response to an event.
void Detach()
Detach this child from its parent.
Definition component.cpp:63
Component & ChildAt(size_t i)
Access the child at index i.
Definition component.cpp:39
virtual ~ComponentBase()
Definition component.cpp:25
virtual void OnAnimation(animation::Params &params)
Called in response to an animation event.
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::shared_ptr< Node > Element
Definition elements.hpp:18
std::vector< Component > Components
std::shared_ptr< ComponentBase > Component
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:26