FTXUI/src/ftxui/component/renderer.cpp

134 lines
3.9 KiB
C++
Raw Normal View History

2021-05-13 17:44:47 +08:00
#include <functional> // for function
2021-08-07 02:32:33 +08:00
#include <memory> // for __shared_ptr_access, shared_ptr
#include <utility> // for move
2021-05-13 17:44:47 +08:00
2021-08-07 02:32:33 +08:00
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#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
2021-05-13 17:44:47 +08:00
namespace ftxui {
2021-05-15 02:56:37 +08:00
/// @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");
2021-05-15 02:56:37 +08:00
/// });
/// screen.Loop(renderer);
/// ```
2021-05-13 17:44:47 +08:00
Component Renderer(std::function<Element()> render) {
2021-08-07 02:32:33 +08:00
class Impl : public ComponentBase {
public:
Impl(std::function<Element()> render) : render_(std::move(render)) {}
Element Render() override { return render_(); }
std::function<Element()> render_;
};
return Make<Impl>(std::move(render));
2021-05-13 17:44:47 +08:00
}
2021-05-15 02:56:37 +08:00
/// @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";
2021-05-15 02:56:37 +08:00
/// auto button = Button(&label, screen.ExitLoopClosure());
/// auto renderer = Renderer(button, [&] {
/// return hbox({
/// text("A button:"),
/// button->Render(),
/// });
/// });
/// screen.Loop(renderer);
/// ```
2021-05-13 17:44:47 +08:00
Component Renderer(Component child, std::function<Element()> render) {
Component renderer = Renderer(std::move(render));
renderer->Add(std::move(child));
return renderer;
}
2021-08-07 02:32:33 +08:00
/// @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");
2021-08-07 02:32:33 +08:00
/// });
/// screen.Loop(renderer);
/// ```
Component Renderer(std::function<Element(bool)> render) {
class Impl : public ComponentBase {
public:
Impl(std::function<Element(bool)> render) : render_(std::move(render)) {}
private:
Element Render() 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) {
return [decorator](Component component) {
return Renderer(component, [component, decorator] {
return component->Render() | decorator;
});
};
}
2021-05-13 17:44:47 +08:00
} // namespace ftxui
// 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.