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|、|RadioboxOption|、|MenuEntryOption|、|MenuOption|からの変換の引数。
24struct FTXUI_EXPORT(COMPONENT) EntryState {
25 std::string label; ///< 表示するラベル。
26 bool state; ///< ボタン/チェックボックス/ラジオボックスの状態
27 bool active; ///< エントリがアクティブなものかどうか。
28 bool focused; ///< エントリがユーザーによってフォーカスされているものかどうか。
29 int index; ///< 該当する場合のエントリのインデックス、または-1。
30};
31
32/// @brief 下線効果のオプション。
33/// @ingroup component
34struct FTXUI_EXPORT(COMPONENT) UnderlineOption {
35 bool enabled = false;
36
37 Color color_active = Color::White;
38 Color color_inactive = Color::GrayDark;
39
40 animation::easing::Function leader_function =
41 animation::easing::QuadraticInOut;
42 animation::easing::Function follower_function =
43 animation::easing::QuadraticInOut;
44
45 animation::Duration leader_duration = std::chrono::milliseconds(250);
46 animation::Duration leader_delay = std::chrono::milliseconds(0);
47 animation::Duration follower_duration = std::chrono::milliseconds(250);
48 animation::Duration follower_delay = std::chrono::milliseconds(0);
49
50 void SetAnimation(animation::Duration d, animation::easing::Function f);
51 void SetAnimationDuration(animation::Duration d);
52 void SetAnimationFunction(animation::easing::Function f);
53 void SetAnimationFunction(animation::easing::Function f_leader,
54 animation::easing::Function f_follower);
55};
56
57/// @brief アニメーション化された可能性のある色に関するオプション。
58/// @ingroup component
59struct FTXUI_EXPORT(COMPONENT) AnimatedColorOption {
60 void Set(
61 Color inactive,
62 Color active,
63 animation::Duration duration = std::chrono::milliseconds(250),
64 animation::easing::Function function = animation::easing::QuadraticInOut);
65
66 bool enabled = false;
67 Color inactive;
68 Color active;
69 animation::Duration duration = std::chrono::milliseconds(250);
70 animation::easing::Function function = animation::easing::QuadraticInOut;
71};
72
73struct FTXUI_EXPORT(COMPONENT) AnimatedColorsOption {
74 AnimatedColorOption background;
75 AnimatedColorOption foreground;
76};
77
78/// @brief MenuEntryコンポーネントのオプション。
79/// @ingroup component
80struct FTXUI_EXPORT(COMPONENT) MenuEntryOption {
81 ConstStringRef label = "MenuEntry";
82 std::function<Element(const EntryState& state)> transform;
83 AnimatedColorsOption animated_colors;
84};
85
86/// @brief Menuコンポーネントのオプション。
87/// @ingroup component
88struct FTXUI_EXPORT(COMPONENT) MenuOption {
89 // 標準コンストラクタ:
90 static MenuOption Horizontal();
91 static MenuOption HorizontalAnimated();
92 static MenuOption Vertical();
93 static MenuOption VerticalAnimated();
94 static MenuOption Toggle();
95
96 ConstStringListRef entries; ///> エントリのリスト。
97 Ref<int> selected = 0; ///> 選択されたエントリのインデックス。
98
99 // スタイル:
100 UnderlineOption underline;
101 MenuEntryOption entries_option;
102 Direction direction = Direction::Down;
103 std::function<Element()> elements_prefix;
104 std::function<Element()> elements_infix;
105 std::function<Element()> elements_postfix;
106
107 // オブザーバー:
108 std::function<void()> on_change; ///> 選択されたエントリが変更されたときに呼び出されます。
109 std::function<void()> on_enter; ///> ユーザーがEnterを押したときに呼び出されます。
110 Ref<int> focused_entry = 0;
111};
112
113/// @brief AnimatedButtonコンポーネントのオプション。
114/// @ingroup component
115struct FTXUI_EXPORT(COMPONENT) ButtonOption {
116 // 標準コンストラクタ:
117 static ButtonOption Ascii();
118 static ButtonOption Simple();
119 static ButtonOption Border();
120 static ButtonOption Animated();
121 static ButtonOption Animated(Color color);
122 static ButtonOption Animated(Color background, Color foreground);
123 static ButtonOption Animated(Color background,
124 Color foreground,
125 Color background_active,
126 Color foreground_active);
127
128 ConstStringRef label = "Button";
129 std::function<void()> on_click = [] {};
130
131 // スタイル:
132 std::function<Element(const EntryState&)> transform;
133 AnimatedColorsOption animated_colors;
134};
135
136/// @brief Checkboxコンポーネントのオプション。
137/// @ingroup component
138struct FTXUI_EXPORT(COMPONENT) CheckboxOption {
139 // 標準コンストラクタ:
140 static CheckboxOption Simple();
141
142 ConstStringRef label = "Checkbox";
143
144 Ref<bool> checked = false;
145
146 // スタイル:
147 std::function<Element(const EntryState&)> transform;
148
149 // オブザーバー:
150 /// ユーザーが状態を変更したときに呼び出されます。
151 std::function<void()> on_change = [] {};
152};
153
154/// @brief Inputコンポーネントのスタイルを定義するために使用されます。
155struct FTXUI_EXPORT(COMPONENT) InputState {
157 bool hovered; ///< 入力がマウスでホバーされているかどうか。
158 bool focused; ///< 入力がユーザーによってフォーカスされているかどうか。
159 bool is_placeholder; ///< 入力が空でプレースホルダーを
160 ///< 表示しているかどうか。
161};
162
163/// @brief Inputコンポーネントのオプション。
164/// @ingroup component
165struct FTXUI_EXPORT(COMPONENT) InputOption {
166 // 定義済みスタイルのセット:
167
168 /// @brief デフォルトの入力スタイルを作成します:
169 static InputOption Default();
170 /// @brief 余白の広い、白地に黒のスタイル:
171 static InputOption Spacious();
172
173 /// 入力の内容。
174 StringRef content = "";
175
176 /// 空のときの入力の内容。
177 StringRef placeholder = "";
178
179 // スタイル:
180 std::function<Element(InputState)> transform;
181 Ref<bool> password = false; ///< 入力内容を'*'を使って隠す。
182 Ref<bool> multiline = true; ///< 入力が複数行かどうか。
183 Ref<bool> insert = true; ///< 挿入モードまたは上書きモードの文字入力。
184
185 /// 内容が変更されたときに呼び出されます。
186 std::function<void()> on_change = [] {};
187 /// ユーザーがEnterを押したときに呼び出されます。
188 std::function<void()> on_enter = [] {};
189
190 // カーソルの文字位置:
191 Ref<int> cursor_position = 0;
192};
193
194/// @brief Radioboxコンポーネントのオプション。
195/// @ingroup component
196struct FTXUI_EXPORT(COMPONENT) RadioboxOption {
197 // 標準コンストラクタ:
198 static RadioboxOption Simple();
199
200 // 内容:
201 ConstStringListRef entries;
202 Ref<int> selected = 0;
203
204 // スタイル:
205 std::function<Element(const EntryState&)> transform;
206
207 // オブザーバー:
208 /// 選択されたエントリが変更されたときに呼び出されます。
209 std::function<void()> on_change = [] {};
210 Ref<int> focused_entry = 0;
211};
212
213struct FTXUI_EXPORT(COMPONENT) ResizableSplitOption {
215 Component back;
216 Ref<Direction> direction = Direction::Left;
217 Ref<int> main_size =
218 (direction() == Direction::Left || direction() == Direction::Right) ? 20
219 : 10;
220 std::function<Element()> separator_func = [] { return ::ftxui::separator(); };
221
222 // main_sizeの制約:
223 Ref<int> min = 0;
224 Ref<int> max = std::numeric_limits<int>::max();
225};
226
227// @brief `Slider`コンポーネントのオプション。
228// @ingroup component
229template <typename T>
232 ConstRef<T> min = T(0);
233 ConstRef<T> max = T(100);
234 ConstRef<T> increment = (max() - min()) / 20;
235 Direction direction = Direction::Right;
236 Color color_active = Color::White;
237 Color color_inactive = Color::GrayDark;
238 std::function<void()> on_change; ///> `value`が更新されたときに呼び出されます。
239};
240
241/// @brief `Window`コンポーネントのレンダー関数に渡される状態。
242/// @ingroup component
243struct FTXUI_EXPORT(COMPONENT) WindowRenderState {
244 Element inner; ///< このウィンドウにラップされた要素。
245 const std::string& title; ///< ウィンドウのタイトル。
246 bool active = false; ///< ウィンドウがアクティブなものかどうか。
247 bool drag = false; ///< ウィンドウがドラッグされているかどうか。
248 bool resize = false; ///< ウィンドウがリサイズされているかどうか。
249 bool hover_left = false; ///< リサイズ可能な左側がホバーされているかどうか。
250 bool hover_right = false; ///< リサイズ可能な右側がホバーされているかどうか。
251 bool hover_top = false; ///< リサイズ可能な上側がホバーされているかどうか。
252 bool hover_down = false; ///< リサイズ可能な下側がホバーされているかどうか。
253};
254
255// @brief `Window`コンポーネントのオプション。
256// @ingroup component
257struct FTXUI_EXPORT(COMPONENT) WindowOptions {
258 Component inner; ///< このウィンドウにラップされたコンポーネント。
259 ConstStringRef title = ""; ///< このウィンドウによって表示されるタイトル。
260
261 Ref<int> left = 0; ///< ウィンドウの左側の位置。
262 Ref<int> top = 0; ///< ウィンドウの上側の位置。
263 Ref<int> width = 20; ///< ウィンドウの幅。
264 Ref<int> height = 10; ///< ウィンドウの高さ。
265
266 Ref<bool> resize_left = true; ///< 左側をリサイズできるか?
267 Ref<bool> resize_right = true; ///< 右側をリサイズできるか?
268 Ref<bool> resize_top = true; ///< 上側をリサイズできるか?
269 Ref<bool> resize_down = true; ///< 下側をリサイズできるか?
270
271 /// ウィンドウの見た目をカスタマイズするためのオプションの関数:
272 std::function<Element(const WindowRenderState&)> render;
273};
274
275/// @brief ドロップダウンコンポーネントのオプション。
276/// @ingroup component
277/// ドロップダウンメニューは、ラジオボックスを開閉するチェックボックスです。
278struct FTXUI_EXPORT(COMPONENT) DropdownOption {
279 /// ドロップダウンが開いているか閉じているか:
280 Ref<bool> open = false;
281 // チェックボックスのオプション:
282 CheckboxOption checkbox;
283 // ラジオボックスのオプション:
284 RadioboxOption radiobox;
285 // 変換関数:
286 std::function<Element(bool open, Element checkbox, Element radiobox)>
287 transform;
288};
289
290} // namespace ftxui
291
292#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */
アダプター。不変オブジェクトを所有または参照します。
Definition ref.hpp:20
アダプター。可変オブジェクトを所有または参照します。
Definition ref.hpp:56
Direction
Directionは、東西南北の4つの基本方向を表す列挙型です。
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:94
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