FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
gallery.cpp
Go to the documentation of this file.
1// Copyright 2020 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#include <functional> // for function
5#include <memory> // for shared_ptr, allocator, __shared_ptr_access
6#include <string> // for string, basic_string
7#include <vector> // for vector
8
9#include "ftxui/component/captured_mouse.hpp" // for ftxui
10#include "ftxui/component/component.hpp" // for Slider, Checkbox, Vertical, Renderer, Button, Input, Menu, Radiobox, Toggle
11#include "ftxui/component/component_base.hpp" // for ComponentBase
12#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
13#include "ftxui/dom/elements.hpp" // for separator, operator|, Element, size, xflex, text, WIDTH, hbox, vbox, EQUAL, border, GREATER_THAN
14
15using namespace ftxui;
16
17// Display a component nicely with a title on the left.
18Component Wrap(std::string name, Component component) {
19 return Renderer(component, [name, component] {
20 return hbox({
21 text(name) | size(WIDTH, EQUAL, 8),
22 separator(),
23 component->Render() | xflex,
24 }) |
25 xflex;
26 });
27}
28
29int main() {
30 auto screen = ScreenInteractive::FitComponent();
31
32 // -- Menu
33 // ----------------------------------------------------------------------
34 const std::vector<std::string> menu_entries = {
35 "Menu 1",
36 "Menu 2",
37 "Menu 3",
38 "Menu 4",
39 };
40 int menu_selected = 0;
41 auto menu = Menu(&menu_entries, &menu_selected);
42 menu = Wrap("Menu", menu);
43
44 // -- Toggle------------------------------------------------------------------
45 int toggle_selected = 0;
46 std::vector<std::string> toggle_entries = {
47 "Toggle_1",
48 "Toggle_2",
49 };
50 auto toggle = Toggle(&toggle_entries, &toggle_selected);
51 toggle = Wrap("Toggle", toggle);
52
53 // -- Checkbox ---------------------------------------------------------------
54 bool checkbox_1_selected = false;
55 bool checkbox_2_selected = false;
56 bool checkbox_3_selected = false;
57 bool checkbox_4_selected = false;
58
59 auto checkboxes = Container::Vertical({
60 Checkbox("checkbox1", &checkbox_1_selected),
61 Checkbox("checkbox2", &checkbox_2_selected),
62 Checkbox("checkbox3", &checkbox_3_selected),
63 Checkbox("checkbox4", &checkbox_4_selected),
64 });
65 checkboxes = Wrap("Checkbox", checkboxes);
66
67 // -- Radiobox ---------------------------------------------------------------
68 int radiobox_selected = 0;
69 std::vector<std::string> radiobox_entries = {
70 "Radiobox 1",
71 "Radiobox 2",
72 "Radiobox 3",
73 "Radiobox 4",
74 };
75 auto radiobox = Radiobox(&radiobox_entries, &radiobox_selected);
76 radiobox = Wrap("Radiobox", radiobox);
77
78 // -- Input ------------------------------------------------------------------
79 std::string input_label;
80 auto input = Input(&input_label, "placeholder");
81 input = Wrap("Input", input);
82
83 // -- Button -----------------------------------------------------------------
84 std::string button_label = "Quit";
85 std::function<void()> on_button_clicked_;
86 auto button = Button(&button_label, screen.ExitLoopClosure());
87 button = Wrap("Button", button);
88
89 // -- Slider -----------------------------------------------------------------
90 int slider_value_1 = 12;
91 int slider_value_2 = 56;
92 int slider_value_3 = 128;
93 auto sliders = Container::Vertical({
94 Slider("R:", &slider_value_1, 0, 256, 1),
95 Slider("G:", &slider_value_2, 0, 256, 1),
96 Slider("B:", &slider_value_3, 0, 256, 1),
97 });
98 sliders = Wrap("Slider", sliders);
99
100 // A large text:
101 auto lorel_ipsum = Renderer([] {
102 return vbox({
103 text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. "),
104 text("Sed do eiusmod tempor incididunt ut labore et dolore magna "
105 "aliqua. "),
106 text("Ut enim ad minim veniam, quis nostrud exercitation ullamco "
107 "laboris nisi ut aliquip ex ea commodo consequat. "),
108 text("Duis aute irure dolor in reprehenderit in voluptate velit esse "
109 "cillum dolore eu fugiat nulla pariatur. "),
110 text("Excepteur sint occaecat cupidatat non proident, sunt in culpa "
111 "qui officia deserunt mollit anim id est laborum. "),
112
113 });
114 });
115 lorel_ipsum = Wrap("Lorel Ipsum", lorel_ipsum);
116
117 // -- Layout
118 // -----------------------------------------------------------------
119 auto layout = Container::Vertical({
120 menu,
121 toggle,
122 checkboxes,
123 radiobox,
124 input,
125 sliders,
126 button,
127 lorel_ipsum,
128 });
129
130 auto component = Renderer(layout, [&] {
131 return vbox({
132 menu->Render(),
133 separator(),
134 toggle->Render(),
135 separator(),
136 checkboxes->Render(),
137 separator(),
138 radiobox->Render(),
139 separator(),
140 input->Render(),
141 separator(),
142 sliders->Render(),
143 separator(),
144 button->Render(),
145 separator(),
146 lorel_ipsum->Render(),
147 }) |
148 xflex | size(WIDTH, GREATER_THAN, 40) | border;
149 });
150
151 screen.Loop(component);
152
153 return 0;
154}
Component Wrap(std::string name, Component component)
Definition gallery.cpp:18
int main()
Definition gallery.cpp:29
Element Render()
Draw the component. Build a ftxui::Element to be drawn on the ftxui::Screen representing this ftxui::...
static ScreenInteractive FitComponent()
Component Menu(MenuOption options)
A list of text. The focused element is selected.
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 Renderer(Component child, std::function< Element()>)
Return a new Component, similar to |child|, but using |render| as the Component::Render() event.
Component Vertical(Components children)
A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or...
Component Input(InputOption options={})
An input box for editing text.
Component Checkbox(CheckboxOption options)
Draw checkable element.
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition node.cpp:59
Element xflex(Element)
Expand/Minimize if possible/needed on the X axis.
Definition flex.cpp:129
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
Element separator()
Draw a vertical or horizontal separation in between two other elements.
Element border(Element)
Draw a border around the element.
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
Component Slider(SliderOption< T > options)
A slider in any direction.
@ GREATER_THAN
Definition elements.hpp:162
std::shared_ptr< ComponentBase > Component