FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
src/ftxui/component/dropdown.cpp
浏览该文件的文档.
1// Copyright 2021 Arthur Sonzogni. All rights reserved.
2// 本源代码的使用受MIT许可证的约束,该许可证可在LICENSE文件中找到。
4#include <functional> // for function
5#include <string> // for string
6
7#include <utility>
8#include "ftxui/component/component.hpp" // for Maybe, Checkbox, Make, Radiobox, Vertical, Dropdown
9#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
10#include "ftxui/component/component_options.hpp" // for CheckboxOption, EntryState
11#include "ftxui/dom/elements.hpp" // for operator|, Element, border, filler, operator|=, separator, size, text, vbox, frame, vscroll_indicator, hbox, HEIGHT, LESS_THAN, bold, inverted
12#include "ftxui/screen/util.hpp" // for clamp
13#include "ftxui/util/ref.hpp" // for ConstStringListRef
14
15namespace ftxui {
16
17/// @brief 一个下拉菜单。
18/// @ingroup component
19/// @param entries 要显示的条目列表。
20/// @param selected 选定条目的索引。
21Component Dropdown(ConstStringListRef entries, int* selected) {
22 DropdownOption option;
23 option.radiobox.entries = std::move(entries);
24 option.radiobox.selected = selected;
25 return Dropdown(option);
26}
27
28/// @brief 一个下拉菜单。
29/// @ingroup component
30/// @param option 下拉菜单的选项。
31// NOLINTNEXTLINE
33 class Impl : public ComponentBase, public DropdownOption {
34 public:
35 explicit Impl(DropdownOption option) : DropdownOption(std::move(option)) {
36 FillDefault();
37 checkbox_ = Checkbox(checkbox);
38 radiobox_ = Radiobox(radiobox);
39
40 Add(Container::Vertical({
41 checkbox_,
42 Maybe(radiobox_, checkbox.checked),
43 }));
44 }
45
46 Element OnRender() override {
47 selected_ =
48 util::clamp(radiobox.selected(), 0, int(radiobox.entries.size()) - 1);
49 selected_ = util::clamp(selected_(), 0, int(radiobox.entries.size()) - 1);
50
51 if (selected_() >= 0 && selected_() < int(radiobox.entries.size())) {
52 title_ = radiobox.entries[selected_()];
53 }
54
55 return transform(*open_, checkbox_->Render(), radiobox_->Render());
56 }
57
58 // 在选择时在复选框和单选框之间切换焦点。
59 bool OnEvent(ftxui::Event event) override {
60 const bool open_old = open_();
61 const int selected_old = selected_();
62 bool handled = ComponentBase::OnEvent(event);
63
64 // Transfer focus to the radiobox when the dropdown is opened.
65 if (!open_old && open_()) {
66 radiobox_->TakeFocus();
67 }
68
69 // 当用户选择一个项目时自动关闭下拉菜单,即使该项目与前一个相同。
70 if (open_old && open_()) {
71 const bool should_close =
72 (selected_() != selected_old) || //
73 (event == Event::Return) || //
74 (event == Event::Character(' ')) || //
75 (event == Event::Escape) || //
76 (event.is_mouse() && event.mouse().button == Mouse::Left &&
77 event.mouse().motion == Mouse::Pressed);
78
79 if (should_close) {
80 checkbox_->TakeFocus();
81 open_ = false;
82 handled = true;
83 }
84 }
85
86 return handled;
87 }
88
89 void FillDefault() {
90 open_ = checkbox.checked;
91 selected_ = radiobox.selected;
92 checkbox.checked = &*open_;
93 radiobox.selected = &*selected_;
94 checkbox.label = &title_;
95
96 if (!checkbox.transform) {
97 checkbox.transform = [](const EntryState& s) {
98 auto prefix = text(s.state ? "↓ " : "→ "); // NOLINT
99 auto t = text(s.label);
100 if (s.active) {
101 t |= bold;
102 }
103 if (s.focused) {
104 t |= inverted;
105 }
106 return hbox({prefix, t});
107 };
108 }
109
110 if (!transform) {
111 transform = [](bool is_open, Element checkbox_element,
112 Element radiobox_element) {
113 if (is_open) {
114 const int max_height = 12;
115 return vbox({
116 std::move(checkbox_element),
117 separator(),
118 std::move(radiobox_element) | vscroll_indicator | frame |
119 size(HEIGHT, LESS_THAN, max_height),
120 }) |
121 border;
122 }
123 return vbox({std::move(checkbox_element), filler()}) | border;
124 };
125 }
126 }
127
128 private:
129 Ref<bool> open_;
130 Ref<int> selected_;
131 Component checkbox_;
132 Component radiobox_;
133 std::string title_;
134 };
135
136 return Make<Impl>(option);
137}
138
139} // namespace ftxui
一个适配器。引用一个字符串列表。
一个适配器。拥有或引用一个可变对象。
定义 ref.hpp:46
struct Mouse mouse
它将自身实现为 ftxui::Element。它通过响应 ftxui::Event 来实现键盘导航。
Component Dropdown(ConstStringListRef entries, int *selected)
一个下拉菜单。
Dropdown 组件的选项。下拉菜单是打开/关闭单选框的复选框。
代表一个事件。它可以是按键事件、终端大小调整等等...
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
std::shared_ptr< T > Make(Args &&... args)
std::shared_ptr< Node > Element
std::shared_ptr< ComponentBase > Component
来自 |ButtonOption|、|CheckboxOption|、 |RadioboxOption|、|MenuEntryOption|、|MenuOption| 的转换参数。