FTXUI 6.1.9 C++ functional terminal UI.

Loading...
Searching...
No Matches
modal_dialog_custom.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 <memory> // for allocator, shared_ptr, __shared_ptr_access
5#include <string> // for string, basic_string, char_traits, operator+
6#include <vector> // for vector
7
8#include "ftxui/component/captured_mouse.hpp" // for ftxui
9#include "ftxui/component/component.hpp" // for Button, Renderer, Horizontal, Tab
10#include "ftxui/component/component_base.hpp" // for ComponentBase
11#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
12#include "ftxui/dom/elements.hpp" // for operator|, Element, filler, text, hbox, separator, center, vbox, bold, border, clear_under, dbox, size, GREATER_THAN, HEIGHT
13
14 int main() {
15 using namespace ftxui;
16 auto screen = ScreenInteractive::TerminalOutput();
17
18 // There are two layers. One at depth = 0 and the modal window at depth = 1;
19 int depth = 0;
20
21 // The current rating of FTXUI.
22 std::string rating = "3/5 stars";
23
24 // At depth=0, two buttons. One for rating FTXUI and one for quitting.
25 auto button_rate_ftxui = Button("Rate FTXUI", [&] { depth = 1; });
26 auto button_quit = Button("Quit", screen.ExitLoopClosure());
27
28 auto depth_0_container = Container::Horizontal({
29 button_rate_ftxui,
30 button_quit,
31 });
32 auto depth_0_renderer = Renderer(depth_0_container, [&] {
33 return vbox({
34 text("Modal dialog example"),
35 separator(),
36 text("☆☆☆ FTXUI:" + rating + " ☆☆☆") | bold,
37 filler(),
38 hbox({
39 button_rate_ftxui->Render(),
40 filler(),
41 button_quit->Render(),
42 }),
43 }) |
44 border | size(HEIGHT, GREATER_THAN, 18) | center;
45 });
46
47 // At depth=1, The "modal" window.
48 std::vector<std::string> rating_labels = {
49 "1/5 stars", "2/5 stars", "3/5 stars", "4/5 stars", "5/5 stars",
50 };
51 auto on_rating = [&](std::string new_rating) {
52 rating = new_rating;
53 depth = 0;
54 };
55 auto depth_1_container = Container::Horizontal({
56 Button(&rating_labels[0], [&] { on_rating(rating_labels[0]); }),
57 Button(&rating_labels[1], [&] { on_rating(rating_labels[1]); }),
58 Button(&rating_labels[2], [&] { on_rating(rating_labels[2]); }),
59 Button(&rating_labels[3], [&] { on_rating(rating_labels[3]); }),
60 Button(&rating_labels[4], [&] { on_rating(rating_labels[4]); }),
61 });
62
63 auto depth_1_renderer = Renderer(depth_1_container, [&] {
64 return vbox({
65 text("Do you like FTXUI?"),
66 separator(),
67 hbox(depth_1_container->Render()),
68 }) |
69 border;
70 });
71
72 auto main_container = Container::Tab(
73 {
74 depth_0_renderer,
75 depth_1_renderer,
76 },
77 &depth);
78
79 auto main_renderer = Renderer(main_container, [&] {
80 Element document = depth_0_renderer->Render();
81
82 if (depth == 1) {
83 document = dbox({
84 document,
85 depth_1_renderer->Render() | clear_under | center,
86 });
87 }
88 return document;
89 });
90
91 screen.Loop(main_renderer);
92 return 0;
93}
captured_mouse.hpp
component.hpp
component_base.hpp
elements.hpp
main
int main()
Definition modal_dialog_custom.cpp:14
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
screen_interactive.hpp