FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
src/ftxui/component/maybe.cpp
浏览该文件的文档.
1// 版权所有 2021 Arthur Sonzogni. 保留所有权利。
2// 此源代码的使用受 MIT 许可证的约束,该许可证可在
3// LICENSE 文件中找到。
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/// true。
18/// @param child 要装饰的组件。
19/// @param show 一个函数,返回 |child| 是否应该显示。
20/// @ingroup component
21Component 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 OnRender() override {
28 return show_() ? ComponentBase::OnRender() : 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 装饰一个组件。它仅在 |show| 函数返回 true 时显示。
46/// 返回 true。
47/// @param show 一个函数,返回被装饰的组件是否应该显示。
48/// 显示。
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/// ```
57ComponentDecorator 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 装饰一个组件 |child|。它仅在 |show| 为 true 时显示。
64/// @param child 要装饰的组件。
65/// @param show 一个布尔值。当 |show| 为 true 时,|child| 显示。
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/// ```
74Component Maybe(Component child, const bool* show) {
75 return Maybe(std::move(child), [show] { return *show; });
76}
77
78/// @brief 装饰一个组件。它仅在 |show| 为 true 时显示。
79/// @param show 一个布尔值。当 |show| 为 true 时,|child| 显示。
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/// ```
88ComponentDecorator Maybe(const bool* show) {
89 return [show](Component child) { return Maybe(std::move(child), show); };
90}
91
92} // namespace ftxui
它将自身实现为 ftxui::Element。它通过响应 ftxui::Event 来实现键盘导航。
Component Maybe(Component, const bool *show)
装饰一个组件 |child|。它仅在 |show| 为 true 时显示。
代表一个事件。它可以是按键事件、终端大小调整等等...
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
std::shared_ptr< T > Make(Args &&... args)
std::shared_ptr< Node > Element
std::function< Component(Component)> ComponentDecorator
std::shared_ptr< ComponentBase > Component