FTXUI/examples/component/modal_dialog.cpp

83 lines
3.1 KiB
C++
Raw Normal View History

2023-08-19 19:56:36 +08:00
// Copyright 2022 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
2022-06-12 23:08:22 +08:00
#include <ftxui/component/component_options.hpp> // for ButtonOption
2022-08-07 20:44:33 +08:00
#include <ftxui/component/mouse.hpp> // for ftxui
2022-06-12 23:08:22 +08:00
#include <functional> // for function
#include <memory> // for allocator, shared_ptr
2021-05-02 02:40:35 +08:00
2022-06-12 23:08:22 +08:00
#include "ftxui/component/component.hpp" // for Button, operator|=, Renderer, Vertical, Modal
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive, Component
#include "ftxui/dom/elements.hpp" // for operator|, separator, text, size, Element, vbox, border, GREATER_THAN, WIDTH, center, HEIGHT
2022-06-12 23:08:22 +08:00
using namespace ftxui;
2021-05-10 02:32:27 +08:00
2022-06-12 23:08:22 +08:00
auto button_style = ButtonOption::Animated();
2022-06-12 23:08:22 +08:00
// Definition of the main component. The details are not important.
Component MainComponent(std::function<void()> show_modal,
std::function<void()> exit) {
auto component = Container::Vertical({
Button("Show modal", show_modal, button_style),
Button("Quit", exit, button_style),
});
2022-06-12 23:08:22 +08:00
// Polish how the two buttons are rendered:
component |= Renderer([&](Element inner) {
return vbox({
2022-06-12 23:08:22 +08:00
text("Main component"),
separator(),
2022-06-12 23:08:22 +08:00
inner,
}) //
| size(WIDTH, GREATER_THAN, 15) //
| size(HEIGHT, GREATER_THAN, 15) //
| border //
| center; //
2021-05-10 02:32:27 +08:00
});
2022-06-12 23:08:22 +08:00
return component;
}
2021-05-10 02:32:27 +08:00
2022-06-12 23:08:22 +08:00
// Definition of the modal component. The details are not important.
Component ModalComponent(std::function<void()> do_nothing,
std::function<void()> hide_modal) {
auto component = Container::Vertical({
Button("Do nothing", do_nothing, button_style),
Button("Quit modal", hide_modal, button_style),
});
2022-06-12 23:08:22 +08:00
// Polish how the two buttons are rendered:
component |= Renderer([&](Element inner) {
return vbox({
2022-06-12 23:08:22 +08:00
text("Modal component "),
separator(),
2022-06-12 23:08:22 +08:00
inner,
}) //
| size(WIDTH, GREATER_THAN, 30) //
| border; //
});
2022-06-12 23:08:22 +08:00
return component;
}
int main(int argc, const char* argv[]) {
auto screen = ScreenInteractive::TerminalOutput();
2021-05-10 02:32:27 +08:00
2022-06-12 23:08:22 +08:00
// State of the application:
bool modal_shown = false;
2022-06-12 23:08:22 +08:00
// Some actions modifying the state:
auto show_modal = [&] { modal_shown = true; };
auto hide_modal = [&] { modal_shown = false; };
auto exit = screen.ExitLoopClosure();
auto do_nothing = [&] {};
2021-05-10 02:32:27 +08:00
2022-06-12 23:08:22 +08:00
// Instanciate the main and modal components:
auto main_component = MainComponent(show_modal, exit);
auto modal_component = ModalComponent(do_nothing, hide_modal);
// Use the `Modal` function to use together the main component and its modal
// window. The |modal_shown| boolean controls whether the modal is shown or
// not.
main_component |= Modal(modal_component, &modal_shown);
2022-06-12 23:08:22 +08:00
screen.Loop(main_component);
return 0;
}