Loading...
Searching...
No Matches
component_options.cpp
Go to the documentation of this file.
// Copyright 2022 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include "ftxui/component/component_options.hpp"
#include <ftxui/screen/color.hpp> // for Color, Color::White, Color::Black, Color::GrayDark, Color::Blue, Color::GrayLight, Color::Red
#include <memory> // for shared_ptr
#include <utility> // for move
#include "ftxui/component/animation.hpp" // for Function, Duration
#include "ftxui/dom/direction.hpp"
#include "ftxui/dom/elements.hpp" // for operator|=, Element, text, bgcolor, inverted, bold, dim, operator|, color, borderEmpty, hbox, automerge, border, borderLight
namespace ftxui {
/// @brief A color option that can be animated.
/// @params _inactive The color when the component is inactive.
/// @params _active The color when the component is active.
/// @params _duration The duration of the animation.
/// @params _function The easing function of the animation.
/// @ingroup component
void AnimatedColorOption::Set(Color _inactive,
Color _active,
animation::Duration _duration,
animation::easing::Function _function) {
enabled = true;
inactive = _inactive;
active = _active;
duration = _duration;
function = std::move(_function);
}
/// @brief Set how the underline should animate.
/// @param d The duration of the animation.
/// @param f The easing function of the animation.
/// @ingroup component
void UnderlineOption::SetAnimation(animation::Duration d,
animation::easing::Function f) {
SetAnimationDuration(d);
SetAnimationFunction(std::move(f));
}
/// @brief Set how the underline should animate.
/// @param d The duration of the animation.
/// @ingroup component
void UnderlineOption::SetAnimationDuration(animation::Duration d) {
leader_duration = d;
follower_duration = d;
}
/// @brief Set how the underline should animate.
/// @param f The easing function of the animation.
/// @ingroup component
void UnderlineOption::SetAnimationFunction(animation::easing::Function f) {
leader_function = f;
follower_function = std::move(f);
}
/// @brief Set how the underline should animate.
/// This is useful to desynchronize the animation of the leader and the
/// follower.
/// @param f_leader The duration of the animation for the leader.
/// @param f_follower The duration of the animation for the follower.
/// @ingroup component
void UnderlineOption::SetAnimationFunction(
animation::easing::Function f_leader,
animation::easing::Function f_follower) {
leader_function = std::move(f_leader);
follower_function = std::move(f_follower);
}
/// @brief Standard options for an horizontal menu.
/// This can be useful to implement a tab bar.
/// @ingroup component
// static
MenuOption MenuOption::Horizontal() {
MenuOption option;
option.direction = Direction::Right;
option.entries_option.transform = [](const EntryState& state) {
Element e = text(state.label);
if (state.focused) {
e |= inverted;
}
if (state.active) {
e |= bold;
}
if (!state.focused && !state.active) {
e |= dim;
}
return e;
};
option.elements_infix = [] { return text(" "); };
return option;
}
/// @brief Standard options for an animated horizontal menu.
/// This can be useful to implement a tab bar.
/// @ingroup component
// static
MenuOption MenuOption::HorizontalAnimated() {
auto option = Horizontal();
option.underline.enabled = true;
return option;
}
/// @brief Standard options for a vertical menu.
/// This can be useful to implement a list of selectable items.
/// @ingroup component
// static
MenuOption MenuOption::Vertical() {
MenuOption option;
option.entries_option.transform = [](const EntryState& state) {
Element e = text((state.active ? "> " : " ") + state.label); // NOLINT
if (state.focused) {
e |= inverted;
}
if (state.active) {
e |= bold;
}
if (!state.focused && !state.active) {
e |= dim;
}
return e;
};
return option;
}
/// @brief Standard options for an animated vertical menu.
/// This can be useful to implement a list of selectable items.
/// @ingroup component
// static
MenuOption MenuOption::VerticalAnimated() {
auto option = MenuOption::Vertical();
option.entries_option.transform = [](const EntryState& state) {
Element e = text(state.label);
if (state.focused) {
e |= inverted;
}
if (state.active) {
e |= bold;
}
if (!state.focused && !state.active) {
e |= dim;
}
return e;
};
option.underline.enabled = true;
return option;
}
/// @brief Standard options for a horitontal menu with some separator.
/// This can be useful to implement a tab bar.
/// @ingroup component
// static
MenuOption MenuOption::Toggle() {
auto option = MenuOption::Horizontal();
option.elements_infix = [] { return text("│") | automerge; };
return option;
}
/// @brief Create a ButtonOption, highlighted using [] characters.
/// @ingroup component
// static
ButtonOption ButtonOption::Ascii() {
ButtonOption option;
option.transform = [](const EntryState& s) {
const std::string t = s.focused ? "[" + s.label + "]" //
: " " + s.label + " ";
return text(t);
};
return option;
}
/// @brief Create a ButtonOption, inverted when focused.
/// @ingroup component
// static
ButtonOption ButtonOption::Simple() {
ButtonOption option;
option.transform = [](const EntryState& s) {
auto element = text(s.label) | borderLight;
if (s.focused) {
element |= inverted;
}
return element;
};
return option;
}
/// @brief Create a ButtonOption. The button is shown using a border, inverted
/// when focused. This is the current default.
/// @ingroup component
ButtonOption ButtonOption::Border() {
ButtonOption option;
option.transform = [](const EntryState& s) {
auto element = text(s.label) | border;
if (s.active) {
element |= bold;
}
if (s.focused) {
element |= inverted;
}
return element;
};
return option;
}
/// @brief Create a ButtonOption, using animated colors.
/// @ingroup component
// static
ButtonOption ButtonOption::Animated() {
return Animated(Color::Black, Color::GrayLight, //
Color::GrayDark, Color::White);
}
/// @brief Create a ButtonOption, using animated colors.
/// @ingroup component
// static
ButtonOption ButtonOption::Animated(Color color) {
return ButtonOption::Animated(
Color::Interpolate(0.85F, color, Color::Black), // NOLINT
Color::Interpolate(0.10F, color, Color::White), // NOLINT
Color::Interpolate(0.10F, color, Color::Black), // NOLINT
Color::Interpolate(0.85F, color, Color::White)); // NOLINT
}
/// @brief Create a ButtonOption, using animated colors.
/// @ingroup component
// static
ButtonOption ButtonOption::Animated(Color background, Color foreground) {
// NOLINTBEGIN
return ButtonOption::Animated(
/*bakground=*/background,
/*foreground=*/foreground,
/*background_active=*/foreground,
/*foreground_active=*/background);
// NOLINTEND
}
/// @brief Create a ButtonOption, using animated colors.
/// @ingroup component
// static
ButtonOption ButtonOption::Animated(Color background,
Color foreground,
Color background_active,
Color foreground_active) {
ButtonOption option;
option.transform = [](const EntryState& s) {
auto element = text(s.label) | borderEmpty;
if (s.focused) {
element |= bold;
}
return element;
};
option.animated_colors.foreground.Set(foreground, foreground_active);
option.animated_colors.background.Set(background, background_active);
return option;
}
/// @brief Option for standard Checkbox.
/// @ingroup component
// static
CheckboxOption CheckboxOption::Simple() {
auto option = CheckboxOption();
option.transform = [](const EntryState& s) {
#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
// Microsoft terminal do not use fonts able to render properly the default
// radiobox glyph.
auto prefix = text(s.state ? "[X] " : "[ ] "); // NOLINT
#else
auto prefix = text(s.state ? "▣ " : "☐ "); // NOLINT
#endif
auto t = text(s.label);
if (s.active) {
t |= bold;
}
if (s.focused) {
t |= inverted;
}
return hbox({prefix, t});
};
return option;
}
/// @brief Option for standard Radiobox
/// @ingroup component
// static
RadioboxOption RadioboxOption::Simple() {
auto option = RadioboxOption();
option.transform = [](const EntryState& s) {
#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
// Microsoft terminal do not use fonts able to render properly the default
// radiobox glyph.
auto prefix = text(s.state ? "(*) " : "( ) "); // NOLINT
#else
auto prefix = text(s.state ? "◉ " : "○ "); // NOLINT
#endif
auto t = text(s.label);
if (s.active) {
t |= bold;
}
if (s.focused) {
t |= inverted;
}
return hbox({prefix, t});
};
return option;
}
/// @brief Standard options for the input component.
/// @ingroup component
// static
InputOption InputOption::Default() {
InputOption option;
option.transform = [](InputState state) {
state.element |= color(Color::White);
if (state.is_placeholder) {
state.element |= dim;
}
if (state.focused) {
state.element |= inverted;
} else if (state.hovered) {
state.element |= bgcolor(Color::GrayDark);
}
return state.element;
};
return option;
}
/// @brief Standard options for a more beautiful input component.
/// @ingroup component
// static
InputOption InputOption::Spacious() {
InputOption option;
option.transform = [](InputState state) {
state.element |= borderEmpty;
state.element |= color(Color::White);
if (state.is_placeholder) {
state.element |= dim;
}
if (state.focused) {
state.element |= bgcolor(Color::Black);
}
if (state.hovered) {
state.element |= bgcolor(Color::GrayDark);
}
return state.element;
};
return option;
}
} // namespace ftxui
animation.hpp
ftxui::Color
A class representing terminal colors.
Definition color.hpp:20
ftxui::Color::White
@ White
Definition color.hpp:67
ftxui::Color::Black
@ Black
Definition color.hpp:52
ftxui::Color::GrayDark
@ GrayDark
Definition color.hpp:60
ftxui::Color::GrayLight
@ GrayLight
Definition color.hpp:59
ftxui::Color::Interpolate
static Color Interpolate(float t, const Color &a, const Color &b)
Definition color.cpp:212
color.hpp
component_options.hpp
direction.hpp
elements.hpp
ftxui::animation::Duration
std::chrono::duration< float > Duration
Definition animation.hpp:20
ftxui
Definition animation.hpp:10
ftxui::bgcolor
Decorator bgcolor(Color)
Decorate using a background color.
Definition color.cpp:124
ftxui::Direction::Right
@ Right
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::Make
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
ftxui::bold
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:33
ftxui::hbox
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
ftxui::MenuEntryOption::transform
std::function< Element(const EntryState &state)> transform
Definition component_options.hpp:79
ftxui::inverted
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
Definition inverted.cpp:34
ftxui::text
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
ftxui::borderLight
Element borderLight(Element)
Draw a light border around the element.
Definition border.cpp:335
ftxui::dim
Element dim(Element)
Use a light font, for elements with less emphasis.
Definition dim.cpp:33
ftxui::automerge
Element automerge(Element child)
Enable character to be automatically merged with others nearby.
Definition automerge.cpp:17
ftxui::border
Element border(Element)
Draw a border around the element.
Definition border.cpp:227
ftxui::borderEmpty
Element borderEmpty(Element)
Draw an empty border around the element.
Definition border.cpp:475
ftxui::color
Decorator color(Color)
Decorate using a foreground color.
Definition color.cpp:110
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::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::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::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::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::Simple
static CheckboxOption Simple()
Option for standard Checkbox.
Definition component_options.cpp:262
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::transform
std::function< Element(InputState)> transform
Definition component_options.hpp:177
ftxui::RadioboxOption
Option for the Radiobox component.
Definition component_options.hpp:193
ftxui::RadioboxOption::Simple
static RadioboxOption Simple()
Option for standard Radiobox.
Definition component_options.cpp:287
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::SetAnimationFunction
void SetAnimationFunction(animation::easing::Function f)
Set how the underline should animate.
Definition component_options.cpp:53
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