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 <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 
14 namespace ftxui {
15 
16 /// @brief Decorate a component |child|. It is shown only when |show| returns
17 /// true.
18 /// @param child the compoenent to decorate.
19 /// @param show a function returning whether |child| should shown.
20 /// @ingroup component
21 Component 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 Render() override {
28  return show_() ? ComponentBase::Render() : 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 /// ```
57 ComponentDecorator 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 compoennt 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 /// ```
74 Component 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 /// ```
88 ComponentDecorator 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...
virtual bool Focusable() const
Return true when the component contains focusable elements. The non focusable Components will be skip...
Definition: component.cpp:158
virtual Element Render()
Draw the component. Build a ftxui::Element to be drawn on the ftxi::Screen representing this ftxui::C...
Definition: component.cpp:109
virtual bool OnEvent(Event)
Called in response to an event.
Definition: component.cpp:123
Component Maybe(Component, const bool *show)
Decorate a component |child|. It is shown only when |show| is true.
Definition: maybe.cpp:74
std::shared_ptr< Node > Element
Definition: elements.hpp:22
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:31
Represent an event. It can be key press event, a terminal resize, or more ...
Definition: event.hpp:27