FTXUI 7.0.3
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// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
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/app.hpp" // for App
10#include "ftxui/component/captured_mouse.hpp" // for ftxui
11#include "ftxui/component/component.hpp" // for MenuEntry, Renderer, Vertical
12#include "ftxui/component/component_base.hpp" // for ComponentBase
13#include "ftxui/component/component_options.hpp" // for MenuEntryOption
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// 為某些選單項目定義特殊的樣式。
20MenuEntryOption Colored(ftxui::Color c) {
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() {
37 auto screen = App::TerminalOutput();
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}
Component Renderer(Component child, std::function< Element()>)
回傳一個新的 Component,類似 |child|,但使用 |render| 作為 Component::Render() 事件。
Component MenuEntry(ConstStringRef label, MenuEntryOption options={})
一個特定的選單項目。可以將它們放入 Container::Vertical 以 組成一個選單。
Element text(std::string_view text)
顯示一段 UTF8 編碼的 Unicode 文字。
Decorator size(WidthOrHeight direction, Constraint constraint, int value)
對元素的大小套用限制。
Element bold(Element child)
使用粗體字型,用於需要更多強調的元素。
Definition bold.cpp:33
Element inverted(Element child)
添加一個濾鏡,它將反轉前景和背景 顏色。
Definition inverted.cpp:34
Element separator()
在兩個元素之間繪製垂直或水平分隔線。
Element color(Color color, Element child)
設定元素的前景顏色。
Definition dom/color.cpp:81
Element vbox(Elements children)
一個垂直一個接一個顯示元素的容器。
Definition vbox.cpp:96
MenuEntryOption Colored(ftxui::Color c)
int main()
FTXUI ftxui:: 命名空間
Definition animation.hpp:11
@ LESS_THAN
Definition elements.hpp:191
std::shared_ptr< Node > Element
Definition elements.hpp:25
Element hbox(Elements children)
一個逐一水平顯示元素的容器。
Definition hbox.cpp:94