2021-05-13 17:44:47 +08:00
|
|
|
#include <functional> // for function
|
2021-05-15 03:43:35 +08:00
|
|
|
#include <memory> // for __shared_ptr_access
|
|
|
|
#include <utility> // for move
|
2021-05-13 17:44:47 +08:00
|
|
|
|
2021-05-15 03:43:35 +08:00
|
|
|
#include "ftxui/component/component.hpp" // for Component, Make, Renderer
|
2021-05-13 17:44:47 +08:00
|
|
|
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
|
|
|
#include "ftxui/dom/elements.hpp" // for Element
|
|
|
|
|
|
|
|
namespace ftxui {
|
|
|
|
|
2021-07-10 20:23:46 +08:00
|
|
|
// @brief A component rendering Element from a function.
|
2021-05-13 17:44:47 +08:00
|
|
|
class RendererBase : public ComponentBase {
|
|
|
|
public:
|
|
|
|
// Access this interface from a Component
|
|
|
|
static RendererBase* From(Component component) {
|
|
|
|
return static_cast<RendererBase*>(component.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructor.
|
|
|
|
RendererBase(std::function<Element()> render) : render_(std::move(render)) {}
|
|
|
|
|
|
|
|
// Component implementation.
|
|
|
|
Element Render() override { return render_(); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::function<Element()> render_;
|
|
|
|
};
|
|
|
|
|
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(L"My interface");
|
|
|
|
/// });
|
|
|
|
/// screen.Loop(renderer);
|
|
|
|
/// ```
|
2021-05-13 17:44:47 +08:00
|
|
|
Component Renderer(std::function<Element()> render) {
|
|
|
|
return Make<RendererBase>(std::move(render));
|
|
|
|
}
|
|
|
|
|
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::wstring label = "Click to quit";
|
|
|
|
/// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // 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.
|