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// 此原始碼的使用受 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/// @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/// ```
85ComponentDecorator Maybe(const bool* show) {
86 return [show](Component child) { return Maybe(std::move(child), show); };
87}
88
89} // namespace ftxui
它將自己實作為 ftxui::Element 進行渲染。它透過回應 ftxui::Event 來實現鍵盤導航。
Component Maybe(Component, const bool *show)
裝飾一個組件 |child|。它只在 |show| 為 true 時顯示。
代表一個事件。它可以是按鍵事件、終端機大小調整,或更多...
Definition event.hpp:27
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
std::shared_ptr< Node > Element
Definition elements.hpp:22
return Make< Impl >(option)
std::function< Component(Component)> ComponentDecorator
Definition component.hpp:31
std::shared_ptr< ComponentBase > Component