FTXUI  4.1.1
C++ functional terminal UI.
Loading...
Searching...
No Matches
modal.cpp
Go to the documentation of this file.
1#include <ftxui/component/event.hpp> // for Event
2#include <ftxui/dom/elements.hpp> // for operator|, Element, center, clear_under, dbox
3#include <memory> // for __shared_ptr_access, shared_ptr
4#include <utility> // for move
5
6#include "ftxui/component/component.hpp" // for Make, Tab, ComponentDecorator, Modal
7#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
8
9namespace ftxui {
10
11// Add a |modal| window on top of the |main| component. It is shown one on the
12// top of the other when |show_modal| is true.
13/// @ingroup component
14// NOLINTNEXTLINE
15Component Modal(Component main, Component modal, const bool* show_modal) {
16 class Impl : public ComponentBase {
17 public:
18 explicit Impl(Component main, Component modal, const bool* show_modal)
19 : main_(std::move(main)),
20 modal_(std::move(modal)),
21 show_modal_(show_modal) {
22 Add(Container::Tab({main_, modal_}, &selector_));
23 }
24
25 private:
26 Element Render() override {
27 selector_ = *show_modal_;
28 auto document = main_->Render();
29 if (*show_modal_) {
30 document = dbox({
31 document,
32 modal_->Render() | clear_under | center,
33 });
34 }
35 return document;
36 }
37
38 bool OnEvent(Event event) override {
39 selector_ = *show_modal_;
40 return ComponentBase::OnEvent(event);
41 }
42
43 Component main_;
44 Component modal_;
45 const bool* show_modal_;
46 int selector_ = *show_modal_;
47 };
48 return Make<Impl>(main, modal, show_modal);
49}
50
51// Decorate a component. Add a |modal| window on top of it. It is shown one on
52// the top of the other when |show_modal| is true.
53/// @ingroup component
54// NOLINTNEXTLINE
55ComponentDecorator Modal(Component modal, const bool* show_modal) {
56 return [modal, show_modal](Component main) {
57 return Modal(std::move(main), modal, show_modal);
58 };
59}
60
61} // namespace ftxui
62
63// Copyright 2022 Arthur Sonzogni. All rights reserved.
64// Use of this source code is governed by the MIT license that can be found in
65// the LICENSE file.
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
Element clear_under(Element element)
Before drawing |child|, clear the pixels below. This is useful in.
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:25
std::shared_ptr< Node > Element
Definition elements.hpp:19
Component Modal(Component main, Component modal, const bool *show_modal)
Definition modal.cpp:15
Element center(Element)
Center an element horizontally and vertically.
Element dbox(Elements)
Stack several element on top of each other.
Definition dbox.cpp:52
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:44
std::function< Component(Component)> ComponentDecorator
Definition component.hpp:30
std::shared_ptr< ComponentBase > Component
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:26