FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
src/ftxui/component/renderer.cpp
Go to the documentation of this file.
1// Copyright 2021 Arthur Sonzogni. All rights reserved.
2// 本原始碼的使用受 MIT 授權約束,詳見 LICENSE 檔案。
3#include <functional> // for function
4#include <utility> // for move
5
6#include "ftxui/component/component.hpp" // for Make, Renderer
7#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
8#include "ftxui/component/event.hpp" // for Event
9#include "ftxui/component/mouse.hpp" // for Mouse
10#include "ftxui/dom/elements.hpp" // for Element, operator|, reflect
11#include "ftxui/screen/box.hpp" // for Box
12
13namespace ftxui {
14
15/// @brief 建立一個元件,使用 |render| 來繪製其介面。
16/// @param render 繪製介面的函式。
17/// @ingroup component
18///
19/// ### 範例
20///
21/// ```cpp
22/// auto screen = ScreenInteractive::TerminalOutput();
23/// auto renderer = Renderer([] {
24/// return text("My interface");
25/// });
26/// screen.Loop(renderer);
27/// ```
28Component Renderer(std::function<Element()> render) {
29 class Impl : public ComponentBase {
30 public:
31 explicit Impl(std::function<Element()> render)
32 : render_(std::move(render)) {}
33 Element OnRender() override { return render_(); }
34 std::function<Element()> render_;
35 };
36
37 return Make<Impl>(std::move(render));
38}
39
40/// @brief 回傳一個新的元件,類似於 |child|,但使用 |render| 作為 Component::Render() 事件。
41/// @param child 用於轉發事件的元件。
42/// @param render 繪製介面的函式。
43/// @ingroup component
44///
45/// ### 範例
46///
47/// ```cpp
48/// auto screen = ScreenInteractive::TerminalOutput();
49/// std::string label = "Click to quit";
50/// auto button = Button(&label, screen.ExitLoopClosure());
51/// auto renderer = Renderer(button, [&] {
52/// return hbox({
53/// text("A button:"),
54/// button->Render(),
55/// });
56/// });
57/// screen.Loop(renderer);
58/// ```
59Component Renderer(Component child, std::function<Element()> render) {
60 Component renderer = Renderer(std::move(render));
61 renderer->Add(std::move(child));
62 return renderer;
63}
64
65/// @brief 建立一個可聚焦的元件,使用 |render| 來繪製其介面。
66/// @param render 繪製介面的函式,它接受一個布林值,表示該元件是否被聚焦。
67/// @ingroup component
68///
69/// ### 範例
70///
71/// ```cpp
72/// auto screen = ScreenInteractive::TerminalOutput();
73/// auto renderer = Renderer([] (bool focused) {
74/// if (focused)
75/// return text("My interface") | inverted;
76/// else
77/// return text("My interface");
78/// });
79/// screen.Loop(renderer);
80/// ```
81Component Renderer(std::function<Element(bool)> render) {
82 class Impl : public ComponentBase {
83 public:
84 explicit Impl(std::function<Element(bool)> render)
85 : render_(std::move(render)) {}
86
87 private:
88 Element OnRender() override { return render_(Focused()) | reflect(box_); }
89 bool Focusable() const override { return true; }
90 bool OnEvent(Event event) override {
91 if (event.is_mouse() && box_.Contain(event.mouse().x, event.mouse().y)) {
92 if (!CaptureMouse(event)) {
93 return false;
94 }
95
96 TakeFocus();
97 }
98
99 return false;
100 }
101 Box box_;
102
103 std::function<Element(bool)> render_;
104 };
105 return Make<Impl>(std::move(render));
106}
107
108/// @brief 裝飾一個元件,透過裝飾它所呈現的內容。
109/// @param decorator 修改其呈現元素的函式。
110/// @ingroup component
111///
112/// ### 範例
113///
114/// ```cpp
115/// auto screen = ScreenInteractive::TerminalOutput();
116/// auto renderer =
117// Renderer([] { return text("Hello");)
118/// | Renderer(bold)
119/// | Renderer(inverted);
120/// screen.Loop(renderer);
121/// ```
123 return [decorator](Component component) { // NOLINT
124 return Renderer(component, [component, decorator] {
125 return component->Render() | decorator;
126 });
127 };
128}
129
130} // namespace ftxui
auto component
Definition gallery.cpp:127
void Add(Component children)
新增一個子項。 @param child 要附加的子項。
Definition component.cpp:69
bool is_mouse() const
Definition event.hpp:107
struct Mouse mouse
Definition event.hpp:142
它將自己實作為 ftxui::Element 進行渲染。它透過回應 ftxui::Event 來實現鍵盤導航。
Component Renderer(Component child, std::function< Element()>)
回傳一個新的元件,類似於 |child|,但使用 |render| 作為 Component::Render() 事件。
代表一個事件。它可以是按鍵事件、終端機大小調整,或更多...
Definition event.hpp:27
Box 是一個表示二維空間中矩形區域的結構。
Definition box.hpp:14
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
std::shared_ptr< Node > Element
Definition elements.hpp:22
return Make< Impl >(option)
std::function< Element(Element)> ElementDecorator
Definition component.hpp:32
std::function< Component(Component)> ComponentDecorator
Definition component.hpp:31
std::shared_ptr< ComponentBase > Component