FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
composition.cpp
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#include <memory> // for allocator, shared_ptr, __shared_ptr_access
5#include <string> // for operator+, to_string
6
7#include "ftxui/component/captured_mouse.hpp" // for ftxui
8#include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer
9#include "ftxui/component/component_base.hpp" // for ComponentBase
10#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
11#include "ftxui/dom/elements.hpp" // for text, separator, Element, operator|, vbox, border
12
13using namespace ftxui;
14
15// An example of how to compose multiple components into one and maintain their
16// interactiveness.
17int main() {
18 auto left_count = 0;
19 auto right_count = 0;
20
21 auto left_buttons = Container::Horizontal({
22 Button("Decrease", [&] { left_count--; }),
23 Button("Increase", [&] { left_count++; }),
24 });
25
26 auto right_buttons = Container::Horizontal({
27 Button("Decrease", [&] { right_count--; }),
28 Button("Increase", [&] { right_count++; }),
29 });
30
31 // Renderer decorates its child with a new rendering function. The way the
32 // children reacts to events is maintained.
33 auto leftpane = Renderer(left_buttons, [&] {
34 return vbox({
35 text("This is the left control"),
36 separator(),
37 text("Left button count: " + std::to_string(left_count)),
38 left_buttons->Render(),
39 }) |
40 border;
41 });
42
43 auto rightpane = Renderer(right_buttons, [&] {
44 return vbox({
45 text("This is the right control"),
46 separator(),
47 text("Right button count: " + std::to_string(right_count)),
48 right_buttons->Render(),
49 }) |
50 border;
51 });
52
53 // Container groups components togethers. To render a Container::Horizontal,
54 // it render its children side by side. It maintains their interactiveness and
55 // provide the logic to navigate from one to the other using the arrow keys.
56 auto composition = Container::Horizontal({leftpane, rightpane});
57
58 auto screen = ScreenInteractive::FitComponent();
59 screen.Loop(composition);
60 return 0;
61}
62
63// Thanks to Chris Morgan for this example!
int main()
static ScreenInteractive FitComponent()
Component Horizontal(Components children)
A list of components, drawn one by one horizontally and navigated horizontally using left/right arrow...
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.
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition node.cpp:59
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 vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
The FTXUI ftxui:: namespace.
Definition animation.hpp:10