FTXUI  0.11.1
C++ functional terminal UI.
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 
13 namespace 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 /// ```
28 Component Renderer(std::function<Element()> render) {
29  class Impl : public ComponentBase {
30  public:
31  Impl(std::function<Element()> render) : render_(std::move(render)) {}
32  Element Render() override { return render_(); }
33  std::function<Element()> render_;
34  };
35 
36  return Make<Impl>(std::move(render));
37 }
38 
39 /// @brief Return a new Component, similar to |child|, but using |render| as the
40 /// Component::Render() event.
41 /// @param child The component to forward events to.
42 /// @param render The function drawing the interface.
43 /// @ingroup component
44 ///
45 /// ### Example
46 ///
47 /// ```cpp
48 /// auto screen = ScreenInteractive::TerminalOutput();
49 /// std::string label = "Click to quit";
50 /// auto button = Button(&label, screen.ExitLoopClosure());
51 /// auto renderer = Renderer(button, [&] {
52 /// return hbox({
53 /// text("A button:"),
54 /// button->Render(),
55 /// });
56 /// });
57 /// screen.Loop(renderer);
58 /// ```
59 Component Renderer(Component child, std::function<Element()> render) {
60  Component renderer = Renderer(std::move(render));
61  renderer->Add(std::move(child));
62  return renderer;
63 }
64 
65 /// @brief Return a focusable component, using |render| to render its interface.
66 /// @param render The function drawing the interface, taking a boolean telling
67 /// whether the component is focused or not.
68 /// @ingroup component
69 ///
70 /// ### Example
71 ///
72 /// ```cpp
73 /// auto screen = ScreenInteractive::TerminalOutput();
74 /// auto renderer = Renderer([] (bool focused) {
75 /// if (focused)
76 /// return text("My interface") | inverted;
77 /// else
78 /// return text("My interface");
79 /// });
80 /// screen.Loop(renderer);
81 /// ```
82 Component Renderer(std::function<Element(bool)> render) {
83  class Impl : public ComponentBase {
84  public:
85  Impl(std::function<Element(bool)> render) : render_(std::move(render)) {}
86 
87  private:
88  Element Render() override { return render_(Focused()) | reflect(box_); }
89  bool Focusable() const override { return true; }
90  bool OnEvent(Event event) override {
91  if (event.is_mouse() && box_.Contain(event.mouse().x, event.mouse().y)) {
92  if (!CaptureMouse(event))
93  return false;
94 
95  TakeFocus();
96  }
97 
98  return false;
99  }
100  Box box_;
101 
102  std::function<Element(bool)> render_;
103  };
104  return Make<Impl>(std::move(render));
105 }
106 
107 } // namespace ftxui
108 
109 // Copyright 2021 Arthur Sonzogni. All rights reserved.
110 // Use of this source code is governed by the MIT license that can be found in
111 // the LICENSE file.
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
std::shared_ptr< Node > Element
Definition: elements.hpp:16
std::shared_ptr< ComponentBase > Component
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:59
Decorator reflect(Box &box)
Definition: reflect.cpp:39
void Render(Screen &screen, const Element &node)
Display an element on a ftxui::Screen.
Definition: node.cpp:40
Represent an event. It can be key press event, a terminal resize, or more ...
Definition: event.hpp:25
bool is_mouse() const
Definition: event.hpp:62
struct Mouse & mouse()
Definition: event.hpp:63