FTXUI  5.0.0
C++ functional terminal UI.
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
12namespace 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
18Component 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 Render() 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
58ComponentDecorator 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
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
virtual bool OnEvent(Event)
Called in response to an event.
Definition: component.cpp:106
Component Tab(Components children, int *selector)
A list of components, where only one is drawn and interacted with at a time. The |selector| gives the...
Definition: container.cpp:405
Element clear_under(Element element)
Before drawing |child|, clear the pixels below. This is useful in.
Definition: clear_under.cpp:37
std::shared_ptr< Node > Element
Definition: elements.hpp:23
std::shared_ptr< ComponentBase > Component
Component Modal(Component main, Component modal, const bool *show_modal)
Definition: modal.cpp:18
Element center(Element)
Center an element horizontally and vertically.
Element dbox(Elements)
Stack several element on top of each other.
Definition: dbox.cpp:108
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition: node.cpp:47
std::function< Component(Component)> ComponentDecorator
Definition: component.hpp:33
Represent an event. It can be key press event, a terminal resize, or more ...
Definition: event.hpp:29