Loading...
Searching...
No Matches
renderer.cpp
Go to the documentation of this file.
// Copyright 2021 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 <functional> // for function
#include <utility> // for move
#include "ftxui/component/component.hpp" // for Make, Renderer
#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
#include "ftxui/component/event.hpp" // for Event
#include "ftxui/component/mouse.hpp" // for Mouse
#include "ftxui/dom/elements.hpp" // for Element, operator|, reflect
#include "ftxui/screen/box.hpp" // for Box
namespace ftxui {
/// @brief Return a component, using |render| to render its interface.
/// @param render The function drawing the interface.
/// @ingroup component
///
/// ### Example
///
/// ```cpp
/// auto screen = ScreenInteractive::TerminalOutput();
/// auto renderer = Renderer([] {
/// return text("My interface");
/// });
/// screen.Loop(renderer);
/// ```
Component Renderer(std::function<Element()> render) {
class Impl : public ComponentBase {
public:
explicit Impl(std::function<Element()> render)
: render_(std::move(render)) {}
Element OnRender() override { return render_(); }
std::function<Element()> render_;
};
return Make<Impl>(std::move(render));
}
/// @brief Return a new Component, similar to |child|, but using |render| as the
/// Component::Render() event.
/// @param child The component to forward events to.
/// @param render The function drawing the interface.
/// @ingroup component
///
/// ### Example
///
/// ```cpp
/// auto screen = ScreenInteractive::TerminalOutput();
/// std::string label = "Click to quit";
/// auto button = Button(&label, screen.ExitLoopClosure());
/// auto renderer = Renderer(button, [&] {
/// return hbox({
/// text("A button:"),
/// button->Render(),
/// });
/// });
/// screen.Loop(renderer);
/// ```
Component Renderer(Component child, std::function<Element()> render) {
Component renderer = Renderer(std::move(render));
renderer->Add(std::move(child));
return renderer;
}
/// @brief Return a focusable component, using |render| to render its interface.
/// @param render The function drawing the interface, taking a boolean telling
/// whether the component is focused or not.
/// @ingroup component
///
/// ### Example
///
/// ```cpp
/// auto screen = ScreenInteractive::TerminalOutput();
/// auto renderer = Renderer([] (bool focused) {
/// if (focused)
/// return text("My interface") | inverted;
/// else
/// return text("My interface");
/// });
/// screen.Loop(renderer);
/// ```
Component Renderer(std::function<Element(bool)> render) {
class Impl : public ComponentBase {
public:
explicit Impl(std::function<Element(bool)> render)
: render_(std::move(render)) {}
private:
Element OnRender() override { return render_(Focused()) | reflect(box_); }
bool Focusable() const override { return true; }
bool OnEvent(Event event) override {
if (event.is_mouse() && box_.Contain(event.mouse().x, event.mouse().y)) {
if (!CaptureMouse(event)) {
return false;
}
TakeFocus();
}
return false;
}
Box box_;
std::function<Element(bool)> render_;
};
return Make<Impl>(std::move(render));
}
/// @brief Decorate a component, by decorating what it renders.
/// @param decorator the function modifying the element it renders.
/// @ingroup component
///
/// ### Example
///
/// ```cpp
/// auto screen = ScreenInteractive::TerminalOutput();
/// auto renderer =
// Renderer([] { return text("Hello");)
/// | Renderer(bold)
/// | Renderer(inverted);
/// screen.Loop(renderer);
/// ```
ComponentDecorator Renderer(ElementDecorator decorator) { // NOLINT
return [decorator](Component component) { // NOLINT
return Renderer(component, [component, decorator] {
return component->Render() | decorator;
});
};
}
} // namespace ftxui
box.hpp
ftxui::ComponentBase
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
Definition component_base.hpp:30
component.hpp
component_base.hpp
elements.hpp
event.hpp
mouse.hpp
ftxui
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::ElementDecorator
std::function< Element(Element)> ElementDecorator
Definition component.hpp:32
ftxui::Renderer
Component Renderer(Component child, std::function< Element()>)
Return a new Component, similar to |child|, but using |render| as the Component::Render() event.
Definition renderer.cpp:61
ftxui::reflect
Decorator reflect(Box &box)
Definition reflect.cpp:43
ftxui::ComponentDecorator
std::function< Component(Component)> ComponentDecorator
Definition component.hpp:31
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24
ftxui::Box
Definition box.hpp:9
ftxui::Event
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:27
ftxui::Event::is_mouse
bool is_mouse() const
Definition event.hpp:107
ftxui::Event::mouse
struct Mouse & mouse()
Definition event.hpp:108
ftxui::Mouse::y
int y
Definition mouse.hpp:42
ftxui::Mouse::x
int x
Definition mouse.hpp:41