FTXUI 7.0.3
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 コンポーネント|child|を装飾します。|show|がtrueを返す場合にのみ表示されます。
17/// @param child 装飾するコンポーネント。
18/// @param show |child|を表示するかどうかを返す関数。
19/// @ingroup component
20Component Maybe(Component child, std::function<bool()> show) {
21 class Impl : public ComponentBase {
22 public:
23 explicit Impl(std::function<bool()> show) : show_(std::move(show)) {}
24
25 private:
26 Element OnRender() override {
27 return show_() ? ComponentBase::OnRender() : std::make_unique<Node>();
28 }
29 bool Focusable() const override {
30 return show_() && ComponentBase::Focusable();
31 }
32 bool OnEvent(Event event) override {
33 return show_() && ComponentBase::OnEvent(event);
34 }
35
36 std::function<bool()> show_;
37 };
38
39 auto maybe = Make<Impl>(std::move(show));
40 maybe->Add(std::move(child));
41 return maybe;
42}
43
44/// @brief コンポーネントを装飾します。|show|関数がtrueを返す場合にのみ表示されます。
45/// @param show 装飾されたコンポーネントを表示するかどうかを返す関数。
46/// @ingroup component
47///
48/// ### 例
49///
50/// ```cpp
51/// auto component = Renderer([]{ return text("Hello World!"); });
52/// auto maybe_component = component | Maybe([&]{ return counter == 42; });
53/// ```
54ComponentDecorator Maybe(std::function<bool()> show) {
55 return [show = std::move(show)](Component child) mutable {
56 return Maybe(std::move(child), std::move(show));
57 };
58}
59
60/// @brief コンポーネント|child|を装飾します。|show|がtrueの場合にのみ表示されます。
61/// @param child 装飾するコンポーネント。
62/// @param show 論理値。|show|がtrueの場合に|child|が表示されます。
63/// @ingroup component
64///
65/// ### 例
66///
67/// ```cpp
68/// auto component = Renderer([]{ return text("Hello World!"); });
69/// auto maybe_component = Maybe(component, &show);
70/// ```
71Component Maybe(Component child, const bool* show) {
72 return Maybe(std::move(child), [show] { return *show; });
73}
74
75/// @brief コンポーネントを装飾します。|show|がtrueの場合にのみ表示されます。
76/// @param show 論理値。|show|がtrueの場合に|child|が表示されます。
77/// @ingroup component
78///
79/// ### 例
80///
81/// ```cpp
82/// auto component = Renderer([]{ return text("Hello World!"); });
83/// auto maybe_component = component | Maybe(&show);
84/// ```
86 return [show](Component child) { return Maybe(std::move(child), show); };
87}
88
89} // namespace ftxui
Component Maybe(Component child, std::function< bool()> show)
コンポーネント|child|を装飾します。|show|がtrueを返す場合にのみ表示されます。
FTXUI ftxui::名前空間
Definition animation.hpp:11
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:28
std::shared_ptr< Node > Element
Definition elements.hpp:25
const bool * show
std::function< Component(Component)> ComponentDecorator
Definition component.hpp:33
std::shared_ptr< ComponentBase > Component
Definition app.hpp:23