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// このソースコードの使用は、LICENSEファイルにあるMITライセンスに準拠しています。
3#include <functional> // for function
4#include <memory> // for make_unique, __shared_ptr_access, __shared_ptr_access<>::element_type, shared_ptr
5#include <utility> // for move
6
7#include "ftxui/component/component.hpp" // for ComponentDecorator, Maybe, Make
8#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
9#include "ftxui/component/event.hpp" // for Event
10#include "ftxui/dom/elements.hpp" // for Element
11#include "ftxui/dom/node.hpp" // for Node
12
13namespace ftxui {
14
15/// @brief コンポーネント|child|を装飾します。|show|がtrueを返す場合にのみ表示されます。
16/// @param child 装飾するコンポーネント。
17/// @param show |child|を表示するかどうかを返す関数。
18/// @ingroup component
19Component Maybe(Component child, std::function<bool()> show) {
20 class Impl : public ComponentBase {
21 public:
22 explicit Impl(std::function<bool()> show) : show_(std::move(show)) {}
23
24 private:
25 Element OnRender() override {
26 return show_() ? ComponentBase::OnRender() : std::make_unique<Node>();
27 }
28 bool Focusable() const override {
29 return show_() && ComponentBase::Focusable();
30 }
31 bool OnEvent(Event event) override {
32 return show_() && ComponentBase::OnEvent(event);
33 }
34 std::function<bool()> show_;
35 };
36
37 auto maybe = Make<Impl>(std::move(show));
38 maybe->Add(std::move(child));
39 return maybe;
40}
41
42/// @brief コンポーネントを装飾します。|show|関数がtrueを返す場合にのみ表示されます。
43/// @param show 装飾されたコンポーネントを表示するかどうかを返す関数。
44/// @ingroup component
45///
46/// ### 例
47///
48/// ```cpp
49/// auto component = Renderer([]{ return text("Hello World!"); });
50/// auto maybe_component = component | Maybe([&]{ return counter == 42; });
51/// ```
52ComponentDecorator Maybe(std::function<bool()> show) {
53 return [show = std::move(show)](Component child) mutable {
54 return Maybe(std::move(child), std::move(show));
55 };
56}
57
58/// @brief コンポーネント|child|を装飾します。|show|がtrueの場合にのみ表示されます。
59/// @param child 装飾するコンポーネント。
60/// @param show 論理値。|show|がtrueの場合に|child|が表示されます。
61/// @ingroup component
62///
63/// ### 例
64///
65/// ```cpp
66/// auto component = Renderer([]{ return text("Hello World!"); });
67/// auto maybe_component = Maybe(component, &show);
68/// ```
69Component Maybe(Component child, const bool* show) {
70 return Maybe(std::move(child), [show] { return *show; });
71}
72
73/// @brief コンポーネントを装飾します。|show|がtrueの場合にのみ表示されます。
74/// @param show 論理値。|show|がtrueの場合に|child|が表示されます。
75/// @ingroup component
76///
77/// ### 例
78///
79/// ```cpp
80/// auto component = Renderer([]{ return text("Hello World!"); });
81/// auto maybe_component = component | Maybe(&show);
82/// ```
83ComponentDecorator Maybe(const bool* show) {
84 return [show](Component child) { return Maybe(std::move(child), show); };
85}
86
87} // namespace ftxui
ftxui::Elementとして自身のレンダリングを実装します。ftxui::Eventに応答してキーボードナビゲーションを実装します。
Component Maybe(Component, const bool *show)
コンポーネント|child|を装飾します。|show|がtrueの場合にのみ表示されます。
イベントを表します。キープレスイベント、ターミナルのリサイズなど、さまざまなイベントがあります。
Definition event.hpp:28
FTXUI ftxui:: 名前空間
Definition animation.hpp:9
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
std::shared_ptr< Node > Element
Definition elements.hpp:21
std::shared_ptr< ComponentBase > Component