Loading...
Searching...
No Matches
component_options.hpp
Go to the documentation of this file.
// Copyright 2021 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#ifndef FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP
#define FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP
#include <chrono> // for milliseconds
#include <ftxui/component/animation.hpp> // for Duration, QuadraticInOut, Function
#include <ftxui/dom/direction.hpp> // for Direction, Direction::Left, Direction::Right, Direction::Down
#include <ftxui/dom/elements.hpp> // for Element, separator
#include <ftxui/util/ref.hpp> // for Ref, ConstRef, StringRef
#include <functional> // for function
#include <string> // for string
#include "ftxui/component/component_base.hpp" // for Component
#include "ftxui/screen/color.hpp" // for Color, Color::GrayDark, Color::White
namespace ftxui {
/// @brief arguments for |ButtonOption::transform|, |CheckboxOption::transform|,
/// |Radiobox::transform|, |MenuEntryOption::transform|,
/// |MenuOption::transform|.
struct EntryState {
std::string label; ///< The label to display.
bool state; ///< The state of the button/checkbox/radiobox
bool active; ///< Whether the entry is the active one.
bool focused; ///< Whether the entry is one focused by the user.
int index; ///< Index of the entry when applicable or -1.
};
struct UnderlineOption {
bool enabled = false;
Color color_active = Color::White;
Color color_inactive = Color::GrayDark;
animation::easing::Function leader_function =
animation::easing::QuadraticInOut;
animation::easing::Function follower_function =
animation::easing::QuadraticInOut;
animation::Duration leader_duration = std::chrono::milliseconds(250);
animation::Duration leader_delay = std::chrono::milliseconds(0);
animation::Duration follower_duration = std::chrono::milliseconds(250);
animation::Duration follower_delay = std::chrono::milliseconds(0);
void SetAnimation(animation::Duration d, animation::easing::Function f);
void SetAnimationDuration(animation::Duration d);
void SetAnimationFunction(animation::easing::Function f);
void SetAnimationFunction(animation::easing::Function f_leader,
animation::easing::Function f_follower);
};
/// @brief Option about a potentially animated color.
/// @ingroup component
struct AnimatedColorOption {
void Set(
Color inactive,
Color active,
animation::Duration duration = std::chrono::milliseconds(250),
animation::easing::Function function = animation::easing::QuadraticInOut);
bool enabled = false;
Color inactive;
Color active;
animation::Duration duration = std::chrono::milliseconds(250);
animation::easing::Function function = animation::easing::QuadraticInOut;
};
struct AnimatedColorsOption {
AnimatedColorOption background;
AnimatedColorOption foreground;
};
/// @brief Option for the MenuEntry component.
/// @ingroup component
struct MenuEntryOption {
ConstStringRef label = "MenuEntry";
std::function<Element(const EntryState& state)> transform;
AnimatedColorsOption animated_colors;
};
/// @brief Option for the Menu component.
/// @ingroup component
struct MenuOption {
// Standard constructors:
static MenuOption Horizontal();
static MenuOption HorizontalAnimated();
static MenuOption Vertical();
static MenuOption VerticalAnimated();
static MenuOption Toggle();
ConstStringListRef entries; ///> The list of entries.
Ref<int> selected = 0; ///> The index of the selected entry.
// Style:
UnderlineOption underline;
MenuEntryOption entries_option;
Direction direction = Direction::Down;
std::function<Element()> elements_prefix;
std::function<Element()> elements_infix;
std::function<Element()> elements_postfix;
// Observers:
std::function<void()> on_change; ///> Called when the selected entry changes.
std::function<void()> on_enter; ///> Called when the user presses enter.
Ref<int> focused_entry = 0;
};
/// @brief Option for the AnimatedButton component.
/// @ingroup component
struct ButtonOption {
// Standard constructors:
static ButtonOption Ascii();
static ButtonOption Simple();
static ButtonOption Border();
static ButtonOption Animated();
static ButtonOption Animated(Color color);
static ButtonOption Animated(Color background, Color foreground);
static ButtonOption Animated(Color background,
Color foreground,
Color background_active,
Color foreground_active);
ConstStringRef label = "Button";
std::function<void()> on_click = [] {};
// Style:
std::function<Element(const EntryState&)> transform;
AnimatedColorsOption animated_colors;
};
/// @brief Option for the Checkbox component.
/// @ingroup component
struct CheckboxOption {
// Standard constructors:
static CheckboxOption Simple();
ConstStringRef label = "Checkbox";
Ref<bool> checked = false;
// Style:
std::function<Element(const EntryState&)> transform;
// Observer:
/// Called when the user change the state.
std::function<void()> on_change = [] {};
};
/// @brief Used to define style for the Input component.
struct InputState {
Element element;
bool hovered; ///< Whether the input is hovered by the mouse.
bool focused; ///< Whether the input is focused by the user.
bool is_placeholder; ///< Whether the input is empty and displaying the
///< placeholder.
};
/// @brief Option for the Input component.
/// @ingroup component
struct InputOption {
// A set of predefined styles:
/// @brief Create the default input style:
static InputOption Default();
/// @brief A white on black style with high margins:
static InputOption Spacious();
/// The content of the input.
StringRef content = "";
/// The content of the input when it's empty.
StringRef placeholder = "";
// Style:
std::function<Element(InputState)> transform;
Ref<bool> password = false; ///< Obscure the input content using '*'.
Ref<bool> multiline = true; ///< Whether the input can be multiline.
Ref<bool> insert = true; ///< Insert or overtype character mode.
/// Called when the content changes.
std::function<void()> on_change = [] {};
/// Called when the user presses enter.
std::function<void()> on_enter = [] {};
// The char position of the cursor:
Ref<int> cursor_position = 0;
};
/// @brief Option for the Radiobox component.
/// @ingroup component
struct RadioboxOption {
// Standard constructors:
static RadioboxOption Simple();
// Content:
ConstStringListRef entries;
Ref<int> selected = 0;
// Style:
std::function<Element(const EntryState&)> transform;
// Observers:
/// Called when the selected entry changes.
std::function<void()> on_change = [] {};
Ref<int> focused_entry = 0;
};
struct ResizableSplitOption {
Component main;
Component back;
Ref<Direction> direction = Direction::Left;
Ref<int> main_size =
(direction() == Direction::Left || direction() == Direction::Right) ? 20
: 10;
std::function<Element()> separator_func = [] { return ::ftxui::separator(); };
};
// @brief Option for the `Slider` component.
// @ingroup component
template <typename T>
struct SliderOption {
Ref<T> value;
ConstRef<T> min = T(0);
ConstRef<T> max = T(100);
ConstRef<T> increment = (max() - min()) / 20;
Direction direction = Direction::Right;
Color color_active = Color::White;
Color color_inactive = Color::GrayDark;
std::function<void()> on_change; ///> Called when `value` is updated.
};
// Parameter pack used by `WindowOptions::render`.
struct WindowRenderState {
Element inner; ///< The element wrapped inside this window.
const std::string& title; ///< The title of the window.
bool active = false; ///< Whether the window is the active one.
bool drag = false; ///< Whether the window is being dragged.
bool resize = false; ///< Whether the window is being resized.
bool hover_left = false; ///< Whether the resizeable left side is hovered.
bool hover_right = false; ///< Whether the resizeable right side is hovered.
bool hover_top = false; ///< Whether the resizeable top side is hovered.
bool hover_down = false; ///< Whether the resizeable down side is hovered.
};
// @brief Option for the `Window` component.
// @ingroup component
struct WindowOptions {
Component inner; ///< The component wrapped by this window.
ConstStringRef title = ""; ///< The title displayed by this window.
Ref<int> left = 0; ///< The left side position of the window.
Ref<int> top = 0; ///< The top side position of the window.
Ref<int> width = 20; ///< The width of the window.
Ref<int> height = 10; ///< The height of the window.
Ref<bool> resize_left = true; ///< Can the left side be resized?
Ref<bool> resize_right = true; ///< Can the right side be resized?
Ref<bool> resize_top = true; ///< Can the top side be resized?
Ref<bool> resize_down = true; ///< Can the down side be resized?
/// An optional function to customize how the window looks like:
std::function<Element(const WindowRenderState&)> render;
};
/// @brief Option for the Dropdown component.
/// @ingroup component
/// A dropdown menu is a checkbox opening/closing a radiobox.
struct DropdownOption {
/// Whether the dropdown is open or closed:
Ref<bool> open = false;
// The options for the checkbox:
CheckboxOption checkbox;
// The options for the radiobox:
RadioboxOption radiobox;
// The transformation function:
std::function<Element(bool open, Element checkbox, Element radiobox)>
transform;
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */
animation.hpp
ftxui::Color
A class representing terminal colors.
Definition color.hpp:20
ftxui::Color::White
@ White
Definition color.hpp:67
ftxui::Color::GrayDark
@ GrayDark
Definition color.hpp:60
ftxui::ConstRef
An adapter. Own or reference an immutable object.
Definition ref.hpp:17
ftxui::ConstStringListRef
An adapter. Reference a list of strings.
Definition ref.hpp:116
ftxui::ConstStringRef
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition ref.hpp:94
ftxui::Ref
An adapter. Own or reference an mutable object.
Definition ref.hpp:46
ftxui::StringRef
An adapter. Own or reference a constant string. For convenience, this class convert multiple mutable ...
Definition ref.hpp:82
color.hpp
component_base.hpp
direction.hpp
elements.hpp
ftxui::animation::Duration
std::chrono::duration< float > Duration
Definition animation.hpp:20
ftxui
Definition animation.hpp:10
ftxui::EntryState::active
bool active
Whether the entry is the active one.
Definition component_options.hpp:26
ftxui::DropdownOption::checkbox
CheckboxOption checkbox
Definition component_options.hpp:274
ftxui::WindowOptions::resize_down
Ref< bool > resize_down
Can the down side be resized?
Definition component_options.hpp:261
ftxui::WindowOptions::inner
Component inner
The component wrapped by this window.
Definition component_options.hpp:250
ftxui::WindowOptions::resize_left
Ref< bool > resize_left
Can the left side be resized?
Definition component_options.hpp:258
ftxui::AnimatedColorsOption::foreground
AnimatedColorOption foreground
Definition component_options.hpp:72
ftxui::EntryState::label
std::string label
The label to display.
Definition component_options.hpp:24
ftxui::WindowRenderState::drag
bool drag
Whether the window is being dragged.
Definition component_options.hpp:239
ftxui::Direction
Direction
Definition direction.hpp:8
ftxui::Direction::Down
@ Down
ftxui::Direction::Right
@ Right
ftxui::Direction::Left
@ Left
ftxui::ResizableSplitOption::separator_func
std::function< Element()> separator_func
Definition component_options.hpp:217
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::EntryState::focused
bool focused
Whether the entry is one focused by the user.
Definition component_options.hpp:27
ftxui::Make
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
ftxui::WindowRenderState::hover_down
bool hover_down
Whether the resizeable down side is hovered.
Definition component_options.hpp:244
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24
ftxui::WindowRenderState::title
const std::string & title
The title of the window.
Definition component_options.hpp:237
ftxui::MenuEntryOption::label
ConstStringRef label
Definition component_options.hpp:78
ftxui::SliderOption::min
ConstRef< T > min
Definition component_options.hpp:225
ftxui::ResizableSplitOption::main_size
Ref< int > main_size
Definition component_options.hpp:214
ftxui::WindowOptions::height
Ref< int > height
The height of the window.
Definition component_options.hpp:256
ftxui::WindowRenderState::resize
bool resize
Whether the window is being resized.
Definition component_options.hpp:240
ftxui::SliderOption::direction
Direction direction
Definition component_options.hpp:228
ftxui::WindowRenderState::inner
Element inner
The element wrapped inside this window.
Definition component_options.hpp:236
ftxui::AnimatedColorsOption::background
AnimatedColorOption background
Definition component_options.hpp:71
ftxui::MenuEntryOption::transform
std::function< Element(const EntryState &state)> transform
Definition component_options.hpp:79
ftxui::ResizableSplitOption::back
Component back
Definition component_options.hpp:212
ftxui::SliderOption::color_active
Color color_active
Definition component_options.hpp:229
ftxui::EntryState::index
int index
Index of the entry when applicable or -1.
Definition component_options.hpp:28
ftxui::SliderOption::increment
ConstRef< T > increment
Definition component_options.hpp:227
ftxui::DropdownOption::radiobox
RadioboxOption radiobox
Definition component_options.hpp:276
ftxui::WindowOptions::resize_top
Ref< bool > resize_top
Can the top side be resized?
Definition component_options.hpp:260
ftxui::WindowRenderState::hover_right
bool hover_right
Whether the resizeable right side is hovered.
Definition component_options.hpp:242
ftxui::InputState::hovered
bool hovered
Whether the input is hovered by the mouse.
Definition component_options.hpp:154
ftxui::WindowOptions::width
Ref< int > width
The width of the window.
Definition component_options.hpp:255
ftxui::WindowOptions::render
std::function< Element(const WindowRenderState &)> render
An optional function to customize how the window looks like:
Definition component_options.hpp:264
ftxui::InputState::is_placeholder
bool is_placeholder
Definition component_options.hpp:156
ftxui::DropdownOption::open
Ref< bool > open
Whether the dropdown is open or closed:
Definition component_options.hpp:272
ftxui::SliderOption::value
Ref< T > value
Definition component_options.hpp:224
ftxui::EntryState::state
bool state
The state of the button/checkbox/radiobox.
Definition component_options.hpp:25
ftxui::MenuEntryOption::animated_colors
AnimatedColorsOption animated_colors
Definition component_options.hpp:80
ftxui::WindowOptions::title
ConstStringRef title
The title displayed by this window.
Definition component_options.hpp:251
ftxui::WindowOptions::resize_right
Ref< bool > resize_right
Can the right side be resized?
Definition component_options.hpp:259
ftxui::WindowRenderState::hover_left
bool hover_left
Whether the resizeable left side is hovered.
Definition component_options.hpp:241
ftxui::SliderOption::max
ConstRef< T > max
Definition component_options.hpp:226
ftxui::SliderOption::on_change
std::function< void()> on_change
Definition component_options.hpp:231
ftxui::WindowRenderState::hover_top
bool hover_top
Whether the resizeable top side is hovered.
Definition component_options.hpp:243
ftxui::SliderOption::color_inactive
Color color_inactive
Definition component_options.hpp:230
ftxui::WindowOptions::left
Ref< int > left
The left side position of the window.
Definition component_options.hpp:253
ftxui::ResizableSplitOption::direction
Ref< Direction > direction
Definition component_options.hpp:213
ftxui::DropdownOption::transform
std::function< Element(bool open, Element checkbox, Element radiobox)> transform
Definition component_options.hpp:279
ftxui::WindowOptions::top
Ref< int > top
The top side position of the window.
Definition component_options.hpp:254
ftxui::InputState::element
Element element
Definition component_options.hpp:153
ftxui::color
Decorator color(Color)
Decorate using a foreground color.
Definition color.cpp:110
ftxui::ResizableSplitOption::main
Component main
Definition component_options.hpp:211
ftxui::AnimatedColorsOption
Definition component_options.hpp:70
ftxui::DropdownOption
Option for the Dropdown component.A dropdown menu is a checkbox opening/closing a radiobox.
Definition component_options.hpp:270
ftxui::EntryState
arguments for |ButtonOption::transform|, |CheckboxOption::transform|, |Radiobox::transform|,...
Definition component_options.hpp:23
ftxui::InputState
Used to define style for the Input component.
Definition component_options.hpp:152
ftxui::ResizableSplitOption
Definition component_options.hpp:210
ftxui::SliderOption
Definition component_options.hpp:223
ftxui::WindowOptions
Definition component_options.hpp:249
ftxui::WindowRenderState
Definition component_options.hpp:235
ref.hpp
ftxui::AnimatedColorOption
Option about a potentially animated color.
Definition component_options.hpp:56
ftxui::AnimatedColorOption::function
animation::easing::Function function
Definition component_options.hpp:67
ftxui::AnimatedColorOption::active
Color active
Definition component_options.hpp:65
ftxui::AnimatedColorOption::duration
animation::Duration duration
Definition component_options.hpp:66
ftxui::AnimatedColorOption::enabled
bool enabled
Definition component_options.hpp:63
ftxui::AnimatedColorOption::inactive
Color inactive
Definition component_options.hpp:64
ftxui::AnimatedColorOption::Set
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....
Definition component_options.cpp:21
ftxui::ButtonOption
Option for the AnimatedButton component.
Definition component_options.hpp:112
ftxui::ButtonOption::Animated
static ButtonOption Animated()
Create a ButtonOption, using animated colors.
Definition component_options.cpp:210
ftxui::ButtonOption::on_click
std::function< void()> on_click
Definition component_options.hpp:126
ftxui::ButtonOption::Border
static ButtonOption Border()
Create a ButtonOption. The button is shown using a border, inverted when focused. This is the current...
Definition component_options.cpp:192
ftxui::ButtonOption::label
ConstStringRef label
Definition component_options.hpp:125
ftxui::ButtonOption::Simple
static ButtonOption Simple()
Create a ButtonOption, inverted when focused.
Definition component_options.cpp:177
ftxui::ButtonOption::Ascii
static ButtonOption Ascii()
Create a ButtonOption, highlighted using [] characters.
Definition component_options.cpp:164
ftxui::ButtonOption::animated_colors
AnimatedColorsOption animated_colors
Definition component_options.hpp:130
ftxui::ButtonOption::transform
std::function< Element(const EntryState &)> transform
Definition component_options.hpp:129
ftxui::CheckboxOption
Option for the Checkbox component.
Definition component_options.hpp:135
ftxui::CheckboxOption::label
ConstStringRef label
Definition component_options.hpp:139
ftxui::CheckboxOption::Simple
static CheckboxOption Simple()
Option for standard Checkbox.
Definition component_options.cpp:262
ftxui::CheckboxOption::checked
Ref< bool > checked
Definition component_options.hpp:141
ftxui::CheckboxOption::on_change
std::function< void()> on_change
Called when the user change the state.
Definition component_options.hpp:148
ftxui::CheckboxOption::transform
std::function< Element(const EntryState &)> transform
Definition component_options.hpp:144
ftxui::InputOption
Option for the Input component.
Definition component_options.hpp:162
ftxui::InputOption::Default
static InputOption Default()
Create the default input style:
Definition component_options.cpp:312
ftxui::InputOption::Spacious
static InputOption Spacious()
A white on black style with high margins:
Definition component_options.cpp:335
ftxui::InputOption::insert
Ref< bool > insert
Insert or overtype character mode.
Definition component_options.hpp:180
ftxui::InputOption::on_enter
std::function< void()> on_enter
Called when the user presses enter.
Definition component_options.hpp:185
ftxui::InputOption::cursor_position
Ref< int > cursor_position
Definition component_options.hpp:188
ftxui::InputOption::password
Ref< bool > password
Obscure the input content using '*'.
Definition component_options.hpp:178
ftxui::InputOption::transform
std::function< Element(InputState)> transform
Definition component_options.hpp:177
ftxui::InputOption::placeholder
StringRef placeholder
The content of the input when it's empty.
Definition component_options.hpp:174
ftxui::InputOption::on_change
std::function< void()> on_change
Called when the content changes.
Definition component_options.hpp:183
ftxui::InputOption::content
StringRef content
The content of the input.
Definition component_options.hpp:171
ftxui::InputOption::multiline
Ref< bool > multiline
Whether the input can be multiline.
Definition component_options.hpp:179
ftxui::RadioboxOption
Option for the Radiobox component.
Definition component_options.hpp:193
ftxui::RadioboxOption::entries
ConstStringListRef entries
Definition component_options.hpp:198
ftxui::RadioboxOption::focused_entry
Ref< int > focused_entry
Definition component_options.hpp:207
ftxui::RadioboxOption::on_change
std::function< void()> on_change
Called when the selected entry changes.
Definition component_options.hpp:206
ftxui::RadioboxOption::selected
Ref< int > selected
Definition component_options.hpp:199
ftxui::RadioboxOption::Simple
static RadioboxOption Simple()
Option for standard Radiobox.
Definition component_options.cpp:287
ftxui::RadioboxOption::transform
std::function< Element(const EntryState &)> transform
Definition component_options.hpp:202
ftxui::UnderlineOption
Definition component_options.hpp:31
ftxui::UnderlineOption::follower_duration
animation::Duration follower_duration
Definition component_options.hpp:44
ftxui::UnderlineOption::leader_function
animation::easing::Function leader_function
Definition component_options.hpp:37
ftxui::UnderlineOption::follower_delay
animation::Duration follower_delay
Definition component_options.hpp:45
ftxui::UnderlineOption::SetAnimationFunction
void SetAnimationFunction(animation::easing::Function f)
Set how the underline should animate.
Definition component_options.cpp:53
ftxui::UnderlineOption::color_active
Color color_active
Definition component_options.hpp:34
ftxui::UnderlineOption::leader_duration
animation::Duration leader_duration
Definition component_options.hpp:42
ftxui::UnderlineOption::SetAnimation
void SetAnimation(animation::Duration d, animation::easing::Function f)
Set how the underline should animate.
Definition component_options.cpp:36
ftxui::UnderlineOption::SetAnimationDuration
void SetAnimationDuration(animation::Duration d)
Set how the underline should animate.
Definition component_options.cpp:45
ftxui::UnderlineOption::follower_function
animation::easing::Function follower_function
Definition component_options.hpp:39
ftxui::UnderlineOption::enabled
bool enabled
Definition component_options.hpp:32
ftxui::UnderlineOption::color_inactive
Color color_inactive
Definition component_options.hpp:35
ftxui::UnderlineOption::leader_delay
animation::Duration leader_delay
Definition component_options.hpp:43