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