FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
menu_entries.cpp
浏览该文件的文档.
1// 版权所有 2020 Arthur Sonzogni. 保留所有权利。
2// 此源代码的使用受 MIT 许可证的约束,该许可证可在
3// LICENSE 文件中找到。
4#include <functional> // for function
5#include <iostream> // for basic_ostream::operator<<, operator<<, endl, basic_ostream, basic_ostream<>::__ostream_type, cout, ostream
6#include <memory> // for allocator, shared_ptr, __shared_ptr_access
7#include <string> // for char_traits, to_string, operator+, string, basic_string
8
9#include "ftxui/component/captured_mouse.hpp" // for ftxui
10#include "ftxui/component/component.hpp" // for MenuEntry, Renderer, Vertical
11#include "ftxui/component/component_base.hpp" // for ComponentBase
12#include "ftxui/component/component_options.hpp" // for MenuEntryOption
13#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
14#include "ftxui/dom/elements.hpp" // for operator|, Element, separator, text, hbox, size, frame, color, vbox, HEIGHT, LESS_THAN, bold, border, inverted
15#include "ftxui/screen/color.hpp" // for Color, Color::Blue, Color::Cyan, Color::Green, Color::Red, Color::Yellow
16
17using namespace ftxui;
18
19// 为某些菜单项定义特殊样式。
21 MenuEntryOption option;
22 option.transform = [c](EntryState state) {
23 state.label = (state.active ? "> " : " ") + state.label;
24 Element e = text(state.label) | color(c);
25 if (state.focused) {
26 e = e | inverted;
27 }
28 if (state.active) {
29 e = e | bold;
30 }
31 return e;
32 };
33 return option;
34}
35
36int main() {
38
39 int selected = 0;
40 auto menu = Container::Vertical(
41 {
42 MenuEntry(" 1. improve"),
43 MenuEntry(" 2. tolerant"),
44 MenuEntry(" 3. career"),
45 MenuEntry(" 4. cast"),
46 MenuEntry(" 5. question"),
47
48 Renderer([] { return separator(); }),
49
50 MenuEntry(" 6. rear", Colored(Color::Red)),
51 MenuEntry(" 7. drown", Colored(Color::Yellow)),
52 MenuEntry(" 8. nail", Colored(Color::Green)),
53 MenuEntry(" 9. quit", Colored(Color::Cyan)),
54 MenuEntry("10. decorative", Colored(Color::Blue)),
55
56 Renderer([] { return separator(); }),
57
58 MenuEntry("11. costume"),
59 MenuEntry("12. pick"),
60 MenuEntry("13. oral"),
61 MenuEntry("14. minister"),
62 MenuEntry("15. football"),
63 MenuEntry("16. welcome"),
64 MenuEntry("17. copper"),
65 MenuEntry("18. inhabitant"),
66 MenuEntry("19. fortune"),
67 },
68 &selected);
69
70 // 将菜单与边框一起显示
71 auto renderer = Renderer(menu, [&] {
72 return vbox({
73 hbox(text("selected = "), text(std::to_string(selected))),
74 separator(),
75 menu->Render() | frame | size(HEIGHT, LESS_THAN, 10),
76 }) |
77 border;
78 });
79
80 screen.Loop(renderer);
81
82 std::cout << "Selected element = " << selected << std::endl;
83}
static ScreenInteractive TerminalOutput()
std::function< Element(const EntryState &state)> transform
Component MenuEntry(MenuEntryOption options)
一個特定的菜單條目。它們可以放入 Container::Vertical 中形成一個菜單。
Component Renderer(Component child, std::function< Element()>)
返回一个新组件,类似于 |child|,但使用 |render| 作为 Component::Render() 事件。
MenuEntry 组件的选项。
virtual void Render(Screen &screen)
在 ftxui::Screen 上显示元素。
Decorator size(WidthOrHeight, Constraint, int value)
对元素大小应用约束。
Element bold(Element)
使用粗体字体,用于强调元素。
Element inverted(Element)
添加一个过滤器,用于反转前景色和背景色。
Element text(std::wstring text)
显示一段Unicode文本。
Element separator()
在两个其他元素之间绘制垂直或水平分隔线。
Decorator color(Color)
使用前景色进行装饰。
Element vbox(Elements)
垂直一个接一个显示元素的容器。
Color 是一个表示终端用户界面中颜色的类。
MenuEntryOption Colored(ftxui::Color c)
int main()
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
std::shared_ptr< Node > Element
Element hbox(Elements)
一个按水平顺序逐一显示元素的容器。
来自 |ButtonOption|、|CheckboxOption|、 |RadioboxOption|、|MenuEntryOption|、|MenuOption| 的转换参数。