FTXUI  5.0.0
C++ functional terminal UI.
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
12 #include <functional> // for function
13 #include <string> // for string
14 
15 #include "ftxui/component/component_base.hpp" // for Component
16 #include "ftxui/screen/color.hpp" // for Color, Color::GrayDark, Color::White
17 
18 namespace ftxui {
19 
20 /// @brief arguments for |ButtonOption::transform|, |CheckboxOption::transform|,
21 /// |Radiobox::transform|, |MenuEntryOption::transform|,
22 /// |MenuOption::transform|.
23 struct EntryState {
24  std::string label; ///< The label to display.
25  bool state; ///< The state of the button/checkbox/radiobox
26  bool active; ///< Whether the entry is the active one.
27  bool focused; ///< Whether the entry is one focused by the user.
28  int index; ///< Index of the entry when applicable or -1.
29 };
30 
32  bool enabled = false;
33 
36 
41 
42  animation::Duration leader_duration = std::chrono::milliseconds(250);
43  animation::Duration leader_delay = std::chrono::milliseconds(0);
44  animation::Duration follower_duration = std::chrono::milliseconds(250);
45  animation::Duration follower_delay = std::chrono::milliseconds(0);
46 
51  animation::easing::Function f_follower);
52 };
53 
54 /// @brief Option about a potentially animated color.
55 /// @ingroup component
57  void Set(
59  Color active,
60  animation::Duration duration = std::chrono::milliseconds(250),
62 
63  bool enabled = false;
66  animation::Duration duration = std::chrono::milliseconds(250);
68 };
69 
73 };
74 
75 /// @brief Option for the MenuEntry component.
76 /// @ingroup component
78  ConstStringRef label = "MenuEntry";
79  std::function<Element(const EntryState& state)> transform;
81 };
82 
83 /// @brief Option for the Menu component.
84 /// @ingroup component
85 struct MenuOption {
86  // Standard constructors:
87  static MenuOption Horizontal();
89  static MenuOption Vertical();
91  static MenuOption Toggle();
92 
93  ConstStringListRef entries; ///> The list of entries.
94  Ref<int> selected = 0; ///> The index of the selected entry.
95 
96  // Style:
100  std::function<Element()> elements_prefix;
101  std::function<Element()> elements_infix;
102  std::function<Element()> elements_postfix;
103 
104  // Observers:
105  std::function<void()> on_change; ///> Called when the selected entry changes.
106  std::function<void()> on_enter; ///> Called when the user presses enter.
108 };
109 
110 /// @brief Option for the AnimatedButton component.
111 /// @ingroup component
112 struct ButtonOption {
113  // Standard constructors:
114  static ButtonOption Ascii();
115  static ButtonOption Simple();
116  static ButtonOption Border();
117  static ButtonOption Animated();
119  static ButtonOption Animated(Color background, Color foreground);
120  static ButtonOption Animated(Color background,
121  Color foreground,
122  Color background_active,
123  Color foreground_active);
124 
125  ConstStringRef label = "Button";
126  std::function<void()> on_click = [] {};
127 
128  // Style:
129  std::function<Element(const EntryState&)> transform;
131 };
132 
133 /// @brief Option for the Checkbox component.
134 /// @ingroup component
136  // Standard constructors:
137  static CheckboxOption Simple();
138 
139  ConstStringRef label = "Checkbox";
140 
142 
143  // Style:
144  std::function<Element(const EntryState&)> transform;
145 
146  // Observer:
147  /// Called when the user change the state.
148  std::function<void()> on_change = [] {};
149 };
150 
151 /// @brief Used to define style for the Input component.
152 struct InputState {
154  bool hovered; ///< Whether the input is hovered by the mouse.
155  bool focused; ///< Whether the input is focused by the user.
156  bool is_placeholder; ///< Whether the input is empty and displaying the
157  ///< placeholder.
158 };
159 
160 /// @brief Option for the Input component.
161 /// @ingroup component
162 struct InputOption {
163  // A set of predefined styles:
164 
165  /// @brief Create the default input style:
166  static InputOption Default();
167  /// @brief A white on black style with high margins:
168  static InputOption Spacious();
169 
170  /// The content of the input.
172 
173  /// The content of the input when it's empty.
175 
176  // Style:
177  std::function<Element(InputState)> transform;
178  Ref<bool> password = false; ///< Obscure the input content using '*'.
179  Ref<bool> multiline = true; ///< Whether the input can be multiline.
180  Ref<bool> insert = true; ///< Insert or overtype character mode.
181 
182  /// Called when the content changes.
183  std::function<void()> on_change = [] {};
184  /// Called when the user presses enter.
185  std::function<void()> on_enter = [] {};
186 
187  // The char position of the cursor:
189 };
190 
191 /// @brief Option for the Radiobox component.
192 /// @ingroup component
194  // Standard constructors:
195  static RadioboxOption Simple();
196 
197  // Content:
200 
201  // Style:
202  std::function<Element(const EntryState&)> transform;
203 
204  // Observers:
205  /// Called when the selected entry changes.
206  std::function<void()> on_change = [] {};
208 };
209 
216  : 10;
217  std::function<Element()> separator_func = [] { return ::ftxui::separator(); };
218 };
219 
220 // @brief Option for the `Slider` component.
221 // @ingroup component
222 template <typename T>
223 struct SliderOption {
225  ConstRef<T> min = T(0);
226  ConstRef<T> max = T(100);
227  ConstRef<T> increment = (max() - min()) / 20;
231  std::function<void()> on_change; ///> Called when `value` is updated.
232 };
233 
234 // Parameter pack used by `WindowOptions::render`.
236  Element inner; ///< The element wrapped inside this window.
237  const std::string& title; ///< The title of the window.
238  bool active = false; ///< Whether the window is the active one.
239  bool drag = false; ///< Whether the window is being dragged.
240  bool resize = false; ///< Whether the window is being resized.
241  bool hover_left = false; ///< Whether the resizeable left side is hovered.
242  bool hover_right = false; ///< Whether the resizeable right side is hovered.
243  bool hover_top = false; ///< Whether the resizeable top side is hovered.
244  bool hover_down = false; ///< Whether the resizeable down side is hovered.
245 };
246 
247 // @brief Option for the `Window` component.
248 // @ingroup component
250  Component inner; ///< The component wrapped by this window.
251  ConstStringRef title = ""; ///< The title displayed by this window.
252 
253  Ref<int> left = 0; ///< The left side position of the window.
254  Ref<int> top = 0; ///< The top side position of the window.
255  Ref<int> width = 20; ///< The width of the window.
256  Ref<int> height = 10; ///< The height of the window.
257 
258  Ref<bool> resize_left = true; ///< Can the left side be resized?
259  Ref<bool> resize_right = true; ///< Can the right side be resized?
260  Ref<bool> resize_top = true; ///< Can the top side be resized?
261  Ref<bool> resize_down = true; ///< Can the down side be resized?
262 
263  /// An optional function to customize how the window looks like:
264  std::function<Element(const WindowRenderState&)> render;
265 };
266 
267 /// @brief Option for the Dropdown component.
268 /// @ingroup component
269 /// A dropdown menu is a checkbox opening/closing a radiobox.
271  /// Whether the dropdown is open or closed:
272  Ref<bool> open = false;
273  // The options for the checkbox:
275  // The options for the radiobox:
277  // The transformation function:
278  std::function<Element(bool open, Element checkbox, Element radiobox)>
280 };
281 
282 } // namespace ftxui
283 
284 #endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */
A class representing terminal colors.
Definition: color.hpp:20
An adapter. Own or reference an immutable object.
Definition: ref.hpp:17
An adapter. Reference a list of strings.
Definition: ref.hpp:116
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition: ref.hpp:94
An adapter. Own or reference a constant string. For convenience, this class convert multiple mutable ...
Definition: ref.hpp:82
float QuadraticInOut(float p)
Definition: animation.cpp:46
std::function< float(float)> Function
Definition: animation.hpp:35
std::chrono::duration< float > Duration
Definition: animation.hpp:20
bool active
Whether the entry is the active one.
Ref< bool > resize_down
Can the down side be resized?
Component inner
The component wrapped by this window.
Ref< bool > resize_left
Can the left side be resized?
AnimatedColorOption foreground
std::string label
The label to display.
bool drag
Whether the window is being dragged.
Direction
Definition: direction.hpp:8
std::function< Element()> separator_func
std::shared_ptr< Node > Element
Definition: elements.hpp:22
bool focused
Whether the entry is one focused by the user.
bool hover_down
Whether the resizeable down side is hovered.
std::shared_ptr< ComponentBase > Component
const std::string & title
The title of the window.
Ref< int > height
The height of the window.
bool resize
Whether the window is being resized.
Element inner
The element wrapped inside this window.
AnimatedColorOption background
std::function< Element(const EntryState &state)> transform
int index
Index of the entry when applicable or -1.
Ref< bool > resize_top
Can the top side be resized?
bool hover_right
Whether the resizeable right side is hovered.
bool hovered
Whether the input is hovered by the mouse.
Ref< int > width
The width of the window.
std::function< Element(const WindowRenderState &)> render
An optional function to customize how the window looks like:
Ref< bool > open
Whether the dropdown is open or closed:
bool state
The state of the button/checkbox/radiobox.
AnimatedColorsOption animated_colors
ConstStringRef title
The title displayed by this window.
Ref< bool > resize_right
Can the right side be resized?
bool hover_left
Whether the resizeable left side is hovered.
std::function< void()> on_change
Element separator()
Draw a vertical or horizontal separation in between two other elements.
Definition: separator.cpp:134
bool hover_top
Whether the resizeable top side is hovered.
Ref< int > left
The left side position of the window.
std::function< Element(bool open, Element checkbox, Element radiobox)> transform
Ref< int > top
The top side position of the window.
Decorator color(Color)
Decorate using a foreground color.
Definition: color.cpp:110
Option for the Dropdown component.A dropdown menu is a checkbox opening/closing a radiobox.
arguments for |ButtonOption::transform|, |CheckboxOption::transform|, |Radiobox::transform|,...
Used to define style for the Input component.
Option for the MenuEntry component.
Option about a potentially animated color.
animation::Duration duration
void Set(Color inactive, Color active, animation::Duration duration=std::chrono::milliseconds(250), animation::easing::Function function=animation::easing::QuadraticInOut)
A color option that can be animated. @params _inactive The color when the component is inactive....
Option for the AnimatedButton component.
static ButtonOption Animated()
Create a ButtonOption, using animated colors.
std::function< void()> on_click
static ButtonOption Border()
Create a ButtonOption. The button is shown using a border, inverted when focused. This is the current...
static ButtonOption Simple()
Create a ButtonOption, inverted when focused.
static ButtonOption Ascii()
Create a ButtonOption, highlighted using [] characters.
AnimatedColorsOption animated_colors
std::function< Element(const EntryState &)> transform
Option for the Checkbox component.
static CheckboxOption Simple()
Option for standard Checkbox.
std::function< void()> on_change
Called when the user change the state.
std::function< Element(const EntryState &)> transform
Option for the Input component.
static InputOption Default()
Create the default input style:
static InputOption Spacious()
A white on black style with high margins:
Ref< bool > insert
Insert or overtype character mode.
std::function< void()> on_enter
Called when the user presses enter.
Ref< bool > password
Obscure the input content using '*'.
std::function< Element(InputState)> transform
StringRef placeholder
The content of the input when it's empty.
std::function< void()> on_change
Called when the content changes.
StringRef content
The content of the input.
Ref< bool > multiline
Whether the input can be multiline.
Option for the Menu component.
std::function< Element()> elements_prefix
static MenuOption Toggle()
Standard options for a horitontal menu with some separator. This can be useful to implement a tab bar...
MenuEntryOption entries_option
std::function< void()> on_enter
Called when the selected entry changes.
UnderlineOption underline
The index of the selected entry.
static MenuOption Horizontal()
Standard options for an horizontal menu. This can be useful to implement a tab bar.
static MenuOption VerticalAnimated()
Standard options for an animated vertical menu. This can be useful to implement a list of selectable ...
static MenuOption Vertical()
Standard options for a vertical menu. This can be useful to implement a list of selectable items.
ConstStringListRef entries
Ref< int > focused_entry
Called when the user presses enter.
std::function< Element()> elements_infix
std::function< Element()> elements_postfix
std::function< void()> on_change
Ref< int > selected
The list of entries.
static MenuOption HorizontalAnimated()
Standard options for an animated horizontal menu. This can be useful to implement a tab bar.
Option for the Radiobox component.
ConstStringListRef entries
std::function< void()> on_change
Called when the selected entry changes.
static RadioboxOption Simple()
Option for standard Radiobox.
std::function< Element(const EntryState &)> transform
animation::Duration follower_duration
animation::easing::Function leader_function
animation::Duration follower_delay
void SetAnimationFunction(animation::easing::Function f)
Set how the underline should animate.
animation::Duration leader_duration
void SetAnimation(animation::Duration d, animation::easing::Function f)
Set how the underline should animate.
void SetAnimationDuration(animation::Duration d)
Set how the underline should animate.
animation::easing::Function follower_function
animation::Duration leader_delay