FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
component_options.cpp
浏览该文件的文档.
1// Copyright 2022 Arthur Sonzogni. All rights reserved.
2// 本源代码受MIT许可证的约束,该许可证可在LICENSE文件中找到。
4
5#include <ftxui/screen/color.hpp> // for Color, Color::White, Color::Black, Color::GrayDark, Color::Blue, Color::GrayLight, Color::Red
6#include <memory> // for shared_ptr
7#include <utility> // for move
8#include "ftxui/component/animation.hpp" // for Function, Duration
10#include "ftxui/dom/elements.hpp" // for operator|=, Element, text, bgcolor, inverted, bold, dim, operator|, color, borderEmpty, hbox, automerge, border, borderLight
11
12namespace ftxui {
13
14/// @brief 一个可动画的颜色选项。
15/// @params _inactive 当组件不活动时的颜色。
16/// @params _active 当组件活动时的颜色。
17/// @params _duration 动画的持续时间。
18/// @params _function 动画的缓动函数。
20 Color _active,
21 animation::Duration _duration,
23 enabled = true;
24 inactive = _inactive;
25 active = _active;
26 duration = _duration;
27 function = std::move(_function);
28}
29
30/// @brief 设置下划线应如何动画。
31/// @param d 动画的持续时间。
32/// @param f 动画的缓动函数。
38
39/// @brief 设置下划线应如何动画。
40/// @param d 动画的持续时间。
45
46/// @brief 设置下划线应如何动画。
47/// @param f 动画的缓动函数。
52
53/// @brief 设置下划线应如何动画。
54/// 这对于解除leader和follower的动画同步非常有用。
55/// @param f_leader leader的动画持续时间。
56/// @param f_follower follower的动画持续时间。
59 animation::easing::Function f_follower) {
60 leader_function = std::move(f_leader);
61 follower_function = std::move(f_follower);
62}
63
64/// @brief 水平菜单的标准选项。
65/// 这对于实现选项卡栏非常有用。
66// static
68 MenuOption option;
70 option.entries_option.transform = [](const EntryState& state) {
71 Element e = text(state.label);
72 if (state.focused) {
73 e |= inverted;
74 }
75 if (state.active) {
76 e |= bold;
77 }
78 if (!state.focused && !state.active) {
79 e |= dim;
80 }
81 return e;
82 };
83 option.elements_infix = [] { return text(" "); };
84
85 return option;
86}
87
88/// @brief 动画水平菜单的标准选项。
89/// 这对于实现选项卡栏非常有用。
90// static
92 auto option = Horizontal();
93 option.underline.enabled = true;
94 return option;
95}
96
97/// @brief 垂直菜单的标准选项。
98/// 这对于实现可选择项目列表非常有用。
99// static
101 MenuOption option;
102 option.entries_option.transform = [](const EntryState& state) {
103 Element e = text((state.active ? "> " : " ") + state.label); // NOLINT
104 if (state.focused) {
105 e |= inverted;
106 }
107 if (state.active) {
108 e |= bold;
109 }
110 if (!state.focused && !state.active) {
111 e |= dim;
112 }
113 return e;
114 };
115 return option;
116}
117
118/// @brief 动画垂直菜单的标准选项。
119/// 这对于实现可选择项目列表非常有用。
120// static
122 auto option = MenuOption::Vertical();
123 option.entries_option.transform = [](const EntryState& state) {
124 Element e = text(state.label);
125 if (state.focused) {
126 e |= inverted;
127 }
128 if (state.active) {
129 e |= bold;
130 }
131 if (!state.focused && !state.active) {
132 e |= dim;
133 }
134 return e;
135 };
136 option.underline.enabled = true;
137 return option;
138}
139
140/// @brief 带有分隔符的水平菜单的标准选项。
141/// 这对于实现选项卡栏非常有用。
142// static
144 auto option = MenuOption::Horizontal();
145 option.elements_infix = [] { return text("│") | automerge; };
146 return option;
147}
148
149/// @brief 创建一个ButtonOption,使用[]字符高亮显示。
150// static
152 ButtonOption option;
153 option.transform = [](const EntryState& s) {
154 const std::string t = s.focused ? "[" + s.label + "]" //
155 : " " + s.label + " ";
156 return text(t);
157 };
158 return option;
159}
160
161/// @brief 创建一个ButtonOption,聚焦时反转显示。
162// static
164 ButtonOption option;
165 option.transform = [](const EntryState& s) {
166 auto element = text(s.label) | borderLight;
167 if (s.focused) {
168 element |= inverted;
169 }
170 return element;
171 };
172 return option;
173}
174
175/// @brief 创建一个ButtonOption。按钮使用边框显示,聚焦时反转。这是当前的默认值。
177 ButtonOption option;
178 option.transform = [](const EntryState& s) {
179 auto element = text(s.label) | border;
180 if (s.active) {
181 element |= bold;
182 }
183 if (s.focused) {
184 element |= inverted;
185 }
186 return element;
187 };
188 return option;
189}
190
191/// @brief 创建一个使用动画颜色的ButtonOption。
192// static
197
198/// @brief 创建一个使用动画颜色的ButtonOption。
199// static
202 Color::Interpolate(0.85F, color, Color::Black), // NOLINT
203 Color::Interpolate(0.10F, color, Color::White), // NOLINT
204 Color::Interpolate(0.10F, color, Color::Black), // NOLINT
205 Color::Interpolate(0.85F, color, Color::White)); // NOLINT
206}
207
208/// @brief 创建一个使用动画颜色的ButtonOption。
209// static
211 // NOLINTBEGIN
213 /*bakground=*/background,
214 /*foreground=*/foreground,
215 /*background_active=*/foreground,
216 /*foreground_active=*/background);
217 // NOLINTEND
218}
219
220/// @brief 创建一个使用动画颜色的ButtonOption。
221// static
223 Color foreground,
224 Color background_active,
225 Color foreground_active) {
226 ButtonOption option;
227 option.transform = [](const EntryState& s) {
228 auto element = text(s.label) | borderEmpty;
229 if (s.focused) {
230 element |= bold;
231 }
232 return element;
233 };
234 option.animated_colors.foreground.Set(foreground, foreground_active);
235 option.animated_colors.background.Set(background, background_active);
236 return option;
237}
238
239/// @brief 标准Checkbox的选项。
240// static
242 auto option = CheckboxOption();
243 option.transform = [](const EntryState& s) {
244#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
245 // Microsoft终端不使用能够正确渲染默认单选框字形的字体。
246 auto prefix = text(s.state ? "[X] " : "[ ] "); // NOLINT
247#else
248 auto prefix = text(s.state ? "▣ " : "☐ "); // NOLINT
249#endif
250 auto t = text(s.label);
251 if (s.active) {
252 t |= bold;
253 }
254 if (s.focused) {
255 t |= inverted;
256 }
257 return hbox({prefix, t});
258 };
259 return option;
260}
261
262/// @brief 标准Radiobox的选项
263// static
265 auto option = RadioboxOption();
266 option.transform = [](const EntryState& s) {
267#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
268 // Microsoft终端不使用能够正确渲染默认单选框字形的字体。
269 auto prefix = text(s.state ? "(*) " : "( ) "); // NOLINT
270#else
271 auto prefix = text(s.state ? "◉ " : "○ "); // NOLINT
272#endif
273 auto t = text(s.label);
274 if (s.active) {
275 t |= bold;
276 }
277 if (s.focused) {
278 t |= inverted;
279 }
280 return hbox({prefix, t});
281 };
282 return option;
283}
284
285/// @brief 输入组件的标准选项。
286// static
288 InputOption option;
289 option.transform = [](InputState state) {
290 state.element |= color(Color::White);
291
292 if (state.is_placeholder) {
293 state.element |= dim;
294 }
295
296 if (state.focused) {
297 state.element |= inverted;
298 } else if (state.hovered) {
299 state.element |= bgcolor(Color::GrayDark);
300 }
301
302 return state.element;
303 };
304 return option;
305}
306
307/// @brief 更美观的输入组件的标准选项。
308// static
310 InputOption option;
311 option.transform = [](InputState state) {
312 state.element |= borderEmpty;
313 state.element |= color(Color::White);
314
315 if (state.is_placeholder) {
316 state.element |= dim;
317 }
318
319 if (state.focused) {
320 state.element |= bgcolor(Color::Black);
321 }
322
323 if (state.hovered) {
324 state.element |= bgcolor(Color::GrayDark);
325 }
326
327 return state.element;
328 };
329 return option;
330}
331
332} // namespace ftxui
static ButtonOption Animated()
创建一个使用动画颜色的ButtonOption。
static MenuOption Toggle()
带有分隔符的水平菜单的标准选项。 这对于实现选项卡栏非常有用。
animation::Duration follower_duration
animation::easing::Function leader_function
static InputOption Default()
创建默认输入样式:
animation::easing::Function function
static ButtonOption Border()
创建一个ButtonOption。按钮使用边框显示,聚焦时反转。这是当前的默认值。
void SetAnimationFunction(animation::easing::Function f)
设置下划线应如何动画。
static InputOption Spacious()
带有大边距的白底黑字样式:
static CheckboxOption Simple()
标准Checkbox的选项。
static ButtonOption Simple()
创建一个ButtonOption,聚焦时反转显示。
std::function< Element(const EntryState &state)> transform
static MenuOption Horizontal()
水平菜单的标准选项。 这对于实现选项卡栏非常有用。
static MenuOption VerticalAnimated()
动画垂直菜单的标准选项。 这对于实现可选择项目列表非常有用。
animation::Duration leader_duration
static MenuOption Vertical()
垂直菜单的标准选项。 这对于实现可选择项目列表非常有用。
static ButtonOption Ascii()
创建一个ButtonOption,使用[]字符高亮显示。
void SetAnimation(animation::Duration d, animation::easing::Function f)
设置下划线应如何动画。
void SetAnimationDuration(animation::Duration d)
设置下划线应如何动画。
animation::easing::Function follower_function
std::function< Element(InputState)> transform
std::function< Element()> elements_infix
AnimatedColorsOption animated_colors
void Set(Color inactive, Color active, animation::Duration duration=std::chrono::milliseconds(250), animation::easing::Function function=animation::easing::QuadraticInOut)
一个可动画的颜色选项。 @params _inactive 当组件不活动时的颜色。 @params _active 当组件活动时的颜色。 @params _duration 动画的持续时间。 @para...
static MenuOption HorizontalAnimated()
动画水平菜单的标准选项。 这对于实现选项卡栏非常有用。
static RadioboxOption Simple()
标准Radiobox的选项
std::function< Element(const EntryState &)> transform
AnimatedButton 组件的选项。
Checkbox 组件的选项。
Input 组件的选项。
Menu 组件的选项。
Radiobox 组件的选项。
Decorator bgcolor(Color)
使用背景色进行装饰。
Element bold(Element)
使用粗体字体,用于强调元素。
Element inverted(Element)
添加一个过滤器,用于反转前景色和背景色。
Element text(std::wstring text)
显示一段Unicode文本。
Element dim(Element)
使用浅色字体,用于不那么重要的元素。
定义 dim.cpp:32
Element automerge(Element child)
启用字符自动与附近的其它字符合并。
Decorator color(Color)
使用前景色进行装饰。
static Color Interpolate(float t, const Color &a, const Color &b)
Color 是一个表示终端用户界面中颜色的类。
std::function< float(float)> Function
std::chrono::duration< float > Duration
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
std::shared_ptr< Node > Element
Element borderEmpty(Element)
Element hbox(Elements)
一个按水平顺序逐一显示元素的容器。
Element border(Element)
Element borderLight(Element)
来自 |ButtonOption|、|CheckboxOption|、 |RadioboxOption|、|MenuEntryOption|、|MenuOption| 的转换参数。
用于定义 Input 组件的样式。