
ftxui::component
模块定义了生成交互式组件的逻辑,这些组件响应用户事件(键盘、鼠标等)。
Example 部分提供了一系列示例。
ftxui::ScreenInteractive
定义了一个渲染组件的主循环。
ftxui::Component
是 ftxui::ComponentBase
的共享指针。后者定义了:
ftxui::Element
用于渲染单个帧。
ftxui::Component
用于渲染动态用户界面,生成多个帧,并在事件发生时更新其状态。
多个组件的 画廊。(演示)

所有预定义组件都可以在 "ftxui/dom/component.hpp" 中找到。
#ifndef FTXUI_COMPONENT_HPP
#define FTXUI_COMPONENT_HPP
#include <functional>
#include <memory>
#include <utility>
struct ButtonOption;
struct CheckboxOption;
struct Event;
struct InputOption;
struct MenuOption;
struct RadioboxOption;
struct MenuEntryOption;
template <class T, class... Args>
std::shared_ptr<T> Make(Args&&... args) {
return std::make_shared<T>(std::forward<Args>(args)...);
}
namespace Container {
}
std::function<void()> on_click,
bool* checked,
StringRef placeholder,
InputOption options = {});
int* selected_,
int* selected_,
RadioboxOption options = {});
template <typename T>
Ref<int> value,
ConstRef<int> min = 0,
ConstRef<int> max = 100,
ConstRef<int> increment = 5);
Ref<float> value,
ConstRef<float> min = 0.f,
ConstRef<float> max = 100.f,
ConstRef<float> increment = 5.f);
Ref<long> value,
ConstRef<long> min = 0L,
ConstRef<long> max = 100L,
ConstRef<long> increment = 5L);
Ref<bool> show = false);
std::function<void()> on_enter,
std::function<void()> on_leave);
std::function<void(bool)> on_change);
std::function<void()> on_leave);
}
#endif
static CheckboxOption Simple()
Option for standard Checkbox.
static ButtonOption Simple()
Create a ButtonOption, inverted when focused.
static MenuOption Vertical()
Standard options for a vertical menu. This can be useful to implement a list of selectable items.
Component Maybe(Component, const bool *show)
Decorate a component |child|. It is shown only when |show| is true.
Component ResizableSplitTop(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Component Menu(MenuOption options)
A list of text. The focused element is selected.
Component MenuEntry(MenuEntryOption options)
A specific menu entry. They can be put into a Container::Vertical to form a menu.
Component Toggle(ConstStringListRef entries, int *selected)
An horizontal list of elements. The user can navigate through them.
Component Radiobox(RadioboxOption options)
A list of element, where only one can be selected.
Component Button(ButtonOption options)
Draw a button. Execute a function when clicked.
Component Modal(Component main, Component modal, const bool *show_modal)
Component Renderer(Component child, std::function< Element()>)
Return a new Component, similar to |child|, but using |render| as the Component::Render() event.
Component Hoverable(Component component, bool *hover)
Wrap a component. Gives the ability to know if it is hovered by the mouse.
Component Window(WindowOptions option)
A draggeable / resizeable window. To use multiple of them, they must be stacked using Container::Stac...
Component Input(InputOption options={})
An input box for editing text.
Component ResizableSplitRight(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
Component Dropdown(ConstStringListRef entries, int *selected)
A dropdown menu.
Component ResizableSplitBottom(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Component Checkbox(CheckboxOption options)
Draw checkable element.
Component ResizableSplitLeft(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
The FTXUI ftxui:: namespace.
std::shared_ptr< Node > Element
std::function< Element(Element)> ElementDecorator
std::shared_ptr< ComponentBase > Component
std::vector< Component > Components
Component ResizableSplit(ResizableSplitOption options)
A split in between two components.
Component Collapsible(ConstStringRef label, Component child, Ref< bool > show=false)
A collapsible component. It displays a checkbox with an arrow. Once activated, the child is displayed...
Component Slider(SliderOption< T > options)
A slider in any direction.
std::function< Component(Component)> ComponentDecorator
Component CatchEvent(Component child, std::function< bool(Event)>)
Input
示例:

由 "ftxui/component/component.hpp" 中的 ftxui::Input()
生成。
过滤输入
可以使用 ftxui::CatchEvent
过滤输入组件接收到的字符。
std::string phone_number;
Component input = Input(&phone_number, "phone number");
input |= CatchEvent([&](Event event) {
return event.is_character() && !std::isdigit(event.character()[0]);
});
input |= CatchEvent([&](Event event) {
return event.is_character() && phone_number.size() >= 10;
});
Menu
定义一个菜单对象。它包含一个条目列表,其中一个被选中。
示例:

由 "ftxui/component/component.hpp" 中的 ftxui::Menu()
生成。
Toggle
一种特殊的菜单。条目水平显示。
示例:

由 "ftxui/component/component.hpp" 中的 ftxui::Toggle()
生成。
CheckBox
此组件定义一个复选框。它是一个可以打开/关闭的单个条目。
示例:

由 "ftxui/component/component.hpp" 中的 ftxui::Checkbox()
生成。
RadioBox
一个单选按钮组件。这是一个条目列表,其中一个可以被打开。
示例:

由 "ftxui/component/component.hpp" 中的 ftxui::Radiobox()
生成。
Dropdown
下拉菜单是一个组件,当打开时,它会显示一个元素列表供用户选择。
示例:

由 "ftxui/component/component.hpp" 中的 ftxui::Dropdown()
生成。
Slider
表示一个滑块对象,它由一个带有分箱中间间隔的范围组成。它可以通过 ftxui::Slider()
创建。
示例:

由 "ftxui/component/component.hpp" 中的 ftxui::Slider()
生成。
Renderer
由 ftxui/component/component.hpp 中的 ftxui::Renderer()
生成。此组件通过使用不同的函数来渲染界面来装饰另一个组件。
示例:
auto inner = [...]
auto renderer = Renderer(inner, [&] {
return inner->Render() | border
});
ftxui::Renderer
也支持组件装饰器模式:
auto component = [...]
component = component
| Renderer([](Element e) { return e | border))
| Renderer(bold)
作为简写,您还可以将组件与元素装饰器组合:
auto component = [...]
component = component | border | bold;
CatchEvent
由 ftxui/component/component.hpp 中的 ftxui::CatchEvent()
生成。此组件装饰其他组件,在底层组件之前捕获事件。
示例:
auto screen = ScreenInteractive::TerminalOutput();
auto renderer = Renderer([] {
return text("My interface");
});
auto component = CatchEvent(renderer, [&](Event event) {
if (event == Event::Character(\'q\')) {
screen.ExitLoopClosure()();
return true;
}
return false;
});
screen.Loop(component);
ftxui::CatchEvent
也可以用作装饰器:
component = component
| CatchEvent(handler_1)
| CatchEvent(handler_2)
| CatchEvent(handler_3)
;
Collapsible
对于用户可以切换可见性的视觉元素很有用。本质上,这是 ftxui::Checkbox()
和 ftxui::Maybe()
组件的组合。
auto collapsible = Collapsible("Show more", inner_element);
Maybe
由 ftxui/component/component.hpp 中的 ftxui::Maybe()
生成。此组件可以通过布尔值或谓词来显示/隐藏任何其他组件。
布尔值示例:
bool show = true;
auto component = Renderer([]{ return "Hello World!"; });
auto maybe_component = Maybe(component, &show)
谓词示例:
auto component = Renderer([]{ return "Hello World!"; });
auto maybe_component = Maybe(component, [&] { return time > 10; })
像往常一样,ftxui::Maybe
也可以用作装饰器:
component = component
| Maybe(&a_boolean)
| Maybe([&] { return time > 10; })
;
Container
Horizontal
由 "ftxui/component/component.hpp" 中的 ftxui::Container::Horizontal()
生成。它水平显示组件列表并处理键盘/鼠标导航。
Vertical
由 "ftxui/component/component.hpp" 中的 ftxui::Container::Vertical()
生成。它垂直显示组件列表并处理键盘/鼠标导航。
Tab
由 "ftxui/component/component.hpp" 中的 ftxui::Container::Tab()
生成。它接受一个组件列表并只显示其中一个。这对于实现选项卡栏很有用。
垂直:

水平:

ResizableSplit
它定义了两个子组件之间的水平或垂直分隔。分隔的位置是可变的,并且可以使用鼠标控制。 有四种可能的分隔:
ftxui::ResizableSplitLeft()
ftxui::ResizableSplitRight()
ftxui::ResizableSplitTop()
ftxui::ResizableSplitBottom()
来自 "ftxui/component/component.hpp"
示例:
强制重绘帧。
通常,ftxui::ScreenInteractive::Loop()
负责在处理完新的一组事件(例如键盘、鼠标、窗口大小调整等)时绘制新帧。但是,您可能希望对 FTXUI 未知的任意事件做出反应。为此,您必须通过线程使用 ftxui::ScreenInteractive::PostEvent
(**这是线程安全的**)发布事件。您将不得不发布事件 ftxui::Event::Custom
。
示例:
screen->PostEvent(Event::Custom);
如果您不需要处理新事件,可以使用:
screen->RequestAnimationFrame();
代替。