FTXUI 7.0.3
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/app.hpp" // for App
8#include "ftxui/component/captured_mouse.hpp" // for ftxui
9#include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer
10#include "ftxui/component/component_base.hpp" // for ComponentBase
11#include "ftxui/dom/elements.hpp" // for text, separator, Element, operator|, vbox, border
12
13using namespace ftxui;
14
15// Un ejemplo de cómo componer múltiples componentes en uno y mantener su
16// interactividad.
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 decora su hijo con una nueva función de renderizado. La forma en que los
32 // hijos reaccionan a los eventos se mantiene. auto leftpane = Renderer(left_buttons, [&] {
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 agrupa componentes. Para renderizar un Container::Horizontal,
54 // renderiza sus hijos uno junto al otro. Mantiene su interactividad y
55 // provee la lógica para navegar de uno a otro usando las teclas de flecha.
56 auto composition = Container::Horizontal({leftpane, rightpane});
57
58 auto screen = App::FitComponent();
59 screen.Loop(composition);
60 return 0;
61}
62
63// ¡Gracias a Chris Morgan por este ejemplo!
int main()
Component Button(ConstStringRef label, std::function< void()> on_click, ButtonOption options=ButtonOption::Simple())
Dibuja un botón. Ejecuta una función cuando se hace clic.
Component Renderer(Component child, std::function< Element()>)
Devuelve un nuevo Component, similar a |child|, pero usando |render| como el evento Component::Render...
Element text(std::string_view text)
Muestra un fragmento de texto Unicode codificado en UTF8.
Element separator()
Dibuja una separación vertical u horizontal entre otros dos elementos.
Element vbox(Elements children)
Un contenedor que muestra elementos verticalmente uno por uno.
Definition vbox.cpp:96
El espacio de nombres ftxui:: de FTXUI.
Definition animation.hpp:11