FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
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#include "ftxui/util/export.hpp"
13
14namespace ftxui {
15
16class Delegate;
17class Focus;
18struct Event;
19
20namespace animation {
21class Params;
22} // namespace animation
23
24class ComponentBase;
25using Component = std::shared_ptr<ComponentBase>;
26using Components = std::vector<Component>;
27
28/// @brief It implement rendering itself as ftxui::Element. It implement
29/// keyboard navigation by responding to ftxui::Event.
30/// @ingroup component
31class FTXUI_EXPORT(COMPONENT) ComponentBase {
32 public:
33 explicit ComponentBase(Components children);
34 virtual ~ComponentBase();
35 ComponentBase();
36
37 // A component is not copyable/movable.
38 ComponentBase(const ComponentBase&) = delete;
39 ComponentBase(ComponentBase&&) = delete;
40 ComponentBase& operator=(const ComponentBase&) = delete;
41 ComponentBase& operator=(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 Element Render();
54
55 // Override this function modify how `Render` works.
56 virtual Element OnRender();
57
58 // Handles an event.
59 // By default, reduce on children with a lazy OR.
60 //
61 // Returns whether the event was handled or not.
62 virtual bool OnEvent(Event);
63
64 // Handle an animation step.
65 virtual void OnAnimation(animation::Params& params);
66
67 // Focus management ----------------------------------------------------------
68 //
69 // If this component contains children, this indicates which one is active,
70 // nullptr if none is active.
71 //
72 // We say an element has the focus if the chain of ActiveChild() from the
73 // root component contains this object.
74 virtual Component ActiveChild();
75
76 // Return true when the component contains focusable elements.
77 // The non focusable Component will be skipped when navigating using the
78 // keyboard.
79 virtual bool Focusable() const;
80
81 // Whether this is the active child of its parent.
82 bool Active() const;
83 // Whether all the ancestors are active.
84 bool Focused() const;
85
86 // Make the |child| to be the "active" one.
87 virtual void SetActiveChild(ComponentBase* child);
88 void SetActiveChild(Component child);
89
90 // Configure all the ancestors to give focus to this component.
91 void TakeFocus();
92
93 protected:
94 CapturedMouse CaptureMouse(const Event& event);
95
96 Components& children();
97 const Components& children() const;
98
99 private:
100 struct Impl;
101 std::unique_ptr<Impl> impl_;
102};
103
104} // namespace ftxui
105
106#endif /* end of include guard: FTXUI_COMPONENT_BASE_HPP */
The FTXUI ftxui::animation:: namespace.
The FTXUI ftxui:: namespace.
Definition animation.hpp:11
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::shared_ptr< Node > Element
Definition elements.hpp:24
std::vector< Component > Components
std::shared_ptr< ComponentBase > Component
Definition app.hpp:23