FTXUI  5.0.0
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 <optional> // for optional
14#include <string> // for string
15
16#include "ftxui/component/component_base.hpp" // for Component
17#include "ftxui/screen/color.hpp" // for Color, Color::GrayDark, Color::White
18
19namespace ftxui {
20
21/// @brief arguments for |ButtonOption::transform|, |CheckboxOption::transform|,
22/// |Radiobox::transform|, |MenuEntryOption::transform|,
23/// |MenuOption::transform|.
24struct EntryState {
25 std::string label; /// < The label to display.
26 bool state; /// < The state of the button/checkbox/radiobox
27 bool active; /// < Whether the entry is the active one.
28 bool focused; /// < Whether the entry is one focused by the user.
29};
30
32 bool enabled = false;
33
36
37 animation::easing::Function leader_function =
38 animation::easing::QuadraticInOut;
39 animation::easing::Function follower_function =
40 animation::easing::QuadraticInOut;
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
47 void SetAnimation(animation::Duration d, animation::easing::Function f);
49 void SetAnimationFunction(animation::easing::Function f);
50 void SetAnimationFunction(animation::easing::Function f_leader,
51 animation::easing::Function f_follower);
52};
53
54/// @brief Option about a potentially animated color.
55/// @ingroup component
57 void Set(
60 animation::Duration duration = std::chrono::milliseconds(250),
61 animation::easing::Function function = animation::easing::QuadraticInOut);
62
63 bool enabled = false;
66 animation::Duration duration = std::chrono::milliseconds(250);
67 animation::easing::Function function = animation::easing::QuadraticInOut;
68};
69
74
75/// @brief Option for the MenuEntry component.
76/// @ingroup component
82
83/// @brief Option for the Menu component.
84/// @ingroup component
85struct 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
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,
124
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.
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
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
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>
231
232// Parameter pack used by `WindowOptions::render`.
234 Element inner; /// < The element wrapped inside this window.
235 const std::string& title; /// < The title of the window.
236 bool active = false; /// < Whether the window is the active one.
237 bool drag = false; /// < Whether the window is being dragged.
238 bool resize = false; /// < Whether the window is being resized.
239 bool hover_left = false; /// < Whether the resizeable left side is hovered.
240 bool hover_right = false; /// < Whether the resizeable right side is hovered.
241 bool hover_top = false; /// < Whether the resizeable top side is hovered.
242 bool hover_down = false; /// < Whether the resizeable down side is hovered.
243};
244
245// @brief Option for the `Window` component.
246// @ingroup component
248 Component inner; /// < The component wrapped by this window.
249 ConstStringRef title = ""; /// < The title displayed by this window.
250
251 Ref<int> left = 0; /// < The left side position of the window.
252 Ref<int> top = 0; /// < The top side position of the window.
253 Ref<int> width = 20; /// < The width of the window.
254 Ref<int> height = 10; /// < The height of the window.
255
256 Ref<bool> resize_left = true; /// < Can the left side be resized?
257 Ref<bool> resize_right = true; /// < Can the right side be resized?
258 Ref<bool> resize_top = true; /// < Can the top side be resized?
259 Ref<bool> resize_down = true; /// < Can the down side be resized?
260
261 /// An optional function to customize how the window looks like:
262 std::function<Element(const WindowRenderState&)> render;
263};
264
265} // namespace ftxui
266
267#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */
A class representing terminal colors.
Definition color.hpp:21
An adapter. Own or reference an immutable object.
Definition ref.hpp:15
An adapter. Reference a list of strings.
Definition ref.hpp:98
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition ref.hpp:86
An adapter. Own or reference an mutable object.
Definition ref.hpp:42
An adapter. Own or reference a constant string. For convenience, this class convert multiple mutable ...
Definition ref.hpp:76
std::chrono::duration< float > Duration
Definition animation.hpp:24
bool active
< The state of the button/checkbox/radiobox
Ref< bool > resize_down
< Can the top side be resized?
Ref< bool > resize_left
< The height of the window.
bool drag
< Whether the window is the active one.
std::function< Element()> separator_func
std::shared_ptr< Node > Element
Definition elements.hpp:23
bool focused
< Whether the entry is the active one.
bool hover_down
< Whether the resizeable top side is hovered.
std::shared_ptr< ComponentBase > Component
const std::string & title
< The element wrapped inside this window.
Ref< int > height
< The width of the window.
bool resize
< Whether the window is being dragged.
std::function< Element(const EntryState &state)> transform
Ref< bool > resize_top
< Can the right side be resized?
bool hover_right
< Whether the resizeable left side is hovered.
Ref< int > width
< The top side position of the window.
Component Slider(SliderOption< T > options)
A slider in any direction.
Definition slider.cpp:339
std::function< Element(const WindowRenderState &)> render
< Can the down side be resized?
bool is_placeholder
< Whether the input is focused by the user.
bool state
< The label to display.
AnimatedColorsOption animated_colors
ConstStringRef title
< The component wrapped by this window.
Ref< bool > resize_right
< Can the left side be resized?
bool hover_left
< Whether the window is being resized.
bool hover_top
< Whether the resizeable right side is hovered.
Ref< int > left
< The title displayed by this window.
Ref< int > top
< The left side position of the window.
Decorator color(Color)
Decorate using a foreground color.
Definition color.cpp:91
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::easing::Function function
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:
std::function< void()> on_enter
Called when the user presses enter.
std::function< Element(InputState)> transform
StringRef placeholder
The content of the input when it's empty.
std::function< void()> on_change
< Whether the input can be multiline.
StringRef content
The content of the input.
Ref< bool > multiline
< Obscure the input content using '*'.
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