FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
component_options.cpp
Go to the documentation of this file.
1// Copyright 2022 Arthur Sonzogni. 版權所有。
2// 本原始碼受 MIT 授權條款約束,詳情請參閱
3// LICENSE 文件。
5
6#include <ftxui/screen/color.hpp> // for Color, Color::White, Color::Black, Color::GrayDark, Color::Blue, Color::GrayLight, Color::Red
7#include <memory> // for shared_ptr
8#include <utility> // for move
9#include "ftxui/component/animation.hpp" // for Function, Duration
11#include "ftxui/dom/elements.hpp" // for operator|=, Element, text, bgcolor, inverted, bold, dim, operator|, color, borderEmpty, hbox, automerge, border, borderLight
12
13namespace ftxui {
14
15/// @brief 可動畫的顏色選項。
16/// @params _inactive 當組件不活動時的顏色。
17/// @params _active 當組件活動時的顏色。
18/// @params _duration 動畫的持續時間。
19/// @params _function 動畫的緩動函數。
21 Color _active,
22 animation::Duration _duration,
24 enabled = true;
25 inactive = _inactive;
26 active = _active;
27 duration = _duration;
28 function = std::move(_function);
29}
30
31/// @brief 設定下劃線的動畫方式。
32/// @param d 動畫的持續時間。
33/// @param f 動畫的緩動函數。
39
40/// @brief 設定下劃線的動畫方式。
41/// @param d 動畫的持續時間。
46
47/// @brief 設定下劃線的動畫方式。
48/// @param f 動畫的緩動函數。
53
54/// @brief 設定下劃線的動畫方式。
55/// 這對於使引導者和追隨者的動畫不同步很有用。
56/// @param f_leader 引導者動畫的持續時間。
57/// @param f_follower 追隨者動畫的持續時間。
60 animation::easing::Function f_follower) {
61 leader_function = std::move(f_leader);
62 follower_function = std::move(f_follower);
63}
64
65/// @brief 水平選單的標準選項。
66/// 這對於實現一個標籤頁列很有用。
67// static
69 MenuOption option;
71 option.entries_option.transform = [](const EntryState& state) {
72 Element e = text(state.label);
73 if (state.focused) {
74 e |= inverted;
75 }
76 if (state.active) {
77 e |= bold;
78 }
79 if (!state.focused && !state.active) {
80 e |= dim;
81 }
82 return e;
83 };
84 option.elements_infix = [] { return text(" "); };
85
86 return option;
87}
88
89/// @brief 動畫水平選單的標準選項。
90/// 這對於實現一個標籤頁列很有用。
91// static
93 auto option = Horizontal();
94 option.underline.enabled = true;
95 return option;
96}
97
98/// @brief 垂直選單的標準選項。
99/// 這對於實現一個可選項目列表很有用。
100// static
102 MenuOption option;
103 option.entries_option.transform = [](const EntryState& state) {
104 Element e = text((state.active ? "> " : " ") + state.label); // NOLINT
105 if (state.focused) {
106 e |= inverted;
107 }
108 if (state.active) {
109 e |= bold;
110 }
111 if (!state.focused && !state.active) {
112 e |= dim;
113 }
114 return e;
115 };
116 return option;
117}
118
119/// @brief 動畫垂直選單的標準選項。
120/// 這對於實現一個可選項目列表很有用。
121// static
123 auto option = MenuOption::Vertical();
124 option.entries_option.transform = [](const EntryState& state) {
125 Element e = text(state.label);
126 if (state.focused) {
127 e |= inverted;
128 }
129 if (state.active) {
130 e |= bold;
131 }
132 if (!state.focused && !state.active) {
133 e |= dim;
134 }
135 return e;
136 };
137 option.underline.enabled = true;
138 return option;
139}
140
141/// @brief 帶有分隔線的水平選單標準選項。
142/// 這對於實現一個標籤頁列很有用。
143// static
145 auto option = MenuOption::Horizontal();
146 option.elements_infix = [] { return text("│") | automerge; };
147 return option;
148}
149
150/// @brief 創建一個 ButtonOption,使用 [] 字元突出顯示。
151// static
153 ButtonOption option;
154 option.transform = [](const EntryState& s) {
155 const std::string t = s.focused ? "[" + s.label + "]" //
156 : " " + s.label + " ";
157 return text(t);
158 };
159 return option;
160}
161
162/// @brief 創建一個 ButtonOption,在聚焦時反轉。
163// static
165 ButtonOption option;
166 option.transform = [](const EntryState& s) {
167 auto element = text(s.label) | borderLight;
168 if (s.focused) {
169 element |= inverted;
170 }
171 return element;
172 };
173 return option;
174}
175
176/// @brief 創建一個 ButtonOption。按鈕使用邊框顯示,聚焦時反轉。
177/// 這是目前的預設值。
179 ButtonOption option;
180 option.transform = [](const EntryState& s) {
181 auto element = text(s.label) | border;
182 if (s.active) {
183 element |= bold;
184 }
185 if (s.focused) {
186 element |= inverted;
187 }
188 return element;
189 };
190 return option;
191}
192
193/// @brief 創建一個 ButtonOption,使用動畫顏色。
194// static
199
200/// @brief 創建一個 ButtonOption,使用動畫顏色。
201// static
204 Color::Interpolate(0.85F, color, Color::Black), // NOLINT
205 Color::Interpolate(0.10F, color, Color::White), // NOLINT
206 Color::Interpolate(0.10F, color, Color::Black), // NOLINT
207 Color::Interpolate(0.85F, color, Color::White)); // NOLINT
208}
209
210/// @brief 創建一個 ButtonOption,使用動畫顏色。
211// static
213 // NOLINTBEGIN
215 /*bakground=*/background,
216 /*foreground=*/foreground,
217 /*background_active=*/foreground,
218 /*foreground_active=*/background);
219 // NOLINTEND
220}
221
222/// @brief 創建一個 ButtonOption,使用動畫顏色。
223// static
225 Color foreground,
226 Color background_active,
227 Color foreground_active) {
228 ButtonOption option;
229 option.transform = [](const EntryState& s) {
230 auto element = text(s.label) | borderEmpty;
231 if (s.focused) {
232 element |= bold;
233 }
234 return element;
235 };
236 option.animated_colors.foreground.Set(foreground, foreground_active);
237 option.animated_colors.background.Set(background, background_active);
238 return option;
239}
240
241/// @brief 標準Checkbox的選項。
242// static
244 auto option = CheckboxOption();
245 option.transform = [](const EntryState& s) {
246#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
247 // Microsoft 終端機不使用能夠正確渲染預設
248 // 單選框字形的字體。
249 auto prefix = text(s.state ? "[X] " : "[ ] "); // NOLINT
250#else
251 auto prefix = text(s.state ? "▣ " : "☐ "); // NOLINT
252#endif
253 auto t = text(s.label);
254 if (s.active) {
255 t |= bold;
256 }
257 if (s.focused) {
258 t |= inverted;
259 }
260 return hbox({prefix, t});
261 };
262 return option;
263}
264
265/// @brief 標準Radiobox的選項。
266// static
268 auto option = RadioboxOption();
269 option.transform = [](const EntryState& s) {
270#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
271 // Microsoft 終端機不使用能夠正確渲染預設
272 // 單選框字形的字體。
273 auto prefix = text(s.state ? "(*) " : "( ) "); // NOLINT
274#else
275 auto prefix = text(s.state ? "◉ " : "○ "); // NOLINT
276#endif
277 auto t = text(s.label);
278 if (s.active) {
279 t |= bold;
280 }
281 if (s.focused) {
282 t |= inverted;
283 }
284 return hbox({prefix, t});
285 };
286 return option;
287}
288
289/// @brief 輸入組件的標準選項。
290// static
292 InputOption option;
293 option.transform = [](InputState state) {
294 state.element |= color(Color::White);
295
296 if (state.is_placeholder) {
297 state.element |= dim;
298 }
299
300 if (state.focused) {
301 state.element |= inverted;
302 } else if (state.hovered) {
303 state.element |= bgcolor(Color::GrayDark);
304 }
305
306 return state.element;
307 };
308 return option;
309}
310
311/// @brief 更美觀的輸入組件的標準選項。
312// static
314 InputOption option;
315 option.transform = [](InputState state) {
316 state.element |= borderEmpty;
317 state.element |= color(Color::White);
318
319 if (state.is_placeholder) {
320 state.element |= dim;
321 }
322
323 if (state.focused) {
324 state.element |= bgcolor(Color::Black);
325 }
326
327 if (state.hovered) {
328 state.element |= bgcolor(Color::GrayDark);
329 }
330
331 return state.element;
332 };
333 return option;
334}
335
336} // namespace ftxui
static ButtonOption Animated()
創建一個 ButtonOption,使用動畫顏色。
static MenuOption Toggle()
帶有分隔線的水平選單標準選項。 這對於實現一個標籤頁列很有用。
animation::Duration follower_duration
animation::easing::Function leader_function
MenuEntryOption entries_option
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 動畫的持續時間。 @params...
static MenuOption HorizontalAnimated()
動畫水平選單的標準選項。 這對於實現一個標籤頁列很有用。
static RadioboxOption Simple()
標準Radiobox的選項。
std::function< Element(const EntryState &)> transform
AnimatedButton 元件的選項。
核取方塊元件的選項。
Input 元件的選項。
Menu 元件的選項。
Radiobox 元件的選項。
Decorator bgcolor(Color)
使用背景顏色進行裝飾。
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 borderLight(Element)
在元素周圍繪製細邊框。
Element dim(Element)
使用淺色字體,適用於不那麼強調的元素。
Definition dim.cpp:33
Element automerge(Element child)
啟用字符自動與附近其他字符合併。
Definition automerge.cpp:17
Element border(Element)
在元素周圍繪製邊框。
Element borderEmpty(Element)
在元素周圍繪製空邊框。
Decorator color(Color)
使用前景顏色進行裝飾。
static Color Interpolate(float t, const Color &a, const Color &b)
Color 是一個在終端使用者介面中表示顏色的類別。
Definition color.hpp:20
std::function< float(float)> Function
Definition animation.hpp:40
std::chrono::duration< float > Duration
Definition animation.hpp:25
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
std::shared_ptr< Node > Element
Definition elements.hpp:22
Element hbox(Elements)
一個逐一水平顯示元素的容器。
Definition hbox.cpp:94
來自 |ButtonOption|、|CheckboxOption|、 |RadioboxOption|、|MenuEntryOption|、|MenuOption| 的轉換參數。
用於定義 Input 元件的樣式。