Loading...
Searching...
No Matches
modal.cpp
Go to the documentation of this file.
// 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.
#include <ftxui/component/event.hpp> // for Event
#include <ftxui/dom/elements.hpp> // for operator|, Element, center, clear_under, dbox
#include <memory> // for __shared_ptr_access, shared_ptr
#include <utility> // for move
#include "ftxui/component/component.hpp" // for Make, Tab, ComponentDecorator, Modal
#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
namespace ftxui {
// Add a |modal| window on top of the |main| component. It is shown one on the
// top of the other when |show_modal| is true.
/// @ingroup component
// NOLINTNEXTLINE
Component Modal(Component main, Component modal, const bool* show_modal) {
class Impl : public ComponentBase {
public:
explicit Impl(Component main, Component modal, const bool* show_modal)
: main_(std::move(main)),
modal_(std::move(modal)),
show_modal_(show_modal) {
Add(Container::Tab({main_, modal_}, &selector_));
}
private:
Element OnRender() override {
selector_ = *show_modal_;
auto document = main_->Render();
if (*show_modal_) {
document = dbox({
document,
modal_->Render() | clear_under | center,
});
}
return document;
}
bool OnEvent(Event event) override {
selector_ = *show_modal_;
return ComponentBase::OnEvent(event);
}
Component main_;
Component modal_;
const bool* show_modal_;
int selector_ = *show_modal_;
};
return Make<Impl>(main, modal, show_modal);
}
// Decorate a component. Add a |modal| window on top of it. It is shown one on
// the top of the other when |show_modal| is true.
/// @ingroup component
// NOLINTNEXTLINE
ComponentDecorator Modal(Component modal, const bool* show_modal) {
return [modal, show_modal](Component main) {
return Modal(std::move(main), modal, show_modal);
};
}
} // namespace ftxui
ftxui::ComponentBase
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
Definition component_base.hpp:30
ftxui::ComponentBase::OnEvent
virtual bool OnEvent(Event)
Called in response to an event.
Definition component.cpp:161
component.hpp
component_base.hpp
elements.hpp
event.hpp
ftxui::Container::Tab
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
ftxui
Definition animation.hpp:10
ftxui::clear_under
Element clear_under(Element element)
Before drawing |child|, clear the pixels below. This is useful in.
Definition clear_under.cpp:38
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::Modal
Component Modal(Component main, Component modal, const bool *show_modal)
Definition modal.cpp:18
ftxui::center
Element center(Element)
Center an element horizontally and vertically.
Definition composite_decorator.cpp:31
ftxui::dbox
Element dbox(Elements)
Stack several element on top of each other.
Definition dbox.cpp:55
ftxui::ComponentDecorator
std::function< Component(Component)> ComponentDecorator
Definition component.hpp:31
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24
ftxui::Event
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:27