FTXUI 6.1.9
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 arguments for transform from |ButtonOption|, |CheckboxOption|,
24/// |RadioboxOption|, |MenuEntryOption|, |MenuOption|.
25struct FTXUI_EXPORT(COMPONENT) EntryState {
26 std::string label; ///< The label to display.
27 bool state; ///< The state of the button/checkbox/radiobox
28 bool active; ///< Whether the entry is the active one.
29 bool focused; ///< Whether the entry is one focused by the user.
30 int index; ///< Index of the entry when applicable or -1.
31};
32
33/// @brief Option for the underline effect.
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 Option about a potentially animated color.
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 Option for the MenuEntry component.
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 Option for the Menu component.
88/// @ingroup component
89struct FTXUI_EXPORT(COMPONENT) MenuOption {
90 // Standard constructors:
91 static MenuOption Horizontal();
92 static MenuOption HorizontalAnimated();
93 static MenuOption Vertical();
94 static MenuOption VerticalAnimated();
95 static MenuOption Toggle();
96
97 ConstStringListRef entries; ///> The list of entries.
98 Ref<int> selected = 0; ///> The index of the selected entry.
99
100 // Style:
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 // Observers:
109 std::function<void()> on_change; ///> Called when the selected entry changes.
110 std::function<void()> on_enter; ///> Called when the user presses enter.
111 Ref<int> focused_entry = 0;
112};
113
114/// @brief Option for the AnimatedButton component.
115/// @ingroup component
116struct FTXUI_EXPORT(COMPONENT) ButtonOption {
117 // Standard constructors:
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 // Style:
133 std::function<Element(const EntryState&)> transform;
134 AnimatedColorsOption animated_colors;
135};
136
137/// @brief Option for the Checkbox component.
138/// @ingroup component
139struct FTXUI_EXPORT(COMPONENT) CheckboxOption {
140 // Standard constructors:
141 static CheckboxOption Simple();
142
143 ConstStringRef label = "Checkbox";
144
145 Ref<bool> checked = false;
146
147 // Style:
148 std::function<Element(const EntryState&)> transform;
149
150 // Observer:
151 /// Called when the user change the state.
152 std::function<void()> on_change = [] {};
153};
154
155/// @brief Used to define style for the Input component.
156struct FTXUI_EXPORT(COMPONENT) InputState {
158 bool hovered; ///< Whether the input is hovered by the mouse.
159 bool focused; ///< Whether the input is focused by the user.
160 bool is_placeholder; ///< Whether the input is empty and displaying the
161 ///< placeholder.
162};
163
164/// @brief Option for the Input component.
165/// @ingroup component
166struct FTXUI_EXPORT(COMPONENT) InputOption {
167 // A set of predefined styles:
168
169 /// @brief Create the default input style:
170 static InputOption Default();
171 /// @brief A white on black style with high margins:
172 static InputOption Spacious();
173
174 /// The content of the input.
175 StringRef content = "";
176
177 /// The content of the input when it's empty.
178 StringRef placeholder = "";
179
180 // Style:
181 std::function<Element(InputState)> transform;
182 Ref<bool> password = false; ///< Obscure the input content using '*'.
183 Ref<bool> multiline = true; ///< Whether the input can be multiline.
184 Ref<bool> insert = true; ///< Insert or overtype character mode.
185
186 /// Called when the content changes.
187 std::function<void()> on_change = [] {};
188 /// Called when the user presses enter.
189 std::function<void()> on_enter = [] {};
190
191 // The char position of the cursor:
192 Ref<int> cursor_position = 0;
193};
194
195/// @brief Option for the Radiobox component.
196/// @ingroup component
197struct FTXUI_EXPORT(COMPONENT) RadioboxOption {
198 // Standard constructors:
199 static RadioboxOption Simple();
200
201 // Content:
202 ConstStringListRef entries;
203 Ref<int> selected = 0;
204
205 // Style:
206 std::function<Element(const EntryState&)> transform;
207
208 // Observers:
209 /// Called when the selected entry changes.
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 // Constraints on main_size:
224 Ref<int> min = 0;
225 Ref<int> max = std::numeric_limits<int>::max();
226};
227
228// @brief Option for the `Slider` component.
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; ///> Called when `value` is updated.
240};
241
242/// @brief State passed to the `Window` component's render function.
243/// @ingroup component
244struct FTXUI_EXPORT(COMPONENT) WindowRenderState {
245 Element inner; ///< The element wrapped inside this window.
246 const std::string& title; ///< The title of the window.
247 bool active = false; ///< Whether the window is the active one.
248 bool drag = false; ///< Whether the window is being dragged.
249 bool resize = false; ///< Whether the window is being resized.
250 bool hover_left = false; ///< Whether the resizeable left side is hovered.
251 bool hover_right = false; ///< Whether the resizeable right side is hovered.
252 bool hover_top = false; ///< Whether the resizeable top side is hovered.
253 bool hover_down = false; ///< Whether the resizeable down side is hovered.
254};
255
256// @brief Option for the `Window` component.
257// @ingroup component
258struct FTXUI_EXPORT(COMPONENT) WindowOptions {
259 Component inner; ///< The component wrapped by this window.
260 ConstStringRef title = ""; ///< The title displayed by this window.
261
262 Ref<int> left = 0; ///< The left side position of the window.
263 Ref<int> top = 0; ///< The top side position of the window.
264 Ref<int> width = 20; ///< The width of the window.
265 Ref<int> height = 10; ///< The height of the window.
266
267 Ref<bool> resize_left = true; ///< Can the left side be resized?
268 Ref<bool> resize_right = true; ///< Can the right side be resized?
269 Ref<bool> resize_top = true; ///< Can the top side be resized?
270 Ref<bool> resize_down = true; ///< Can the down side be resized?
271
272 /// An optional function to customize how the window looks like:
273 std::function<Element(const WindowRenderState&)> render;
274};
275
276/// @brief Option for the Dropdown component.
277/// @ingroup component
278/// A dropdown menu is a checkbox opening/closing a radiobox.
279struct FTXUI_EXPORT(COMPONENT) DropdownOption {
280 /// Whether the dropdown is open or closed:
281 Ref<bool> open = false;
282 // The options for the checkbox:
283 CheckboxOption checkbox;
284 // The options for the radiobox:
285 RadioboxOption radiobox;
286 // The transformation function:
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 */
An adapter. Own or reference an immutable object.
Definition ref.hpp:20
An adapter. Own or reference an mutable object.
Definition ref.hpp:56
Direction
Direction is an enumeration that represents the four cardinal directions.
Definition direction.hpp:13
The FTXUI ftxui:: namespace.
Definition animation.hpp:11
std::shared_ptr< Node > Element
Definition elements.hpp:24
Direction direction
Definition elements.hpp:82
const Element & element
Definition node.hpp:95
std::shared_ptr< ComponentBase > Component
Definition app.hpp:23
std::uint8_t top
Definition screen.cpp:143
std::uint8_t left
Definition screen.cpp:142
std::function< void()> on_change