FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
examples/component/button.cpp
浏览该文件的文档.
1// 版权所有 2020 Arthur Sonzogni. 保留所有权利。
2// 本源代码的使用受 MIT 许可证的约束,该许可证可在
3// LICENSE 文件中找到。
4#include <memory> // for shared_ptr, __shared_ptr_access
5#include <string> // for operator+, to_string
6
7#include "ftxui/component/captured_mouse.hpp" // for ftxui
8#include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer
9#include "ftxui/component/component_base.hpp" // for ComponentBase
10#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
11#include "ftxui/dom/elements.hpp" // for separator, gauge, text, Element, operator|, vbox, border
12
13using namespace ftxui;
14
15// 这是一个辅助函数,用于创建具有自定义样式的按钮。
16// 样式由一个 lambda 函数定义,该函数接受一个 EntryState 并
17// 返回一个 Element。
18// 我们使用 `center` 来使文本在按钮内居中,然后使用 `border` 来
19// 在按钮周围添加边框,最后使用 `flex` 来使按钮填充
20// 可用空间。
22 auto option = ButtonOption::Animated();
23 option.transform = [](const EntryState& s) {
24 auto element = text(s.label);
25 if (s.focused) {
26 element |= bold;
27 }
28 return element | center | borderEmpty | flex;
29 };
30 return option;
31}
32
33int main() {
34 int value = 50;
35
36 // clang-format off
37 auto btn_dec_01 = Button("-1", [&] { value -= 1; }, Style());
38 auto btn_inc_01 = Button("+1", [&] { value += 1; }, Style());
39 auto btn_dec_10 = Button("-10", [&] { value -= 10; }, Style());
40 auto btn_inc_10 = Button("+10", [&] { value += 10; }, Style());
41 // clang-format on
42
43// 组件树。这定义了如何使用键盘导航。
44// 选定的 `row` 共享以获得网格布局。
45
46 // 修改它们在屏幕上的渲染方式:
47 auto component = Renderer(buttons, [&] {
48 return vbox({
49 text("value = " + std::to_string(value)),
50 separator(),
51 buttons->Render() | flex,
52 }) |
53 flex | border;
54 });
55
56 auto screen = ScreenInteractive::FitComponent();
57 screen.Loop(component);
58 return 0;
59}
ButtonOption Style()
static ButtonOption Animated()
创建一个使用动画颜色的ButtonOption。
static ScreenInteractive FitComponent()
创建一个 ScreenInteractive,其宽度和高度与正在绘制的组件匹配。
Component Button(ButtonOption options)
绘制一个按钮。点击时执行一个函数。
Component Renderer(Component child, std::function< Element()>)
返回一个新组件,类似于 |child|,但使用 |render| 作为 Component::Render() 事件。
AnimatedButton 组件的选项。
virtual void Render(Screen &screen)
在 ftxui::Screen 上显示元素。
Element flex(Element)
使子元素按比例扩展以填充容器中剩余的空间。
Element bold(Element)
使用粗体字体,用于强调元素。
Element text(std::wstring text)
显示一段Unicode文本。
Element separator()
在两个其他元素之间绘制垂直或水平分隔线。
Element vbox(Elements)
垂直一个接一个显示元素的容器。
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
来自 |ButtonOption|、|CheckboxOption|、 |RadioboxOption|、|MenuEntryOption|、|MenuOption| 的转换参数。