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
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
18namespace ftxui {
19
20/// @brief arguments for transform from |ButtonOption|, |CheckboxOption|,
21/// |RadioboxOption|, |MenuEntryOption|, |MenuOption|.
22struct EntryState {
23 std::string label; ///< The label to display.
24 bool state; ///< The state of the button/checkbox/radiobox
25 bool active; ///< Whether the entry is the active one.
26 bool focused; ///< Whether the entry is one focused by the user.
27 int index; ///< Index of the entry when applicable or -1.
28};
29
52
53/// @brief Option about a potentially animated color.
54/// @ingroup component
56 void Set(
59 animation::Duration duration = std::chrono::milliseconds(250),
60 animation::easing::Function function = animation::easing::QuadraticInOut);
61
62 bool enabled = false;
65 animation::Duration duration = std::chrono::milliseconds(250);
67};
68
73
74/// @brief Option for the MenuEntry component.
75/// @ingroup component
81
82/// @brief Option for the Menu component.
83/// @ingroup component
84struct MenuOption {
85 // Standard constructors:
86 static MenuOption Horizontal();
88 static MenuOption Vertical();
90 static MenuOption Toggle();
91
92 ConstStringListRef entries; ///> The list of entries.
93 Ref<int> selected = 0; ///> The index of the selected entry.
94
95 // Style:
99 std::function<Element()> elements_prefix;
100 std::function<Element()> elements_infix;
101 std::function<Element()> elements_postfix;
102
103 // Observers:
104 std::function<void()> on_change; ///> Called when the selected entry changes.
105 std::function<void()> on_enter; ///> Called when the user presses enter.
107};
108
109/// @brief Option for the AnimatedButton component.
110/// @ingroup component
112 // Standard constructors:
113 static ButtonOption Ascii();
114 static ButtonOption Simple();
115 static ButtonOption Border();
116 static ButtonOption Animated();
117 static ButtonOption Animated(Color color);
118 static ButtonOption Animated(Color background, Color foreground);
119 static ButtonOption Animated(Color background,
120 Color foreground,
121 Color background_active,
122 Color foreground_active);
123
125 std::function<void()> on_click = [] {};
126
127 // Style:
128 std::function<Element(const EntryState&)> transform;
130};
131
132/// @brief Option for the Checkbox component.
133/// @ingroup component
135 // Standard constructors:
136 static CheckboxOption Simple();
137
138 ConstStringRef label = "Checkbox";
139
141
142 // Style:
143 std::function<Element(const EntryState&)> transform;
144
145 // Observer:
146 /// Called when the user change the state.
147 std::function<void()> on_change = [] {};
148};
149
150/// @brief Used to define style for the Input component.
153 bool hovered; ///< Whether the input is hovered by the mouse.
154 bool focused; ///< Whether the input is focused by the user.
155 bool is_placeholder; ///< Whether the input is empty and displaying the
156 ///< placeholder.
157};
158
159/// @brief Option for the Input component.
160/// @ingroup component
162 // A set of predefined styles:
163
164 /// @brief Create the default input style:
165 static InputOption Default();
166 /// @brief A white on black style with high margins:
167 static InputOption Spacious();
168
169 /// The content of the input.
171
172 /// The content of the input when it's empty.
174
175 // Style:
176 std::function<Element(InputState)> transform;
177 Ref<bool> password = false; ///< Obscure the input content using '*'.
178 Ref<bool> multiline = true; ///< Whether the input can be multiline.
179 Ref<bool> insert = true; ///< Insert or overtype character mode.
180
181 /// Called when the content changes.
182 std::function<void()> on_change = [] {};
183 /// Called when the user presses enter.
184 std::function<void()> on_enter = [] {};
185
186 // The char position of the cursor:
188};
189
190/// @brief Option for the Radiobox component.
191/// @ingroup component
193 // Standard constructors:
194 static RadioboxOption Simple();
195
196 // Content:
199
200 // Style:
201 std::function<Element(const EntryState&)> transform;
202
203 // Observers:
204 /// Called when the selected entry changes.
205 std::function<void()> on_change = [] {};
207};
208
218
219// @brief Option for the `Slider` component.
220// @ingroup component
221template <typename T>
232
233// Parameter pack used by `WindowOptions::render`.
235 Element inner; ///< The element wrapped inside this window.
236 const std::string& title; ///< The title of the window.
237 bool active = false; ///< Whether the window is the active one.
238 bool drag = false; ///< Whether the window is being dragged.
239 bool resize = false; ///< Whether the window is being resized.
240 bool hover_left = false; ///< Whether the resizeable left side is hovered.
241 bool hover_right = false; ///< Whether the resizeable right side is hovered.
242 bool hover_top = false; ///< Whether the resizeable top side is hovered.
243 bool hover_down = false; ///< Whether the resizeable down side is hovered.
244};
245
246// @brief Option for the `Window` component.
247// @ingroup component
249 Component inner; ///< The component wrapped by this window.
250 ConstStringRef title = ""; ///< The title displayed by this window.
251
252 Ref<int> left = 0; ///< The left side position of the window.
253 Ref<int> top = 0; ///< The top side position of the window.
254 Ref<int> width = 20; ///< The width of the window.
255 Ref<int> height = 10; ///< The height of the window.
256
257 Ref<bool> resize_left = true; ///< Can the left side be resized?
258 Ref<bool> resize_right = true; ///< Can the right side be resized?
259 Ref<bool> resize_top = true; ///< Can the top side be resized?
260 Ref<bool> resize_down = true; ///< Can the down side be resized?
261
262 /// An optional function to customize how the window looks like:
263 std::function<Element(const WindowRenderState&)> render;
264};
265
266/// @brief Option for the Dropdown component.
267/// @ingroup component
268/// A dropdown menu is a checkbox opening/closing a radiobox.
270 /// Whether the dropdown is open or closed:
272 // The options for the checkbox:
274 // The options for the radiobox:
276 // The transformation function:
277 std::function<Element(bool open, Element checkbox, Element radiobox)>
279};
280
281} // namespace ftxui
282
283#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */
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 an mutable object.
Definition ref.hpp:46
An adapter. Own or reference a constant string. For convenience, this class convert multiple mutable ...
Definition ref.hpp:82
std::function< void()> on_click
std::function< Element()> elements_prefix
MenuEntryOption entries_option
animation::easing::Function function
Ref< bool > insert
Insert or overtype character mode.
std::function< void()> on_enter
UnderlineOption underline
std::function< Element(const EntryState &state)> transform
ConstStringListRef entries
Ref< bool > password
Obscure the input content using '*'.
std::function< Element(InputState)> transform
std::function< Element()> elements_infix
Ref< bool > open
Whether the dropdown is open or closed:
StringRef placeholder
The content of the input when it's empty.
std::function< Element()> elements_postfix
AnimatedColorsOption animated_colors
std::function< void()> on_change
StringRef content
The content of the input.
std::function< Element(bool open, Element checkbox, Element radiobox)> transform
Ref< bool > multiline
Whether the input can be multiline.
std::function< Element(const EntryState &)> transform
static ButtonOption Animated()
Create a ButtonOption, using animated colors.
static MenuOption Toggle()
Standard options for a horizontal menu with some separator. This can be useful to implement a tab bar...
static InputOption Default()
Create the default input style:
static ButtonOption Border()
Create a ButtonOption. The button is shown using a border, inverted when focused. This is the current...
void SetAnimationFunction(animation::easing::Function f)
Set how the underline should animate.
static InputOption Spacious()
A white on black style with high margins:
static CheckboxOption Simple()
Option for standard Checkbox.
static ButtonOption Simple()
Create a ButtonOption, inverted when focused.
static MenuOption Horizontal()
Standard options for a 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.
static ButtonOption Ascii()
Create a ButtonOption, highlighted using [] characters.
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.
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....
static MenuOption HorizontalAnimated()
Standard options for an animated horizontal menu. This can be useful to implement a tab bar.
static RadioboxOption Simple()
Option for standard Radiobox.
Option about a potentially animated color.
Option for the AnimatedButton component.
Option for the Checkbox component.
Option for the Dropdown component.A dropdown menu is a checkbox opening/closing a radiobox.
Option for the Input component.
Option for the MenuEntry component.
Option for the Menu component.
Option for the Radiobox component.
A class representing terminal colors.
Definition color.hpp:20
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
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::shared_ptr< Node > Element
Definition elements.hpp:22
std::shared_ptr< ComponentBase > Component
arguments for transform from |ButtonOption|, |CheckboxOption|, |RadioboxOption|, |MenuEntryOption|,...
bool active
Whether the entry is the active one.
std::string label
The label to display.
bool focused
Whether the entry is one focused by the user.
int index
Index of the entry when applicable or -1.
bool state
The state of the button/checkbox/radiobox.
Used to define style for the Input component.
bool focused
Whether the input is focused by the user.
bool hovered
Whether the input is hovered by the mouse.
std::function< Element()> separator_func
std::function< void()> on_change
animation::Duration follower_duration
animation::easing::Function leader_function
animation::Duration follower_delay
animation::Duration leader_duration
animation::easing::Function follower_function
animation::Duration leader_delay
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?
Ref< int > height
The height of the window.
Ref< bool > resize_top
Can the top side be resized?
Ref< int > width
The width of the window.
std::function< Element(const WindowRenderState &)> render
An optional function to customize how the window looks like:
ConstStringRef title
The title displayed by this window.
Ref< bool > resize_right
Can the right side be resized?
Ref< int > left
The left side position of the window.
Ref< int > top
The top side position of the window.
bool active
Whether the window is the active one.
bool drag
Whether the window is being dragged.
bool hover_down
Whether the resizeable down side is hovered.
const std::string & title
The title of the window.
bool resize
Whether the window is being resized.
Element inner
The element wrapped inside this window.
bool hover_right
Whether the resizeable right side is hovered.
bool hover_left
Whether the resizeable left side is hovered.
bool hover_top
Whether the resizeable top side is hovered.