FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
src/ftxui/component/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 <utility> // for move
7
8#include "ftxui/component/component.hpp" // for ComponentDecorator, Maybe, Make
9#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
10#include "ftxui/component/event.hpp" // for Event
11#include "ftxui/dom/elements.hpp" // for Element
12#include "ftxui/dom/node.hpp" // for Node
13
14namespace ftxui {
15
16/// @brief Decorate a component |child|. It is shown only when |show| returns
17/// true.
18/// @param child the component to decorate.
19/// @param show a function returning whether |child| should shown.
20/// @ingroup component
21Component Maybe(Component child, std::function<bool()> show) {
22 class Impl : public ComponentBase {
23 public:
24 explicit Impl(std::function<bool()> show) : show_(std::move(show)) {}
25
26 private:
27 Element OnRender() override {
28 return show_() ? ComponentBase::OnRender() : std::make_unique<Node>();
29 }
30 bool Focusable() const override {
31 return show_() && ComponentBase::Focusable();
32 }
33 bool OnEvent(Event event) override {
34 return show_() && ComponentBase::OnEvent(event);
35 }
36
37 std::function<bool()> show_;
38 };
39
40 auto maybe = Make<Impl>(std::move(show));
41 maybe->Add(std::move(child));
42 return maybe;
43}
44
45/// @brief Decorate a component. It is shown only when the |show| function
46/// returns true.
47/// @param show a function returning whether the decorated component should
48/// be shown.
49/// @ingroup component
50///
51/// ### Example
52///
53/// ```cpp
54/// auto component = Renderer([]{ return text("Hello World!"); });
55/// auto maybe_component = component | Maybe([&]{ return counter == 42; });
56/// ```
57ComponentDecorator Maybe(std::function<bool()> show) {
58 return [show = std::move(show)](Component child) mutable {
59 return Maybe(std::move(child), std::move(show));
60 };
61}
62
63/// @brief Decorate a component |child|. It is shown only when |show| is true.
64/// @param child the component to decorate.
65/// @param show a boolean. |child| is shown when |show| is true.
66/// @ingroup component
67///
68/// ### Example
69///
70/// ```cpp
71/// auto component = Renderer([]{ return text("Hello World!"); });
72/// auto maybe_component = Maybe(component, &show);
73/// ```
74Component Maybe(Component child, const bool* show) {
75 return Maybe(std::move(child), [show] { return *show; });
76}
77
78/// @brief Decorate a component. It is shown only when |show| is true.
79/// @param show a boolean. |child| is shown when |show| is true.
80/// @ingroup component
81///
82/// ### Example
83///
84/// ```cpp
85/// auto component = Renderer([]{ return text("Hello World!"); });
86/// auto maybe_component = component | Maybe(&show);
87/// ```
88ComponentDecorator Maybe(const bool* show) {
89 return [show](Component child) { return Maybe(std::move(child), show); };
90}
91
92} // namespace ftxui
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
Component Maybe(Component, const bool *show)
Decorate a component |child|. It is shown only when |show| is true.
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:29
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
std::shared_ptr< Node > Element
Definition elements.hpp:22
std::function< Component(Component)> ComponentDecorator
Definition component.hpp:31
std::shared_ptr< ComponentBase > Component