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. 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 <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
13 using namespace ftxui;
14
15 auto button_style = ButtonOption::Animated();
16
17// Definition of the main component. The details are not important.
18 Component 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 // Polish how the two buttons are rendered:
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// Definition of the modal component. The details are not important.
40 Component 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 // Polish how the two buttons are rendered:
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
59 int main(int argc, const char* argv[]) {
60 auto screen = ScreenInteractive::TerminalOutput();
61
62 // State of the application:
63 bool modal_shown = false;
64
65 // Some actions modifying the state:
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 // Instanciate the main and modal components:
72 auto main_component = MainComponent(show_modal, exit);
73 auto modal_component = ModalComponent(do_nothing, hide_modal);
74
75 // Use the `Modal` function to use together the main component and its modal
76 // window. The |modal_shown| boolean controls whether the modal is shown or
77 // not.
78 main_component |= Modal(modal_component, &modal_shown);
79
80 screen.Loop(main_component);
81 return 0;
82}
component.hpp
component_options.hpp
elements.hpp
main
int main()
Definition examples/component/button.cpp:33
ftxui::ButtonOption::Animated
static ButtonOption Animated()
Create a ButtonOption, using animated colors.
Definition component_options.cpp:196
ftxui::ScreenInteractive::TerminalOutput
static ScreenInteractive TerminalOutput()
Definition screen_interactive.cpp:410
ftxui::Button
Component Button(ButtonOption options)
Draw a button. Execute a function when clicked.
Definition src/ftxui/component/button.cpp:175
ftxui::Modal
Component Modal(Component main, Component modal, const bool *show_modal)
Definition modal.cpp:18
ftxui::Renderer
Component Renderer(Component child, std::function< Element()>)
Return a new Component, similar to |child|, but using |render| as the Component::Render() event.
Definition src/ftxui/component/renderer.cpp:61
ftxui::Container::Vertical
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
ftxui::size
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Definition src/ftxui/dom/size.cpp:89
ftxui::center
Element center(Element)
Center an element horizontally and vertically.
Definition composite_decorator.cpp:31
ftxui::text
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
ftxui::separator
Element separator()
Draw a vertical or horizontal separation in between two other elements.
Definition src/ftxui/dom/separator.cpp:134
ftxui::border
Element border(Element)
Draw a border around the element.
Definition src/ftxui/dom/border.cpp:227
ftxui::vbox
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
button_style
auto button_style
Definition modal_dialog.cpp:15
ModalComponent
Component ModalComponent(std::function< void()> do_nothing, std::function< void()> hide_modal)
Definition modal_dialog.cpp:40
MainComponent
Component MainComponent(std::function< void()> show_modal, std::function< void()> exit)
Definition modal_dialog.cpp:18
mouse.hpp
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::HEIGHT
@ HEIGHT
Definition elements.hpp:161
ftxui::WIDTH
@ WIDTH
Definition elements.hpp:161
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::GREATER_THAN
@ GREATER_THAN
Definition elements.hpp:162
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24
screen_interactive.hpp