FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
menu_entries.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// 本原始碼的使用受 MIT 授權條款約束,詳情請參閱 LICENSE 檔案。
3#include <functional> // for function
4#include <iostream> // for basic_ostream::operator<<, operator<<, endl, basic_ostream, basic_ostream<>::__ostream_type, cout, ostream
5#include <memory> // for allocator, shared_ptr, __shared_ptr_access
6#include <string> // for char_traits, to_string, operator+, string, basic_string
7
8#include "ftxui/component/captured_mouse.hpp" // for ftxui
9#include "ftxui/component/component.hpp" // for MenuEntry, Renderer, Vertical
10#include "ftxui/component/component_base.hpp" // for ComponentBase
11#include "ftxui/component/component_options.hpp" // for MenuEntryOption
12#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
13#include "ftxui/dom/elements.hpp" // for operator|, Element, separator, text, hbox, size, frame, color, vbox, HEIGHT, LESS_THAN, bold, border, inverted
14#include "ftxui/screen/color.hpp" // for Color, Color::Blue, Color::Cyan, Color::Green, Color::Red, Color::Yellow
15
16using namespace ftxui;
17
18// 為某些選單項目定義特殊樣式。
20 MenuEntryOption option;
21 option.transform = [c](EntryState state) {
22 state.label = (state.active ? "> " : " ") + state.label;
23 Element e = text(state.label) | color(c);
24 if (state.focused) {
25 e = e | inverted;
26 }
27 if (state.active) {
28 e = e | bold;
29 }
30 return e;
31 };
32 return option;
33}
34
35int main() {
37
38 int selected = 0;
39 auto menu = Container::Vertical(
40 {
41 MenuEntry(" 1. improve"),
42 MenuEntry(" 2. tolerant"),
43 MenuEntry(" 3. career"),
44 MenuEntry(" 4. cast"),
45 MenuEntry(" 5. question"),
46
47 Renderer([] { return separator(); }),
48
49 MenuEntry(" 6. rear", Colored(Color::Red)),
50 MenuEntry(" 7. drown", Colored(Color::Yellow)),
51 MenuEntry(" 8. nail", Colored(Color::Green)),
52 MenuEntry(" 9. quit", Colored(Color::Cyan)),
53 MenuEntry("10. decorative", Colored(Color::Blue)),
54
55 Renderer([] { return separator(); }),
56
57 MenuEntry("11. costume"),
58 MenuEntry("12. pick"),
59 MenuEntry("13. oral"),
60 MenuEntry("14. minister"),
61 MenuEntry("15. football"),
62 MenuEntry("16. welcome"),
63 MenuEntry("17. copper"),
64 MenuEntry("18. inhabitant"),
65 MenuEntry("19. fortune"),
66 },
67 &selected);
68
69 // 將選單與邊框一起顯示
70 auto renderer = Renderer(menu, [&] {
71 return vbox({
72 hbox(text("selected = "), text(std::to_string(selected))),
73 separator(),
74 menu->Render() | frame | size(HEIGHT, LESS_THAN, 10),
75 }) |
76 border;
77 });
78
79 screen.Loop(renderer);
80
81 std::cout << "Selected element = " << selected << std::endl;
82}
auto menu
Definition gallery.cpp:39
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)
Decorator size(WidthOrHeight, Constraint, int value)
限制元素的大小。
Element bold(Element)
使用粗體字型,用於需要更多強調的元素。
Definition bold.cpp:33
Element inverted(Element)
添加一個濾鏡,它將反轉前景和背景 顏色。
Definition inverted.cpp:34
Element text(std::wstring text)
顯示一段 Unicode 文字。
Definition text.cpp:160
Element separator()
在兩個元素之間繪製垂直或水平分隔線。
Decorator color(Color)
使用前景顏色進行裝飾。
Element vbox(Elements)
一個垂直一個接一個顯示元素的容器。
Definition vbox.cpp:95
Color 是一個在終端使用者介面中表示顏色的類別。
Definition color.hpp:20
MenuEntryOption Colored(ftxui::Color c)
int main()
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
std::shared_ptr< Node > Element
Definition elements.hpp:22
Element hbox(Elements)
一個逐一水平顯示元素的容器。
Definition hbox.cpp:94
@ LESS_THAN
Definition elements.hpp:159
來自 |ButtonOption|、|CheckboxOption|、 |RadioboxOption|、|MenuEntryOption|、|MenuOption| 的轉換參數。