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