Skip to content
You are viewing documentation for v2.0.0. Switch to latest (main).

Module ftxui/component ​

Classes & Structs ​

ClassDescription
ftxui::ButtonOptionOption for the Button component.
ftxui::CheckboxOptionOption for the Checkbox component.
ftxui::ComponentBaseIt implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ftxui::Event.
ftxui::EventRepresent an event. It can be key press event, a terminal resize, or more ...
ftxui::InputOptionOption for the Input component.
ftxui::MenuEntryOptionOption for the MenuEntry component.
ftxui::MenuOptionOption for the Menu component.
ftxui::MouseA mouse event. It contains the coordinate of the mouse, the button pressed and the modifier (shift, ctrl, meta).
ftxui::RadioboxOptionOption for the Radiobox component.
ftxui::ScreenInteractive
ftxui::ToggleOptionOption for the Toggle component.

Type Aliases ​

CapturedMouse ​

cpp
using CapturedMouse = std::unique_ptr< CapturedMouseInterface >;

Component ​

cpp
using Component = std::shared_ptr< ComponentBase >;

Examples: component/checkbox, component/collapsible, component/gallery, component/homescreen, component/input, component/maybe, component/menu2, component/menu_multiple, component/toggle

Components ​

cpp
using Components = std::vector< Component >;

Functions ​

SymbolDescription
ButtonDraw a button. Execute a function when clicked.
CheckboxDraw checkable element.
ActiveReturns if the element if the currently active child of its parent.
ActiveChildReturn the currently Active child.
AddAdd a child. @param child The child to be attached.
CaptureMouseTake the CapturedMouse if available. There is only one component of them. It represents a component taking priority over others.
ChildAtAccess the child at index i.
ChildCountReturns the number of children.
DetachDetach this child from its parent.
DetachAllChildrenRemove all children.
FocusableReturn true when the component contains focusable elements. The non focusable Components will be skipped when navigating using the keyboard.
FocusedReturns if the elements if focused by the user. True when the ComponentBase is focused by the user. An element is Focused when it is with all its ancestors the ActiveChild() of their parents.
OnEventCalled in response to an event.
ParentReturn the parent ComponentBase, or nul if any.
RenderDraw the component. Build a ftxui::Element to be drawn on the ftxi::Screen representing this ftxui::ComponentBase.
SetActiveChildMake the child to be the "active" one.
TakeFocusConfigure all the ancestors to give focus to this component.
HorizontalA list of components, drawn one by one horizontally and navigated horizontally using left/right arrow key or 'h'/'l' keys.
TabA list of components, where only one is drawn and interacted with at a time. The selector gives the index of the selected component. This is useful to implement tabs.
VerticalA list of components, drawn one by one vertically and navigated vertically using up/down arrow key or 'j'/'k' keys.
InputAn input box for editing text.
MenuA list of text. The focused element is selected.
RadioboxA list of element, where only one can be selected.
RendererReturn a component, using render to render its interface.
ResizableSplitBottomAn vertical split in between two components, configurable using the mouse.
ResizableSplitLeftAn horizontal split in between two components, configurable using the mouse.
ResizableSplitRightAn horizontal split in between two components, configurable using the mouse.
ResizableSplitTopAn vertical split in between two components, configurable using the mouse.
SliderAn horizontal slider.
ToggleAn horizontal list of elements. The user can navigate through them.

Button ​

cpp
Component Button(ConstStringRef label, std::function< void()> on_click, Ref< ButtonOption > = {});

Draw a button. Execute a function when clicked.

Parameters:

  • label: The label of the button.
  • on_click: The action to execute when clicked.
  • option: Additional optional parameters.

See also: ButtonBase

Example

cpp
auto screen = ScreenInteractive::FitComponent();
std::string label = "Click to quit";
Component button = Button(&label, screen.ExitLoopClosure());
screen.Loop(button)

Output

text
┌─────────────┐
│Click to quit│
└─────────────┘

Examples: component/button, component/composition, component/gallery, component/modal_dialog, component/nested_screen, component/renderer, component/with_restored_io

Checkbox ​

cpp
Component Checkbox(ConstStringRef label, bool * checked, Ref< CheckboxOption > option = {});

Draw checkable element.

Parameters:

  • label: The label of the checkbox.
  • checked: Whether the checkbox is checked or not.
  • option: Additional optional parameters.

See also: CheckboxBase

Example

cpp
auto screen = ScreenInteractive::FitComponent();
std::string label = "Make a sandwidth";
bool checked = false;
Component checkbox = Checkbox(&label, &checked);
screen.Loop(checkbox)

Output

text
☐ Make a sandwitch

Examples: component/checkbox_in_frame, component/flexbox_gallery, component/gallery, component/homescreen, component/maybe

Active ​

cpp
bool Active() const;

Returns if the element if the currently active child of its parent.

ActiveChild ​

cpp
virtual Component ActiveChild();

Return the currently Active child.

Returns: the currently Active child.

Add ​

cpp
void Add(Component children);

Add a child. @param child The child to be attached.

Examples: component/checkbox, component/checkbox_in_frame

CaptureMouse ​

cpp
CapturedMouse CaptureMouse(const Event & event);

Take the CapturedMouse if available. There is only one component of them. It represents a component taking priority over others.

Parameters:

  • event:

ChildAt ​

cpp
Component & ChildAt(size_t i);

Access the child at index i.

ChildCount ​

cpp
size_t ChildCount() const;

Returns the number of children.

Detach ​

cpp
void Detach();

Detach this child from its parent.

See also: Detach, Parent

DetachAllChildren ​

Remove all children.

Focusable ​

cpp
virtual bool Focusable() const;

Return true when the component contains focusable elements. The non focusable Components will be skipped when navigating using the keyboard.

Focused ​

cpp
bool Focused() const;

Returns if the elements if focused by the user. True when the ComponentBase is focused by the user. An element is Focused when it is with all its ancestors the ActiveChild() of their parents.

Examples: component/renderer

OnEvent ​

cpp
virtual bool OnEvent(Event);

Called in response to an event.

Parameters:

  • event: The event.

Returns: True when the event has been handled. The default implementation called OnEvent on every child until one return true. If none returns true, return false.

Parent ​

cpp
ComponentBase * Parent() const;

Return the parent ComponentBase, or nul if any.

See also: Detach, Parent

Render ​

cpp
virtual Element Render();

Draw the component. Build a ftxui::Element to be drawn on the ftxi::Screen representing this ftxui::ComponentBase.

Examples: component/button, component/canvas_animated, component/checkbox, component/checkbox_in_frame, component/collapsible, component/composition, component/flexbox_gallery, component/focus, component/gallery, component/homescreen, component/input, component/maybe (+45 more)

SetActiveChild ​

cpp
virtual void SetActiveChild(ComponentBase * child);

Make the child to be the "active" one.

Parameters:

  • child: the child to become active.

SetActiveChild ​

cpp
void SetActiveChild(Component child);

Make the child to be the "active" one.

Parameters:

  • child: the child to become active.

TakeFocus ​

cpp
void TakeFocus();

Configure all the ancestors to give focus to this component.

Horizontal ​

A list of components, drawn one by one horizontally and navigated horizontally using left/right arrow key or 'h'/'l' keys.

Parameters:

  • children: the list of components.

See also: ContainerBase

Example

cpp
int selected_children = 2;
auto container = Container::Horizontal({
  children_1,
  children_2,
  children_3,
  children_4,
}, &selected_children);

Examples: component/button, component/canvas_animated, component/composition, component/dropdown, component/flexbox_gallery, component/homescreen, component/menu2, component/menu_multiple, component/menu_style, component/modal_dialog, component/tab_vertical, component/with_restored_io

Horizontal ​

cpp
Component Horizontal(Components children, int * selector);

A list of components, drawn one by one horizontally and navigated horizontally using left/right arrow key or 'h'/'l' keys.

Parameters:

  • children: the list of components.
  • selector: A reference to the index of the selected children.

See also: ContainerBase

Example

cpp
int selected_children = 2;
auto container = Container::Horizontal({
  children_1,
  children_2,
  children_3,
  children_4,
}, selected_children);

Examples: component/button, component/canvas_animated, component/composition, component/dropdown, component/flexbox_gallery, component/homescreen, component/menu2, component/menu_multiple, component/menu_style, component/modal_dialog, component/tab_vertical, component/with_restored_io

Tab ​

cpp
Component Tab(Components children, int * selector);

A list of components, where only one is drawn and interacted with at a time. The selector gives the index of the selected component. This is useful to implement tabs.

Parameters:

  • children: The list of components.
  • selector: The index of the drawn children.

See also: ContainerBase

Example

cpp
int tab_drawn = 0;
auto container = Container::Tab({
  children_1,
  children_2,
  children_3,
  children_4,
}, &tab_drawn);

Examples: component/canvas_animated, component/homescreen, component/modal_dialog, component/tab_horizontal, component/tab_vertical

Vertical ​

A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or 'j'/'k' keys.

Parameters:

  • children: the list of components.

See also: ContainerBase

Example

cpp
auto container = Container::Vertical({
  children_1,
  children_2,
  children_3,
  children_4,
});

Examples: component/checkbox, component/checkbox_in_frame, component/collapsible, component/dropdown, component/flexbox_gallery, component/focus, component/gallery, component/homescreen, component/input, component/maybe, component/menu_entries, component/menu_multiple (+5 more)

Vertical ​

cpp
Component Vertical(Components children, int * selector);

A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or 'j'/'k' keys. This is useful for implementing a Menu for instance.

Parameters:

  • children: the list of components.
  • selector: A reference to the index of the selected children.

See also: ContainerBase

Example

cpp
auto container = Container::Vertical({
  children_1,
  children_2,
  children_3,
  children_4,
});

Examples: component/checkbox, component/checkbox_in_frame, component/collapsible, component/dropdown, component/flexbox_gallery, component/focus, component/gallery, component/homescreen, component/input, component/maybe, component/menu_entries, component/menu_multiple (+5 more)

Input ​

cpp
Component Input(StringRef content, ConstStringRef placeholder, Ref< InputOption > option = {});

An input box for editing text.

Parameters:

  • content: The editable content.
  • placeholder: The text displayed when content is still empty.
  • option: Additional optional parameters.

See also: InputBase

Example

cpp
auto screen = ScreenInteractive::FitComponent();
std::string content= "";
std::string placeholder = "placeholder";
Component input = Input(&content, &placeholder);
screen.Loop(input);

Output

text
placeholder

Examples: component/checkbox, component/gallery, component/homescreen, component/input

cpp
Component Menu(ConstStringListRef entries, int * selected_, Ref< MenuOption > = {});

A list of text. The focused element is selected.

Parameters:

  • entries: The list of entries in the menu.
  • selected: The index of the currently selected element.
  • option: Additional optional parameters.

See also: MenuBase

Example

cpp
auto screen = ScreenInteractive::TerminalOutput();
std::vector<std::string> entries = {
    "entry 1",
    "entry 2",
    "entry 3",
};
int selected = 0;
auto menu = Menu(&entries, &selected);
screen.Loop(menu);

Output

text
> entry 1
  entry 2
  entry 3

Examples: component/canvas_animated, component/gallery, component/homescreen, component/menu, component/menu2, component/menu_in_frame, component/menu_multiple, component/menu_style, component/tab_vertical

Radiobox ​

cpp
Component Radiobox(ConstStringListRef entries, int * selected_, Ref< RadioboxOption > option = {});

A list of element, where only one can be selected.

Parameters:

  • entries: The list of entries in the list.
  • selected: The index of the currently selected element.
  • option: Additional optional parameters.

See also: RadioboxBase

Example

cpp
auto screen = ScreenInteractive::TerminalOutput();
std::vector<std::string> entries = {
    "entry 1",
    "entry 2",
    "entry 3",
};
int selected = 0;
auto menu = Radiobox(&entries, &selected);
screen.Loop(menu);

Output

text
◉ entry 1
○ entry 2
○ entry 3

Examples: component/flexbox_gallery, component/gallery, component/homescreen, component/maybe, component/radiobox, component/radiobox_in_frame, component/tab_horizontal, component/tab_vertical

Renderer ​

cpp
Component Renderer(std::function< Element()>);

Return a component, using render to render its interface.

Parameters:

  • render: The function drawing the interface.

Example

cpp
auto screen = ScreenInteractive::TerminalOutput();
auto renderer = Renderer([] {
  return text("My interface");
});
screen.Loop(renderer);

Examples: component/button, component/canvas_animated, component/checkbox, component/checkbox_in_frame, component/collapsible, component/composition, component/flexbox_gallery, component/focus, component/gallery, component/homescreen, component/input, component/maybe (+16 more)

Renderer ​

cpp
Component Renderer(Component child, std::function< Element()>);

Return a new Component, similar to child, but using render as the Component::Render() event.

Parameters:

  • child: The component to forward events to.
  • render: The function drawing the interface.

Example

cpp
auto screen = ScreenInteractive::TerminalOutput();
std::string label = "Click to quit";
auto button = Button(&label, screen.ExitLoopClosure());
auto renderer = Renderer(button, [&] {
  return hbox({
    text("A button:"),
    button->Render(),
  });
});
screen.Loop(renderer);

Examples: component/button, component/canvas_animated, component/checkbox, component/checkbox_in_frame, component/collapsible, component/composition, component/flexbox_gallery, component/focus, component/gallery, component/homescreen, component/input, component/maybe (+16 more)

Renderer ​

cpp
Component Renderer(std::function< Element(bool)>);

Return a focusable component, using render to render its interface.

Parameters:

  • render: The function drawing the interface, taking a boolean telling whether the component is focused or not.

Example

cpp
auto screen = ScreenInteractive::TerminalOutput();
auto renderer = Renderer([] (bool focused) {
  if (focused)
    return text("My interface") | inverted;
  else
    return text("My interface");
});
screen.Loop(renderer);

Examples: component/button, component/canvas_animated, component/checkbox, component/checkbox_in_frame, component/collapsible, component/composition, component/flexbox_gallery, component/focus, component/gallery, component/homescreen, component/input, component/maybe (+16 more)

ResizableSplitBottom ​

cpp
Component ResizableSplitBottom(Component main, Component back, int * main_size);

An vertical split in between two components, configurable using the mouse.

Parameters:

  • main: The main component of size main_size, on the bottom.
  • back: The back component taking the remaining size, on the top.
  • main_size: The size of the main component.

Example

cpp
auto screen = ScreenInteractive::Fullscreen();
int bottom_size = 1;
auto top = Renderer([] { return text("Top") | center;});
auto bottom = Renderer([] { return text("Bottom") | center;});
auto split = ResizableSplit::Bottom(bottom, top, &bottom_size);
screen.Loop(split);

Output

text
   top
────────────
   bottom

Examples: component/flexbox_gallery, component/resizable_split

ResizableSplitLeft ​

cpp
Component ResizableSplitLeft(Component main, Component back, int * main_size);

An horizontal split in between two components, configurable using the mouse.

Parameters:

  • main: The main component of size main_size, on the left.
  • back: The back component taking the remaining size, on the right.
  • main_size: The size of the main component.

Example

cpp
auto screen = ScreenInteractive::Fullscreen();
int left_size = 10;
auto left = Renderer([] { return text("Left") | center;});
auto right = Renderer([] { return text("right") | center;});
auto split = ResizableSplitLeft(left, right, &left_size);
screen.Loop(split);

Output

text
       │
left   │   right
       │

Examples: component/homescreen, component/resizable_split

ResizableSplitRight ​

cpp
Component ResizableSplitRight(Component main, Component back, int * main_size);

An horizontal split in between two components, configurable using the mouse.

Parameters:

  • main: The main component of size main_size, on the right.
  • back: The back component taking the remaining size, on the left.
  • main_size: The size of the main component.

Example

cpp
auto screen = ScreenInteractive::Fullscreen();
int right_size = 10;
auto left = Renderer([] { return text("Left") | center;});
auto right = Renderer([] { return text("right") | center;});
auto split = ResizableSplitRight(right, left, &right_size);
screen.Loop(split);

Output

text
       │
left   │   right
       │

Examples: component/flexbox_gallery, component/resizable_split

ResizableSplitTop ​

cpp
Component ResizableSplitTop(Component main, Component back, int * main_size);

An vertical split in between two components, configurable using the mouse.

Parameters:

  • main: The main component of size main_size, on the top.
  • back: The back component taking the remaining size, on the bottom.
  • main_size: The size of the main component.

Example

cpp
auto screen = ScreenInteractive::Fullscreen();
int top_size = 1;
auto top = Renderer([] { return text("Top") | center;});
auto bottom = Renderer([] { return text("Bottom") | center;});
auto split = ResizableSplitTop(top, bottom, &top_size);
screen.Loop(split);

Output

text
   top
────────────
   bottom

Examples: component/resizable_split

Slider ​

cpp
Component Slider(ConstStringRef label, T * value, T min, T max, T increment);

An horizontal slider.

Parameters:

  • label: The name of the slider.
  • value: The current value of the slider.
  • min: The minimum value.
  • max: The maximum value.
  • increment: The increment when used by the cursor.

Example

cpp
auto screen = ScreenInteractive::TerminalOutput();
int value = 50;
auto slider = Slider("Value:", &value, 0, 100, 1);
screen.Loop(slider);

Output

text
Value:[██████████████████████████                          ]

Examples: component/focus, component/gallery, component/slider, component/slider_rgb

Toggle ​

cpp
Component Toggle(ConstStringListRef entries, int * selected, Ref< ToggleOption > option = {});

An horizontal list of elements. The user can navigate through them.

Parameters:

  • entries: The list of selectable entries to display.
  • selected: Reference the selected entry.
  • option: Additional optional parameters.

Examples: component/gallery, component/homescreen, component/tab_horizontal, component/toggle