FTXUI  5.0.0
C++ functional terminal UI.
maybe.cpp
Go to the documentation of this file.
1// Copyright 2021 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#include <functional> // for function
5#include <memory> // for make_unique, __shared_ptr_access, __shared_ptr_access<>::element_type, shared_ptr
6#include <type_traits> // for remove_reference, remove_reference<>::type
7#include <utility> // for move
8
9#include "ftxui/component/component.hpp" // for ComponentDecorator, Maybe, Make
10#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
11#include "ftxui/component/event.hpp" // for Event
12#include "ftxui/dom/elements.hpp" // for Element
13#include "ftxui/dom/node.hpp" // for Node
14
15namespace ftxui {
16
17/// @brief Decorate a component |child|. It is shown only when |show| returns
18/// true.
19/// @param child the compoenent to decorate.
20/// @param show a function returning whether |child| should shown.
21/// @ingroup component
22Component Maybe(Component child, std::function<bool()> show) {
23 class Impl : public ComponentBase {
24 public:
25 explicit Impl(std::function<bool()> show) : show_(std::move(show)) {}
26
27 private:
28 Element Render() override {
29 return show_() ? ComponentBase::Render() : std::make_unique<Node>();
30 }
31 bool Focusable() const override {
32 return show_() && ComponentBase::Focusable();
33 }
34 bool OnEvent(Event event) override {
35 return show_() && ComponentBase::OnEvent(event);
36 }
37
38 std::function<bool()> show_;
39 };
40
41 auto maybe = Make<Impl>(std::move(show));
42 maybe->Add(std::move(child));
43 return maybe;
44}
45
46/// @brief Decorate a component. It is shown only when the |show| function
47/// returns true.
48/// @param show a function returning whether the decorated component should
49/// be shown.
50/// @ingroup component
51///
52/// ### Example
53///
54/// ```cpp
55/// auto component = Renderer([]{ return text("Hello World!"); });
56/// auto maybe_component = component | Maybe([&]{ return counter == 42; });
57/// ```
58ComponentDecorator Maybe(std::function<bool()> show) {
59 return [show = std::move(show)](Component child) mutable {
60 return Maybe(std::move(child), std::move(show));
61 };
62}
63
64/// @brief Decorate a component |child|. It is shown only when |show| is true.
65/// @param child the compoennt to decorate.
66/// @param show a boolean. |child| is shown when |show| is true.
67/// @ingroup component
68///
69/// ### Example
70///
71/// ```cpp
72/// auto component = Renderer([]{ return text("Hello World!"); });
73/// auto maybe_component = Maybe(component, &show);
74/// ```
75Component Maybe(Component child, const bool* show) {
76 return Maybe(std::move(child), [show] { return *show; });
77}
78
79/// @brief Decorate a component. It is shown only when |show| is true.
80/// @param show a boolean. |child| is shown when |show| is true.
81/// @ingroup component
82///
83/// ### Example
84///
85/// ```cpp
86/// auto component = Renderer([]{ return text("Hello World!"); });
87/// auto maybe_component = component | Maybe(&show);
88/// ```
89ComponentDecorator Maybe(const bool* show) {
90 return [show](Component child) { return Maybe(std::move(child), show); };
91}
92
93} // namespace ftxui
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
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
virtual bool OnEvent(Event)
Called in response to an event.
Definition: component.cpp:106
Component Maybe(Component, const bool *show)
Decorate a component |child|. It is shown only when |show| is true.
Definition: maybe.cpp:75
std::shared_ptr< Node > Element
Definition: elements.hpp:23
std::shared_ptr< ComponentBase > Component
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition: node.cpp:47
std::function< Component(Component)> ComponentDecorator
Definition: component.hpp:33
Represent an event. It can be key press event, a terminal resize, or more ...
Definition: event.hpp:29