FTXUI 7.0.3
C++ functional terminal UI.
Loading...
Searching...
No Matches
component_options.hpp
Go to the documentation of this file.
1// Copyright 2021 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#ifndef FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP
5#define FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP
6
7#include <chrono> // for milliseconds
8#include <ftxui/component/animation.hpp> // for Duration, QuadraticInOut, Function
9#include <ftxui/dom/direction.hpp> // for Direction, Direction::Left, Direction::Right, Direction::Down
10#include <ftxui/dom/elements.hpp> // for Element, separator
11#include <ftxui/util/ref.hpp> // for Ref, ConstRef, StringRef
13#include <functional> // for function
14#include <limits> // for numeric_limits
15#include <string> // for string
16
17#include "ftxui/component/component_base.hpp" // for Component
18#include "ftxui/screen/color.hpp" // for Color, Color::GrayDark, Color::White
19#include "ftxui/util/export.hpp" // for FTXUI_EXPORT
20
21namespace ftxui {
22
23/// @brief 來自 |ButtonOption|、|CheckboxOption|、
24/// |RadioboxOption|、|MenuEntryOption|、|MenuOption| 的轉換參數。
25struct FTXUI_EXPORT(COMPONENT) EntryState {
26 std::string label; ///< 要顯示的標籤。
27 bool state; ///< 按鈕/核取方塊/單選框的狀態
28 bool active; ///< 該項目是否為作用中的項目。
29 bool focused; ///< 該項目是否為使用者所聚焦的項目。
30 int index; ///< 該項目的索引(若適用),否則為 -1。
31};
32
33/// @brief 底線效果的選項。
34/// @ingroup component
35struct FTXUI_EXPORT(COMPONENT) UnderlineOption {
36 bool enabled = false;
37
38 Color color_active = Color::White;
39 Color color_inactive = Color::GrayDark;
40
41 animation::easing::Function leader_function =
42 animation::easing::QuadraticInOut;
43 animation::easing::Function follower_function =
44 animation::easing::QuadraticInOut;
45
46 animation::Duration leader_duration = std::chrono::milliseconds(250);
47 animation::Duration leader_delay = std::chrono::milliseconds(0);
48 animation::Duration follower_duration = std::chrono::milliseconds(250);
49 animation::Duration follower_delay = std::chrono::milliseconds(0);
50
51 void SetAnimation(animation::Duration d, animation::easing::Function f);
52 void SetAnimationDuration(animation::Duration d);
53 void SetAnimationFunction(animation::easing::Function f);
54 void SetAnimationFunction(animation::easing::Function f_leader,
55 animation::easing::Function f_follower);
56};
57
58/// @brief 關於潛在動畫顏色的選項。
59/// @ingroup component
60struct FTXUI_EXPORT(COMPONENT) AnimatedColorOption {
61 void Set(
62 Color inactive,
63 Color active,
64 animation::Duration duration = std::chrono::milliseconds(250),
65 animation::easing::Function function = animation::easing::QuadraticInOut);
66
67 bool enabled = false;
68 Color inactive;
69 Color active;
70 animation::Duration duration = std::chrono::milliseconds(250);
71 animation::easing::Function function = animation::easing::QuadraticInOut;
72};
73
74struct FTXUI_EXPORT(COMPONENT) AnimatedColorsOption {
75 AnimatedColorOption background;
76 AnimatedColorOption foreground;
77};
78
79/// @brief MenuEntry 元件的選項。
80/// @ingroup component
81struct FTXUI_EXPORT(COMPONENT) MenuEntryOption {
82 ConstStringRef label = "MenuEntry";
83 std::function<Element(const EntryState& state)> transform;
84 AnimatedColorsOption animated_colors;
85};
86
87/// @brief Menu 元件的選項。
88/// @ingroup component
89struct FTXUI_EXPORT(COMPONENT) MenuOption {
90 // 標準建構子:
91 static MenuOption Horizontal();
92 static MenuOption HorizontalAnimated();
93 static MenuOption Vertical();
94 static MenuOption VerticalAnimated();
95 static MenuOption Toggle();
96
97 ConstStringListRef entries; ///> 項目清單。
98 Ref<int> selected = 0; ///> 選取項目的索引。
99
100 // 樣式:
101 UnderlineOption underline;
102 MenuEntryOption entries_option;
103 Direction direction = Direction::Down;
104 std::function<Element()> elements_prefix;
105 std::function<Element()> elements_infix;
106 std::function<Element()> elements_postfix;
107
108 // 觀察者:
109 std::function<void()> on_change; ///> 當選取項目改變時呼叫。
110 std::function<void()> on_enter; ///> 當使用者按下 Enter 鍵時呼叫。
111 Ref<int> focused_entry = 0;
112};
113
114/// @brief AnimatedButton 元件的選項。
115/// @ingroup component
116struct FTXUI_EXPORT(COMPONENT) ButtonOption {
117 // 標準建構子:
118 static ButtonOption Ascii();
119 static ButtonOption Simple();
120 static ButtonOption Border();
121 static ButtonOption Animated();
122 static ButtonOption Animated(Color color);
123 static ButtonOption Animated(Color background, Color foreground);
124 static ButtonOption Animated(Color background,
125 Color foreground,
126 Color background_active,
127 Color foreground_active);
128
129 ConstStringRef label = "Button";
130 std::function<void()> on_click = [] {};
131
132 // 樣式:
133 std::function<Element(const EntryState&)> transform;
134 AnimatedColorsOption animated_colors;
135};
136
137/// @brief 核取方塊元件的選項。
138/// @ingroup component
139struct FTXUI_EXPORT(COMPONENT) CheckboxOption {
140 // 標準建構子:
141 static CheckboxOption Simple();
142
143 ConstStringRef label = "Checkbox";
144
145 Ref<bool> checked = false;
146
147 // 樣式:
148 std::function<Element(const EntryState&)> transform;
149
150 // 觀察者:
151 /// 當使用者改變狀態時呼叫。
152 std::function<void()> on_change = [] {};
153};
154
155/// @brief 用於定義 Input 元件的樣式。
156struct FTXUI_EXPORT(COMPONENT) InputState {
158 bool hovered; ///< 輸入框是否被滑鼠懸停。
159 bool focused; ///< 輸入框是否被使用者聚焦。
160 bool is_placeholder; ///< 輸入框是否為空並顯示
161 ///< 佔位文字。
162};
163
164/// @brief Input 元件的選項。
165/// @ingroup component
166struct FTXUI_EXPORT(COMPONENT) InputOption {
167 // 一組預先定義的樣式:
168
169 /// @brief 建立預設輸入樣式:
170 static InputOption Default();
171 /// @brief 具有高邊距的黑底白字樣式:
172 static InputOption Spacious();
173
174 /// 輸入框的內容。
175 StringRef content = "";
176
177 /// 輸入框為空時的內容。
178 StringRef placeholder = "";
179
180 // 樣式:
181 std::function<Element(InputState)> transform;
182 Ref<bool> password = false; ///< 使用 '*' 遮蔽輸入內容。
183 Ref<bool> multiline = true; ///< 輸入框是否可以多行輸入。
184 Ref<bool> insert = true; ///< 插入或覆寫字元模式。
185
186 /// 當內容改變時呼叫。
187 std::function<void()> on_change = [] {};
188 /// 當使用者按下 Enter 鍵時呼叫。
189 std::function<void()> on_enter = [] {};
190
191 // 游標的字元位置:
192 Ref<int> cursor_position = 0;
193};
194
195/// @brief Radiobox 元件的選項。
196/// @ingroup component
197struct FTXUI_EXPORT(COMPONENT) RadioboxOption {
198 // 標準建構子:
199 static RadioboxOption Simple();
200
201 // 內容:
202 ConstStringListRef entries;
203 Ref<int> selected = 0;
204
205 // 樣式:
206 std::function<Element(const EntryState&)> transform;
207
208 // 觀察者:
209 /// 當選取項目改變時呼叫。
210 std::function<void()> on_change = [] {};
211 Ref<int> focused_entry = 0;
212};
213
214struct FTXUI_EXPORT(COMPONENT) ResizableSplitOption {
216 Component back;
217 Ref<Direction> direction = Direction::Left;
218 Ref<int> main_size =
219 (direction() == Direction::Left || direction() == Direction::Right) ? 20
220 : 10;
221 std::function<Element()> separator_func = [] { return ::ftxui::separator(); };
222
223 // main_size 的限制條件:
224 Ref<int> min = 0;
225 Ref<int> max = std::numeric_limits<int>::max();
226};
227
228// @brief `Slider` 元件的選項。
229// @ingroup component
230template <typename T>
233 ConstRef<T> min = T(0);
234 ConstRef<T> max = T(100);
235 ConstRef<T> increment = (max() - min()) / 20;
236 Direction direction = Direction::Right;
237 Color color_active = Color::White;
238 Color color_inactive = Color::GrayDark;
239 std::function<void()> on_change; ///> 當 `value` 更新時呼叫。
240};
241
242/// @brief 傳遞給 `Window` 元件渲染函式的狀態。
243/// @ingroup component
244struct FTXUI_EXPORT(COMPONENT) WindowRenderState {
245 Element inner; ///< 包裹在此視窗內的元素。
246 const std::string& title; ///< 視窗的標題。
247 bool active = false; ///< 該視窗是否為作用中的視窗。
248 bool drag = false; ///< 該視窗是否正在被拖曳。
249 bool resize = false; ///< 該視窗是否正在被調整大小。
250 bool hover_left = false; ///< 可調整大小的左側邊是否被懸停。
251 bool hover_right = false; ///< 可調整大小的右側邊是否被懸停。
252 bool hover_top = false; ///< 可調整大小的上側邊是否被懸停。
253 bool hover_down = false; ///< 可調整大小的下側邊是否被懸停。
254};
255
256// @brief `Window` 元件的選項。
257// @ingroup component
258struct FTXUI_EXPORT(COMPONENT) WindowOptions {
259 Component inner; ///< 此視窗所包裹的元件。
260 ConstStringRef title = ""; ///< 此視窗所顯示的標題。
261
262 Ref<int> left = 0; ///< 視窗的左側位置。
263 Ref<int> top = 0; ///< 視窗的上側位置。
264 Ref<int> width = 20; ///< 視窗的寬度。
265 Ref<int> height = 10; ///< 視窗的高度。
266
267 Ref<bool> resize_left = true; ///< 左側邊是否可調整大小?
268 Ref<bool> resize_right = true; ///< 右側邊是否可調整大小?
269 Ref<bool> resize_top = true; ///< 上側邊是否可調整大小?
270 Ref<bool> resize_down = true; ///< 下側邊是否可調整大小?
271
272 /// 一個選用的函式,用來自訂視窗的外觀:
273 std::function<Element(const WindowRenderState&)> render;
274};
275
276/// @brief Dropdown 元件的選項。
277/// @ingroup component
278/// 下拉式選單是開啟/關閉單選方塊的核取方塊。
279struct FTXUI_EXPORT(COMPONENT) DropdownOption {
280 /// 下拉選單是開啟還是關閉:
281 Ref<bool> open = false;
282 // 核取方塊的選項:
283 CheckboxOption checkbox;
284 // 單選框的選項:
285 RadioboxOption radiobox;
286 // 轉換函式:
287 std::function<Element(bool open, Element checkbox, Element radiobox)>
288 transform;
289};
290
291} // namespace ftxui
292
293#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */
一個適配器。擁有或引用一個不可變的物件。
Definition ref.hpp:20
一個適配器。擁有或引用一個可變的物件。
Definition ref.hpp:56
Direction
Direction 是一個列舉,表示四個主要方向。
Definition direction.hpp:14
FTXUI ftxui:: 命名空間
Definition animation.hpp:11
std::shared_ptr< Node > Element
Definition elements.hpp:25
Direction direction
Definition elements.hpp:80
const Element & element
Definition node.hpp:92
std::shared_ptr< ComponentBase > Component
Definition app.hpp:23
std::uint8_t top
Definition screen.cpp:148
std::uint8_t left
Definition screen.cpp:147
std::function< void()> on_change