FTXUI  5.0.0
C++ functional terminal UI.
component.hpp
Go to the documentation of this file.
1 // Copyright 2021 Arthur Sonzogni. All rights reserved.
2 // Use of this source code is governed by the MIT license that can be found in
3 // the LICENSE file.
4 #ifndef FTXUI_COMPONENT_HPP
5 #define FTXUI_COMPONENT_HPP
6 
7 #include <functional> // for function
8 #include <memory> // for make_shared, shared_ptr
9 #include <string> // for wstring
10 #include <utility> // for forward
11 #include <vector> // for vector
12 
13 #include "ftxui/component/component_base.hpp" // for Component, Components
14 #include "ftxui/component/component_options.hpp" // for ButtonOption, CheckboxOption, MenuOption
15 #include "ftxui/dom/elements.hpp" // for Element
16 #include "ftxui/util/ref.hpp" // for ConstRef, Ref, ConstStringRef, ConstStringListRef, StringRef
17 
18 namespace ftxui {
19 struct ButtonOption;
20 struct CheckboxOption;
21 struct Event;
22 struct InputOption;
23 struct MenuOption;
24 struct RadioboxOption;
25 struct MenuEntryOption;
26 
27 template <class T, class... Args>
28 std::shared_ptr<T> Make(Args&&... args) {
29  return std::make_shared<T>(std::forward<Args>(args)...);
30 }
31 
32 // Pipe operator to decorate components.
33 using ComponentDecorator = std::function<Component(Component)>;
34 using ElementDecorator = std::function<Element(Element)>;
35 Component operator|(Component component, ComponentDecorator decorator);
36 Component operator|(Component component, ElementDecorator decorator);
37 Component& operator|=(Component& component, ComponentDecorator decorator);
38 Component& operator|=(Component& component, ElementDecorator decorator);
39 
40 namespace Container {
41 Component Vertical(Components children);
42 Component Vertical(Components children, int* selector);
44 Component Horizontal(Components children, int* selector);
45 Component Tab(Components children, int* selector);
46 Component Stacked(Components children);
47 } // namespace Container
48 
51  std::function<void()> on_click,
53 
56  bool* checked,
58 
59 Component Input(InputOption options = {});
60 Component Input(StringRef content, InputOption options = {});
61 Component Input(StringRef content,
62  StringRef placeholder,
63  InputOption options = {});
64 
65 Component Menu(MenuOption options);
66 Component Menu(ConstStringListRef entries,
67  int* selected_,
68  MenuOption options = MenuOption::Vertical());
69 Component MenuEntry(MenuEntryOption options);
70 Component MenuEntry(ConstStringRef label, MenuEntryOption options = {});
71 
72 Component Radiobox(RadioboxOption options);
73 Component Radiobox(ConstStringListRef entries,
74  int* selected_,
75  RadioboxOption options = {});
76 
77 Component Dropdown(ConstStringListRef entries, int* selected);
78 Component Dropdown(DropdownOption options);
79 
80 Component Toggle(ConstStringListRef entries, int* selected);
81 
82 // General slider constructor:
83 template <typename T>
84 Component Slider(SliderOption<T> options);
85 
86 // Shorthand without the `SliderOption` constructor:
87 Component Slider(ConstStringRef label,
88  Ref<int> value,
89  ConstRef<int> min = 0,
90  ConstRef<int> max = 100,
91  ConstRef<int> increment = 5);
92 Component Slider(ConstStringRef label,
93  Ref<float> value,
94  ConstRef<float> min = 0.f,
95  ConstRef<float> max = 100.f,
96  ConstRef<float> increment = 5.f);
97 Component Slider(ConstStringRef label,
98  Ref<long> value,
99  ConstRef<long> min = 0l,
100  ConstRef<long> max = 100l,
101  ConstRef<long> increment = 5l);
102 
103 Component ResizableSplit(ResizableSplitOption options);
104 Component ResizableSplitLeft(Component main, Component back, int* main_size);
105 Component ResizableSplitRight(Component main, Component back, int* main_size);
106 Component ResizableSplitTop(Component main, Component back, int* main_size);
107 Component ResizableSplitBottom(Component main, Component back, int* main_size);
108 
109 Component Renderer(Component child, std::function<Element()>);
110 Component Renderer(std::function<Element()>);
111 Component Renderer(std::function<Element(bool /* focused */)>);
113 
114 Component CatchEvent(Component child, std::function<bool(Event)>);
115 ComponentDecorator CatchEvent(std::function<bool(Event)> on_event);
116 
117 Component Maybe(Component, const bool* show);
118 Component Maybe(Component, std::function<bool()>);
119 ComponentDecorator Maybe(const bool* show);
120 ComponentDecorator Maybe(std::function<bool()>);
121 
122 Component Modal(Component main, Component modal, const bool* show_modal);
123 ComponentDecorator Modal(Component modal, const bool* show_modal);
124 
126  Component child,
127  Ref<bool> show = false);
128 
129 Component Hoverable(Component component, bool* hover);
130 Component Hoverable(Component component,
131  std::function<void()> on_enter,
132  std::function<void()> on_leave);
133 Component Hoverable(Component component, //
134  std::function<void(bool)> on_change);
135 ComponentDecorator Hoverable(bool* hover);
136 ComponentDecorator Hoverable(std::function<void()> on_enter,
137  std::function<void()> on_leave);
138 ComponentDecorator Hoverable(std::function<void(bool)> on_change);
139 
141 
142 } // namespace ftxui
143 
144 #endif /* end of include guard: FTXUI_COMPONENT_HPP */
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition: ref.hpp:86
Component Horizontal(Components children)
A list of components, drawn one by one horizontally and navigated horizontally using left/right arrow...
Definition: container.cpp:360
Component Vertical(Components children)
A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or...
Definition: container.cpp:317
Component Stacked(Components children)
A list of components to be stacked on top of each other. Events are propagated to the first component...
Definition: container.cpp:432
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...
Definition: container.cpp:405
Component Maybe(Component, const bool *show)
Decorate a component |child|. It is shown only when |show| is true.
Definition: maybe.cpp:75
Component ResizableSplitTop(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
std::shared_ptr< Node > Element
Definition: elements.hpp:23
Component Menu(MenuOption options)
A list of text. The focused element is selected.
Definition: menu.cpp:515
Component MenuEntry(MenuEntryOption options)
A specific menu entry. They can be put into a Container::Vertical to form a menu.
Definition: menu.cpp:617
std::function< Element(Element)> ElementDecorator
Definition: component.hpp:34
std::shared_ptr< ComponentBase > Component
Component Toggle(ConstStringListRef entries, int *selected)
An horizontal list of elements. The user can navigate through them.
Definition: menu.cpp:557
std::vector< Component > Components
Component Radiobox(RadioboxOption options)
A list of element, where only one can be selected.
Definition: radiobox.cpp:208
Component Collapsible(ConstStringRef label, Component child, Ref< bool > show=false)
Component Button(ButtonOption options)
Draw a button. Execute a function when clicked.
Definition: button.cpp:179
Component Modal(Component main, Component modal, const bool *show_modal)
Definition: modal.cpp:18
Component Renderer(Component child, std::function< Element()>)
Return a new Component, similar to |child|, but using |render| as the Component::Render() event.
Definition: renderer.cpp:63
Component Hoverable(Component component, bool *hover)
Wrap a component. Gives the ability to know if it is hovered by the mouse.
Definition: hoverable.cpp:44
std::shared_ptr< T > Make(Args &&... args)
Definition: component.hpp:28
Component ResizableSplit(ResizableSplitOption options)
A split in between two components.
Component Window(WindowOptions option)
A draggeable / resizeable window. To use multiple of them, they must be stacked using Container::Stac...
Definition: window.cpp:306
Component operator|(Component component, ComponentDecorator decorator)
Definition: util.cpp:12
Component Input(InputOption options={})
An input box for editing text.
Definition: input.cpp:567
Component ResizableSplitRight(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
Component CatchEvent(Component child, std::function< bool(Event)>)
Component Dropdown(ConstStringListRef entries, int *selected)
A dropdown menu.
Definition: dropdown.cpp:22
Component Slider(SliderOption< T > options)
A slider in any direction.
Definition: slider.cpp:341
Component ResizableSplitBottom(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Component & operator|=(Component &component, ComponentDecorator decorator)
Definition: util.cpp:22
Component Checkbox(CheckboxOption options)
Draw checkable element.
Definition: checkbox.cpp:112
Component ResizableSplitLeft(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
std::function< Component(Component)> ComponentDecorator
Definition: component.hpp:33
Option for the AnimatedButton component.
static ButtonOption Simple()
Create a ButtonOption, inverted when focused.
Option for the Checkbox component.
static CheckboxOption Simple()
Option for standard Checkbox.
Represent an event. It can be key press event, a terminal resize, or more ...
Definition: event.hpp:29
Option for the Input component.
static MenuOption Vertical()
Standard options for a vertical menu. This can be useful to implement a list of selectable items.