FTXUI 6.1.9 C++ functional terminal UI.

Loading...
Searching...
No Matches
modal.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/event.hpp> // for Event
5#include <ftxui/dom/elements.hpp> // for operator|, Element, center, clear_under, dbox
6#include <memory> // for __shared_ptr_access, shared_ptr
7#include <utility> // for move
8
9#include "ftxui/component/component.hpp" // for Make, Tab, ComponentDecorator, Modal
10#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
11
12 namespace ftxui {
13
14// Add a |modal| window on top of the |main| component. It is shown one on the
15// top of the other when |show_modal| is true.
16/// @ingroup component
17// NOLINTNEXTLINE
18 Component Modal(Component main, Component modal, const bool* show_modal) {
19 class Impl : public ComponentBase {
20 public:
21 explicit Impl(Component main, Component modal, const bool* show_modal)
22 : main_(std::move(main)),
23 modal_(std::move(modal)),
24 show_modal_(show_modal) {
25 Add(Container::Tab({main_, modal_}, &selector_));
26 }
27
28 private:
29 Element OnRender() override {
30 selector_ = *show_modal_;
31 auto document = main_->Render();
32 if (*show_modal_) {
33 document = dbox({
34 document,
35 modal_->Render() | clear_under | center,
36 });
37 }
38 return document;
39 }
40
41 bool OnEvent(Event event) override {
42 selector_ = *show_modal_;
43 return ComponentBase::OnEvent(event);
44 }
45
46 Component main_;
47 Component modal_;
48 const bool* show_modal_;
49 int selector_ = *show_modal_;
50 };
51 return Make<Impl>(main, modal, show_modal);
52}
53
54// Decorate a component. Add a |modal| window on top of it. It is shown one on
55// the top of the other when |show_modal| is true.
56/// @ingroup component
57// NOLINTNEXTLINE
58 ComponentDecorator Modal(Component modal, const bool* show_modal) {
59 return [modal, show_modal](Component main) {
60 return Modal(std::move(main), modal, show_modal);
61 };
62}
63
64} // namespace ftxui
component.hpp
component_base.hpp
elements.hpp
event.hpp
main
int main()
Definition examples/component/button.cpp:33
ftxui::ComponentBase
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
Definition component_base.hpp:30
ftxui::Modal
Component Modal(Component main, Component modal, const bool *show_modal)
Definition modal.cpp:18
ftxui::Event
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:29
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::Make
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::ComponentDecorator
std::function< Component(Component)> ComponentDecorator
Definition component.hpp:31
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24