
ftxui::component 模組定義了生成互動元件的邏輯,這些元件響應使用者事件(鍵盤、滑鼠等)。
Example 章節提供了一系列範例。
ftxui::ScreenInteractive 定義了一個主循環,用於渲染元件。
ftxui::Component 是 ftxui::ComponentBase 的共享指針。後者定義了:
ftxui::Element 用於渲染單個畫面。
ftxui::Component 用於渲染動態使用者介面,生成多個畫面,並在事件發生時更新其狀態。
畫廊 多個元件的集合。 (demo)

所有預定義的元件都可以在 "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)...);
}
}
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()
標準Checkbox的選項。
static ButtonOption Simple()
創建一個 ButtonOption,在聚焦時反轉。
static MenuOption Vertical()
垂直選單的標準選項。 這對於實現一個可選項目列表很有用。
Component Horizontal(Components children)
一個元件列表,水平地一個接一個繪製,並使用左/右箭頭鍵或 'h'/'l' 鍵進行水平導航。
Component Maybe(Component, const bool *show)
裝飾一個組件 |child|。它只在 |show| 為 true 時顯示。
Component ResizableSplitTop(Component main, Component back, int *main_size)
兩個元件之間的垂直分割,可透過滑鼠設定。
Component Checkbox(CheckboxOption options)
Component Menu(MenuOption options)
文字列表。選定的元素會被聚焦。
Component MenuEntry(MenuEntryOption options)
一個特定的菜單條目。它們可以放入 Container::Vertical 中以形成菜單。
Component Toggle(ConstStringListRef entries, int *selected)
元素的水平列表。使用者可以在其中導航。
Component Radiobox(RadioboxOption options)
元素清單,只能選擇一個。
Component Button(ButtonOption options)
繪製一個按鈕。點擊時執行一個函數。
Component Modal(Component main, Component modal, const bool *show_modal)
Component Renderer(Component child, std::function< Element()>)
回傳一個新的元件,類似於 |child|,但使用 |render| 作為 Component::Render() 事件。
Component Hoverable(Component component, bool *hover)
包裝一個元件。提供能力以判斷滑鼠是否懸停在其上方。
Component Window(WindowOptions option)
一個可拖曳/可調整大小的視窗。要使用多個視窗,它們必須透過 Container::Stacked({...}) 元件堆疊。
Component Vertical(Components children)
一個元件列表,垂直地一個接一個繪製,並使用上/下箭頭鍵或 'j'/'k' 鍵進行垂直導航。
Component Input(InputOption options={})
用於編輯文字的輸入框。
Component ResizableSplitRight(Component main, Component back, int *main_size)
兩個元件之間的水平分割,可透過滑鼠設定。
Component Dropdown(ConstStringListRef entries, int *selected)
下拉式選單。
Component Stacked(Components children)
一個元件列表,將彼此堆疊。 事件會傳播到第一個元件,如果未處理則傳播到第二個,依此類推。 元件以給定的相反順序繪製。 當一個元件獲得焦點時,它會被放到最前面,而不改變其他元素的相對順序。
Component ResizableSplitBottom(Component main, Component back, int *main_size)
兩個元件之間的垂直分割,可透過滑鼠設定。
Component ResizableSplitLeft(Component main, Component back, int *main_size)
兩個元件之間的水平分割,可透過滑鼠設定。
Component Tab(Components children, int *selector)
一個元件列表,一次只繪製一個並與之互動。|selector| 給出所選元件的索引。這對於實作分頁很有用。
FTXUI ftxui::Container:: 命名空間
std::shared_ptr< T > Make(Args &&... args)
std::shared_ptr< Node > Element
std::function< Element(Element)> ElementDecorator
std::vector< Component > Components
Component ResizableSplit(ResizableSplitOption options)
兩個元件之間的分隔。
Component operator|(Component component, ComponentDecorator decorator)
Component Collapsible(ConstStringRef label, Component child, Ref< bool > show=false)
可折疊元件。它顯示一個帶有箭頭的核取方塊。一旦啟用,子元件就會顯示。
Component Slider(SliderOption< T > options)
Component & operator|=(Component &component, ComponentDecorator decorator)
std::function< Component(Component)> ComponentDecorator
std::shared_ptr< ComponentBase > Component
Component CatchEvent(Component child, std::function< bool(Event)>)
Input
範例:

由 ftxui::Input() 從 "ftxui/component/component.hpp" 生成。
Filtered 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::Menu() 從 "ftxui/component/component.hpp" 生成。
Toggle
一種特殊的選單。條目水平顯示。
範例:

由 ftxui::Toggle() 從 "ftxui/component/component.hpp" 生成。
CheckBox
此元件定義了一個核取方塊。它是一個單一條目,可以開啟/關閉。
範例:

由 ftxui::Checkbox() 從 "ftxui/component/component.hpp" 生成。
RadioBox
一個單選按鈕元件。這是一個條目列表,其中一個可以開啟。
範例:

由 ftxui::Radiobox() 從 "ftxui/component/component.hpp" 生成。
Dropdown
下拉式選單是一個元件,當開啟時,會顯示一個元素列表供使用者選擇。
範例:

由 ftxui::Dropdown() 從 "ftxui/component/component.hpp" 生成。
Slider
表示一個滑塊物件,它由一個帶有分箱中間間隔的範圍組成。它可以使用 ftxui::Slider() 創建。
範例:

由 ftxui::Slider() 從 "ftxui/component/component.hpp" 生成。
Renderer
由 ftxui::Renderer() 從 ftxui/component/component.hpp 生成。此元件通過使用不同的函數來渲染介面,從而裝飾另一個元件。
範例:
auto inner = [...]
auto renderer = Renderer(inner, [&] {
return inner->Render() | border
});
ftxui::Renderer 也支援元件裝飾器模式:
| Renderer([](Element e) { return e | border))
| Renderer(bold)
作為簡寫,您還可以將元件與元素裝飾器組合:
CatchEvent
由 ftxui::CatchEvent() 從 ftxui/component/component.hpp 生成。此元件裝飾其他元件,在底層元件之前捕獲事件。
範例:
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;
});
ftxui::CatchEvent 也可以用作裝飾器:
| CatchEvent(handler_1)
| CatchEvent(handler_2)
| CatchEvent(handler_3)
;
Collapsible
對於使用者可以開啟或關閉其可見性的視覺元素很有用。本質上,這是 ftxui::Checkbox() 和 ftxui::Maybe() 元件的組合。
auto collapsible = Collapsible("Show more", inner_element);
Maybe
由 ftxui::Maybe() 從 ftxui/component/component.hpp 生成。 此元件可用於通過布林值或謂詞顯示/隱藏任何其他元件。
布林值範例:
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 也可以用作裝飾器:
| Maybe(&a_boolean)
| Maybe([&] { return time > 10; })
;
Container
Horizontal
由 ftxui::Container::Horizontal() 從 "ftxui/component/component.hpp" 生成。它水平顯示元件列表並處理鍵盤/滑鼠導航。
Vertical
由 ftxui::Container::Vertical() 從 "ftxui/component/component.hpp" 生成。它垂直顯示元件列表並處理鍵盤/滑鼠導航。
Tab
由 ftxui::Container::Tab() 從 "ftxui/component/component.hpp" 生成。它接受元件列表並僅顯示其中一個。這對於實現分頁欄很有用。
垂直:

水平:

ResizableSplit
它定義了兩個子元件之間的水平或垂直分隔。分隔的位置是可變的,並且可以使用滑鼠控制。 有四種可能的分隔:
範例:
Force a frame redraw.
通常,ftxui::ScreenInteractive::Loop() 負責在處理完新的事件組(例如鍵盤、滑鼠、視窗大小調整等)時繪製新畫面。但是,您可能希望響應 FTXUI 未知的任意事件。為此,您必須通過執行緒使用 ftxui::ScreenInteractive::PostEvent(**這是執行緒安全的**)發布事件。您必須發布事件 ftxui::Event::Custom。
範例:
screen->PostEvent(Event::Custom);
如果您不需要處理新事件,可以使用:
screen->RequestAnimationFrame();
代替。
```