FTXUI/include/ftxui/component/component_options.hpp

174 lines
5.5 KiB
C++
Raw Normal View History

#ifndef FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP
#define FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP
2022-03-14 01:51:46 +08:00
#include <chrono> // for milliseconds
#include <ftxui/component/animation.hpp> // for Duration, QuadraticInOut, Function
#include <ftxui/dom/elements.hpp> // for Decorator, bold, inverted, operator|, Element, nothing
#include <ftxui/util/ref.hpp> // for Ref
#include <functional> // for function
#include <optional> // for optional
#include <string> // for string, allocator
#include "ftxui/screen/color.hpp" // for Color, Color::GrayDark, Color::White
2021-07-08 04:37:50 +08:00
namespace ftxui {
2022-03-14 01:51:46 +08:00
/// @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.
};
2022-03-14 01:51:46 +08:00
struct UnderlineOption {
bool enabled = false;
2021-07-10 18:59:36 +08:00
2022-03-14 01:51:46 +08:00
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 {
2022-03-14 01:51:46 +08:00
std::function<Element(EntryState state)> transform;
AnimatedColorsOption animated_colors;
};
2022-03-14 01:51:46 +08:00
/// @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();
// Style:
UnderlineOption underline;
MenuEntryOption entries;
enum Direction { Up, Down, Left, Right };
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 seelcted entry changes.
std::function<void()> on_enter; ///> Called when the user presses enter.
Ref<int> focused_entry = 0;
};
/// @brief Option for the AnimatedButton component.
2021-07-10 20:23:46 +08:00
/// @ingroup component
2021-07-08 04:23:07 +08:00
struct ButtonOption {
2022-03-14 01:51:46 +08:00
// 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);
// Style:
std::function<Element(EntryState)> transform;
AnimatedColorsOption animated_colors;
2021-07-08 04:23:07 +08:00
};
2021-07-10 17:50:17 +08:00
/// @brief Option for the Checkbox component.
2021-07-10 20:23:46 +08:00
/// @ingroup component
2021-07-08 04:37:50 +08:00
struct CheckboxOption {
2022-03-14 01:51:46 +08:00
// Standard constructors:
static CheckboxOption Simple();
// Style:
std::function<Element(EntryState)> transform;
2021-07-08 04:37:50 +08:00
2022-03-14 01:51:46 +08:00
// Observer:
2021-07-08 04:37:50 +08:00
/// Called when the user change the state.
2022-03-14 01:51:46 +08:00
std::function<void()> on_change = [] {};
2021-07-08 04:37:50 +08:00
};
2021-07-10 17:50:17 +08:00
/// @brief Option for the Input component.
2021-07-10 20:23:46 +08:00
/// @ingroup component
2021-07-08 06:01:42 +08:00
struct InputOption {
2021-07-10 17:50:17 +08:00
/// Called when the content changes.
2021-07-08 06:01:42 +08:00
std::function<void()> on_change = [] {};
2021-07-10 17:50:17 +08:00
/// Called when the user presses enter.
2021-07-08 06:01:42 +08:00
std::function<void()> on_enter = [] {};
2021-07-10 18:29:39 +08:00
/// Obscure the input content using '*'.
Ref<bool> password = false;
/// When set different from -1, this attributes is used to store the cursor
/// position.
Ref<int> cursor_position = -1;
2021-07-08 06:01:42 +08:00
};
2021-07-10 17:50:17 +08:00
/// @brief Option for the Radiobox component.
2021-07-10 20:23:46 +08:00
/// @ingroup component
2021-07-10 16:50:25 +08:00
struct RadioboxOption {
2022-03-14 01:51:46 +08:00
// Standard constructors:
static RadioboxOption Simple();
2021-07-10 16:50:25 +08:00
2022-03-14 01:51:46 +08:00
// Style:
std::function<Element(EntryState)> transform;
2021-07-10 17:03:01 +08:00
2022-03-14 01:51:46 +08:00
// Observers:
2021-07-10 17:50:17 +08:00
/// Called when the selected entry changes.
std::function<void()> on_change = [] {};
2021-07-10 19:15:28 +08:00
Ref<int> focused_entry = 0;
2021-07-10 17:03:01 +08:00
};
2021-07-10 20:23:46 +08:00
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */
2021-07-10 19:20:43 +08:00
// 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.