FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
modal_dialog.cpp
Go to the documentation of this file.
1// Copyright 2022 Arthur Sonzogni. Todos los derechos reservados.
2// El uso de este código fuente se rige por la licencia MIT que se puede encontrar
3// en el archivo LICENSE.
4#include <ftxui/component/component_options.hpp> // for ButtonOption
5#include <ftxui/component/mouse.hpp> // for ftxui
6#include <functional> // for function
7#include <memory> // for allocator, shared_ptr
8
9#include "ftxui/component/component.hpp" // for Button, operator|=, Renderer, Vertical, Modal
10#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive, Component
11#include "ftxui/dom/elements.hpp" // for operator|, separator, text, size, Element, vbox, border, GREATER_THAN, WIDTH, center, HEIGHT
12
13using namespace ftxui;
14
16
17// Definición del componente principal. Los detalles no son importantes.
18Component MainComponent(std::function<void()> show_modal,
19 std::function<void()> exit) {
20 auto component = Container::Vertical({
21 Button("Show modal", show_modal, button_style),
22 Button("Quit", exit, button_style),
23 });
24 // Mejora cómo se renderizan los dos botones:
25 component |= Renderer([&](Element inner) {
26 return vbox({
27 text("Main component"),
28 separator(),
29 inner,
30 }) //
31 | size(WIDTH, GREATER_THAN, 15) //
32 | size(HEIGHT, GREATER_THAN, 15) //
33 | border //
34 | center; //
35 });
36 return component;
37}
38
39// Definición del componente modal. Los detalles no son importantes.
40Component ModalComponent(std::function<void()> do_nothing,
41 std::function<void()> hide_modal) {
42 auto component = Container::Vertical({
43 Button("Do nothing", do_nothing, button_style),
44 Button("Quit modal", hide_modal, button_style),
45 });
46 // Mejora cómo se renderizan los dos botones:
47 component |= Renderer([&](Element inner) {
48 return vbox({
49 text("Modal component "),
50 separator(),
51 inner,
52 }) //
53 | size(WIDTH, GREATER_THAN, 30) //
54 | border; //
55 });
56 return component;
57}
58
59int main(int argc, const char* argv[]) {
61
62 // Estado de la aplicación:
63 bool modal_shown = false;
64
65 // Algunas acciones que modifican el estado:
66 auto show_modal = [&] { modal_shown = true; };
67 auto hide_modal = [&] { modal_shown = false; };
68 auto exit = screen.ExitLoopClosure();
69 auto do_nothing = [&] {};
70
71 // Instanciar los componentes principal y modal:
72 auto main_component = MainComponent(show_modal, exit);
73 auto modal_component = ModalComponent(do_nothing, hide_modal);
74
75 // Usa la función `Modal` para usar juntos el componente principal y su ventana
76 // modal. El booleano |modal_shown| controla si el modal se muestra o no.
77 main_component |= Modal(modal_component, &modal_shown);
78
79 screen.Loop(main_component);
80 return 0;
81}
auto screen
static ScreenInteractive TerminalOutput()
static ButtonOption Animated()
Component Button(ButtonOption options)
Dibuja un botón. Ejecuta una función al hacer clic.
Component Modal(Component main, Component modal, const bool *show_modal)
Definition modal.cpp:18
Component Renderer(Component child, std::function< Element()>)
Retorna un nuevo Componente, similar a |child|, pero usando |render| como el evento Component::Render...
Decorator size(WidthOrHeight, Constraint, int value)
Aplica una restricción al tamaño de un elemento.
Element center(Element)
Centra un elemento horizontal y verticalmente.
Element text(std::wstring text)
Muestra un fragmento de texto Unicode.
Definition text.cpp:160
Element separator()
Dibuja una separación vertical u horizontal entre otros dos elementos.
Element border(Element)
Draw a border around the element.
auto button_style
Component ModalComponent(std::function< void()> do_nothing, std::function< void()> hide_modal)
Component MainComponent(std::function< void()> show_modal, std::function< void()> exit)
El espacio de nombres ftxui:: de FTXUI.
Definition animation.hpp:10
std::shared_ptr< Node > Element
Definition elements.hpp:22
Element vbox(Elements)
@ GREATER_THAN
Definition elements.hpp:162
std::shared_ptr< ComponentBase > Component