FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
examples/component/renderer.cpp
浏览该文件的文档.
1// 版权所有 2020 Arthur Sonzogni。保留所有权利。
2// 本源代码的使用受 MIT 许可证的约束,该许可证可在 LICENSE 文件中找到。
3#include <memory> // for shared_ptr, allocator, __shared_ptr_access
4
5#include "ftxui/component/captured_mouse.hpp" // for ftxui
6#include "ftxui/component/component.hpp" // for Renderer, Button, Vertical
7#include "ftxui/component/component_base.hpp" // for ComponentBase
8#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
9#include "ftxui/dom/elements.hpp" // for operator|, Element, text, bold, border, center, color
10#include "ftxui/screen/color.hpp" // for Color, Color::Red
11
12int main() {
13 using namespace ftxui;
14 auto screen = ScreenInteractive::FitComponent();
15
16 // Renderer() 是一个使用 lambda 函数作为参数来渲染自身的组件。
17 // 1. 可聚焦渲染器的示例:
18 auto renderer_focusable = Renderer([](bool focused) {
19 if (focused) {
20 return text("FOCUSABLE RENDERER()") | center | bold | border;
21 } else {
22 return text(" Focusable renderer() ") | center | border;
23 }
24 });
25
26 // 2. 不可聚焦渲染器的示例。
27 auto renderer_non_focusable = Renderer([&] {
28 return text("~~~~~ Non Focusable renderer() ~~~~~"); //
29 });
30
31 // 3. Renderer 可以包装其他组件以重新定义它们的 Render() 函数。
32 auto button = Button("Wrapped quit button", screen.ExitLoopClosure());
33 auto renderer_wrap = Renderer(button, [&] {
34 if (button->Focused()) {
35 return button->Render() | bold | color(Color::Red);
36 } else {
37 return button->Render();
38 }
39 });
40
41 // Let's renderer everyone:
42 screen.Loop(Container::Vertical({
43 renderer_focusable,
44 renderer_non_focusable,
45 renderer_wrap,
46 }));
47}
Element color(const LinearGradient &gradient, Element child)
使用线性渐变效果设置元素的前景色。
Element border(Element child)
在元素周围绘制边框。
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase