FTXUI  4.1.1
C++ functional terminal UI.
Loading...
Searching...
No Matches
renderer.cpp
Go to the documentation of this file.
1#include <functional> // for function
2#include <memory> // for __shared_ptr_access, shared_ptr
3#include <utility> // for move
4
5#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
6#include "ftxui/component/component.hpp" // for Make, Renderer
7#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
8#include "ftxui/component/event.hpp" // for Event
9#include "ftxui/component/mouse.hpp" // for Mouse
10#include "ftxui/dom/elements.hpp" // for Element, operator|, reflect
11#include "ftxui/screen/box.hpp" // for Box
12
13namespace ftxui {
14
15/// @brief Return a component, using |render| to render its interface.
16/// @param render The function drawing the interface.
17/// @ingroup component
18///
19/// ### Example
20///
21/// ```cpp
22/// auto screen = ScreenInteractive::TerminalOutput();
23/// auto renderer = Renderer([] {
24/// return text("My interface");
25/// });
26/// screen.Loop(renderer);
27/// ```
28Component Renderer(std::function<Element()> render) {
29 class Impl : public ComponentBase {
30 public:
31 explicit Impl(std::function<Element()> render)
32 : render_(std::move(render)) {}
33 Element Render() override { return render_(); }
34 std::function<Element()> render_;
35 };
36
37 return Make<Impl>(std::move(render));
38}
39
40/// @brief Return a new Component, similar to |child|, but using |render| as the
41/// Component::Render() event.
42/// @param child The component to forward events to.
43/// @param render The function drawing the interface.
44/// @ingroup component
45///
46/// ### Example
47///
48/// ```cpp
49/// auto screen = ScreenInteractive::TerminalOutput();
50/// std::string label = "Click to quit";
51/// auto button = Button(&label, screen.ExitLoopClosure());
52/// auto renderer = Renderer(button, [&] {
53/// return hbox({
54/// text("A button:"),
55/// button->Render(),
56/// });
57/// });
58/// screen.Loop(renderer);
59/// ```
60Component Renderer(Component child, std::function<Element()> render) {
61 Component renderer = Renderer(std::move(render));
62 renderer->Add(std::move(child));
63 return renderer;
64}
65
66/// @brief Return a focusable component, using |render| to render its interface.
67/// @param render The function drawing the interface, taking a boolean telling
68/// whether the component is focused or not.
69/// @ingroup component
70///
71/// ### Example
72///
73/// ```cpp
74/// auto screen = ScreenInteractive::TerminalOutput();
75/// auto renderer = Renderer([] (bool focused) {
76/// if (focused)
77/// return text("My interface") | inverted;
78/// else
79/// return text("My interface");
80/// });
81/// screen.Loop(renderer);
82/// ```
83Component Renderer(std::function<Element(bool)> render) {
84 class Impl : public ComponentBase {
85 public:
86 explicit Impl(std::function<Element(bool)> render)
87 : render_(std::move(render)) {}
88
89 private:
90 Element Render() override { return render_(Focused()) | reflect(box_); }
91 bool Focusable() const override { return true; }
92 bool OnEvent(Event event) override {
93 if (event.is_mouse() && box_.Contain(event.mouse().x, event.mouse().y)) {
94 if (!CaptureMouse(event)) {
95 return false;
96 }
97
98 TakeFocus();
99 }
100
101 return false;
102 }
103 Box box_;
104
105 std::function<Element(bool)> render_;
106 };
107 return Make<Impl>(std::move(render));
108}
109
110/// @brief Decorate a component, by decorating what it renders.
111/// @param decorator the function modifying the element it renders.
112/// @ingroup component
113///
114/// ### Example
115///
116/// ```cpp
117/// auto screen = ScreenInteractive::TerminalOutput();
118/// auto renderer =
119// Renderer([] { return text("Hello");)
120/// | Renderer(bold)
121/// | Renderer(inverted);
122/// screen.Loop(renderer);
123/// ```
125 return [decorator](Component component) { // NOLINT
126 return Renderer(component, [component, decorator] {
127 return component->Render() | decorator;
128 });
129 };
130}
131
132} // namespace ftxui
133
134// Copyright 2021 Arthur Sonzogni. All rights reserved.
135// Use of this source code is governed by the MIT license that can be found in
136// the LICENSE file.
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:25
std::shared_ptr< Node > Element
Definition elements.hpp:19
std::function< Element(Element)> ElementDecorator
Definition component.hpp:31
Component Renderer(Component child, std::function< Element()>)
Return a new Component, similar to |child|, but using |render| as the Component::Render() event.
Definition renderer.cpp:60
Decorator reflect(Box &box)
Definition reflect.cpp:39
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:44
std::function< Component(Component)> ComponentDecorator
Definition component.hpp:30
std::shared_ptr< ComponentBase > Component
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:26
bool is_mouse() const
Definition event.hpp:68
struct Mouse & mouse()
Definition event.hpp:69